People constantly tell me to use at least Vertex Arrays. But i think its not a good idea since i'm using glPushMatrix()
with glTranslatef/glRotatef
to position an object in the 3d world.
So, should i stop using glPushMatrix()
and calculate the rotated/moved vertex positions in the world "manually" and then push their vertex data into a vertex array and then render it all at once?
But, all this will get even more messed up when i use different texture surfaces for all the objects on the screen which are also depth sorted.
So:
- I would have to store each object with the texture surface ID as well.
- I would sort all the visible objects by their Z position (the game is viewed top-down only, and all objects are flat).
- I would go through this sorted array:
- Create new buffer and copy only the vertex/texcoord/color/normal into this buffer:
- Every time the texture surface ID has changed from previous ID, i will bind to the correct texture ID:
- Upload the vertex data i collected.
- Free the buffer used for temporary vertex array.
- Repeat steps 4-7 until i have gone through all the data i sorted at the first place.
- Free my sorted array data, and repeat steps 1-9.
Am i doing it right?
Also, how should i design the data structure for the objects i will sort? For example, is it good to use std::vector
to store the vertex data for each object? Or is there better alternative? I was thinking that the std::vector
to store all this data would look like:
struct GameObject {
int TexID;
float z; // we will sort by this
vector<VTCNStruct> VertexData; // store each point of the object here (including color/normal/texcoord points).
};
vector<GameObject> GameObjectBuffer; // push all sortable objects here
Also, at step 4: is it possible to use the already existing std::vector
in this case? I've had the idea i that i must use raw arrays, like new float[100]
for sending the vertex array to my GPU, or could i somehow use my already existing sorted std::vector
here somehow (efficiently) without creating new buffer every time the texture ID changes?