4
votes

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;
}
3
How are you generating the cube and the textured face? If you post some of your code it may be easier to answer your question.hughes
Can you show us some code, and perhaps a picture? ThxNick Rolando
Okay, I see the LoadGLTextures() function, but what about where you're generating the geometry?hughes
any ideas ? i just want to apply the texture on one side and leave the other sides colored correctly.wbaccari

3 Answers

6
votes

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

//...
2
votes

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
...
0
votes

you are absolutely right, here is the correct code now:

    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, texture);
    glBegin(GL_QUADS);
    //back
    glColor3f(1.0f,1.0f,1.0f); //white
    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_TEXTURE_2D);
    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
    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();