openGL texture and colors
12,901
Solution 1
OpenGL is a state machine. This means that the last texture coordinate set will be applied to all following vertices. So even if you stop supplying texture coordinates it will still apply the texture's colour at the last set texture coordinate applied to the whole faces.
To stop texturing you have to disable texturing with glDisable(GL_TEXTURE_2D);
Putting it in your code:
// ...
App.SetActive();
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D); // <- enable texturing for the one face
glBegin(GL_QUADS);
//back
glColor3f(0.0f,0.0f,0.0f); //cyan
glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.2f,0.2f,0.2f); //top left
glTexCoord2f(1.0f, 1.0f); glVertex3f(0.2f,0.2f,0.2f); //top right
glTexCoord2f(1.0f, 0.0f); glVertex3f(0.2f,-0.2f,0.2f); //bottom right
glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.2f,-0.2f,0.2f); //bottom left
glEnd(); // <- first end the current primitive list
glDisable(GL_TEXTURE_2D); // <- disable texturing for the rest of the faces
glBegin(GL_QUADS);
//bottom
glColor3f(0.0f,1.0f,0.0f); //green
glVertex3f(0.2f,-0.2f,0.2f); //back right
glVertex3f(-0.2f,-0.2f,0.2f); //back left
glVertex3f(-0.2f,-0.2f,-0.2f); //front left
glVertex3f(0.2f,-0.2f,-0.2f); //front right
//front
//...
Solution 2
Try enabling GL_TEXTURE2D right before your textured faces are drawn, and disable it immediately after.
...
//back
glEnable(GL_TEXTURE2D);
glBegin(GL_QUADS);
glColor3f(0.0f,0.0f,0.0f); //cyan
glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.2f,0.2f,0.2f); //top left
glTexCoord2f(1.0f, 1.0f); glVertex3f(0.2f,0.2f,0.2f); //top right
glTexCoord2f(1.0f, 0.0f); glVertex3f(0.2f,-0.2f,0.2f); //bottom right
glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.2f,-0.2f,0.2f); //bottom left
glEnd();
glDisable(GL_TEXTURE2D);
glBegin(GL_QUADS);
//bottom
...
Author by
wbaccari
Updated on June 14, 2022Comments
-
wbaccari about 2 years
I'm learning how to apply texture in openGL. I applied a texture to one face of a cube and colored the other faces using primitive colors. Before i apply the texture the colors looked fine, and after i apply it they become darker. what's wrong?
void LoadGLTextures() { // Load Texture Image *image1; // allocate space for texture image1 = (Image *) malloc(sizeof(Image)); if (image1 == NULL) { printf("Error allocating space for image"); exit(0); } if (!ImageLoad("dd.bmp", image1)) { exit(1); } // Create Texture glGenTextures(1, &texture[0]); glBindTexture(GL_TEXTURE_2D, texture[0]); // 2d texture (x and y size) glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // scale linearly when image bigger than texture glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // scale linearly when image smalled than texture // 2d texture, level of detail 0 (normal), 3 components (red, green, blue), x size from image, y size from image, // border 0 (normal), rgb color data, unsigned byte data, and finally the data itself. glTexImage2D(GL_TEXTURE_2D, 0, 3, image1->sizeX, image1->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, image1->data); } int main() { sf::Window App(sf::VideoMode(600, 600, 32), "SFML OpenGL", sf::Style::Close); glEnable(GL_DEPTH_TEST); glDepthMask(GL_TRUE); glDepthFunc(GL_LEQUAL); glEnable(GL_TEXTURE_2D); glShadeModel(GL_SMOOTH); LoadGLTextures(); glBindTexture(GL_TEXTURE_2D, texture[0]); while (App.IsOpened()) { App.SetActive(); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glBegin(GL_QUADS); //back glColor3f(0.0f,0.0f,0.0f); //cyan glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.2f,0.2f,0.2f); //top left glTexCoord2f(1.0f, 1.0f); glVertex3f(0.2f,0.2f,0.2f); //top right glTexCoord2f(1.0f, 0.0f); glVertex3f(0.2f,-0.2f,0.2f); //bottom right glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.2f,-0.2f,0.2f); //bottom left //bottom glColor3f(0.0f,1.0f,0.0f); //green glVertex3f(0.2f,-0.2f,0.2f); //back right glVertex3f(-0.2f,-0.2f,0.2f); //back left glVertex3f(-0.2f,-0.2f,-0.2f); //front left glVertex3f(0.2f,-0.2f,-0.2f); //front right //front glColor3f(0.0f,0.0f,1.0f); //blue glVertex3f(0.2f,-0.2f,-0.2f); //bottom right glVertex3f(-0.2f,-0.2f,-0.2f); //bottom left glVertex3f(-0.2f,0.2f,-0.2f); //top left glVertex3f(0.2f,0.2f,-0.2f); //top right //top glColor3f(1.0f,1.0f,0.0f); //yellow glVertex3f(0.2f,0.2f,-0.2f); //front right glVertex3f(-0.2f,0.2f,-0.2f); //front left glVertex3f(-0.2f,0.2f,0.2f); //back left glVertex3f(0.2f,0.2f,0.2f); //back right //left glColor3f(0.0f,1.0f,1.0f); //pink glVertex3f(-0.2f,-0.2f,-0.2f); //bottom front glVertex3f(-0.2f,-0.2f,0.2f); //bottom back glVertex3f(-0.2f,0.2f,0.2f); //top back glVertex3f(-0.2f,0.2f,-0.2f); //top front //right glColor3f(1.0f,0.0f,0.0f); //red glVertex3f(0.2f,-0.2f,-0.2f); //bottom front glVertex3f(0.2f,-0.2f,0.2f); //bottom back glVertex3f(0.2f,0.2f,0.2f); //top back glVertex3f(0.2f,0.2f,-0.2f); //top front glEnd(); glFlush(); App.Display(); sf::Event Event; while (App.GetEvent(Event)) { if (Event.Type == sf::Event::Closed) App.Close(); if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape)) App.Close(); if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Right)) glRotated(20.0d,0.0d,0.2d,0.0d); if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Left)) glRotated(-20.0d,0.0d,0.2d,0.0d); if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Up)) glRotated(20.0d,0.2d,0.0d,0.0d); if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Down)) glRotated(-20.0d,0.2d,0.0d,0.0d); if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::PageUp)) glRotated(20.0d,0.0d,0.0d,0.2d); if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::PageDown)) glRotated(-20.0d,0.0d,0.0d,0.2d); } } return EXIT_SUCCESS; }
-
datenwolf about 13 years
glEnable(…)
/glDisable(…)
is not permitted within aglBegin(…)
/glEnd()
block. -
Nicol Bolas about 13 years-1: for not being correct OpenGL. As Datenwolf said, you can't activate or deactivate textures in the middle of glBegin/glEnd.
-
hughes about 13 yearsok. Apart from that is glDisable(GL_TEXTURE2D) not the solution?
-
BЈовић about 11 yearsafter enabling, you need to bind to a texture
-
datenwolf about 11 years@BЈовић: I ommited that part deliberately. Also a texture doesn't unbind by disabling texturing.
-
BЈовић about 11 yearsOk, I just compared your and his solutions (he posted as other answer). His differs only in bind. But looking at his question, I see he binds after generating a texture