Help using vbo's

Started by
4 comments, last by karwosts 13 years, 11 months ago
I'm trying to use vbo's with a simple shader but I get a crash when calling glDrawElements.

glUseProgram(shader);
glBindBuffer(GL_ARRAY_BUFFER_ARB, vbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, ibo);

GLuint attr;
attr = glGetAttribLocation(shader, "inPosition");
glEnableVertexAttribArray(attr);
glVertexAttribPointer(attr, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), cube_verts);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.f, 640.f/480.f, 0.2f, 1000.f);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.f, 0.f, 2.f,
	0.f, 0.f, 0.f,
	0.f, 1.f, 0.f);


glColor3f(0.f, 1.f, 0.f);
glDrawElements(GL_TRIANGLES, 12, GL_UNSIGNED_SHORT, 0);




As far as I can tell, my shader and vertex/index buffers have been successfully created. My vertex shader looks like this:

attribute vec4 inPosition;

uniform mat4 WorldView;
uniform mat4 Projection;

void main()
{
	gl_Position = gl_ModelViewMatrix * inPosition;
}



The data I used for the vbo and ibo:

struct Vertex
{
    float x, y, z, w;
}

Vertex unit_cube_verts[8] = {{1.f, 1.f, -1.f, 1.0},
{1.f, 1.f, 1.f, 1.0},
{-1.f, 1.f, 1.f, 1.0},
{-1.f, 1.f, -1.f, 1.0},
{1.f, -1.f, -1.f, 1.0},
{1.f, -1.f, 1.f, 1.0},
{-1.f, -1.f, 1.f, 1.0},
{-1.f, -1.f, -1.f, 1.0} };

uint16 unit_cube_indices[36] =	{1, 2, 3,
3, 4, 1,
5, 1, 4,
4, 8, 5,
7, 6, 5,
5, 8, 7,
3, 2, 6,
6, 7, 3,
6, 2, 1,
1, 5, 6,
4, 3, 7,
7, 8, 4 };



Am I setting things up correctly? The crash is a memory access violation: First-chance exception at 0x0033ff09 in Droid.exe: 0xC0000005: Access violation reading location 0x018dc368. Unhandled exception at 0x0033ff09 in Droid.exe: 0xC0000005: Access violation reading location 0x018dc368. Thanks! [Edited by - YellowMaple on May 2, 2010 11:22:30 PM]
Advertisement
I think your IBO should be indexed from zero, not from one. Your IBO is reaching one beyond the number of vertices you have, which could be causing the crash.
[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game
you can read some stuff at
http://www.opengl.org/wiki/Vertex_Arrays

http://www.opengl.org/wiki/Vertex_Buffer_Object
http://www.opengl.org/wiki/General_OpenGL
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);
D'oh! Thanks! :D
So the crash is fixed now, but I can't figure out why nothing is drawing. I've been messing around for over a night now and can't find the problem. Can anyone point out if I'm setting things up incorrectly or perhaps its just another silly error like the indexing that I can't seem to spot?

I create my vertex and index buffers like this:
GLuint vbo;glGenBuffers(1, &vbo);glBindBuffer(GL_ARRAY_BUFFER_ARB, vbo);glBufferData(GL_ARRAY_BUFFER_ARB, 8 * sizeof(Vertex), 0, GL_DYNAMIC_DRAW);glBindBuffer(GL_ARRAY_BUFFER_ARB, 0);GLuint ibo;glGenBuffers(1, &ibo);glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, ibo);glBufferData(GL_ELEMENT_ARRAY_BUFFER_ARB, 36 * sizeof(unsigned short), 0, GL_DYNAMIC_DRAW);glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);glBindBuffer(GL_ARRAY_BUFFER_ARB, vbo);glBufferSubData(vbo, 0, sizeof(Vertex) * 8, unit_cube_verts);glBindBuffer(GL_ARRAY_BUFFER_ARB, 0);glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, ibo);glBufferSubData(ibo, 0, sizeof(unsigned short) * 36, unit_cube_indices);glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);

The function I use to create my frag/vertex shaders looks like this (with VertexShader being a typedef for GLuint):
VertexShader CreateVertexShader(const char* source){	std::ifstream file;	size_t size;	char* buffer;	file.open(source, std::ios_base::in|std::ios_base::binary);	if(!file.good())	{		check(0);		file.close();		return 0;	}	file.seekg(0, std::ios::end);	size = file.tellg();	file.clear();	file.seekg(0, std::ios::beg);	buffer = new char[ size + 1 ];	file.read(buffer, size);	buffer[ size ] = 0;	file.close();	VertexShader shader = glCreateShader(GL_VERTEX_SHADER);	glShaderSource(shader, 1, (const GLchar**)&buffer, 0);	glCompileShader(shader);	GLint compiled;	glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);	if (!compiled)	{		int32 logLength;		int32 charsWritten;		glGetShaderiv(shader, GL_INFO_LOG_LENGTH, (GLint*)&logLength);		if (logLength > 0)		{			char* infoLog = new char[logLength];			glGetInfoLogARB(shader, logLength, (GLsizei*)&charsWritten, infoLog);			std::cerr << infoLog << std::endl;			delete[] infoLog;		}		check(0);	}	delete[] buffer;	return shader;}

I create the shader program like this (again, ShaderProgram is a typedef for GLuint):
ShaderProgram CreateShaderProgram(VertexShader vShader, FragmentShader pShader){	ShaderProgram prog = glCreateProgram();	glAttachShader(prog, vShader);	glAttachShader(prog, pShader);	glLinkProgram(prog);	GLint linked;	glGetProgramiv(prog, GL_LINK_STATUS, &linked);	if (!linked)	{	   check(0);	}	return prog;}

And finally the code to render my cube data which is the same as in my original post, with the indices decremented by 1:
glUseProgram(shader);glBindBuffer(GL_ARRAY_BUFFER_ARB, vbo);glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, ibo);GLuint attr = glGetAttribLocation(shader, name);glEnableVertexAttribArray(attr);glVertexAttribPointer(attr, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), unit_cube_verts);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluPerspective(60.f, 640.f/480.f, 0.2f, 1000.f);glMatrixMode(GL_MODELVIEW);glLoadIdentity();gluLookAt(	0.f, 0.f, -2.f,		0.f, 0.f, 0.f,		0.f, 1.f, 0.f);glColor4f(0.f, 1.f, 0.f, 1.f);glDrawElements(GL_TRIANGLES, 12, GL_UNSIGNED_SHORT, 0);


Oh and Vertex is a struct with four floats (x, y, z, & w). I'm pretty stuck right now, any help is much appreciated. Thanks.
Whenever you run into a problem like this you need to break it down into smaller pieces until you can find out what is the problem. There are too many things that could be wrong, be it your shader logic, your view matricies, the vbo vertices, the vbo indices, the binding, etc.

Keep removing parts of the program until you are only testing one thing at a time. Its a bit of a pain but you've got a lot of stuff going on there.

Start with no shaders, no indices, and just try to draw a single triangle. Then try it with indexing. Then switch back to your original vertex and index list. Then try toggling on your shader. Build it in working pieces until you are done, and you will always know what step to look at when it stops working.

Just as a longshot, but in your original shader you posted you are not multiplying by projection matrix at all, could be a problem if you haven't fixed it yet.
[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game

This topic is closed to new replies.

Advertisement