我的程序的目的是在每个面上加载和显示一个具有相同纹理的简单立方体。问题是前两个面(前,后)都很好。我在纹理数组中尝试了几种顶点组合,但它不起作用。我不知道是否需要在纹理数组中添加更多顶点或更改顺序或更改索引数组。
#define OFFSET_BUFFER(bytes) ((GLfloat *)NULL + bytes)
float vertices[] =
{
-1.0f, -1.0f, 1.0f, // 0
1.0f, -1.0f, 1.0f, // 1
-1.0f, 1.0f, 1.0f, // 2
1.0f, 1.0f, 1.0f, // 3
-1.0f, -1.0f, -1.0f,// 4
1.0f, -1.0f, -1.0f, // 5
-1.0f, 1.0f, -1.0f, // 6
1.0f, 1.0f, -1.0f, // 7
};
GLubyte indices[] =
{
0,1,2, 1,3,2, //front face
6,7,5, 6,5,4, //rear face
1,5,3, 5,7,3, //problem on the right face
//2,3,6, 3,6,7,
//2,4,0, 2,4,6
0,5,1, 5,4,0
};
float textures[] =
{
0.0f, 1.0f,//0
1.0f, 1.0f,//1
0.0f, 0.0f,//2
1.0f, 0.0f,//3
0.0f, 1.0f,//0
1.0f, 1.0f,//1
0.0f, 0.0f,//2
1.0f, 0.0f,//3
};
int main(int argc, char *argv[])
{
SDL_Init(SDL_INIT_VIDEO);
SDL_WM_SetCaption("Texture Mapping",NULL);
SDL_SetVideoMode(500, 500, 32, SDL_OPENGL);
bool continuer = true;
SDL_Event event;
GLuint texCube;
glClearDepth(1.0f);
glClearColor(0.1f, 0.1f, 0.1f, 0.1f);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(70.0f, (float)500.0f / (float)500.0f, 1.0f, 3000.0f);
glewInit();
texCube = loadTexture("caisse.jpg");
glBindTexture(GL_TEXTURE_2D, texCube);
glEnable(GL_TEXTURE_2D);
while (continuer)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
SDL_WaitEvent(&event);
switch(event.type)
{
case SDL_QUIT:
continuer = false;
}
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0f, 0.0f, 3.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
//Draw Cube ---------------------------------------
glPushMatrix();
glRotatef(90.0f, 1.0f, 1.0f, 0.0f);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(2, GL_FLOAT, 0, textures);
glDrawElements(GL_TRIANGLES, 24, GL_UNSIGNED_BYTE, indices);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
//-------------------------------------------------
glPopMatrix();
glFlush();
SDL_GL_SwapBuffers();
}
glDisable(GL_TEXTURE_2D);
SDL_Quit();
return 0;
}