Jump to content
  • Advertisement
Sign in to follow this  
hordak

OpenGL opengl beginner problems with glDrawElements

This topic is 3755 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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





Share this post


Link to post
Share on other sites
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);

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!