Quote:Original post by Black Knight
How would you put states in a structure?Can you elaborate on that a little more?
Is it something like:
struct tRenderState
{
bool blending;
bool lighting;
bool texturing;
int vertexbufferID;
int textureID;
int shaderID;
..
};
Then each renderable can have a renderState instance and pass it to a queue in the renderer?Or did I totally miss the point?
Specifically how you do this depends on many factors, perhaps the biggest factor is whether you plan on using a shader based system or fixed function one. Most people would recommend a shader based approach and you can then (if you wanted to) implement the fixed function pipeline as a special shader.
You also need to consider the order things happen in, for example heres a simple overview of a state-sorting rendering pipeline of an engine:
1) iterate over world entities i) request a renderOp from each entity (see later description) ii) push visible renderOps onto a renderQueue (or renderScheduler)2) sort renderQueue by: ShaderID -and- Shader parameters -and- vertexBuffer (if available)3) for each renderOp in renderQueue i) bind shader (if not current) ii) bind vertexBuffer, or create one and upload data into it (if not current) iii) upload shader parameters (if not current) iv) set any other states necessary (e.g. fog, render targets, etc) v) render
A renderOp is a structure that is requested from each renderable entity at render-time, it needs to contain (references to) the geometry that will be rendered, the shader to render the geometry with, all the shader parameters and possibly non-shader related states like fog blending (or depending on how you abstract a shader these might be part of the shader parameters anyway).
At stage-2 when sorting takes place, we sort based on these states and/or any others that we might have available to us, we dont necessarily need to sort by every state as there is likely a breakeven point between the cost of sorting by another state and simply running the risk of needlessly setting it.
We dont need a structure with booleans that enable or disable texturing etc, because this is very fixed-function and it is shaders that decide whether they want to texture or not. If we want a simple textured mesh then the renderable will return a renderOp that contains the mesh to use, a diffuseTexture shader and a texture to use. If we want a non-texture phong shaded sphere then renderOp contains the sphere mesh, a suitable shader and a NULL texture pointer/handle.
So a simple renderOp implementation could look like:
class renderOp{public: // Required data for each and every renderable Geometry* geometry; // The geometry stored in the system RAM Matrix* matrix; // Transformation int shaderID; // shader handle int vertexBufferID; // VBO handle (or (-1) if not cached in the video-RAM) int textures[MAX_TEXTURES]; // Texture handles /* Any other states you think every renderable should explicitly give /* // Possibly some way to allow states that not every renderable need have // Or just have every conceivable state in this class and give then default values. std::map<std::string, OptionalStateType> otherStates;};
Quote:Original post by Gage64
You can still do something similar with seperate functions that are not virtual and are inline. Instead of actually changing the state, they will simply update an internal structure like the one you described. Then when it's time to render, a virtual flushState() function is called that sets all states that havn't changed.
The renderer can still have a function that sets all states at once, it's just that sometimes it is convinient to set individual states (maybe for small demos or something).
Yes you might well want to make the actual data members of the renderOp class private and use functions similar to those you describe as its interface.
I wouldnt advocate the use of helper or utility functions as part of the renderer as its just not necessary and leads to code bloat, such functions in this case would belong in a utility namespace somewhere I suspect.
I hope that as some help? [smile]