Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Texture::Bind(Normalized) does not mix with Texture::Bind(Pixels) #235

Closed
mrwonko opened this issue Jun 13, 2012 · 4 comments
Closed

Texture::Bind(Normalized) does not mix with Texture::Bind(Pixels) #235

mrwonko opened this issue Jun 13, 2012 · 4 comments

Comments

@mrwonko
Copy link

mrwonko commented Jun 13, 2012

The Texture::Draw(Texture::Pixels) changes the Texture matrix, but Texture::Bind(Texture::Normalized) does not! So the OpenGL uv coordinates are completely off when drawing a Normalized texture after a Pixel-mapped one.

Here's some simple code to reproduce the problem:

#include <SFML/Graphics.hpp>
#include <SFML/OpenGL.hpp>

void DrawQuad(float size)
{
    glBegin(GL_QUADS);
        glTexCoord2f(0, 0); glVertex2f(-size, -size);
        glTexCoord2f(0, 4); glVertex2f(-size,  size);
        glTexCoord2f(4, 4); glVertex2f( size,  size);
        glTexCoord2f(4, 0); glVertex2f( size, -size);
    glEnd();
}
int main()
{
    sf::Texture texture;
    if(!texture.loadFromFile("resources/texture.jpg")) return EXIT_FAILURE;
    texture.setRepeated(true);

    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML OpenGL", sf::Style::Default, sf::ContextSettings(32));
    window.draw(sf::CircleShape()); // to initialize view etc.

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }
        window.clear();

        glLoadIdentity();
        glTranslatef(100, 100, 0);

        // Bind with type pixels
        texture.bind(sf::Texture::Pixels);
        DrawQuad(100);

        glTranslatef(250, 0, 0);

        // Bind normalized - does not work, looks just the same!
        texture.bind(sf::Texture::Normalized);
        DrawQuad(100);

        glTranslatef(250, 0, 0);

        // only after manually resetting the texture matrix it works:
        glMatrixMode(GL_TEXTURE);
        glLoadIdentity();
        glMatrixMode(GL_MODELVIEW);
        DrawQuad(100);

        window.display();
    }

    return EXIT_SUCCESS;
}
@LaurentGomila
Copy link
Member

You're not supposed to mix both modes. Pixels is meant for internal use, and Normalized for user code.

@mrwonko
Copy link
Author

mrwonko commented Jun 14, 2012

Okay. I ran into this when I used OpenGL while also using SFML for 2D rendering (happens with sf::Sprite and sf::Text, for example) - I did not want to do pop/pushGLStates() because that does more than required (and thus is slower), but it took me a while to figure out that resetting the Texture matrix is required.

@LaurentGomila
Copy link
Member

If you don't call pop/pushGLStates() you have to call resetGlStates() so that SFML sets its own OpenGL states and is ready do draw.

@mrwonko
Copy link
Author

mrwonko commented Jun 14, 2012

I do. I meant in my GL code. In order for glTexCoord2f() to work as expected, I need to reset the texture matrix first:

while (window.isOpen())
{
    <<event code>>

    // SFML Draw code - this could also be after the OpenGL part
    window.draw(sprite);

    //  3D setup
    //without resetting the texture matrix, the UV coordinates would be wrong.
    glMatrixMode(GL_TEXTURE);
    glLoadIdentity();

    << more OpenGL setup - Depth Buffer enabling, projection matrix etc. >>

    glEnable(GL_TEXTURE_2D);
    texture.bind(sf::Texture::Normalized); // does not change the matrix

    << draw something textured >>
    glTexCoord2f(0, 1); [...]

    //  2D/SFML Setup
    window.resetGLStates();

    window.display();
}

But from what you explained this is the intended behaviour. Is it documented anywhere?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants