Sign in to follow this  
riruilo

Problem with glDrawArrays

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_ELEMENT_ARRAY_BUFFER, 0);
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,      
    };
	
   	glEnableClientState(GL_VERTEX_ARRAY);
	glEnableClientState(GL_TEXTURE_COORD_ARRAY);
	
	glVertexPointer(3, GL_FLOAT, 0, vertices);
    glTexCoordPointer(2, GL_SHORT, 0, texcoords);
    
	glEnable(GL_TEXTURE_2D); 
	glBindTexture( GL_TEXTURE_2D,m_texture_id_fondo); 
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);    
	glColor4f(1, 1, 1, 1);
	glPushMatrix();
		glTranslatef(256,256,0);
		glScalef(100,100,0);
		glDrawArrays(GL_QUADS, 0, 4);
	glPopMatrix();
	glDisable(GL_TEXTURE_2D); 

	glDisableClientState(GL_TEXTURE_COORD_ARRAY);
	glDisableClientState(GL_VERTEX_ARRAY);

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

glDraw

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

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