OO architecture for rendering in shader based games

前端 未结 2 560
难免孤独
难免孤独 2021-02-05 18:57

I keep hitting this problem when building game engines where my classes want to look like this:

interface Entity {
  draw();
}

class World {
  draw() {
    for          


        
相关标签:
2条回答
  • 2021-02-05 19:15

    This is not an easy question to answer, since there are many ways to deal with the problem. A good idea is to look into some Game/Rendering engines and see how this is handled there. A good starting point would be Ogre, since its well documented and open source.

    As far as I know, it separates the vertex data from the material components (shaders) through the built-in material scripts. The renderer itself knows what mesh is to be drawn in what order and with what shader (and its passes).

    I know this answer is a bit vague, but I hope I could give you a useful hint.

    0 讨论(0)
  • 2021-02-05 19:21

    Use a two stages approach: First loop through all entities, but instead of drawing let them insert references to themself into a (the) drawing batch list. Then sort the list by OpenGL state and shader use; after sorting insert state changer objects at every state transistion.

    Finally iterate through the list executing the drawing routine of each object referenced in the list.

    0 讨论(0)
提交回复
热议问题