Sarper Sarper - 1 month ago 12
C Question

When I display image, I can't use colors anymore openGL

When I open and display an image in openGL, all colors on screen (except image's) suddenly turn into black and I can't use another color to draw anymore.

It's my colorful button on screen

void drawButton(int x1,int y1,int x2,int y2,int r,int g,int b)
{
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glColor3f(r,g,b);
glBegin(GL_QUADS);
glVertex2f(x1,y1);
glVertex2f(x2,y1);
glVertex2f(x2,y2);
glVertex2f(x1,y2);
glEnd();
glFlush();
glutSwapBuffers();
}


And here's display image function

void display_image(char* file_name)
{
GLuint texture_id=loadIm(file_name);
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture_id);
glLoadIdentity();
glColor3f(1, 1, 1);
glFrontFace(GL_CCW);
glCullFace(GL_FRONT);
glBindTexture(GL_TEXTURE_2D, texture_id);
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
glBegin(GL_QUADS);
glTexCoord2f(0,1); glVertex2f(0, 0.0);
glTexCoord2f(0,0); glVertex2f(0, wh);
glTexCoord2f(1,0); glVertex2f(ww,wh);
glTexCoord2f(1,1); glVertex2f(ww, 0.0);
glEnd();
glFlush();
glutSwapBuffers();
drawArea();

}


EDIT: glDisable(GL_TEXTURE_2D)

BDL BDL
Answer

OpenGL is a state machine. This means when one enables GL_TEXTURE_2D to draw with textures, this has to be disabled afterwards to render with colors only. Calling glDisable(GL_TEXTURE_2D); before starting to render the colored polygon should fix the problem.