Jump to content
  • Advertisement
Sign in to follow this  
Forkaster

Vertex Buffer Objects

This topic is 3428 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 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
Advertisement
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.

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
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!