Hi ! I have some issues with batching my sprites.
I created two VBOs, one that stores UV (2 floats per vertice) and the positions (2 floats per instance) and another one that stores the sizes (2 floats per instance). Positions and UV are supposed to change a lot because they are moving sprites.
I also have one vbo which represents an indexed rectangle (8 floats)
These are my vertex pointers respectively to the rectangle shape, the uv, the position, and the size.
glVertexAttribPointer(vertexLoc, 2, GL_FLOAT, GL_FALSE, 0, 0); //rect glVertexAttribPointer(uvLoc, 2, GL_FLOAT, GL_FALSE, 0, 0); //uv glVertexAttribPointer(posLoc, 2, GL_FLOAT, GL_FALSE, 0, (void*)(uv_offset*sizeof(GLfloat)));// pos_x pos_y glVertexAttribPointer(sizeLoc, 2, GL_FLOAT, GL_FALSE, 0, 0); // size_x size_y
Then i also use
glVertexAttribDivisor with parameter :
- 0 for UV because I use it for each vertex
- 1 for pos and size
What I want to do is to draw all the sprites that have the same texture in one draw call (indexed and instanced).
Instanced because I want to draw the rectangle several times and then move and scale the rectangle in the shader.
So it looks like this :
put data on GPU define pointers for all sprites batched by texture bind texture
Indexed and Instanced draw all sprites that have the same texture
I use this function to draw ( batches[i].numVertices/numVerticesPerInstance is the number of sprites that have texture i) :
glDrawElementsInstanced(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, nullptr, batches[i].numVertices/numVerticesPerInstance);
Should I use another draw function ?
With 1 sprite I have a rectangle of the correct size but position doesn’t work (0,0) and uv are strange (I cannot post 2 images but it is symmetric with regard to the bottom left to top right diagonal and it seems to be stretched horizontally for top left part (1st column of pixels on left is stretched ) and vertically for the other one (symmetric)).
With 2 sprites with the same texture i have the same result for both even if I change size of second one. Even
gl_InstanceID doesn’t work in shader (I tried to change position with it).
I am sure that the arrays contain what I want in the right order and the last parameter in the draw function is 2 for that case so the problem may be with
glVertexAttribDivisor or draw function. Thank you for your help !