OpenGl with C++ - Textures

132 views Asked by At

I'm trying to map some textures in OpenGl to a face of a cube. But, when I run my program my texture doesn't appear. So I have this block of code:

        glEnable(GL_TEXTURE_2D);
        glBindTexture(GL_TEXTURE_2D,texture[1]);

        glPushMatrix();

        glBegin(GL_QUADS);                                  // Begin drawing the color cube with 6 quads
                                                            // Top face (y = 1.0f)
                                                            // Define vertices in counter-clockwise (CCW) order with normal pointing out
        //glColor3f(0.0f, 1.0f, 0.0f);     // Green
        glVertex3f( 1.0f+i*2, -2.0f+y*2, -1.0f+j*2);
        glVertex3f(-1.0f+i*2, -2.0f+y*2, -1.0f+j*2);
        glVertex3f(-1.0f+i*2, -2.0f+y*2,  1.0f+j*2);
        glVertex3f( 1.0f+i*2, -2.0f+y*2,  1.0f+j*2);

                                                            // Bottom face (y = -1.0f)
        //glColor3f(1.0f, 0.5f, 0.0f);     // Orange
        glVertex3f( 1.0f+i*2, -4.0f+y*2,  1.0f+j*2);
        glVertex3f(-1.0f+i*2, -4.0f+y*2,  1.0f+j*2);
        glVertex3f(-1.0f+i*2, -4.0f+y*2, -1.0f+j*2);
        glVertex3f( 1.0f+i*2, -4.0f+y*2, -1.0f+j*2);

                                                            // Front face  (z = 1.0f)
        //glColor3f(1.0f, 0.0f, 0.0f);     // Red
        glTexCoord2f(0.0f,0.0f);  glVertex3f( 1.0f+i*2,  -2.0f+y*2, 1.0f+j*2);
        glTexCoord2f(10.0f,0.0f); glVertex3f(-1.0f+i*2,  -2.0f+y*2, 1.0f+j*2);
        glTexCoord2f(10.0f,10.0f); glVertex3f(-1.0f+i*2, -4.0f+y*2, 1.0f+j*2);
        glTexCoord2f(0.0f,10.0f); glVertex3f( 1.0f+i*2, -4.0f+y*2, 1.0f+j*2);

                                                            // Back face (z = -1.0f)
        //glColor3f(1.0f, 1.0f, 0.0f);     // Yellow
        glVertex3f( 1.0f+i*2, -4.0f+y*2, -1.0f+j*2);
        glVertex3f(-1.0f+i*2, -4.0f+y*2, -1.0f+j*2);
        glVertex3f(-1.0f+i*2,  -2.0f+y*2, -1.0f+j*2);
        glVertex3f( 1.0f+i*2,  -2.0f+y*2, -1.0f+j*2);

                                                            // Left face (x = -1.0f)
        //glColor3f(0.0f, 0.0f, 1.0f);     // Blue
        glVertex3f(-1.0f+i*2,  -2.0f+y*2,  1.0f+j*2);
        glVertex3f(-1.0f+i*2,  -2.0f+y*2, -1.0f+j*2);
        glVertex3f(-1.0f+i*2, -4.0f+y*2, -1.0f+j*2);
        glVertex3f(-1.0f+i*2, -4.0f+y*2,  1.0f+j*2);

                                                            // Right face (x = 1.0f)
        //glColor3f(1.0f, 0.0f, 1.0f);     // Magenta
        glVertex3f(1.0f+i*2,  -2.0f+y*2, -1.0f+j*2);
        glVertex3f(1.0f+i*2,  -2.0f+y*2,  1.0f+j*2);
        glVertex3f(1.0f+i*2, -4.0f+y*2,  1.0f+j*2);
        glVertex3f(1.0f+i*2, -4.0f+y*2, -1.0f+j*2);
        glEnd();                                            // End of drawing color-cube

        glPopMatrix();

        glDisable(GL_TEXTURE_2D);

My Init function is this:

void initGL() {
     glClearColor(0.0f, 0.0f, 0.0f, 1.0f);                       // Set background color to black and opaque
     glShadeModel(GL_SMOOTH);                                    // Enable smooth shading
     cria_texturas();
glEnable(GL_TEXTURE_2D);
     //glClearDepth(1.0f);                                         // Set background depth to farthest
     glEnable(GL_DEPTH_TEST);                                    // Enable depth testing for z-culling
     //glDepthFunc(GL_LEQUAL);                                     // Set the type of depth-test
     //glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);          // Nice perspective corrections
}

To load a texture I have written this code that uses a premade class that loads the image:

glGenTextures(1, &texture[0]);
glBindTexture(GL_TEXTURE_2D, texture[1]);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
imag.LoadBmpFile("chao.bmp");
glTexImage2D(GL_TEXTURE_2D, 0, 3,
             imag.GetNumCols(),
             imag.GetNumRows(), 0, GL_RGB, GL_UNSIGNED_BYTE,
             imag.ImageData());

So I don't understand why the texture doesn't appear in the face of the cube. Can you help me?

1

There are 1 answers

5
datenwolf On

When using the immediate mode (glBegin, glEnd) the call of glVertex is finishing the build of one single vertex. Essentially what glVertex does is coalescing the attributes that have been placed in the other attribute "registers" and sends it into a buffer.

In your code you have the following:

    glTexCoord2f(0.0f,0.0f);  glVertex3f( 1.0f+i*2,  -2.0f+y*2, 1.0f+j*2);
    glTexCoord2f(10.0f,0.0f); glVertex3f(-1.0f+i*2,  -2.0f+y*2, 1.0f+j*2);
    glTexCoord2f(10.0f,10.0f); glVertex3f(-1.0f+i*2, -4.0f+y*2, 1.0f+j*2);
    glTexCoord2f(0.0f,10.0f); glVertex3f( 1.0f+i*2, -4.0f+y*2, 1.0f+j*2);

                                                        // Back face (z = -1.0f)

    glVertex3f( 1.0f+i*2, -4.0f+y*2, -1.0f+j*2);

Only the last of these glTexCoord calls actually has an effect, the other ones will just set the register, but that value gets immediately overwritten by the following calls.

The way to use immediate mode is

foreach vertex in model:
    glColor
    glNormal
    glTexCoord
    ...
    glVertex // must be the last call for specifying a vertex!

You can not group glColor, glNormal and the other calls. But then you should not use immediate mode anyway! It has been deprecated since 1996, when OpenGL-1.1 did introduce vertex arrays. Do you really insist on using technology that has been outdated for almost 20 years?