View Transformation

64
View View Transformation Transformation CSC 830 Note 3 CSC 830 Note 3 Course note credit to Prof. Seth Teller, MI

description

View Transformation. CSC 830 Note 3. Course note credit to Prof. Seth Teller, MIT. View Transformation. Transform (i.e., express) geometry into coordinates that are well-suited to (simple) clipping and projection hardware. Positioning Synthetic Camera. - PowerPoint PPT Presentation

Transcript of View Transformation

Page 1: View Transformation

View TransformationView TransformationCSC 830 Note 3CSC 830 Note 3

Course note credit to Prof. Seth Teller, MIT

Page 2: View Transformation

View Transformation

Transform (i.e., express) geometry into coordinates that arewell-suited to (simple) clipping and projection hardware

Page 3: View Transformation

Positioning Synthetic Camera

What are our “degrees of freedom” in camera positioning?To achieve effective visual simulation, we want:1) the eye point to be in proximity of modeled scene2) the view to be directed toward region of interest, and3) the image plane to have a reasonable “twist”

Page 4: View Transformation

Eye Coordinates

Eyepoint at originu axis toward “right” of image planev axis toward “top” of image planeview direction along negative n axis

Page 5: View Transformation

Transformation to Eye Coordinates

Our task: construct the transformation M that re-expresses world coordinates in the viewer frame

Page 6: View Transformation

Machinery: Changing Orthobases

Suppose you are given an orthobasis u, v, nWhat is the action of the matrix M withrows u, v, and n as below?

Page 7: View Transformation

Applying M to u, v, n

Two equally valid interpretations, depending on reference frame:1: Think of uvn basis as a rigid object in a fixed world spaceThen M “rotates” uvn basis into xyz basis2: Think of a fixed axis triad, with “labels” from xyz spaceThen M “reexpresses” an xyz point p in uvn coords!It is this second interpretation that we use todayto “relabel” world-space geometry with eye space coordinates

Page 8: View Transformation

Positioning Synthetic Camera

Given eyepoint e, basis ˆu, ˆv, ˆnDeduce M that expresses world in eye coordinates:Overlay origins, then change bases:

Page 9: View Transformation

Positioning Synthetic Camera

Check: does M re-express world geometry in eye coordinates?

Page 10: View Transformation

Positioning Synthetic Camera

Camera specification must include:World-space eye position eWorld-space “lookat direction” -nAre e and -n enough to determine the camera DOFs (degrees of freedom)?

Page 11: View Transformation

Positioning Synthetic Camera

Are e and -n enough to determine the camera DOFs?No. Note that we were not given u and v!(Why not simply require the user to specify them?)

We must also determine u and v, i.e., camera “twist” about n.Typically done by specification of a world-space “up vector”provided by user interface, e.g., using gluLookat(e, c, up)“Twist” constraint: Align v with world up vector (How?)

Page 12: View Transformation

Positioning Synthetic Camera

Page 13: View Transformation

Where are we?

Page 14: View Transformation

What is Projection?

Any operation that reduces dimension (e.g., 3D to 2D)

Orthographic ProjectionPerspective Projection

Page 15: View Transformation

Orthographic Projection• focal point at infinity • rays are parallel and orthogonal to the image

plane

Image

World

F

F

Image

World

I

W

Page 16: View Transformation

Comparison

Page 17: View Transformation

Simple Perspective Camera• camera looks along z-axis• focal point is the origin• image plane is parallel to xy-plane at

distance d• d is call focal length

Page 18: View Transformation

Similar TrianglesY

Z[0, d][0, 0]

[Y, Z]

[(d/Z)Y, d]

• Similar situation with x-coordinate• Similar Triangles:

point [x,y,z] projects to [(d/z)x, (d/z)y, d]

Page 19: View Transformation

Projection Matrix Projection using homogeneous coordinates:

– transform [x, y, z] to [(d/z)x, (d/z)y, d]

•2-D image point:•discard third coordinate•apply viewport transformation to

obtain physical pixel coordinates

d 0 0 00 d 0 00 0 d 00 0 1 0

xyz1

dx dy dz z dzx dzy d

Divide by 4th coordinate(the “w” coordinate)

Page 20: View Transformation

Perspective Projection

Page 21: View Transformation

Perspective Projection

z = 0 not allowed (what happens to points on plane z = 0?)Operation well-defined for all other points

Page 22: View Transformation

Perspective Projection

Matrix formulation using “homogeneous 4-vectors”:

Finally, recover projected point using homogenous convention:Divide by 4th element to convert 4-vector to 3-vector:

Page 23: View Transformation

Are we ready to rasterize? Not yet.

• It is difficult to do clipping directly in the viewing frustum

Page 24: View Transformation

The View Frustum

Page 25: View Transformation

Canonical View Volume

Page 26: View Transformation

Matrix Formulation

Page 27: View Transformation

Perspective ProjectionSuppose we have transformed from World to Eye to Canonical coordinatesHow do we project onto “image plane”?

Simply ignore z coordinate!

Page 28: View Transformation

Qualitative Features of Perspective Projection

Equal-sized objects at different depths project to different sizes!

Perspective projection does not preserve shape of planar figures!

Page 29: View Transformation

Families of parallel lines have “vanishing points”projection of point at infinity in direction of lines

Page 30: View Transformation

Continue withContinue withOpenGLOpenGL

Page 31: View Transformation

OpenGL OpenGL is a low-level graphics API Window system independent

No facility for window events/user input Can use additionally libraries (eg. GLUT)

Vertex driven Primitives assembled from vertices

***OpenGL is a state machine***

Page 32: View Transformation

OpenGL

per vertexoperations &primitiveassembly

Rasterization per pixeloperations

FrameBuffer

Commands or display list

Page 33: View Transformation

Clearing the BuffersClears the buffers using the specified valuesglClearColor(GLclampf red,

GLclampf green, GLclampf blue, GLclampf alpha)

glClear(GLbitfield mask)

Masks:GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT,GL_ACCUM_BUFFER_BIT, GL_STENCIL_BUFFER_BIT

Page 34: View Transformation

Drawing PrimitivesBegin/End drawing a primitive

glBegin(GLenum mode)

glEnd()

Modes:

GL_POINTS, GL_LINES, GL_TRIANGLES,GL_TRIANGLE_STRIP, GL_QUADS,GL_POLYGON

Page 35: View Transformation

Drawing PrimitivesDraw a vertex of a primitive

glVertex3f(GLfloat x, GLfloat y, GLfloat z)

The vertex is drawn according to the current state

Variants:glVertex3fv(const GLfloat *v)

2d,3d,4d, shorts, ints, floats, doubles

Page 36: View Transformation

Elements of Current State

Current: Color Normal Texture coordinates Drawing mode Matrix mode Matrix on:

Modelview stack Perspective stack Texture stack

Page 37: View Transformation

Drawing ExampleDraw a simple square:

glBegin(GL_POLYGON); glVertex3f(-0.5, 0.5,0.5); glVertex3f( 0.5, 0.5,0.5); glVertex3f( 0.5,-0.5,0.5); glVertex3f(-0.5,-0.5,0.5);glEnd();

Page 38: View Transformation

Changing the Current Color

Set the current color:

glColor3f(GLfloat red,GLfloat green,GLfloat blue)

Variants:glColor4f(red, green, blue, alpha)

bytes, unsigned bytes,shorts, ints, floats, doubles

Page 39: View Transformation

Drawing ExampleAdding color:

glBegin(GL_POLYGON); glColor3f(1,0,0); glVertex3f(-0.5, 0.5,0.5); glVertex3f( 0.5, 0.5,0.5); glVertex3f( 0.5,-0.5,0.5); glVertex3f(-0.5,-0.5,0.5);glEnd();

Page 40: View Transformation

Changing the Current Normal

Set the current normal:

glNormal3f(GLfloat nx, GLfloat ny, GLfloat nz)

Variants:glNormal3fv(const GLfloat *v)

bytes, shorts, ints, floats, doubles

Page 41: View Transformation

Drawing ExampleAdding normals:

glBegin(GL_POLYGON); glColor3f(1,0,0); glNormal3f(0,0,1); glVertex3f(-0.5, 0.5,0.5); glVertex3f( 0.5, 0.5,0.5); glVertex3f( 0.5,-0.5,0.5); glVertex3f(-0.5,-0.5,0.5);glEnd();

Page 42: View Transformation

Transformation PipelineStages of vertex transformations:

Objectcoords

ModelviewMatrix

ProjectionMatrix

ViewportMapping

Cameracoords

Normalizedcoords

Windowcoords

Page 43: View Transformation

Transformation PipelineMatrices are set up on stacks

Matrix commands are post-multiplied onto thecurrent matrix

the last command issued is the first transformation applied to the object

Can save/restore the current matrix

Page 44: View Transformation

Transformation PipelineSave/Restore the current matrix:

glPushMatrix()

glPopMatrix()

Change the current matrix stack:glMatrixMode(GLenum mode)

GL_MODELVIEW, GL_PROJECTION,GL_TEXTURE

Page 45: View Transformation

Transformation PipelineTranslate:

glTranslatef(GLfloat x, GLfloat y, GLfloat z)

Scale:glScalef(GLfloat x,

GLfloat y, GLfloat z)

Page 46: View Transformation

Transformation PipelineRotate:

glRotatef(GLfloat angle,GLfloat x,GLfloat y,GLfloat z)

is given in degreesangle

is the unit rotation axisX,y,z

Page 47: View Transformation

Transformation PipelineHierarchical Modelling:

glTranslatef(waistx,waisty,waistz);glPushMatrix(); glRotatef(ra,rx,ry,rz); // draw right armglPopMatrix();glPushMatrix(); glRotatef(la,lx,ly,lz); // draw left armglPopMatrix();

Page 48: View Transformation

Drawing ExampleAdding transformations:glMatrixMode(GL_MODELVIEW);glBegin(GL_POLYGON); glTranslatef(0,0,0); glRotatef(0,0,1,0); glScalef(1,1,1); glColor3f(1,0,0); glNormal3f(0,0,1); glVertex3f(-0.5, 0.5,0.5); glVertex3f( 0.5, 0.5,0.5); glVertex3f( 0.5,-0.5,0.5); glVertex3f(-0.5,-0.5,0.5);glEnd();

Page 49: View Transformation

CamerasPerspective Camera

glFrustum(GLdouble left, GLdouble right,GLdouble bottom, GLdouble top,GLdouble near, GLdouble far)

Orthographic Camera

glOrtho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far)

Page 50: View Transformation

ShadingSetting the Shading ModelglShadeModel(GLenum mode)

Modes:GL_FLAT - FLAT shadingGL_SMOOTH - GOURAUD shading

Page 51: View Transformation

LightsDefining LightsglLightfv(GLenum light, GLenum pname,

GLfloat *param)glEnable(GL_LIGHTING)glEnable(GL_LIGHT0)

Parameters:GL_AMBIENT - RGBA ambient intensityGL_DIFFUSE - RGBA diffuse intensityGL_SPECULAR - RGBA specular intensityGL_POSITION – light position

Page 52: View Transformation

LightsSetting the Ambient LightglLightModelfv(GLenum pname,

GLfloat *param)

Parameters:GL_LIGHT_MODEL_AMBIENT

- global RGBA ambient intensity

Page 53: View Transformation

MaterialsDefining Material Properties

glMaterialfv(GLenum face, GLenum pname, GLfloat *param)

Faces:GL_FRONT, GL_BACK, GL_FRONT_AND_BACK

Parameters:GL_AMBIENT, GL_DIFFUSE, GL_SPECULAR,GL_EMISSION, GL_SHININESS

Page 54: View Transformation

Drawing ExampleAdding Lighting:float lPos[] = {1.0,1.0,1.0,1.0};glLightfv(GL_LIGHT0,GL_POSITION,lPos);glEnable(GL_LIGHTING);glEnable(GL_LIGHT0);glShadeModel(GL_SMOOTH);

float diffuse[] = {1.0,0.0,0.0,1.0};glMaterialfv(GL_FRONT,GL_DIFFUSE,diffuse);

// Setup camera// Draw objects

Page 55: View Transformation

Event Driven Programming

Program responds to events Events are handled by user

defined callback functions Callbacks must know context and

event type (passed through variables)

Page 56: View Transformation

Event Driven Programming

MainEventLoop

DisplayHandler

KeyboardHandler

MouseHandler

Page 57: View Transformation

Simple GLUT ExampleDisplaying a squareint main (int argc, char *argv[]){ glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE); int windowHandle = glutCreateWindow("Simple GLUT App");

glutDisplayFunc(redraw);

glutMainLoop();

return 0;}

Page 58: View Transformation

Display CallbackCalled when window is redrawnvoid redraw(){ glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_QUADS); glColor3f(1, 0, 0); glVertex3f(-0.5, 0.5, 0.5); glVertex3f(0.5, 0.5, 0.5); glVertex3f(0.5, -0.5, 0.5); glVertex3f(-0.5, -0.5, 0.5); glEnd(); // GL_QUADS glutSwapBuffers();}

Page 59: View Transformation

More GLUTAdditional GLUT functions glutPositionWindow(int x,int y); glutReshapeWindow(int w, int h);

Additional callback functions glutReshapeFunction(reshape); glutMouseFunction(mousebutton); glutMotionFunction(motion); glutKeyboardFunction(keyboardCB); glutSpecialFunction(special); glutIdleFunction(animate);

Page 60: View Transformation

Reshape CallbackCalled when the window is resizedvoid reshape(int w, int h){ glViewport(0.0,0.0,w,h);

glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0,w,0.0,h, -1.0, 1.0);

glMatrixMode(GL_MODELVIEW); glLoadIdentity(); }

Page 61: View Transformation

Mouse CallbacksCalled when the mouse button is pressedvoid mousebutton(int button, int state, int x, int y){ if (button==GLUT_LEFT_BUTTON && state==GLUT_DOWN) { rx = x; ry = winHeight - y; }}

Called when the mouse is moved with button downvoid motion(int x, int y){ rx = x; ry = winHeight - y;}

Page 62: View Transformation

Keyboard CallbacksCalled when a button is pressedvoid keyboardCB(unsigned char key, int x, int y){ switch(key) { case 'a': cout<<"a Pressed"<<endl; break; }}

Called when a special button is pressedvoid special(int key, int x, int y){ switch(key) { case GLUT_F1_KEY:

cout<<“F1 Pressed"<<endl; break; }}

Page 63: View Transformation

Animation CallbacksCalled when the system is idlevoid animationCB(){ float time = glutGet(GLUT_ELAPSED_TIME); glRotated(time,0.0,1.0,0.0); glutPostRedisplay();}

Page 64: View Transformation

Menu CallbacksCreating a menu enum { M_NONE, M_OPEN, M_SAVE, M_EXIT }; glutCreateMenu(menuFunc); glutAddMenuEntry("Open", M_OPEN); glutAddMenuEntry("-----------------------", M_NONE); glutAddMenuEntry("Exit", M_EXIT); glutAttachMenu(GLUT_RIGHT_BUTTON);

Called when a menu button is pressedvoid menuFunc(int value){ switch(value) { case M_EXIT: exit(0); break; }}