A Camera Class for OpenGL John McGuiness October 2006.

Slides:



Advertisements
Similar presentations
SI23 Introduction to Computer Graphics
Advertisements

Transformations We want to be able to make changes to the image larger/smaller rotate move This can be efficiently achieved through mathematical operations.
Three Dimensional Modeling Transformations
Advanced Programming for 3D Applications CE Bob Hobbs Staffordshire university Data Structures for Human Motion Lecture 5.
1 Computer Graphics Week6 –Basic Transformations- Translation & Scaling.
Chapter 5 Geometric Transformations
HCI 530 : Seminar (HCI) Damian Schofield. HCI 530: Seminar (HCI) Transforms –Two Dimensional –Three Dimensional The Graphics Pipeline.
CMPE 466 COMPUTER GRAPHICS
Based on slides created by Edward Angel
1 3D modelling with OpenGL Brian Farrimond Robina Hetherington.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Computer Viewing Ed Angel Professor of Computer Science, Electrical and Computer Engineering,
Ch. 2: Rigid Body Motions and Homogeneous Transforms
2.1 si SI31 Advanced Computer Graphics AGR Lecture 2 Basic Modelling.
3-D Geometry.
3.1si31_2001 SI31 Advanced Computer Graphics AGR Lecture 3 Viewing Transformation Getting Started with OpenGL Introduction to Projections.
OpenGL (II). How to Draw a 3-D object on Screen?
CS 4731: Computer Graphics Lecture 8: 3D Affine transforms Emmanuel Agu.
CS 4731: Computer Graphics Lecture 11: 3D Viewing Emmanuel Agu.
1 Computer Graphics Week7 -2D Rotation. 3-Rotation A shape can be rotated about any of the three axes. A rotation about the z-axis will actually rotate.
Introduction to 3D viewing 3D is just like taking a photograph!
3D VIEWING ILLUSTRATED. WHAT YOU SEE DEPENDS ON YOUR POSITION In the real world, what you see depends on where you stand, the direction you look, how.
ENGINEERING MECHANICS CHAPTER 2 FORCES & RESULTANTS
Transformations Aaron Bloomfield CS 445: Introduction to Graphics
Mouse-Based Viewing & Navigation Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Monday, November 3, 2003.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
Transformation of Graphics
Kinematics of Robot Manipulator
TWO DIMENSIONAL GEOMETRIC TRANSFORMATIONS CA 302 Computer Graphics and Visual Programming Aydın Öztürk
Computer Graphics World, View and Projection Matrices CO2409 Computer Graphics Week 8.
Lecture 3 Transformations.
Geometric transformations The Pipeline
CSE 681 Review: Transformations. CSE 681 Transformations Modeling transformations build complex models by positioning (transforming) simple components.
CO1301: Games Concepts Dr Nick Mitchell (Room CM 226) Material originally prepared by Gareth Bellaby.
Homogeneous Form, Introduction to 3-D Graphics Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Monday, October 20,
Stages of Vertex Transformation To specify viewing, modeling, and projection transformations, you construct a 4 × 4 matrix M, which is then multiplied.
Computer Graphics 2D Transformations. 2 of 74 Contents In today’s lecture we’ll cover the following: –Why transformations –Transformations Translation.
Computer Graphics I, Fall 2010 Computer Viewing.
The Camera Course Information CVG: Programming 4 My Name: Mark Walsh Website: Recommended Reading.
OpenGL The Viewing Pipeline: Definition: a series of operations that are applied to the OpenGL matrices, in order to create a 2D representation from 3D.
CO1301: Games Concepts Dr Nick Mitchell (Room CM 226) Material originally prepared by Gareth Bellaby.
Advanced Viewing Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Friday, October 31, 2003.
Computer Graphics 3D Transformations. Translation.
Affine Transformation. Affine Transformations In this lecture, we will continue with the discussion of the remaining affine transformations and composite.
Lecture 3 Transformations. 2D Object Transformations The functions used for modifying the size, location, and orientation of objects or of the camera.
Transformations CS 445/645 Introduction to Computer Graphics David Luebke, Spring 2003.
Graphics CSCI 343, Fall 2015 Lecture 16 Viewing I
Composite 3D Transformations. Example of Composite 3D Transformations Try to transform the line segments P 1 P 2 and P 1 P 3 from their start position.
3D Transformation A 3D point (x,y,z) – x,y, and z coordinates
Introduction to Computer Graphics: Viewing Transformations Rama C
Computer Graphics Matrices
CS 4731: Computer Graphics Lecture 12: More 3D Viewing Emmanuel Agu.
Graphics Lecture 2: Slide 1 Lecture 2 Transformations for animation.
Learning Objectives Affine transformations Affine transformations Translation Translation Rotation Rotation Scaling Scaling Reflection Reflection Shear.
Camera. “Up”, “Forward” and “Along” The three camera view vectors are defined as shown:
Geometric Transformations. Transformations Linear transformations Rigid transformations Affine transformations Projective transformations T Global reference.
CS5500 Computer Graphics March 20, Computer Viewing Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts.
End effector End effector - the last coordinate system of figure Located in joint N. But usually, we want to specify it in base coordinates. 1.
Geometric Transformations Ceng 477 Introduction to Computer Graphics Computer Engineering METU.
TA: Ryan Freedman Sushma Kini Chi Zhou.  Due Date: March , 12:30 PM  We want a zip folder named cs418_mp1_{netid}.zip which contains  Source.
OpenGL LAB III.
Transformations. Transformations Introduce standard transformations ◦ Rotation ◦ Translation ◦ Scaling ◦ Shear Derive homogeneous coordinate transformation.
Computer Graphics Lecture 42 Viewing Using OpenGL II Taqdees A. Siddiqi
Introduction to 3-D Viewing Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Monday, October 27, 2003.
Computer Viewing.
A Camera Class for OpenGL
GAM 325/425: Applied 3D Geometry
Projection in 3-D Glenn G. Chappell
More on Widgets, Misc. Topics
Geometric Objects and Transformations (II)
Presentation transcript:

A Camera Class for OpenGL John McGuiness October 2006

Necessity for a Camera Class Existing available camera tool - gluLookAt() Existing available camera tool - gluLookAt() Basic utility which encapsulates a series of rotate and translate commands Basic utility which encapsulates a series of rotate and translate commands Allows viewing along an arbitrary line of sight with an “Up” vector defined Allows viewing along an arbitrary line of sight with an “Up” vector defined Need extra transformations to provide greater flexibility Need extra transformations to provide greater flexibility Need to modify “Forward” and “Along” vectors as well as “Up” to improve on gluLookAt() Need to modify “Forward” and “Along” vectors as well as “Up” to improve on gluLookAt() Camera class may be built to encapsulate commands for greater ease of use Camera class may be built to encapsulate commands for greater ease of use

“Up”, “Forward” and “Along” The three camera view vectors are defined as shown: The three camera view vectors are defined as shown:

Proposed Camera Features The camera class should: The camera class should: Provide motion along the view vectors as well as arbitrary axes (in some cases) Provide motion along the view vectors as well as arbitrary axes (in some cases) Provide rotation about the view vectors as well as arbitrary axes (in some cases) Provide rotation about the view vectors as well as arbitrary axes (in some cases) Maintain the camera’s own orientation by keeping the viewing vectors orthogonal to each other Maintain the camera’s own orientation by keeping the viewing vectors orthogonal to each other Need to define motion for two possible types of camera: Need to define motion for two possible types of camera: Land camera – e.g. for road vehicles simulation Land camera – e.g. for road vehicles simulation Air camera – e.g. for flight simulation Air camera – e.g. for flight simulation

Camera Motion Walking Walking This is motion along the Forward vector (or Z-axis): This is motion along the Forward vector (or Z-axis):

Camera Motion Strafing Strafing This is side to side motion on the Along vector (or X-axis): This is side to side motion on the Along vector (or X-axis):

Camera Motion Flying Flying This is vertical motion on the Up vector (or Y-axis): This is vertical motion on the Up vector (or Y-axis):

Camera Rotation Pitching Pitching This is rotation about the Along vector – looking up and down This is rotation about the Along vector – looking up and down

Camera Rotation Yawing Yawing This is rotation about the Up vector – looking left and right This is rotation about the Up vector – looking left and right

Camera Rotation Rolling Rolling This is rotation about the Forward vector – twisting left and right This is rotation about the Forward vector – twisting left and right

Camera Class Declaration The camera class uses a type called Vector3D which provides storage and common operations (e.g. dot product, cross product etc.) for vectors The camera class uses a type called Vector3D which provides storage and common operations (e.g. dot product, cross product etc.) for vectors #include "Vector3D.h" The enumerated type defined below is used to distinguish between the two types of camera The enumerated type defined below is used to distinguish between the two types of camera enum CAM_TYPE { LAND_CAM, AIR_CAM };

Camera Class Declaration First, we need private variables for each view vector as well as the camera type and current position: First, we need private variables for each view vector as well as the camera type and current position: class Camera { private: CAM_TYPE CameraType; Vector3D Position; Vector3D Along; Vector3D Up; Vector3D Forward;...

Camera Class Declaration Various construction/destruction, update and control functions are then declared publically: Various construction/destruction, update and control functions are then declared publically: class Camera {...public: Camera(CAM_TYPE ct = LAND_CAM); // Default: land virtual ~Camera(); void SetCameraType(CAM_TYPE ct); Vector3D GetPosition(); void Reset(); void Update();...

Camera Class Declaration Finally, the motion and rotation functions are declared. Finally, the motion and rotation functions are declared. The boolean array, Wall[4], is an extra feature which modifies the motion of land cameras if they have to slide against walls (as opposed to going through them) The boolean array, Wall[4], is an extra feature which modifies the motion of land cameras if they have to slide against walls (as opposed to going through them) class Camera {...public:... void Pitch(GLfloat theta); void Yaw(GLfloat theta); void Roll(GLfloat theta); void Walk(GLfloat delta, bool Wall[4]); void Strafe(GLfloat delta, bool Wall[4]); void Fly(GLfloat delta); };

Setup and Control Functions The code listing on the following two slides is fairly self- explanatory The code listing on the following two slides is fairly self- explanatory It comprises the basic constructor and destructor as well as a function to alter the camera type It comprises the basic constructor and destructor as well as a function to alter the camera type The Reset() function sets the camera position to (0,0,0) and aligns the viewing axes with the local drawing coordinate system The Reset() function sets the camera position to (0,0,0) and aligns the viewing axes with the local drawing coordinate system Note that the default Forward vector points along the negative Z-axis Note that the default Forward vector points along the negative Z-axis

Setup and Control Functions Camera::Camera(CAM_TYPE ct) { SetCameraType(ct);Reset();} Camera::~Camera() { } void Camera::SetCameraType(CAM_TYPEct) { CameraType = ct; }

Setup and Control Functions Vector3D Camera::GetPosition() { return Position; } void Camera::Reset() { Position = Vector3D(0.0, 0.0, 0.0); Along = Vector3D(1.0, 0.0, 0.0); Up = Vector3D(0.0, 1.0, 0.0); Forward = Vector3D(0.0, 0.0, -1.0); Update();}

Building the View Matrix The last function called by Reset() is probably the most important The last function called by Reset() is probably the most important The Update() function applies all changes made to the viewing axes and camera position, and updates the view in MODELVIEW mode The Update() function applies all changes made to the viewing axes and camera position, and updates the view in MODELVIEW mode In actual fact, as with gluLookAt(), the perception of camera motion is achieved by moving the objects around the scene while keeping the camera at a fixed position In actual fact, as with gluLookAt(), the perception of camera motion is achieved by moving the objects around the scene while keeping the camera at a fixed position Instead of using translations and rotations, a view matrix may be built, meaning that just one OpenGL function call is needed – glLoadMatrix() Instead of using translations and rotations, a view matrix may be built, meaning that just one OpenGL function call is needed – glLoadMatrix()

Building the View Matrix First we obtain the camera virtual position coordinates using the dot product of pairs of the view vectors: First we obtain the camera virtual position coordinates using the dot product of pairs of the view vectors: void Camera::Update() { GLfloat x = DotProduct(Along, Position); GLfloat y = DotProduct(Up, Position); GLfloat z = DotProduct(Forward, Position);... These will be used to translate the camera (or rather, the scene) to its correct position These will be used to translate the camera (or rather, the scene) to its correct position

Building the View Matrix The translation part of the view matrix is shown below: The translation part of the view matrix is shown below: T = –x –y z 1 Note that we must remember to make z positive, since for convenience we have taken “Forward” as meaning the direction into the screen which is opposite to OpenGL convention (Z-axis is positive outwards) Note that we must remember to make z positive, since for convenience we have taken “Forward” as meaning the direction into the screen which is opposite to OpenGL convention (Z-axis is positive outwards)

Building the View Matrix The rotation part of the view matrix is built from the view vectors as shown: The rotation part of the view matrix is built from the view vectors as shown: A.x U.x –F.x R =A.y U.y –F.y A.z U.z –F.z Again, the Forward vector is reversed Again, the Forward vector is reversed

Building the View Matrix Combining these two matrices, we get: Combining these two matrices, we get: A.x U.x –F.x0 A.x U.x –F.x0 V = A.y U.y –F.y0 = A.y U.y –F.y A.z U.z –F.z0 A.z U.z –F.z0 –x –y z –x –y z 1 The code on the following slides shows the rest of the implemented function The code on the following slides shows the rest of the implemented function

Building the View Matrix void Camera::Update() {... Glfloat ViewMatrix[4][4]; ViewMatrix[0][0] = Along.x; ViewMatrix[0][1] = Up.x; ViewMatrix[0][2] = -Forward.x; ViewMatrix[0][3] = 0.0; ViewMatrix[1][0] = Along.y; ViewMatrix[1][1] = Up.y; ViewMatrix[1][2] = -Forward.y; ViewMatrix[1][3] = 0.0;...

Building the View Matrix... ViewMatrix[2][0] = Along.z; ViewMatrix[2][1] = Up.z; ViewMatrix[2][2] = -Forward.z; ViewMatrix[2][3] = 0.0; ViewMatrix[3][0] = -x; ViewMatrix[3][1] = -y; ViewMatrix[3][2] = z; ViewMatrix[3][3] = 1.0; glMatrixMode(GL_MODELVIEW); glLoadMatrixf((GLfloat *)&ViewMatrix); }

Camera Rotation Functions The Pitch(), Yaw() and Roll() functions change the direction of the Forward, Along and Up vectors respectively The Pitch(), Yaw() and Roll() functions change the direction of the Forward, Along and Up vectors respectively In each case, the rotation will result in the alteration of a second view vector, leaving one unchanged In each case, the rotation will result in the alteration of a second view vector, leaving one unchanged The second modified vector is found by calculating the cross product of the other two vectors The second modified vector is found by calculating the cross product of the other two vectors This means that mutual orthognality is maintained for the three vectors This means that mutual orthognality is maintained for the three vectors

Camera Rotation Functions Looking at a yaw from above, we can see how to calculate the new direction of the Along vector: Looking at a yaw from above, we can see how to calculate the new direction of the Along vector:

Camera Rotation Functions Thus, for the Yaw function definition, we have: Thus, for the Yaw function definition, we have: void Camera::Yaw(GLfloat theta) { Along = Along * cos(theta * DEG2RAD) + Forward * sin(theta * DEG2RAD); Along.Normalize(); Forward = CrossProduct(Along, Up) * -1.0; Update();} Pitch() and Roll() on the following slide look very similar Pitch() and Roll() on the following slide look very similar

Camera Rotation Functions void Camera::Pitch(GLfloat theta) { // Invert UP/DOWN for air cameras if(CameraType == AIR_CAM) theta = -theta; Forward = Forward * cos(theta * DEG2RAD) + Up * sin(theta * DEG2RAD); + Up * sin(theta * DEG2RAD);Forward.Normalize(); Up = CrossProduct(Forward, Along) * -1.0; Update();} void Camera::Roll(GLfloat theta) { if(CameraType == LAND_CAM) return; // Not for land cams Up = Up * cos(theta * DEG2RAD) - Along * sin(theta * DEG2RAD); - Along * sin(theta * DEG2RAD);Up.Normalize(); Along = CrossProduct(Forward, Up); Update();}

Camera Motion Functions Walk(), Strafe() and Fly() are a little easier to implement Walk(), Strafe() and Fly() are a little easier to implement In each case, all we have to do is add the correct scaled vector to the camera’s Position vector and update In each case, all we have to do is add the correct scaled vector to the camera’s Position vector and update As with rotation functions, motion functions work slightly differently depending on the type of camera being used As with rotation functions, motion functions work slightly differently depending on the type of camera being used For example, when walking forward with a land camera, if the view has been pitched upwards, we do not want to move up the camera’s forward vector, but rather along a modified vector with the Y componet set to 0 – this will achieve the effect of staying on the ground rather than taking off into the air. For example, when walking forward with a land camera, if the view has been pitched upwards, we do not want to move up the camera’s forward vector, but rather along a modified vector with the Y componet set to 0 – this will achieve the effect of staying on the ground rather than taking off into the air.

Camera Rotation Functions The Walk function with wall handling also implemented: The Walk function with wall handling also implemented: void Camera::Walk(GLfloat delta, bool Wall[4]) { if(CameraType == LAND_CAM) if(CameraType == LAND_CAM) Position -= Vector3D(Forward.x * Position -= Vector3D(Forward.x * !(Wall[0] && Forward.x * delta > 0.0 || !(Wall[0] && Forward.x * delta > 0.0 || Wall[1] && Forward.x * delta < 0.0), Wall[1] && Forward.x * delta < 0.0), 0.0, Forward.z * 0.0, Forward.z * !(Wall[2] && Forward.z * delta > 0.0 || !(Wall[2] && Forward.z * delta > 0.0 || Wall[3] && Forward.z * delta < 0.0)) Wall[3] && Forward.z * delta < 0.0)) * delta; * delta; else Position -= Forward * delta; // Air camera else Position -= Forward * delta; // Air camera Update(); Update();}

Camera Rotation Functions Similarly, the Strafe function is defined as follows: Similarly, the Strafe function is defined as follows: void Camera::Strafe(GLfloat delta, bool Wall[4]) { if(CameraType == LAND_CAM) if(CameraType == LAND_CAM) Position -= Vector3D(Along.x * Position -= Vector3D(Along.x * !(Wall[0] && Along.x * delta > 0.0 || !(Wall[0] && Along.x * delta > 0.0 || Wall[1] && Along.x * delta < 0.0), Wall[1] && Along.x * delta < 0.0), 0.0, Along.z * 0.0, Along.z * !(Wall[2] && Along.z * delta > 0.0 || !(Wall[2] && Along.z * delta > 0.0 || Wall[3] && Along.z * delta < 0.0)) Wall[3] && Along.z * delta < 0.0)) * delta; * delta; else Position += Along * delta; // Air camera else Position += Along * delta; // Air camera Update(); Update();}

Camera Rotation Functions Finally, flying is, of course, only allowed for air cameras: Finally, flying is, of course, only allowed for air cameras: void Camera::Fly(GLfloat delta, bool Wall[4]) { // Don't allow for land cameras // Don't allow for land cameras if(CameraType == LAND_CAM) return; if(CameraType == LAND_CAM) return; Position += Up * delta; Position += Up * delta; Update(); Update();} Although flying through walls has been allowed here, this would be implemented in the same manner as the previous two functions Although flying through walls has been allowed here, this would be implemented in the same manner as the previous two functions

References Frank D. Luna, 2003, Introduction to 3D Game Programming with DirectX 9.0, Wordware Publishing, Inc. Frank D. Luna, 2003, Introduction to 3D Game Programming with DirectX 9.0, Wordware Publishing, Inc. Silicon Graphics Inc., 1997, OpenGL Programming Guide, Chapter 3 – Viewing, Addison-Wesley Publishing Company Silicon Graphics Inc., 1997, OpenGL Programming Guide, Chapter 3 – Viewing, Addison-Wesley Publishing Company Philipp Crocoll, The Advanced CodeColony Camera, Philipp Crocoll, The Advanced CodeColony Camera,