0
\$\begingroup\$

Whenever I render textures, all shapes get the color of the texture. I'm not sure why, but I think it's something to do with the way I render textures. Here's what I use to draw textures:

public void drawTex(Texture t, int x, int y, int width, int height)
    {
        t.bind();
        glBegin(GL_QUADS);
        glTexCoord2f(0,0);
        glVertex2f(x,y);
        glTexCoord2f(1,0);
        glVertex2f(x+t.getTextureWidth(),y);
        glTexCoord2f(1,1);
        glVertex2f(x+t.getTextureWidth(),y+t.getTextureHeight());
        glTexCoord2f(0,1);
        glVertex2f(x,y+t.getTextureHeight());
        glEnd();
    }

Now I draw shapes like this:

public void drawQuad(int x, int y, int width, int height, float r, float g, float b)
    {

        glBegin(GL_QUADS);
    //  glColor3f (r, g, b);
        glVertex2f(x, y);
        glVertex2f(x + width, y);
        glVertex2f(x + width, y + height);
        glVertex2f(x, y + height);
        glEnd();
    }
\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

You need to unbind your texture object after using it for drawing certain shape.

// In Texture class add
void unbind()
{
 glBindTexture(0);
}

public void drawTex(Texture t, int x, int y, int width, int height)
{
        t.bind();
        glBegin(GL_QUADS);
        glTexCoord2f(0,0);
        glVertex2f(x,y);
        glTexCoord2f(1,0);
        glVertex2f(x+t.getTextureWidth(),y);
        glTexCoord2f(1,1);
        glVertex2f(x+t.getTextureWidth(),y+t.getTextureHeight());
        glTexCoord2f(0,1);
        glVertex2f(x,y+t.getTextureHeight());
        glEnd();
        t.unbind();
}

Though, this raise the question of how leaky OpenGL states could be used with OOP languages. Is it appropriate to call OpenGL functions that can affect the global state in class objects.. One way to handle this is to restrict OpenGL calls in certain(one?) places, for example OpenGLRenderer class.

\$\endgroup\$
1
  • \$\begingroup\$ Thanks, i never saw any tutorials unbind it so I thought it was my error. \$\endgroup\$
    – The User
    Commented Apr 12, 2015 at 16:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .