Jump to content
  • Advertisement
Sign in to follow this  
CirdanValen

Trying to get VBO up and running

This topic is 2721 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

I've done VBOs so many times you would think I would have figured it out by now >.<. I'm getting an Access Violation (reading 0x000008) on glDrawArrays. I've practically copied code that I've written before which works perfectly fine. Something changed that caused it to break?

I'm generating a VBO for sprites like this:
Sprite::Sprite(Texture *t, Rect& src) : 
mTexture(t), mSrcRect(src) {

// Generate the VBO
glGenBuffersARB(1, &mVbo);

int texW = t->getWidth();
int texH = t->getHeight();

// Calculate the verticies
Vertex verts[] = {{0, 0, src.x / texW, src.y / texH},
{src.w, 0, (src.x + src.w) / texW, src.y / texH},
{src.w, src.h, (src.x + src.w) / texW, (src.y + src.h) / texH},
{0, src.h, src.x / texW, (src.y + src.h) / texH}};

int vertSize = sizeof(verts);

// Bind the VBO
glBindBufferARB(GL_ARRAY_BUFFER_ARB, mVbo);

// Submit the vertex data to the GPU
glBufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(Vertex) * 4, &verts[0].x, GL_STATIC_DRAW_ARB);

// Unbind the VBO
glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
}


Game loop looks like this:
		g->beginScene();

g->beginSpriteBatch(t);
g->drawSprite(s, 10, 10);
g->endSpriteBatch();

g->endScene();


And the code behind it:
void Graphics::drawSprite(Sprite *s, int x, int y) {

glBindBufferARB(GL_ARRAY_BUFFER, s->getVbo());
glDrawArrays(GL_QUADS, 0, 4);
}

void Graphics::beginSpriteBatch(Texture *t) {
glBindTexture(GL_TEXTURE_2D, t->getId());

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);

glVertexPointer(2, GL_FLOAT, sizeof(Vertex), (void*)offsetof(Vertex, x));
glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), (void*)offsetof(Vertex, tx));
}

void Graphics::endSpriteBatch() {
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
}

void Graphics::beginScene() {
glClear(GL_COLOR_BUFFER_BIT);
}

void Graphics::endScene() {
SDL_GL_SwapBuffers();
}

Share this post


Link to post
Share on other sites
Advertisement
You need to bind the buffer before you call glVertexPointer, not before glDrawArrays. glDrawArrays doesn't care what is bound, only where the vertexpointer is.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!