Reputation: 325
I'm trying to draw a map made from tiles in OpenGL (version 3.3 as a minimum). They way I'm currently doing this is compiling all the vertex data from each tile on the map into a single array, and binding that data to a buffer inside a vertex array. This is also the case for normals and texture coordinates; each having their own buffer. There is then a vertex attribute pointer to that data, so that the vertex shader gets a hold of it.
That being said, when it comes to each tile having its own color, this being a single, solid color (not a texture or gradient), I'm essentially passing an array that's the same size as the vertex array into the shader, even though the color is constant for all 6 vertices of the tile! This seems awfully wasteful, as it's essentially the same data x6. Is there a way to effectively reuse the same data every 6 vertices when rendering the map?
I'd use a uniform or attribute for each tile, but remember all tiles are placed into a single vertex array, so I can't re-bind those for each tile separately.
The current method does work, and it is quite fast (2000 fps for 256x256 tiles on a GT 750m), but I can't help feeling guilty for simply copying the data throughout the array so it matches each vertex, even though the data is constant for each primitive (in fact 2 primitives: two triangles which make up the square tile). Surely their has to be a better way?
Upvotes: 2
Views: 806
Reputation: 8325
You have various ways to overcome your problem.
Palettes: Use a vertex attribute (just a ubyte) to select for each tile a value from an array of 256 uniforms the color (color is 3/4 ubytes so you just saved 2/3 ubytes). Of course I assume you have no more than 256 different colors (you could probably use a short and up to 65536 colors if 256 is not enough)
Geometry/Tessellation shaders: If tiles are just squares or other simple shapes then you have to take as input a single vertex and given some basic information (like size of the tile) you emit the full primitives (this just allows you to reduce data size by 5 or more times, but you have to learn a lot of extra GL functionalities, wich is not bad anyway)
Upvotes: 1