|OpenGL Camera Demo - Part 3.
This is the fourth demo in the OpenGL camera demo series and it builds on the OpenGL Camera Demo - Part 2 and the OpenGL Third Person Camera Demo - Part 1.
This demo shows how various camera behaviors can be combined together. The following camera behaviors are implemented in this demo: first person, spectator, flight, and orbit. The first person camera behavior simulates the view from the perspective of the player. The spectator camera behavior simulates a floating camera that moves in the direction that the camera is looking. The flight camera behavior simulates the view from the cockpit of an airplane. Finally the orbit camera behavior simulates a third person view of the player and allows the camera to orbit the player.
The camera class that accompanies this demo uses quaternions throughout. Of particular note is the class' implementation of the orbit camera behavior. The orbit camera implementation differs considerably from the third person camera model used in the OpenGL Third Person Camera Demo - Part 1.
The OpenGL Third Person Camera Demo maintained an offset vector and used quaternions to rotate this offset vector when the camera was being rotated. The new camera position was then used to build a look-at style view matrix.
This demo's orbit logic begins by setting the camera's orientation to that of the orbit target. Rotating the camera will affect this orientation. So far this is exactly how the other camera behaviors (first person, spectator, and flight) work. But once the orbit camera is rotated the camera's view matrix is updated to accommodate the new orientation. The view matrix requires the camera position to perform a dot product with each of the three local axes. To implement a third person orbit style view of the target the camera's position is calculated here to be a fixed distance from the target's position. Specifically this demo takes the orbit target's world position and translates this point by m_orbitOffsetDistance world units along the camera's local Z axis.
This demo uses the Windows XP raw input model and associated APIs to retrieve mouse data directly from the mouse driver. Retrieving raw input from the mouse allows us to perform our own filtering on the mouse data and also allows us to support high DPI resolution mice.
1. This demo requires OpenGL 1.3 or higher support.
2. This demo requires Windows XP or higher to run. The minimum supported operating system for the raw input APIs is Windows XP.
3. This demo requires the Visual C++ 2010 Library Runtimes. Download instructions can be found here.
Download executable, source, and Visual C++ 2010 solution files.
10 July 2010.
Added missing .vcxproj.filters file to the project.
13 June 2010.
Updated solution to Microsoft Visual Studio 2010.
30 November 2009.
camera.cpp: Removed redundant normalize() call on the m_xAxis member variable in the lookAt() method. Added a call to updateViewMatrix() at the end of the lookAt() method. This extra call is required if the camera's movement is being automated using the lookAt() method. Thanks goes to Pierre Proske for submitting these corrections.
2 February 2008.
Updated solution to Microsoft Visual Studio 2008.
17 January 2008.
camera.cpp: Fixed bug in updatePosition() where the displacement was being accidentally clamped to zero when the camera was no longer moving in a particular direction. This resulted in the camera abruptly stopping when it was no longer moving rather than slowly decelerating back to a stationary position.
input.h: Added the following methods to the Mouse class: isMouseSmoothing(), weightModifier(), setWeightModifier(), and smoothMouse().
input.cpp: Changes made to the Mouse class. Changed WEIGHT_MODIFIER to 0.2f. Updated handleMsg() to filter the mouse input only if mouse smoothing is enabled. Added implementations of the setWeightModifier() and smoothMouse() methods.
main.cpp: Updated file documentation. Added multisample anti-aliasing support. Removed wireframe support. Renamed DrawFloor(), DrawFrame(), DrawModel(), and DrawText() to RenderFloor(), RenderFrame(), RenderModel(), and RenderModel(). Changes signature of GetMovementDirection() function. Updated the debug text renderer to the screen. Added ability to enable and disable mouse smoothing. Added ability to change the mouse sensitivity. This maps to the mouse weight modifier.
22 December 2007.
gl_font.h: Fixed incorrect include statement. Updated code sample in the class description.
gl_font.cpp: Modified the begin() method to reduce the amount of state saving prior to beginning font rendering. Modified the end() method to reduce the amount of state restore after font rendering.
main.cpp: Modified DrawFloor() function to unbind textures before function returns.
17 December 2007.
main.cpp: Added GetMovementDirection() function. Updated UpdateCamera() function to call GetMovementDirection() function. The GetMovementDirection() function detects when a movement key is pressed and released and adjusts the camera's current velocity accordingly. When a movement key is first pressed the current velocity for movement in that direction is reset back to zero. This prevents the camera from slowly decelerating and then accelerating in the desired direction of movement.
camera.h: Added setCurrentVelocity() methods. Added overloaded setVelocity() method.
camera.cpp: Added implementation for setCurrentVelocity() methods. Added implementation for overloaded setVelocity() method.
gl_font.h: Removed mathlib.h header file. Moved gl2.h header file to the gl_font.cpp file. Replaced the Vector2 types in the Glyph structure with float arrays. Added nextPower2() method.
gl_font.cpp: Updated ClearType detection code in the constructor. Changed min and mag filter to GL_NEAREST in the createTexture() method. Added nextPower2() method.
11 September 2007.
main.cpp: Removed CAMERA_OFFSET constant. Renamed constants CAMERA_BOUNDS_MAX and CAMERA_BOUNDS_MIN into global variables g_cameraBoundsMax and g_cameraBoundsMin respectively. Added g_modelTextures global variable to store the OBJ file's material textures. Updated DrawModel() to use the model's textures if present. Added ability to toggle wireframe rendering. Updated InitCamera() to correctly determine the camera's initial height based on the OBJ model's height. Updated InitCamera() to correctly setup the camera bounds around the scene.
model_obj.h: Refactored bounds() and scale() into private methods. Added getCenter(), getWidth(), getHeight(), and getLength() methods. Updated method signature of addVertex() method. Added bounding box. Updated the declaration of m_vertexCache.
model_obj.cpp: Added overloaded bounds() method to calculate the model's bounding box. Updated import() to calculate vertex normals if not present in loaded model. Updated addVertex() to more correctly handle duplicate vertices. Added generateNormals(). Updated importGeometrySecondPass() to triangulate all loaded faces. Added triangulateLastInsertedFace().