Viewing and Camera Rotations
description
Transcript of Viewing and Camera Rotations
![Page 1: Viewing and Camera Rotations](https://reader035.fdocuments.in/reader035/viewer/2022062520/568160e8550346895dd01f3d/html5/thumbnails/1.jpg)
Viewing and Camera Rotations
CSE 781Prof. Roger Crawfis
![Page 2: Viewing and Camera Rotations](https://reader035.fdocuments.in/reader035/viewer/2022062520/568160e8550346895dd01f3d/html5/thumbnails/2.jpg)
Introduction to 3D viewing
• 3D is just like taking a photograph!
2
![Page 3: Viewing and Camera Rotations](https://reader035.fdocuments.in/reader035/viewer/2022062520/568160e8550346895dd01f3d/html5/thumbnails/3.jpg)
Viewing Transformation
• Position and orient your camera
3
![Page 4: Viewing and Camera Rotations](https://reader035.fdocuments.in/reader035/viewer/2022062520/568160e8550346895dd01f3d/html5/thumbnails/4.jpg)
Projection Transformation
• Control the “lens” of the camera• Project the object from 3D world to 2D screen
4
![Page 5: Viewing and Camera Rotations](https://reader035.fdocuments.in/reader035/viewer/2022062520/568160e8550346895dd01f3d/html5/thumbnails/5.jpg)
Viewing Transformation (2)
• Important camera parameters to specify
5
Camera (eye) position (Ex,Ey,Ez) in world coordinate system
Center of interest (coi) (cx, cy, cz) Orientation (which way is up?) View-up vector (Up_x,
Up_y, Up_z)
world(cx, cy, cz)
(ex, ey, ez)view up vector(Up_x, Up_y, Up_z)
![Page 6: Viewing and Camera Rotations](https://reader035.fdocuments.in/reader035/viewer/2022062520/568160e8550346895dd01f3d/html5/thumbnails/6.jpg)
Viewing Transformation (3)
• Transformation? – Form a camera (eye) coordinate frame
– Transform objects from world to eye space 6
world
uv n
x
y
z
Eye coordinate frame coi
![Page 7: Viewing and Camera Rotations](https://reader035.fdocuments.in/reader035/viewer/2022062520/568160e8550346895dd01f3d/html5/thumbnails/7.jpg)
Viewing Transformation (4)
• Eye space?
• Transform to eye space can simplify many downstream operations (such as projection) in the pipeline
7
world
uv n
x
y
z
(0,0,0) coi
(1,0,0)(0,1,0)(0,0,1)
![Page 8: Viewing and Camera Rotations](https://reader035.fdocuments.in/reader035/viewer/2022062520/568160e8550346895dd01f3d/html5/thumbnails/8.jpg)
Viewing Transformation (5)
• In OpenGL: - gluLookAt (Ex, Ey, Ez, cx, cy, cz, Up_x, Up_y, Up_z) - The view up vector is usually (0,1,0) - Remember to set the OpenGL matrix mode to GL_MODELVIEW first
8
![Page 9: Viewing and Camera Rotations](https://reader035.fdocuments.in/reader035/viewer/2022062520/568160e8550346895dd01f3d/html5/thumbnails/9.jpg)
Viewing Transformation (6)
9
void display() { glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0,0,1,0,0,0,0,1,0); display_all(); // your display routine}
If this is constant, why do it every frame?
![Page 10: Viewing and Camera Rotations](https://reader035.fdocuments.in/reader035/viewer/2022062520/568160e8550346895dd01f3d/html5/thumbnails/10.jpg)
Suppose we have three orthogonal vectors…
• v1,v2,v3
• Let’s build a matrix like this:
• This will rotate: v1 onto the x axis, v2 onto the y axis, v3 onto the z axis
10
1000000
),,(,3,3,3
,2,2,2
,1,1,1
321zyx
zyx
zyx
vvvvvvvvv
vvvR
![Page 11: Viewing and Camera Rotations](https://reader035.fdocuments.in/reader035/viewer/2022062520/568160e8550346895dd01f3d/html5/thumbnails/11.jpg)
My Version of gluLookAt()
11
void mygluLookAt(Point3 eye, Point3 center, Point3 up){ Point3 cameraZ = Normalize( eye – center ); // v3 Point3 cameraX = Normalize( Cross(up, cameraZ) ); // v1 Point3 cameraY = Cross( cameraZ, cameraX ); // v2 GLdouble m[16]; // Fill the matrix in row by row m[0] = cameraX.X; m[4] = cameraX.Y; m[8] = cameraX.Z; m[12] = 0.0; m[1] = cameraY.X; m[5] = cameraY.Y; m[9] = cameraY.Z; m[13] = 0.0; m[2] = cameraZ.X; m[6] = cameraZ.Y; m[10] = cameraZ.Z; m[14] = 0.0; m[3] = m[7] = m[11] = 0.0; m[15] = 1.0; glMultMatrixd( m ); glTranslated( -eye.X, -eye.Y, -eye.Z );}
Order of transformations!
![Page 12: Viewing and Camera Rotations](https://reader035.fdocuments.in/reader035/viewer/2022062520/568160e8550346895dd01f3d/html5/thumbnails/12.jpg)
Messing with the camera
• What if I want to PAN the camera?• We need to define “PAN”– There are two
possibilities• Crab• Pan
12
![Page 13: Viewing and Camera Rotations](https://reader035.fdocuments.in/reader035/viewer/2022062520/568160e8550346895dd01f3d/html5/thumbnails/13.jpg)
Panning
• Suppose we pan around the camera Y axis– This is NOT “up” in world space.– We need to change the lookAt point.
13
![Page 14: Viewing and Camera Rotations](https://reader035.fdocuments.in/reader035/viewer/2022062520/568160e8550346895dd01f3d/html5/thumbnails/14.jpg)
Operations on Points in Space
• LookAt is a point in space• Need these transformations:– Translate by –eye– Rotate frame onto axis (using some M)– Rotate around Y by pan angle– Inverse rotate M– Translate by eye
• PP=T(eye) MT RY(q) M T(-eye)
14
![Page 15: Viewing and Camera Rotations](https://reader035.fdocuments.in/reader035/viewer/2022062520/568160e8550346895dd01f3d/html5/thumbnails/15.jpg)
Easier #1
• Just replace the matrix using gluLookAt.– Problems?
15
![Page 16: Viewing and Camera Rotations](https://reader035.fdocuments.in/reader035/viewer/2022062520/568160e8550346895dd01f3d/html5/thumbnails/16.jpg)
Easier #2
• The first 2 operations are already what is done to set the camera up:– M T(-eye)
• We just need to add a rotate after this is done.– Implies we want to pre-multiply by a rotation
matrix.
16
![Page 17: Viewing and Camera Rotations](https://reader035.fdocuments.in/reader035/viewer/2022062520/568160e8550346895dd01f3d/html5/thumbnails/17.jpg)
Easier #2
• Steps:1. Read out the current matrix.2. Set the matrix to the identity matrix.3. Rotate about the y-axis the amount we want to
pan.4. Post-multiply by the matrix read out in step #1.
17
With GLSL Shaders, the OpenGL matrix system does not buy you much, so just use your own matrix classes.
![Page 18: Viewing and Camera Rotations](https://reader035.fdocuments.in/reader035/viewer/2022062520/568160e8550346895dd01f3d/html5/thumbnails/18.jpg)
Camera Controls
• Tilt• Roll• Dolly• Boom• Zoom (same as dolly?)• General camera (or entity) movement and the
user interface / control.
18
Eliot Lash, 2007 (from Wikipedia.org – Camera dolly)
![Page 19: Viewing and Camera Rotations](https://reader035.fdocuments.in/reader035/viewer/2022062520/568160e8550346895dd01f3d/html5/thumbnails/19.jpg)
Camera Controls
• Stationary• 2 degrees of freedom +
zoom• QuicktimeVR
19
![Page 20: Viewing and Camera Rotations](https://reader035.fdocuments.in/reader035/viewer/2022062520/568160e8550346895dd01f3d/html5/thumbnails/20.jpg)
Interactive Applications
• How do we add interactive control?• Many different paradigms– Examiner => Object in hand– Fly-thru => In a virtual vehicle pod– Walk-thru => Constrained to stay on ground.– Move-to / re-center => Pick a location to fly to.
• Collision detection?– Can we pass thru objects like ghosts?
![Page 21: Viewing and Camera Rotations](https://reader035.fdocuments.in/reader035/viewer/2022062520/568160e8550346895dd01f3d/html5/thumbnails/21.jpg)
Interactive Applications
• What do we use to control the motion?– Mouse• One-button, two-button, three-button• What button does what?• Only when mouse is clicked down, released up, or
continuously as the mouse moves?– Keyboard• Arrow keys?
![Page 22: Viewing and Camera Rotations](https://reader035.fdocuments.in/reader035/viewer/2022062520/568160e8550346895dd01f3d/html5/thumbnails/22.jpg)
Input Devices
• Interactive user control devices– Mouse– 3D pointer - Polhemus, Microscribe, …– Spaceball– Hand-held wand– Data Glove– Gesture– Custom
![Page 23: Viewing and Camera Rotations](https://reader035.fdocuments.in/reader035/viewer/2022062520/568160e8550346895dd01f3d/html5/thumbnails/23.jpg)
![Page 24: Viewing and Camera Rotations](https://reader035.fdocuments.in/reader035/viewer/2022062520/568160e8550346895dd01f3d/html5/thumbnails/24.jpg)
A Virtual Trackball
• A rather standard and easy-to-use interface.• Examiner type of interaction.• Consider a hemi-sphere over the image-plane.• Each point in the
image is projectedonto the hemi-sphere.
![Page 25: Viewing and Camera Rotations](https://reader035.fdocuments.in/reader035/viewer/2022062520/568160e8550346895dd01f3d/html5/thumbnails/25.jpg)
A Virtual Trackball
• Points inside the projection of the hemi-sphere are mapped up to the surface.– Determine distance from point (mouse position) to the
image-plane center.– Scale such that points on
the silhouette of the sphere have unit length.
– Add the z-coordinate tonormalize the vector.
![Page 26: Viewing and Camera Rotations](https://reader035.fdocuments.in/reader035/viewer/2022062520/568160e8550346895dd01f3d/html5/thumbnails/26.jpg)
A Virtual Trackball• Do this for all points.• Keep track of the last trackball (mouse) location and the
current location.• This is the direction we want the scene to move in.• Take the direction
perpendicular to this and use it as the axis of rotation.
• Use the distance between the two points to determine the rotation angle (or amount).
![Page 27: Viewing and Camera Rotations](https://reader035.fdocuments.in/reader035/viewer/2022062520/568160e8550346895dd01f3d/html5/thumbnails/27.jpg)
A Virtual Trackball
• Rotation axis:
Where, v1 and v2 are themouse points mappedto the sphere.
v1
v2
1 2u v v
| sin q| = ||||
||
21 ppn
![Page 28: Viewing and Camera Rotations](https://reader035.fdocuments.in/reader035/viewer/2022062520/568160e8550346895dd01f3d/html5/thumbnails/28.jpg)
Virtual Trackball
• How to calculate p1 and p2? • Assuming the mouse position is (x,y), then the sphere point P also
has x and y coordinates equal to x and y • Assume the radius of the hemi-sphere is 1. So the z coordinate of P
is
• Note: normalize viewport y extend to -1 to 1• If a point is outside the circle, project it to the nearest point on the circle (set z to 0 and renormalize (x,y))
22 yx1
z
y
(x,y,0)
x
![Page 29: Viewing and Camera Rotations](https://reader035.fdocuments.in/reader035/viewer/2022062520/568160e8550346895dd01f3d/html5/thumbnails/29.jpg)
Virtual Trackball
Visualization of the algorithm
![Page 30: Viewing and Camera Rotations](https://reader035.fdocuments.in/reader035/viewer/2022062520/568160e8550346895dd01f3d/html5/thumbnails/30.jpg)
Example
• Example from Ed Angel’s OpenGL Primer • In this example, the virtual trackball is used to rotate a
color cube• The code for the colorcube function is omitted• I will not cover the following code, but I am sure you
will find it useful• Note you can use the trackball to move the camera or
you can apply the rotations to a transform node in your scene graph to move an object. The rotations are
negated from each other though.
![Page 31: Viewing and Camera Rotations](https://reader035.fdocuments.in/reader035/viewer/2022062520/568160e8550346895dd01f3d/html5/thumbnails/31.jpg)
Initialization#define bool int /* if system does not support bool type */#define false 0#define true 1#define M_PI 3.14159 /* if not in math.h */
int winWidth, winHeight;
float angle = 0.0, axis[3], trans[3];
bool trackingMouse = false;bool redrawContinue = false;bool trackballMove = false;
float lastPos[3] = {0.0, 0.0, 0.0};int curx, cury;int startX, startY;
![Page 32: Viewing and Camera Rotations](https://reader035.fdocuments.in/reader035/viewer/2022062520/568160e8550346895dd01f3d/html5/thumbnails/32.jpg)
The Projection Stepvoid trackball_ptov(int x, int y, int width, int height, float v[3]){ float d, a; /* project x,y onto a hemisphere centered within width, height ,
note z is up here*/ v[0] = (2.0*x - width) / width; v[1] = (height - 2.0F*y) / height; d = sqrt(v[0]*v[0] + v[1]*v[1]); v[2] = cos((M_PI/2.0) * ((d < 1.0) ? d : 1.0)); a = 1.0 / sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]); v[0] *= a; v[1] *= a; v[2] *= a;}
![Page 33: Viewing and Camera Rotations](https://reader035.fdocuments.in/reader035/viewer/2022062520/568160e8550346895dd01f3d/html5/thumbnails/33.jpg)
glutMotionFunc (1)Void mouseMotion(int x, int y){ float curPos[3], dx, dy, dz; /* compute position on hemisphere */ trackball_ptov(x, y, winWidth, winHeight, curPos); if(trackingMouse) { /* compute the change in position on the hemisphere */ dx = curPos[0] - lastPos[0]; dy = curPos[1] - lastPos[1]; dz = curPos[2] - lastPos[2];
![Page 34: Viewing and Camera Rotations](https://reader035.fdocuments.in/reader035/viewer/2022062520/568160e8550346895dd01f3d/html5/thumbnails/34.jpg)
glutMotionFunc (2)if (dx || dy || dz) { /* compute theta and cross product */ angle = 90.0 * sqrt(dx*dx + dy*dy + dz*dz); axis[0] = lastPos[1]*curPos[2] – lastPos[2]*curPos[1]; axis[1] = lastPos[2]*curPos[0] – lastPos[0]*curPos[2]; axis[2] = lastPos[0]*curPos[1] – lastPos[1]*curPos[0]; /* update position */ lastPos[0] = curPos[0]; lastPos[1] = curPos[1]; lastPos[2] = curPos[2]; } } glutPostRedisplay();}
![Page 35: Viewing and Camera Rotations](https://reader035.fdocuments.in/reader035/viewer/2022062520/568160e8550346895dd01f3d/html5/thumbnails/35.jpg)
Idle and Display Callbacksvoid spinCube(){ if (redrawContinue) glutPostRedisplay();}
void display(){ glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); if (trackballMove) { glRotatef(angle, axis[0], axis[1], axis[2]);
} colorcube();
glutSwapBuffers();}
![Page 36: Viewing and Camera Rotations](https://reader035.fdocuments.in/reader035/viewer/2022062520/568160e8550346895dd01f3d/html5/thumbnails/36.jpg)
Mouse Callbackvoid mouseButton(int button, int state, int x, int y){if(button==GLUT_RIGHT_BUTTON) exit(0);
/* holding down left button allows user to rotate cube */if(button==GLUT_LEFT_BUTTON) switch(state) { case GLUT_DOWN: y=winHeight-y; startMotion( x,y); break;
case GLUT_UP: stopMotion( x,y); break;
} }
![Page 37: Viewing and Camera Rotations](https://reader035.fdocuments.in/reader035/viewer/2022062520/568160e8550346895dd01f3d/html5/thumbnails/37.jpg)
Start Functionvoid startMotion(int x, int y){ trackingMouse = true; redrawContinue = false; startX = x; startY = y; curx = x; cury = y; trackball_ptov(x, y, winWidth, winHeight, lastPos); trackballMove=true;}
![Page 38: Viewing and Camera Rotations](https://reader035.fdocuments.in/reader035/viewer/2022062520/568160e8550346895dd01f3d/html5/thumbnails/38.jpg)
Stop Functionvoid stopMotion(int x, int y){ trackingMouse = false; /* check if position has changed */ if (startX != x || startY != y) redrawContinue = true;
else { angle = 0.0; redrawContinue = false; trackballMove = false;
}}
![Page 39: Viewing and Camera Rotations](https://reader035.fdocuments.in/reader035/viewer/2022062520/568160e8550346895dd01f3d/html5/thumbnails/39.jpg)
public void KeyTrackballArrows( object sender, Forms.KeyEventArgs e ) { // This provides a simple mapping of the keyboard to rotations of the // object (or the world if used in a camera). // The SHIFT key can be used for gross changes, the CTRL key can be // used for fine-control. Vector3 rotationAxis = XAxis; float sign = 1.0f; if( e.KeyCode == Keys.Up ) { rotationAxis = XAxis; sign = -1.0f; } else if( e.KeyCode == Keys.Down ) { rotationAxis = XAxis; } else if( e.KeyCode == Keys.Left ) { rotationAxis = YAxis; sign = -1.0f; } else if( e.KeyCode == Keys.Right ) { rotationAxis = Yaxis; } else { return; } float rotationAngle = RotationIncrementStandard; if( e.Modifiers == Keys.Shift ) rotationAngle = RotationIncrementLarge; else if( e.Modifiers == Keys.Control ) rotationAngle = RotationIncrementSmall; if( rotate != null ) { rotate( sign*rotationAngle, rotationAxis ); }}