Sign in to follow this  
YellowMaple

Help using vbo's

Recommended Posts

YellowMaple    174
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]

Share this post


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

Share this post


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

Share this post


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

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