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

Author Topic: Why I got different result from texture produced by sfml and stb_image?  (Read 1411 times)

0 Members and 1 Guest are viewing this topic.

Hythloday

  • Newbie
  • *
  • Posts: 10
    • View Profile
It should n't be like this since sfml use stb_image internally?

Anyway the different result are shown as the picture file attached.

If I use sfml texture class with opengl, the left half of the model is not correctly rendered, it seems as if the texture coords numbers are wrong or something, however if I change the way to read texture from sfml to directly use stb_image, this problems are gone.

The relavant codes are like this:

to read image files in sfml
else if ((ext == DATA_JPG) || (ext == DATA_PNG))
            {
                Image image;
                if (!image.loadFromFile(path))
                {
                    throw Exception(EX_BROKEN_DATA_FILE, name);
                }
                //image.createMaskFromColor(Color::White);

                if (!insertAsset<Texture>(name)->loadFromImage(image))
                {
                    throw Exception(EX_FAILED_TO_LOAD_TEXTURE, name);
                }
            }

to set the texture uniforms in opengl
                mat->GetTexture(type, i, &str);
                sf::Texture* current_tex = getAsset2<sf::Texture>(str.C_Str());
                if (current_tex)
                {
                    TextureInfo current_texture_info;
                    current_tex->generateMipmap();
                    current_texture_info.glID = current_tex->getNativeHandle();
                    current_texture_info.type = typeName;
                    current_texture_info.filename = str.C_Str();
                    textures.push_back(current_texture_info);
                }

To bind the texture in opengl
                // now set the sampler to the correct texture unit
                //shader.setInt((name + number), i);
                glUniform1i(glGetUniformLocation(shader.getID(), (name + number).c_str()), i);
                // and finally bind the texture
                glBindTexture(GL_TEXTURE_2D, textures[i].glID);

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Do you really want to color mask the image? Why not use real transparent colors instead?

If you render the texture with a SFML sprite, does it display correctly?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Hythloday

  • Newbie
  • *
  • Posts: 10
    • View Profile
The color mask is irrelevant here, I tried use sf::Texture::loadFromFile directly, and the problem is the same.

I tried make the texture into an sprite and render with sfml as you said, it is normal and fine(as shown in attached png)

In fact the left part of the body when using sf::Texture is fine in my main post, and the right half are using the exact same texture, the 3d model just flipped those texcoords in its vertices data file.

Hythloday

  • Newbie
  • *
  • Posts: 10
    • View Profile
Do you really want to color mask the image? Why not use real transparent colors instead?

If you render the texture with a SFML sprite, does it display correctly?

Ah, I fixed it, the reason is that to render this model we need to enable the repeat mode in opengl for this texture, however this is on default false in sfml.

 

anything