DOC PREVIEW
Berkeley COMPSCI 184 - Viewing and Camera Control in OpenGL

This preview shows page 1-2 out of 6 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Viewing and Camera Control in OpenGLNiels Joubert & James Andrews∗2008-10-281 Basic OpenGL TransformationsWhenever we work in OpenGL, we have access to its API that defines functions for basic transformations.We use the following functions to create a transform matrix and push it onto the current transform stack1:glScalef(x, y, z) - scale object by x,y,z scalars along the current axesglTranslatef(x, y, z) - move an object by x,y,z along the current axes.glRotatef(x, y, z, angle); - rotate an object by angle around vector [x,y,z]Using techniques from linear algebra, any transform can be dec omposed into a combination of thesesimple transforms. Luckily, we do not have to resort to always using only these transforms - OpenGL definesan interface to intuitively work with the camera’s orientation and perspective, or we can define arbitrarytransforms and load this into OpenGL.2 OpenGL Transformation StacksWe think of viewing in OpenGL as sp ecifying properties about a hypothetical camera inside our scene. To dothis, we will be specifying transformations that must be applied to our world during the rendering process.OpenGL supports this by storing transformations in a use r-controllable stack. OpenGL has 3 differenttransformation stacks which are applied at different times of the rendering pipeline. We are concerned withtwo of these stacks: the Modelview stack and the Projection stack.2We will explore the relation between ourhypothetical camera and these transformations stacks. Each of these OpenGL transformation stacks specifythe following information about the camera:• GL MODELVIEW - The position and orientation of the camera and the world.• GL PROJECTION - How the camera sees the world3 Projection transformati onThe projection transform defines how a point (or vertex) is transformed from world space to the 2D planeof the screen (screen space). This is part of what we studied when we discussed perspective transforms.OpenGL gives you functions to define an orthographic or a perspective projection relative to the camera:glFustrum (left, right, bottom, top, near, far);gluPerspective(fovy, aspect, near, far);glOrtho(left, right, bottom, top, near, far);The following sketches should explain each of these functions:∗Much of this handout was adapted from Stu Pomerantz at the Pittsburgh Supercomputi ng Center. http://www.psc.edu/1See section 22There is also a Texture transform stack.1(a) glOrtho given left, right, bottom, top, near and far(b) glFustrum give left, right, bottom, top, near and far(c) glPerspective given field-of-view, aspect-ratio, near and farFigure 1: OpenGL perspective functions24 Modelview transformati onThe Modelview transformation specifies both the position and orientation of the camera and the objects inthe world. Why don’t we have a model transform and a view transform? Because translating the worldand translating the camera has exactly the same effect - thus we combine it into one Modelview transform.We tend to specify a viewing transform to place the camera, followed by transformations on the objects.Note that placing the camera using the viewing transform is exactly the same as applying the rotations andtranslations to place your camera using OpenGL’s transformation functions.Define the viewing transform:void gluLookAt(eyeX,eyeY,eyeZ, centerX,centerY,centerZ, upX,upY,upZ)PARAMETERSeyeX,eyeY, eyeZ - Specifies the position of the eye point.centerX, centerY, centerZ - Specifies the position of the reference point.upX, upY, upZ - Specifies the direction of the up vector.Since the default transformation on any stack is the identity, this translates into a default camera witheye at (0, 0, 1) and center at (0, 0, 0) with an up direction of (0, 1, 0) along the y axis. In other words,gluLookAt(0,0,1,0,0,0,0,1,0) is the default view.5 Managing the Transformation StacksToday’s graphics programs dem and complex scenes with many objects, each rendered under its own trans-form. To facilitate this, as mentioned, OpenGL stores transformations in a stack. We control this stackthrough the following four methods:• glMatrixMode(STACK) - Selects the stack to affect.• glLoadIdentity() - Resets the selected stack to the Identity transform.• glPushMatrix() - Duplicates the top transform of the current stack.• glPopMatrix() - Deletes the top transform of the current stack.We tend to initialize the ModelView stack to the identity, then apply our viewing transform (positioningthe camera). Once this has o cc urred, for each object we want to render, we push a duplicate matrix onto themodelview stack, apply the transformations for the given object, draw the object, and pop the top matrixoff the stack, thereby returning to the original view transform, ready to draw the next object.6 Putting it all together - Specify the CameraIn order to specify the view in OpenGL:• Set the viewport• Set the projection transformation• Set the modelview transformationIf you specify your viewport and projection on initialization, you need to specify only your modelviewtransformation every time you render. OpenGL’s stack-based approach then allows you to apply differenttransformations to each object. Thus, a p os sible Reshape callback function (also called on initialization)would look like the following:3Listing 1: Specifying the viewvoid re s ha p e ( int w, int h ) {// S et t h e v i e w po r tglViewpo rt ( 0 ,0 ,w, h ) ;// S et t h e p r o j e c t i o n tra ns fo rmglMatrixMode (GL PROJECTION ) ;g l L o a d Ide n t i t y ( ) ;g l uP e rs p e c t iv e ( 4 5 , 1 , 5 , 1 0 0 ) ;}And a possible display function would look like the following:Listing 2: Rendering the scenef l o a t zoom , rotx , roty , tx , ty ;void d i s pl a y ( ) {g l C l e a r (GL COLOR BUFFER BIT | GL DEPTH BUFFER BIT ) ; // Clea r Z−B u f f e r// S et t h e camera o r i e n t a t i o n :glMatrixMode (GL MODELVIEW) ;g l L o a d Ide n t i t y ( ) ;gluLookAt (0 ,0 , −1 , 0 , 0 , 0 , 0 , 1 , 0 ) ;// Ro tat e and zoom t h e camera . This o rde r g i v e s you maya−l i k e c o n t r o l .g l T r a n s l a t e f (0 ,0 , − zoom ) ;g l T r a n s l a t e f ( tx , ty , 0 ) ;g l R o t a te f ( rotx , 1 , 0 , 0 ) ;g l R o t a te f ( roty , 0 , 1 , 0 ) ;//FOR EACH OBJECT:glPushMatrix ( ) ; //Save the c u r r e n t view ma tr ix .//HERE YOU CAN APPLY TRANSFORMATIONS TO EACH OBJECT BEFORE DRAWING IT//AND HERE YOU CAN DRAW IT !glPopMatrix ( ) ; // R es tore t o th e view matri xgl u tSw apB u ff e rs ( ) ;}For more


View Full Document

Berkeley COMPSCI 184 - Viewing and Camera Control in OpenGL

Documents in this Course
Load more
Download Viewing and Camera Control in OpenGL
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Viewing and Camera Control in OpenGL and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Viewing and Camera Control in OpenGL 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?