Reputation: 5641
the purpose of my program is to load and display a simple cube with the same texture on each face. the problem is the 2 first faces (front, rear) are good. I tried several combinations of vertices in the textures array but it doesn't work. I don't know if I need to add more vertices in the textures array or change the order or change the indices array.
#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;
}
Upvotes: 0
Views: 964
Reputation: 33
If you really want to save a few bytes, you can still factorize a few vertices (16 vertices instead of 24). You consider your cube without two opposite faces and add these two faces separately. Here is an IQM model which does just that:
# Inter-Quake Export
mesh "Cube"
material "cube.tga"
vp 1 1 -1
vn 1 1 -1
vt 0 0
vp 1 -1 -1
vn 1 -1 -1
vt 0 1
vp -1 -1 -1
vn -1 -1 -1
vt 1 1
vp -1 1 -1
vn -1 1 -1
vt 1 0
vp 1 1 1
vn 1 1 1
vt 1 0
vp -1 1 1
vn -1 1 1
vt 0 0
vp -1 -1 1
vn -1 -1 1
vt 0 1
vp 1 -1 1
vn 1 -1 1
vt 1 1
vp 1 1 -1
vn 1 1 -1
vt 0 0
vp -1 1 -1
vn -1 1 -1
vt 1 0
vp -1 1 1
vn -1 1 1
vt 1 1
vp 1 1 1
vn 1 1 1
vt 0 1
vp 1 1 -1
vn 1 1 -1
vt 0 0
vp -1 1 -1
vn -1 1 -1
vt 1 0
vp -1 1 1
vn -1 1 1
vt 1 1
vp 1 1 1
vn 1 1 1
vt 0 1
fm 0 1 2
fm 0 2 3
fm 0 4 7
fm 0 7 1
fm 4 6 7
fm 4 5 6
fm 5 2 6
fm 5 3 2
fm 8 9 10
fm 8 10 11
fm 12 15 14
fm 12 14 13
Upvotes: 0
Reputation: 5641
And the solution with VBO :
#define OFFSET_BUFFER(bytes) ((GLfloat *)NULL + bytes)
float angle = 0.0f;
float vertices[72] =
{
-1.0f, -1.0f, 1.0f,//VO - 0
1.0f, -1.0f, 1.0f,//V1 - 1
-1.0f, 1.0f, 1.0f,//V2 - 2
1.0f, 1.0f, 1.0f,//V3 - 3
-1.0f, -1.0f, -1.0f,//V4 - 4
1.0f, -1.0f, -1.0f,//V5 - 5
-1.0f, 1.0f, -1.0f,//V6 - 6
1.0f, 1.0f, -1.0f,//V7 - 7
-1.0f, 1.0f, 1.0f,//V2 - 8
1.0f, 1.0f, 1.0f,//V3 - 9
-1.0f, 1.0f, -1.0f,//V6 - 10
1.0f, 1.0f, -1.0f,//V7 - 11
-1.0f, -1.0f, 1.0f,//VO - 12
1.0f, -1.0f, 1.0f,//V1 - 13
-1.0f, -1.0f, -1.0f,//V4 - 14
1.0f, -1.0f, -1.0f,//V5 - 15
-1.0f, -1.0f, 1.0f,//VO - 16
-1.0f, 1.0f, 1.0f,//V2 - 17
-1.0f, -1.0f, -1.0f,//V4 - 18
-1.0f, 1.0f, -1.0f,//V6 - 19
1.0f, -1.0f, 1.0f,//V1 - 20
1.0f, 1.0f, 1.0f,//V3 - 21
1.0f, -1.0f, -1.0f,//V5 - 22
1.0f, 1.0f, -1.0f,//V7 - 23
};
GLubyte indices[36] =
{
0,1,2, 1,3,2,
6,7,5, 6,5,4,
8,9,10, 9,10,11,
12,13,14, 13,14,15,
17,16,18, 17,18,19,
20,21,22, 21,22,23
};
float textures[48] =
{
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
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
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;
GLuint VBO[3];
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");
glGenBuffers(2, VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, VBO[1]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, VBO[2]);
glBufferData(GL_ARRAY_BUFFER, sizeof(textures), textures, GL_STATIC_DRAW);
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(angle, 1.0f, 1.0f, 0.0f);
glBindTexture(GL_TEXTURE_2D, texCube);
glBindBuffer(GL_ARRAY_BUFFER, VBO[0]);
glVertexPointer(3, GL_FLOAT, 0, OFFSET_BUFFER(0));
glBindBuffer(GL_ARRAY_BUFFER, VBO[2]);
glTexCoordPointer(2, GL_FLOAT, 0, OFFSET_BUFFER(0));
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glDrawElements(GL_TRIANGLES, 48, GL_UNSIGNED_BYTE, OFFSET_BUFFER(0));
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
//-------------------------------------------------
angle += 1.0f;
glPopMatrix();
glFlush();
SDL_GL_SwapBuffers();
}
glDisable(GL_TEXTURE_2D);
SDL_Quit();
return 0;
}
Upvotes: 1
Reputation: 5641
The solution of my problem is the following code :
#define OFFSET_BUFFER(bytes) ((GLfloat *)NULL + bytes)
float angle = 0.0f;
float vertices[72] =
{
-1.0f, -1.0f, 1.0f,//VO - 0
1.0f, -1.0f, 1.0f,//V1 - 1
-1.0f, 1.0f, 1.0f,//V2 - 2
1.0f, 1.0f, 1.0f,//V3 - 3
-1.0f, -1.0f, -1.0f,//V4 - 4
1.0f, -1.0f, -1.0f,//V5 - 5
-1.0f, 1.0f, -1.0f,//V6 - 6
1.0f, 1.0f, -1.0f,//V7 - 7
-1.0f, 1.0f, 1.0f,//V2 - 8
1.0f, 1.0f, 1.0f,//V3 - 9
-1.0f, 1.0f, -1.0f,//V6 - 10
1.0f, 1.0f, -1.0f,//V7 - 11
-1.0f, -1.0f, 1.0f,//VO - 12
1.0f, -1.0f, 1.0f,//V1 - 13
-1.0f, -1.0f, -1.0f,//V4 - 14
1.0f, -1.0f, -1.0f,//V5 - 15
-1.0f, -1.0f, 1.0f,//VO - 16
-1.0f, 1.0f, 1.0f,//V2 - 17
-1.0f, -1.0f, -1.0f,//V4 - 18
-1.0f, 1.0f, -1.0f,//V6 - 19
1.0f, -1.0f, 1.0f,//V1 - 20
1.0f, 1.0f, 1.0f,//V3 - 21
1.0f, -1.0f, -1.0f,//V5 - 22
1.0f, 1.0f, -1.0f,//V7 - 23
};
GLubyte indices[36] =
{
0,1,2, 1,3,2,
6,7,5, 6,5,4,
8,9,10, 9,10,11,
12,13,14, 13,14,15,
17,16,18, 17,18,19,
20,21,22, 21,22,23
};
float textures[48] =
{
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
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
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(angle, 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, 48, GL_UNSIGNED_BYTE, indices);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
//-------------------------------------------------
angle += 1.0f;
glPopMatrix();
glFlush();
SDL_GL_SwapBuffers();
}
glDisable(GL_TEXTURE_2D);
SDL_Quit();
return 0;
}
Upvotes: 1
Reputation: 1221
Your corners are sharing texture coordinates, which just isn't going to work. you should have 4 vertices for each face, as the texture coordinates for each corner will be different depending on the face.
Upvotes: 1