Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Loading SFML Textures in Opengl  (Read 1237 times)

0 Members and 1 Guest are viewing this topic.

Bogdan

  • Jr. Member
  • **
  • Posts: 97
    • View Profile
Loading SFML Textures in Opengl
« on: December 08, 2024, 08:57:10 pm »
Hello there,
I've encountered a new problem with sfml/opengl loading of textures. There is much outdated info on the net about opengl and sfml...
As the example below uses a different library, I wanted to ask: How to do the texture loading part just with SFML and Opengl. If I try using sf::Texture, it always says that it's incompatible wit Gluint type.

unsigned int texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
// set the texture wrapping/filtering options (on the currently bound texture object)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);  
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// load and generate the texture
int width, height, nrChannels;
unsigned char *data = stbi_load("container.jpg", &width, &height, &nrChannels, 0);
if (data)
{
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
    glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
    std::cout << "Failed to load texture" << std::endl;
}
stbi_image_free(data);

Bogdan

  • Jr. Member
  • **
  • Posts: 97
    • View Profile
Re: Loading SFML Textures in Opengl
« Reply #1 on: December 08, 2024, 09:54:50 pm »
I've found a modern example.
It seems to be done with "sf::Image".

main.cpp
https://gist.github.com/alexmercerind/a1d9115d9944fa1e82f8c2abcd1c9b5d

shader_compiler.cpp
https://gist.github.com/alexmercerind/6b15e7eb3872d5a284f25a93fab7c625

But why does the square still stay white in the code example? (no texture is shown, no visible errors)
« Last Edit: December 08, 2024, 09:57:56 pm by Bogdan »

Hapax

  • Hero Member
  • *****
  • Posts: 3387
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Loading SFML Textures in Opengl
« Reply #2 on: December 09, 2024, 07:03:07 pm »
Do you have those shader files (that that code attempts to load) as well and are they in the correct location?

Are you using SFML 2 or SFML 3?

Does this work:
https://github.com/SFML/SFML/blob/master/examples/opengl/OpenGL.cpp
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Bogdan

  • Jr. Member
  • **
  • Posts: 97
    • View Profile
Re: Loading SFML Textures in Opengl
« Reply #3 on: December 10, 2024, 02:26:05 pm »
Unfortunately I don't have those shader files.
I'm using SFML 2 as SFML 3 is still experimental.
The given code example doesn't work (Visual Studio 2022), because it doesn't recognize GLAD.
Esp. one function: gladLoadGL(sf::Context::getFunction);
Is there a way to circumvent those additional libraries and just do it with sfml, GLM and opengl?

At least I've found a working example from a different site (made with SFML 2.5):
https://gist.github.com/gamepopper/1931ca297f3decdee90e785f12762192

Is this good enough as modern example?
« Last Edit: December 12, 2024, 04:02:54 pm by Bogdan »

Hapax

  • Hero Member
  • *****
  • Posts: 3387
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Loading SFML Textures in Opengl
« Reply #4 on: December 10, 2024, 09:01:40 pm »
It looks like that original "modern" example you gave uses sf::Image to load the image data in but then uploads it to the graphics card using GL commands (sf::Image is basically just an array of pixels). Without the shaders that that code uses, it won't be able to show those textures.

SFML 3 is currently on its second Release Candidate so it's borderline being released. ;)

I think I may have misunderstood your original question. Are you trying to use raw OpenGL calls and pass an SFML texture to use in it? If so, would it not be simpler to also use raw OpenGL calls to set up the texture?

With that said, the official example (the one that you said requires GLAD) shows how to use an sf::Texture within OpenGL.
The one I linked was the example for SFML 3. Does this work?
https://github.com/SFML/SFML/blob/2.6.x/examples/opengl/OpenGL.cpp

Anyway, in that example, this part binds the texture:
https://github.com/SFML/SFML/blob/2.6.x/examples/opengl/OpenGL.cpp#L115-L117

For some more information, you can see sf::Texture's documentation about "bind":
https://www.sfml-dev.org/documentation/2.6.2/classsf_1_1Texture.php#ae9a4274e7b95ebf7244d09c7445833b0
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Bogdan

  • Jr. Member
  • **
  • Posts: 97
    • View Profile
Re: Loading SFML Textures in Opengl
« Reply #5 on: December 12, 2024, 04:02:28 pm »
Yes, I'm trying to use raw Opengl without unnecessary dependencies, with the exemption
of sfml (input events,textures,window....) and GLM for the 3d camera/view as SFML won't allow me to do it.


Hapax

  • Hero Member
  • *****
  • Posts: 3387
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Loading SFML Textures in Opengl
« Reply #6 on: December 16, 2024, 05:45:20 pm »
I see.
And where you able to bind the texture as I mentioned?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*