The best way to texture a cube in openGL

16,093

I ran into the same issue a while back working with opengl es. My research indicated that there was no other way to do it because each vertex could only have one texture coordinate associated with it.

Share:
16,093
Luca
Author by

Luca

Computer Vision Engineer @ Intel

Updated on June 09, 2022

Comments

  • Luca
    Luca almost 2 years

    Which is the best way (lowest memory, fastest speed) to texture a cube? after a while i have find this solution:

    data struct:

    GLfloat Cube::vertices[] =
     {-0.5f, 0.0f, 0.5f,   0.5f, 0.0f, 0.5f,   0.5f, 1.0f, 0.5f,  -0.5f, 1.0f, 0.5f,
      -0.5f, 1.0f, -0.5f,  0.5f, 1.0f, -0.5f,  0.5f, 0.0f, -0.5f, -0.5f, 0.0f, -0.5f,
      0.5f, 0.0f, 0.5f,   0.5f, 0.0f, -0.5f,  0.5f, 1.0f, -0.5f,  0.5f, 1.0f, 0.5f,
      -0.5f, 0.0f, -0.5f,  -0.5f, 0.0f, 0.5f,  -0.5f, 1.0f, 0.5f, -0.5f, 1.0f, -0.5f
      };
    
     GLfloat Cube::texcoords[] = { 0.0,0.0, 1.0,0.0, 1.0,1.0, 0.0,1.0,
                                   0.0,0.0, 1.0,0.0, 1.0,1.0, 0.0,1.0,
                                   0.0,0.0, 1.0,0.0, 1.0,1.0, 0.0,1.0,
                                   0.0,0.0, 1.0,0.0, 1.0,1.0, 0.0,1.0
                                 };
    
     GLubyte Cube::cubeIndices[24] = {0,1,2,3, 4,5,6,7, 3,2,5,4, 7,6,1,0,
                                      8,9,10,11, 12,13,14,15};
    

    draw function:

            glEnable(GL_TEXTURE_2D);
            glBindTexture(GL_TEXTURE_2D, texture);
            glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
            glColor3f(1.0f, 1.0f, 1.0f);
    
            glEnableClientState(GL_TEXTURE_COORD_ARRAY);
            glEnableClientState(GL_VERTEX_ARRAY);
    
            glTexCoordPointer(2, GL_FLOAT, 0, texcoords);
            glVertexPointer(3, GL_FLOAT, 0, vertices);
    
            glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, cubeIndices);
            glDisableClientState(GL_VERTEX_ARRAY);
            //glDisableClientState(GL_COLOR_ARRAY);
            glDisable(GL_TEXTURE_2D);
    

    As you can see the result is correct: cube

    but to rech this result i have to redefine some vertex (there are 16 3D points in the vertices array) otherwise the texture fail to map in the DrawElement function.

    Someone knows a better way to texture a cube in a vertex array?

  • Luca
    Luca over 11 years
    it's to early for me the use of a shader
  • debonair
    debonair over 11 years
    i don't get you what do you mean by "early for me".. ?
  • Nicol Bolas
    Nicol Bolas over 11 years
    @PhantomFav: That's no excuse. You should be starting with shaders. Shaders are not "advanced"; they're standard nowadays.
  • Seideun
    Seideun almost 2 years
    You are right. I read from my GPU textbook that one of the limitations of GPU is that we can hardly communicate with other parallel vertices, so there should be some duplicated vertices for each facet.