Vertex Buffer Objects

Started by
5 comments, last by Stani R 14 years, 10 months ago
Hi everyone, I am trying to draw my terrain with a VBO. The thing is i can't get it to "draw. Can some one help me? Here is my source:

void terrainVBO()
{
	terrainDisplayList = glGenLists(1);
	
	terrainLoadRandomFile();
		
	double* vertexB = (double*) malloc(sizeof(double)*currentTerrain.width*currentTerrain.height*3);
	double* normalB = currentTerrain.normals; /*(double*) malloc(sizeof(double)*currentTerrain.width*currentTerrain.height*3)*/;
	
	for(int i = 0;  i < currentTerrain.width; i++)
	{
		for(int j = 0; j < currentTerrain.height; j++)
		{
			vertexB[(i*currentTerrain.height+j)*3+0 ] = i;
			vertexB[(i*currentTerrain.height+j)*3+1 ] = terrainGetHeight(i, j);
			vertexB[(i*currentTerrain.height+j)*3+2 ] = j;
		}
	}

	GLuint * buffers = (GLuint*) malloc(sizeof(GLuint)*2);
	
	glGenBuffers(2, buffers);
	
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,buffers[0]);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(vertexB),vertexB,GL_STATIC_DRAW);
	glVertexPointer(3,GL_DOUBLE,0,0);
	
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,buffers[1]);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(normalB),normalB,GL_STATIC_DRAW);
	glNormalPointer(GL_DOUBLE,0,0);
	
	glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT);
	glEnableClientState(GL_VERTEX_ARRAY);
	glEnableClientState(GL_NORMAL_ARRAY);
	
	glNewList(terrainDisplayList, GL_COMPILE);
	
	glDrawElements(GL_TRIANGLE_STRIP, currentTerrain.width*currentTerrain.height, GL_DOUBLE, &buffers);
			
	glEndList();
	glPopClientAttrib();

}

Advertisement
Have you tried calling glGetError()?
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);
First thing that I see is GL_ELEMENT_ARRAY_BUFFER. This is usually used for indices; for vertex data you should use GL_ARRAY_BUFFER instead.
The last parameter of glDrawElements is a pointer to an indices array, or NULL if you're using a GL_ELEMENT_ARRAY_BUFFER (Which contains index data). So passing the VBO ID array isn't going to work.
Hi again,

Thanks for your help, but i still haven't nailed it.

I have added a few glGetError() calls to see what I'd get.

I got a error number 1280 after glVertexPointer() I think it means invalid enum, but which one?.

I have also changed my code to use GL_ARRAY_BUFFER.

It looks like this now:

void terrainVBO(){	terrainDisplayList = glGenLists(1);		terrainLoadRandomFile();			double* vertexB = (double*) malloc(sizeof(double)*currentTerrain.width*currentTerrain.height*3);	double* normalB = currentTerrain.normals;		for(int i = 0;  i < currentTerrain.width; i++)	{		for(int j = 0; j < currentTerrain.height; j++)		{			vertexB[(i*currentTerrain.height+j)*3+0 ] = i;			vertexB[(i*currentTerrain.height+j)*3+1 ] = terrainGetHeight(i, j);			vertexB[(i*currentTerrain.height+j)*3+2 ] = j;		}	}	GLuint * buffers = (GLuint*) malloc(sizeof(GLuint)*2);		glGenBuffers(2, buffers);		glBindBuffer(GL_ARRAY_BUFFER,buffers[0]);	glBufferData(GL_ARRAY_BUFFER,sizeof(vertexB),vertexB,GL_STATIC_DRAW);	glVertexPointer(3,GL_DOUBLE,0,0);	GLenum err = glGetError();		glBindBuffer(GL_ARRAY_BUFFER,buffers[1]);	glBufferData(GL_ARRAY_BUFFER,sizeof(normalB),normalB,GL_STATIC_DRAW);	glNormalPointer(GL_DOUBLE,0,0);	err = glGetError(); 		glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT);	glEnableClientState(GL_VERTEX_ARRAY);	glEnableClientState(GL_NORMAL_ARRAY);		glNewList(terrainDisplayList, GL_COMPILE);		glDrawArrays(GL_TRIANGLE_STRIP,0,currentTerrain.width*currentTerrain.height);				glEndList();	glPopClientAttrib();}


can some one help?
Why are you using doubles? AFAIK their isn't hardware support for them anyway....

Also what are you doing making a display list and a VBO?


Take a look at this site for a good run down on VBOs
http://www.opengl.org/wiki/GL_ARB_vertex_buffer_object

I'm out of ideas. The code looks correct up to the point where you start doing fancy things (compiling the vbo into a display list.. not sure that's even possible). You are getting that error code on your first time calling the function terrainVBO()?

This topic is closed to new replies.

Advertisement