简体   繁体   中英

How do games move around objects (in general)

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

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.

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 (ie 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM