views:

217

answers:

3

I'm sure there's not just 1 answer to this but, do game engines actually change the vectors in memory, or use gltransformations? Because pushing and popping the matrix all the time seems inefficient, but if you keep modifying the verticies you cant make use of display lists. So I'm wondering how it's done in general. Thanks

+3  A: 

There are many ways of doing this depending on the game engine.

But probably the most common approach is a division of responsibilities between the game engine and the graphics card which is something like the following:

  • The game engine keeps track of the locations, orientation, velocity etc. of all objects in the game by modifying the appropriate vectors and matrices in an event loop. Game logic, physics, collision detection etc. are applied to this memory model of game objects.

  • When an object needs to be rendered, OpenGL calls are used to set up the appropriate render state and transform matrices - this can be on a per-object basis although frequently optimisations are included to minimise the number of state changes.

  • OpenGL calls are then made to draw the necessary mesh(es) with glDrawElements (assuming you are using Vertex Buffer Objects). This means that all the transformations required for the individual mesh vertices are performed by the graphics hardware. As hundreds of individual primitives can be drawn efficiently with a single OpenGL call this can be very important for performance.

mikera
p.s. although there are many differences you can substitute display lists for VBOs in terms of the overall approach, or mix the two
mikera
+3  A: 

Depends on your goals/objects.

Objects that never change are moved by setting matrices (it is possible to do that using glRotatef/glTranslatef and such too). It is certainly faster than transforming object vertex by vertex. Object is stored in DisplayList, VertexArray or in VBO. You CAN send entire object every time using glVertex* command, but if object never changes, there is no need for that.

Characters (skinned/rigged) are transformed using multiple matrices, which is normally handled by vertex shader. You can do it on CPU, if you want, but unless you have a VERY advanced rigging/skinning engine, there is no need for that.

Calculations that cannot be completely performed on Videocard, are performed on CPU. Normally it is particle systems, cloth and procedural geometry, sometimes shadows (for shadow volume calculation using stencil shadows). Objects like this are normally stored in memory, at least partially, and transformations performed on CPU. This is strictly hardware-dependent. Depending on the power of videocard and OpenGL version, it is possible to perform some or all of those tasks on videocard (without involving CPU), but not always. For example, normally it isn't possible to build shadow volume for procedural geometry (i.e. topology varies each frame as well as vertex positions) on GPU. However, there are tricks to avoid it, and situation might change with future versions.

SigTerm
+1  A: 

Writing a 16-number matrix onto the stack is a trivial operation, and certainly a lot quicker than modifying a load of different vertices. Think about the number of vertices in a typical 3D model and you'll see why.

Kylotan