Hi,
I just started with OpenGL, so this question is certainly a silly one, but I hope it's easy to answer for a lot of you.
I just started experimenting with OpenGL, and what I'm trying to do is drawing an indexed vertex array, which is just a simple cube. I took the vertices and indices from a blender x3d export and copied them into an array. I also evaluated the vertices and triangels on a paper and they should be correct. The result on the screen however is not much of a cube. It's a square and a few triangles pointing in weird directions.
So here is the code of my draw method, any help would be highly appreciated.
I guess there's something completely wrong with the way I thought drawing indexed primitives might work.
Thank in advance.
those guys are defined globally:
and here's the drawing code, it draws a cube and updates the lookat to give a crappy flyby effect:
const GLfloat cubeVertices[] =
{
1, 1, -1,
1, -1, -1,
-1, -1, -1,
-1, 1, -1,
1, 1, 1,
1 -1, 1,
-1, -1, 1,
-1, 1, 1,
};
const GLushort cubeIndices[] =
{
0, 1, 2,
0, 2, 3,
4, 7, 6,
4, 6, 5,
0, 4, 5,
0, 5, 1,
1, 5, 6,
1, 6, 2,
2, 6, 7,
2, 7, 3,
4, 0, 3,
4, 3, 7,
};
const GLubyte squareColors[] =
{
255, 255, 0, 255,
0, 255, 255, 255,
0, 0, 0, 0,
255, 0, 255, 255,
0, 0, 0, 0,
255, 0, 255, 255,
255, 255, 0, 255,
0, 255, 255, 255,
0, 0, 0, 0,
255, 0, 255, 255,
0, 0, 0, 0,
255, 0, 255, 255,
255, 255, 0, 255,
0, 255, 255, 255,
0, 0, 0, 0,
255, 0, 255, 255,
0, 0, 0, 0,
255, 0, 255, 255,
255, 255, 0, 255,
0, 255, 255, 255,
0, 0, 0, 0,
255, 0, 255, 255,
0, 0, 0, 0,
255, 0, 255, 255,
255, 255, 0, 255,
0, 255, 255, 255,
0, 0, 0, 0,
255, 0, 255, 255,
0, 0, 0, 0,
255, 0, 255, 255,
255, 255, 0, 255,
0, 255, 255, 255,
0, 0, 0, 0,
255, 0, 255, 255,
0, 0, 0, 0,
255, 0, 255, 255,
};
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
GLfloat zNear = 0.1, zFar = 1000.0, fieldOfView = 60.0;
GLfloat size = 0;
size = zNear * tanf(DEGREES_TO_RADIANS(fieldOfView) / 2.0);
glFrustum(-size, size, -size / (640.0f / 480.0f), size / (640.0f / 480.0f), zNear, zFar);
cameraX -= 0.01f;
gluLookAt(4.0f, cameraX, cameraX, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
glMatrixMode(GL_MODELVIEW);
glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glVertexPointer(3, GL_FLOAT, 0, cubeVertices);
glEnableClientState(GL_VERTEX_ARRAY);
glColorPointer(4, GL_UNSIGNED_BYTE, 0, squareColors);
glEnableClientState(GL_COLOR_ARRAY);
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_SHORT, cubeIndices);
glFlush();