Jump to content
  • Advertisement
Sign in to follow this  

OpenGL More VAOs "Stuff"...

This topic is 1451 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'm still trying to get my head wrapped VAOs. In my OpenGL ES 2.0 days, I'd try to keep my objects sorted by shader, then by material, and finally by depth (never got the depth task done though). Sorting my objects by shader allows me to minimize shader swaps --provided that all my models were using only a single shader and material. I'd enable/disable vertex attributes accordingly. glEnable/DisableVertexAttribArray() doesn't get called directly. My own wrapper would be called instead, and it would only call the corresponding gl attrib array call to enable or disable an array as long as the bitfield's bit is different. If it is different, the call is made, and the bitfield's bit is set to whatever corresponding value.


Here's an example:

#define VERTEX_ATTRIB0		1
#define VERTEX_ATTRIB1		2
#define VERTEX_ATTRIB2		4
#define VERTEX_ATTRIB3		8
#define VERTEX_ATTRIB4		16
#define VERTEX_ATTRIB5		32
#define VERTEX_ATTRIB6		64
#define VERTEX_ATTRIB7		128
#define VERTEX_ATTRIB8		256
#define VERTEX_ATTRIB9		512
#define VERTEX_ATTRIB10		1024
#define VERTEX_ATTRIB11		2048
#define VERTEX_ATTRIB12		4096
#define VERTEX_ATTRIB13		8192
#define VERTEX_ATTRIB14		16384
#define VERTEX_ATTRIB15		32768

unsigned int vertexAttribArrayFlags = 0;

bool SetVertexAttribArray(unsigned int index, bool enable)
	bool result = false;
	unsigned int flag = 1 << index;
	// either attempt to enable, or disable the vertex attrib array by index
		// make sure the current flag ISN'T set
		if(!(vertexAttribArrayFlags & flag))
			glEnableVertexAttribArray(index); // enable
			vertexAttribArrayFlags |= flag;
			result = true;
	} else {
		// make sure the current flag IS set
		if(vertexAttribArrayFlags & flag)
			glDisableVertexAttribArray(index); // disable
			vertexAttribArrayFlags &= ~flag;
			result = true;
	return result;

void SetVertexAttribArrays(unsigned int flags)
	for(int i=0;i<16;++i)
		SetVertexAttribArray(i, (flags & (1 << i)) ? true : false);

This code was written off the top of my head, but that's kind of close to what I had. If I needed to have attributes 0, 1, 2 and 5 enabled while all others were disabled, SetVertexAttribArrays() would only call glEnableVertexAttribArray() on 0, 1, 2 and 5 if they weren't already set. It'd also call glDisableVertexAttribArray() on all other attributes if they were current enabled. The variable, vertexAttribArrayFlags, is what keeps track of what arrays are enabled or disabled on the client's side. This only works if glEnable/DisableVertexAttribArray() are called through these functions. This also only works across a single context unless the code above is wrapped in a GraphicsContext class that's instantiated for each OpenGL context, but that's an additional dimension OpenGL to keep in sync.


Anyway, enter: VAOs. I could keep this code around by creating a single VAO upon initialization, and just keep doing what I'm currently doing, provided that this was even a good solution to begin with, and release the VAO when the game shuts down. Now that I'm getting into modern desktop OpenGL where VAOs are mandatory, would this be a good approach?

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!