opengl beginner problems with glDrawElements

Started by
0 comments, last by V-man 15 years, 9 months ago
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:


float cameraX = 4.0f;
#define DEGREES_TO_RADIANS(__ANGLE__) ((__ANGLE__) / 180.0 * M_PI)



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();	





Advertisement
It's because of the index list.

Here's some better code
http://www.geocities.com/vmelkon/glhlibrary.html

//HOW TO USE://glhCubeObject2 Cube;//memset(&Cube, 0, sizeof(glhCubeObject2));//Cube.Width=1.0; Cube.Height=1.0; Cube.Length=1.0;//Cube.WidthDivision=1; Cube.HeightDivision=1; Cube.LengthDivision=1;//Cube.AverageNormals=GL_FALSE;//Cube.IndexFormat=GLH_INDEXFORMAT_16BIT;//Cube.VertexFormat=GLHVERTEXFORMAT_VNT;//Cube.TexCoordStyle[0]=1;////glhCreateCubef2(&Cube);////HOW TO RENDER (You might want to use VBO, I'm just using VA here)://glEnable(GL_TEXTURE_2D);//glBindTexture(GL_TEXTURE_2D, TextureID);////glVertexPointer(3, GL_FLOAT, sizeof(GLHVertex_VNT), Cube.pVertex);//uint mypointer=(uint)Cube.pVertex;//mypointer+=12;//glNormalPointer(GL_FLOAT, sizeof(GLHVertex_VNT), (uint *)mypointer);//mypointer+=12;//glTexCoordPointer(2, GL_FLOAT, sizeof(GLHVertex_VNT), (uint *)mypointer);////glDrawRangeElements(GL_TRIANGLES, Cube.Start_DrawRangeElements, Cube.End_DrawRangeElements,//    Cube.TotalIndex, GL_UNSIGNED_SHORT, Cube.pIndex16Bit);////and to deallocate, call glhDeleteCubef2(&Cube);
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

This topic is closed to new replies.

Advertisement