opengles 2.0 2D scene graph implementation
I want to create a 2d opengles engine to use for my apps. This engine should support a scene graph. Each node of the graph can have it's own shader, texture, material and transformation matrix according to it's parent node. But I'm new to opengles 2.0 and so I have some questions:
- How does the matrix multiplication work in opengles 2.0? Is it a good approach to draw first parent, then it's child nodes (will it give some optimization when multiplying modelview matrices). Where does the matrix multiplication take place? Should i do it on the CPU or on GPU.
- Is it a better approach to draw nodes according to the shader开发者_运维知识库 it uses, then to texture and material? How should i implement scene graph transformations in this case (on CPU or GPU and how)?
How does the matrix multiplication work in opengles 2.0? Is it a good approach to draw first parent, then it's child nodes (will it give some optimization when multiplying modelview matrices). Where does the matrix multiplication take place? Should i do it on the CPU or on GPU.
Except some ancient SGI machines, transformation matrix multiplication always took place on the CPU. While it is possible to do matrix multiplication in a shader, this should not be used for implementing a transformation hierachy. You should use, or implement a small linear algebra library. If it is tailored for 4x4 homogenous tranformations, those used in 3D graphics, it can be implemented in under 1k lines of C code.
Instead of relying on the old OpenGL matrix stack, which has been deprecated and removed from later versions, for every level in the transformation stack you create a copy of the current matrix, apply the next transform on it and supply the new matrix to the transformation uniform.
Is it a better approach to draw nodes according to the shader it uses, then to texture and material? How should i implement scene graph transformations in this case (on CPU or GPU and how)?
Usually one uses a two phase approach to rendering. In the first phase you collect all the information about which objects to draw and the drawing input data (shaders, textures, transformation matrices) and put this in a list. Then you sort that list according to some criteria. One wants to minimize the total cost of state changes.
The most expensive kind of state change is switching textures as this invalidates the caches. Swapping textures between texturing units has some cost but its still cheaper than switching shaders, which invalidates the state of the execution path predictor. Changing uniform data however is very cheap, so one should not be anxious to switch uniforms too often. So if you can make substantial settings through uniforms, do it that way. However you must be aware that conditional code in shaders has a performance it there, so you must balance the cost of switching shaders against the cost of conditional shader code.
精彩评论