What are some best practices for OpenGL coding (esp. w.r.t. object orientation)?

前端 未结 5 1384
走了就别回头了
走了就别回头了 2020-12-12 10:01

This semester, I took a course in computer graphics at my University. At the moment, we\'re starting to get into some of the more advanced stuff like heightmaps, averaging n

5条回答
  •  隐瞒了意图╮
    2020-12-12 10:04

    Object transformations

    Avoid depending on OpenGL to do your transformations. Often, tutorials teach you how to play with the transformation matrix stack. I would not recommend using this approach since you may need some matrix later that will only be accessible through this stack, and using it is very long since the GPU bus is designed to be fast from CPU to GPU but not the other way.

    Master object

    A 3D scene is often thought as a tree of objects in order to know object dependencies. There is a debate about what should be at the root of this tree, a list of object or a master object.

    I advice using a master object. While it does not have a graphical representation, it will be simpler because you will be able to use recursion more effectively.

    Decouple scene manager and renderer

    I disagree with @ejac that you should have a method on each object doing OpenGL calls. Having a separate Renderer class browsing your scene and doing all the OpenGL calls will help you decouple your scene logic and OpenGL code.

    This is adds some design difficulty but will give you more flexibility if you ever have to change from OpenGL to DirectX or anything else API related.

提交回复
热议问题