My idea was to use instancing to display multiple sprites using the same texture, but with different modelView matrices applied to them and using different frames of a sprite sheet if appropriate.
The basic idea being:
1. Load texture, set some things up with the sprite sheet.
2. Set up a sprite to be drawn by adding a textured quad to a VBO by passing the position etc. and sprite sheet index to a function.
3. Call a draw function once to draw all of the added sprites at once.
Now, I got to the point where I could draw 1 sprite no problem, or many sprites as long as I called the function with glDrawArrays() right after adding another quad.
But when I added more than 1, a problem occurred with the texture. I then figured out that what seemed to be happening was that all of the sprites were being drawn with the same texture, which appeared to be a combination of all of the intended sprites. i.e. Right now I'm testing it with a bitmap font, and if I tried to draw 3 sprites with the letters A, B and C, I'd get all 3 quads with the same texture, which would appear to be the A, B and C images superimposed over each other in some way. I'm using transparency BTW, so any non-letter area of the sprite is transparent, hence how I can see through.
Now... after looking at the code and thinking about it, I'm pretty sure I understand why that's happening; simply because I set up the modelView matrix (the positions are all working perfectly) using glVertexAttribDivisor() and put in the texture coordinates as normal, therefore meaning that all vertices in a single draw call have the same texture coordinates, yes?
HOWEVER, while I assume that if I simply set up the texture coords in the same way, it would start to work as desired (and I've always realized as a result that the vertex coord code should probably be cleaned up a bit)... I can't actually quite get my head around just WHY this is happening, and I'd really like to find out before going forward.
The setup is:
- Every time I call the addQuad() function, the texture coordinates for that sprite are calculated using the sprite sheet index, then the vertices and tex coords are added to a VBO thus:
for( int i = 0; i < 6; i++ )
{
vbo.addData( &vertices[i], sizeof( glm::vec3 ) );
vbo.addData( &texVerts[i], sizeof( glm::vec2 ) );
}
Where vbo is an instance of a class that wraps a vertex buffer object, and addData places data sequentially in an internal byte array before the call of glBufferData() which occurs when drawSprites() is called.
There are 6 vertices and tex coords because I'm drawing 2 triangles without indices.
- In the drawSprites() function, after the call to glBufferData(), I set up the vertex attributes:
//Vertex positions
glEnableVertexAttribArray( 0 );
glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, sizeof( glm::vec3 ) + sizeof( glm::vec2 ), 0 );
//Texture coordinates
glEnableVertexAttribArray( 1 );
glVertexAttribPointer( 1, 2, GL_FLOAT, GL_FALSE, sizeof( glm::vec3 ) + sizeof( glm::vec2 ), reinterpret_cast<void*>( sizeof( glm::vec3 ) ) );
As you can see, the buffers contains a vec3 for a vertex position, followed by a vec2 for texture coordinates, repeating for as many vertices as there are.
The start of the vertex shader looks like this:
#version 330
uniform mat4 projMatrix;
layout (location = 0) in vec3 inPosition;
layout (location = 1) in vec2 inCoord;
layout (location = 2) in mat4 modelView; //(Takes up locations 2-5)
out vec2 texCoord;
Where inCoord is the texture coordinate input and it's just later passed to the fragment shader by the texCoord out variable, where it is then sampled using texture2D().
Like I said, it all works when 1 quad is used, so I assume there aren't any fundamental errors here.
f there are 2 quads, say, that would mean 6*2 = 12 vec2 structures in the VBO (or 24 floats). Because I'm trying to draw multiple objects with 1 draw call and the texture coordinates aren't properly set up for this, that means I'm applying all of these to just 6 vertices for every quad.
I assume what's happening is, one sprite is being applied to the texture, and then another is being applied later on top of it, resulting in the garbled image I'm seeing.
But HOW exactly is that happening? What are the stages it's all going through? I would have thought that the data in the buffer would be traversed sequentially and the result would be that each vertex would be paired up with the correct texture coordinates, would would be sampled to apply the texture to that vertex's location on screen... not resulting in what I've been seeing here. But somehow, even the texture coordinates at the end of the buffer wind up influencing the texture that is applied to the vertex at the very start of the buffer.
I'm sure it's just something simple I'm missing about how these things work, but I just can't quite make sense of it, even though I see that it kind of makes sense when I consider that all quads must have the same texture coordinates.
Is the texture colour applied in more than 1 pass? Does OpenGL maybe deal with all of the vertex positions first (i.e. the vertex shader, though even when the first vertex position is passed in, so is its texture coordinate pair), create the areas on screen that are to be drawn to, then texture over them with the first set of texture coordinates (i.e. applies the first quad's texture to every quad), then eventually comes to the last sprite's texture coordinates and applies those over every quad again?
That must be how it works, but I never thought it worked like that. I'd imagined it much more... parallel or something. But maybe I just never quite thought too hard about it.
Sorry if I've rambled, but I've just been struggling to try and understand this.