What's up with my VAO/VBO Loading/Rendering code?

Started by
6 comments, last by mynameisnafe 10 years, 2 months ago

Hi there, I've got some OpenGL 3.3+ rendering code that's not quite where it should be at in terms of behaviour, so I thought I'd get an nth opinion :P

All my vertex data is in separate buffers - no interleaved stuff happening here - however, I do have vertex position, normal, and UV data that I'd like to get into the shaders.

The problem seems to be my understanding and usage of glEnableVertexAttribArray and glVertexAttribPointer.. et la:

Any obvious errors/bus/issues?

Thanks in advance


// elsewhere:
#define POSITION_ATTRIBS 0
#define NORMAL_ATTRIBS   1
//etc

bool GL_VAO::OnCard(GeometryData* geometry)
{
	if( onCard == true )
		return true;
	
	topology = geometry->GetTopology();

	if( _VAO == -1 )
	{
		glGenVertexArrays( 1, &_VAO );				//Allocate an OpenGL VAO
		glBindVertexArray( _VAO );					//Bind the VAO to store all the buffers and attributes we create here

		if( _VxBuffer == -1 && geometry->numVerts != 0 )
		{	//position data
			numVertices = geometry->numVerts;
			glGenBuffers(1, &_VxBuffer);				//Generate an ID for the Vertex Buffer
			glBindBuffer(GL_ARRAY_BUFFER, _VxBuffer);	//Bind the Vertex Buffer and load the vertices into the Vertex Buffer	
				glBufferData(GL_ARRAY_BUFFER,  geometry->Vertices()->BufferSizeBytes(), geometry->Vertices()->Data(), GL_STATIC_DRAW);		
				glVertexAttribPointer( POSITION_ATTRIBS, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid * ) 0 );
				glEnableVertexAttribArray( POSITION_ATTRIBS );
		}

		if( _NxBuffer == -1 && geometry->numNorms != 0 )
		{	//	normal data
			glGenBuffers(1, &_NxBuffer);
			glBindBuffer(GL_ARRAY_BUFFER, _NxBuffer);
				glBufferData(GL_ARRAY_BUFFER, geometry->Normals()->BufferSizeBytes(), geometry->Normals()->Data(), GL_STATIC_DRAW);		
				glVertexAttribPointer( NORMAL_ATTRIBS, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid * ) 0 );		//attribute 1 gets normal data
				glEnableVertexAttribArray( NORMAL_ATTRIBS );
		}
		
		if( _TxBuffer == -1 && geometry->numUVs != 0 )
		{	// texcoord data
			glGenBuffers(1, &_TxBuffer);
			glBindBuffer(GL_ARRAY_BUFFER, _TxBuffer);
				glBufferData(GL_ARRAY_BUFFER, geometry->Textures()->BufferSizeBytes(), geometry->Textures()->Data(), GL_STATIC_DRAW);
				glVertexAttribPointer( UV0_ATTRIBS, 2, GL_FLOAT, GL_FALSE, 0, (const GLvoid * ) 0 );	//attribute 3, UV data, is paits of two unnormalised flots with no offsets	
				glEnableVertexAttribArray( UV0_ATTRIBS );
		}
		

		if( _IxBuffer == -1 && geometry->GetIndicesCount() != 0 )
		{	//	Faces / Indices
			numIndices = unsigned int(geometry->GetIndicesCount());

			glGenBuffers(1, &_IxBuffer);
			glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _IxBuffer);
				glBufferData(GL_ELEMENT_ARRAY_BUFFER, geometry->Indices()->BufferSizeBytes() , geometry->Indices()->Data(), GL_STATIC_DRAW);
		}
	}

	if( _VAO != -1 )
	{
		glBindVertexArray(0); //done binding to meshVAO
		onCard = true;
	}

	return onCard;
}

void GL_VAO::Render()
{
	glBindVertexArray( _VAO );

		glVertexAttribPointer( POSITION_ATTRIBS, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid * ) 0 );
		glEnableVertexAttribArray( POSITION_ATTRIBS );

		glVertexAttribPointer( NORMAL_ATTRIBS, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*)0 );
		glEnableVertexAttribArray( NORMAL_ATTRIBS );

		

		glPolygonMode( GL_FRONT_AND_BACK, fillmode );

			if(numIndices > 0)
				glDrawElements( topology, numIndices, GL_UNSIGNED_INT, (const GLvoid*)0 );
			else
				glDrawArrays( topology, 0, numVertices );

		glDisableVertexAttribArray(POSITION_ATTRIBS);
		glDisableVertexAttribArray(NORMAL_ATTRIBS);

	glBindVertexArray( 0 );
}


Advertisement



Aww for fcks sake the editor swolled up 90% of my post...

This is fucking annoying. And that stupid code box is still there, can't delete it.

Anyway, two things:

VAOs store what you defined for the attributes (their layout with vertex attrib pointer and if they're enabled or not for rendering), so you don't have to redefine/reenable them when you make the draw call, just bind the VAO, then make the draw call.

VAOs don't store the index buffer association so you do need to bind it before making the draw call. As you're doing things it might never get to be unbound since you don't unbind any buffer besides the VAO as far as I can see.

For the draw call you only have to bind the VAO, make the draw call, unbind the VAO.

And if you're using index/element buffers, bind the VAO, bind the index/element buffer, make indexed draw call, unbind both.

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

Thanks for getting back to me!

I've been doing some reading: http://arcsynthesis.org/gltut/Positioning/Tutorial%2005.html#d0e5073

I see what you mean though - glDisableVertexAttribute not a good idea if you still want to use it..

Thing is though I don't want to be setting attribute pointers each render call for each model in my scene..

I had this feeling for weeks now that my render code could look like this: (and yes, the offsetting stuff is a mess jammed in there like that)


void GL_VAO::Render()
{
	glBindVertexArray( _VAO );

		if( offset != 0.0f )
		{
			switch(fillmode)
			{
				case GL_POINT:	glEnable(GL_POLYGON_OFFSET_POINT); break;
				case GL_LINE:	glEnable(GL_POLYGON_OFFSET_LINE);  break;
				case GL_FILL:	glEnable(GL_POLYGON_OFFSET_FILL);  break;
			}
			
			glPolygonOffset(offset, 1.0f);
		}

		glPolygonMode( GL_FRONT_AND_BACK, fillmode );

			if(numIndices > 0)
				glDrawElements( topology, numIndices, GL_UNSIGNED_INT, (const GLvoid*)0 );
			else
				glDrawArrays( topology, 0, numVertices );

		if( offset != 0.0f )
			glDisable(GL_POLYGON_OFFSET_LINE);

	glBindVertexArray( 0 );
}

And now it does - It's all about setting the VAO state in OnCard() (which is like LoadBuffers or LoadVAO) - I'm planning on a VAO for each 'ThingILoadFromFile'.. So Loading VBOs onto a VAO I need to look at what I got out of the .obj/.3ds etc.. Here's my load buffers, which is no longer lets you scramble one vertex attribute with another, because it calls glEnableVertexAttribArray() before glVertexAttribPointer(), I think!



bool GL_VAO::OnCard(Cream::GeometryData* geometry)
{
	if( onCard == true )
		return true;
	
	topology = geometry->GetTopology();

	if( _VAO == -1 )
	{
		glGenVertexArrays( 1, &_VAO );				//Allocate an OpenGL VAO
		glBindVertexArray( _VAO );					//Bind the VAO to store all the buffers and attributes we create here

		if( _VxBuffer == -1 && geometry->numVerts != 0 )
		{	//position data
			numVertices = geometry->numVerts;
			glGenBuffers(1, &_VxBuffer);				//Generate an ID for the Vertex Buffer
			glBindBuffer(GL_ARRAY_BUFFER, _VxBuffer);	//Bind the Vertex Buffer and load the vertices into the Vertex Buffer	
				glBufferData(GL_ARRAY_BUFFER,  geometry->Vertices()->BufferSizeBytes(), geometry->Vertices()->Data(), GL_STATIC_DRAW);
				glEnableVertexAttribArray( POSITION_ATTRIBS );
				glVertexAttribPointer( POSITION_ATTRIBS, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*)0);
		}

		if( _NxBuffer == -1 && geometry->numNorms != 0 )
		{	//	normal data
			glGenBuffers(1, &_NxBuffer);
			glBindBuffer(GL_ARRAY_BUFFER, _NxBuffer);
				glBufferData(GL_ARRAY_BUFFER, geometry->Normals()->BufferSizeBytes(), geometry->Normals()->Data(), GL_STATIC_DRAW);	
				glEnableVertexAttribArray( NORMAL_ATTRIBS );
				glVertexAttribPointer( NORMAL_ATTRIBS, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*)0);	
		}
		
		if( _TxBuffer == -1 && geometry->numUVs != 0 )
		{	// texcoord data
			glGenBuffers(1, &_TxBuffer);
			glBindBuffer(GL_ARRAY_BUFFER, _TxBuffer);
				glBufferData(GL_ARRAY_BUFFER, geometry->Textures()->BufferSizeBytes(), geometry->Textures()->Data(), GL_STATIC_DRAW);
				glEnableVertexAttribArray( UV0_ATTRIBS );
				glVertexAttribPointer( UV0_ATTRIBS, 2, GL_FLOAT, GL_FALSE, 0, (const GLvoid * ) 0 );
		}
		

		if( _IxBuffer == -1 && geometry->GetIndicesCount() != 0 )
		{	//	Faces / Indices
			numIndices = unsigned int(geometry->GetIndicesCount());

			glGenBuffers(1, &_IxBuffer);
			glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _IxBuffer);
				glBufferData(GL_ELEMENT_ARRAY_BUFFER, geometry->Indices()->BufferSizeBytes() , geometry->Indices()->Data(), GL_STATIC_DRAW);
		}
	}

	if( _VAO != -1 )
	{
		glBindVertexArray(0); //done binding to meshVAO
		onCard = true;
	}

	return onCard;
}

Okay! Now to go write a phong shader and use dem dere normals - if it works for a cube it'll likely work for a 100,000+ poly car :D


VAOs don't store the index buffer association so you do need to bind it before making the draw call.

...

And if you're using index/element buffers, bind the VAO, bind the index/element buffer, make indexed draw call, unbind both.

Wrong, GL_ELEMENT_ARRAY_BUFFER is part of the VAO state.


VAOs don't store the index buffer association so you do need to bind it before making the draw call.

...

And if you're using index/element buffers, bind the VAO, bind the index/element buffer, make indexed draw call, unbind both.

Wrong, GL_ELEMENT_ARRAY_BUFFER is part of the VAO state.

I see your edits now - apologies!
And.. I thought it was too - you bind all buffers / your interleaved buffer, and the indexing is consistent across them all for that VAO, no?

So if Mona is correct, my code is good?

Surely unbinding the VAO unbinds all the VBO's associated with it?

VAOs don't store the index buffer association so you do need to bind it before making the draw call.

...

And if you're using index/element buffers, bind the VAO, bind the index/element buffer, make indexed draw call, unbind both.

Wrong, GL_ELEMENT_ARRAY_BUFFER is part of the VAO state.


Yes and no. It should be part of the state but in several drivers it is not, at least not in all context versions. I don't remember having had that problem when I explicitly requested a 3.3 or better context, but currently I'm working on something just taking any context and I ran into the problem.
Unless you find more definitive information when it is actually supported by all relevant drivers it might be best to work under the assumption that the element array buffer state could be part of the VAO but you are in no way guaranteed that either way.

Just my two cents here.

I've found in my experience (nVidia GTX 480/560 cards - can't remember Driver Version off heart) but I had to rebind the Index buffer before each Draw Call... I also had to call glEnableVertexAttribArray() for each Vertex Attrib Array before each Draw Call.

Indeed, I should of said I'm on an ATI radeon here, Nvidia Quadro at work.. I can see myself #ifdef ing my way you of trouble here..

This topic is closed to new replies.

Advertisement