8

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
  • 1,270
  • 1
  • 18
  • 34
  • 3
    why do you need the "fastest/lowest memory", do you plan on having a billion cubes on screen? Also, what do you mean with "redefine some vertex"? – Luke B. Jan 24 '13 at 16:31
  • After i have set the cube textures, the fps is decreased (from 60 to 40). I have redefine some vertex in the "vertices" array, if you look closely there are 16 points(3D) in that array, but to define a cube 8 are enough. – Luca Jan 24 '13 at 16:35
  • 2
    You're doing something very wrong then and it's probably not the cube, 8 or 16 vertices won't make much a difference. Also consider that each vertex connect 3 faces and they all have a different uv coordinate, you need those "extra" vertices. I'm not sure, but you only have to call glTexParameteri once, when you create the texture, if that's on your loop it might be the reason for the slowdown. – Luke B. Jan 24 '13 at 16:54

2 Answers2

2

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.

jmathew
  • 1,522
  • 17
  • 29
  • 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. – Seideun Jun 19 '22 at 07:13
0

Well, you can use cube map textures in which your vertex shader will take care of texture coordinates. All you have to do to generate tex coords is add this line in your vertex shader:

out vec2 texcoords;
texcoords=normalize(vertex_data.xy)

No need to redefine vertices, also you can improve performance by using vertex buffer objects.

debonair
  • 2,505
  • 4
  • 33
  • 73