Jump to content
  • Advertisement
Sign in to follow this  

Problem with glDrawArrays

This topic is 3396 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 friends! This is my RenderBackground code, it is just a square and a texture, my problem is that if I remove first 2 lines (glBindBuffer), the code does not work, so my question is, why does not work without these lines? I thought glDrawArrays didn't need glBindBuffer. Any idea or suggestion about what is going on? Thanks in advance.
glBindBuffer(GL_ARRAY_BUFFER, 0);

    float vertices[] = {
        -0.5, 0.5, 0.0,             
        -0.5, -0.5, 0.0,            
        0.5, -0.5, 0.0,             
        0.5, 0.5, 0.0,              
    short texcoords[] = {
        0, 1,      
        0, 0,      
        1, 0,       
        1, 1,      
	glVertexPointer(3, GL_FLOAT, 0, vertices);
    glTexCoordPointer(2, GL_SHORT, 0, texcoords);
	glBindTexture( GL_TEXTURE_2D,m_texture_id_fondo); 
	glColor4f(1, 1, 1, 1);
		glDrawArrays(GL_QUADS, 0, 4);


Share this post

Link to post
Share on other sites
Did you bind a buffer somewhere before that, maybe in your main drawing code on the previous frame? If so, that buffer would still be bound, which would explain why you need to unbind it here first.

By the way, you don't need to set glTexParameter every time you draw, that's just wasting instructions.

Share this post

Link to post
Share on other sites
Yes, probably that is the problem. If I use this code on my constructor, should I call glBindBuffer(GL_ARRAY_BUFFER, 0); after uploading data to the video card?

// buffer for vertex positions
float vertex_positions[]={ -0.5, 0.5, 0, -0.5, -0.5, 0, 0.5, 0.5, 0, 0.5, -0.5, 0 };
glGenBuffers( 1, &m_vertex_positions_buffer_id );
glBindBuffer( GL_ARRAY_BUFFER, m_vertex_positions_buffer_id );
glBufferData( GL_ARRAY_BUFFER, 3*sizeof(float)*4, vertex_positions, GL_STATIC_DRAW );

// texcoords
float vertex_texcoord[]={ 0, 1, 0, 0, 1, 1, 1, 0};
glGenBuffers(1, &m_vertex_texcoords_buffer_id );
glBindBuffer(GL_ARRAY_BUFFER, m_vertex_texcoords_buffer_id );
glBufferData(GL_ARRAY_BUFFER, 2*sizeof(float)*4, vertex_texcoord, GL_STATIC_DRAW );

// buffer for indices
unsigned char indices[]={ 0, 1, 2, 1, 3, 2};
glGenBuffers(1, &m_vertex_indices_buffer_id );
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_vertex_indices_buffer_id);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned char)*6, indices, GL_STATIC_DRAW);

Share this post

Link to post
Share on other sites
Turn them off after drawing with them

glBindBuffer(..., value);
glBindBuffer(..., value);
// value is the current active source for draw data


glBindBuffer(..., 0);
glBindBuffer(..., 0);
// buffers disabled to allow drawing from your local arrays for renderbackground
// useing gl*Pointer() calls

Share this post

Link to post
Share on other sites
The way it works is, if you have a VBO bound, then the last parameter of gl*Pointer is the offset in the buffer and not the pointer to your data. So if you want to draw without VBO (just plain VA) after drawing with VBO, you of course have to unbind the VBO first. And glDrawArrays works with the VBO if a VBO is bound, too.

Share this post

Link to post
Share on other sites
I'm using glDrawArrays and glDrawElements in the same program, is that a problem?

Thanks for replies.

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!