Sign in to follow this  
Forkaster

Vertex Buffer Objects

Recommended Posts

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

}

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this