Q: How do I make a camera move independently from my objects in OpenGL?I've answered this question frequently enough. The long answer is that you really need to understand matrix math, and what the OpenGL transform pipeline is. Meshes/geometry are issues in "model space" where typically 0,0,0 is the "feet" of the model. These are then transformed by the model matrix, to put them into 'world' space; then they are transformed by the view matrix, to put them into 'camera' space. Because this transformation is usually always done in one go; GL combines these into the MODELVIEW matrix for efficiency. Typically, you'll want to construct a matrix for your camera, then a matrix for your model position/orientation, then multiply them together to create your modelview, and load that using glLoadMatrix(). This means you re-create the modelview for each entity you want to render (but the camera matrix can stay intact across a frame). If this is too much for you to do right now and you just want a system where a camera can move around, pitch around its "side" axis, and rotate around its "up" axis, and entites are places at a position, with a rotation around their "up" axis, then the following quick-and-dirty code suggestion may help.
This code is not super efficient, but it's also not super inefficient -- chances are, if you have performance problems, they're not in this code but somewhere else. To move the camera, simply change "camPos" to put it where you want. To move an entity, simply change "ent->pos" to be where you want it. To rotate the camera or the entity, change camHeading, camPitch, or ent->heading appropriately. This is actually all you need to make a simple world where you can drive the camera around, and have objects independently move around the world; i e a simple shooter like "Quake" could be done with this camera control. However, for more realistic simulations, you'll need full degrees of freedom in both how the camera is oriented, and how entites are oriented. At that point, please rip out this version of renderScene() and put in something real instead. |