1 / 29

CS380 LAB III OpenGL

CS380 LAB III OpenGL. Jonghyeob Lee Reference1. [OpenGL course slides by Rasmus Stenholt] Reference2. [ http://nehe.gamedev.net/ ]. Goal. Introduce OpenGL programming Help you do CS380 homework by yourself. Notice. Use Noah board for your questions ( http://noah.kaist.ac.kr/course/CS380 ).

yves
Télécharger la présentation

CS380 LAB III OpenGL

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS380 LAB III OpenGL Jonghyeob Lee Reference1. [OpenGL course slides by Rasmus Stenholt] Reference2. [http://nehe.gamedev.net/]

  2. Goal Introduce OpenGL programming Help you do CS380 homework by yourself

  3. Notice Use Noah board for your questions (http://noah.kaist.ac.kr/course/CS380)

  4. Outline • Viewing Transformation • Projection and Orthographic Projection

  5. Transformation pipeline Modelview Matrix Projection Matrix Viewport Mapping Object coords Camera coords Normalized coords Window coords Stages of vertex transformation

  6. Transformation pipeline • Matrices are set up on stacks • Matrix commands are post-multiplied onto the current matrix • The last command issued is the first transformation applied to the object • Can save/restore the current matrix

  7. Transformation pipeline • Save / Restore the current matirx: • glPushMatrix() • glPopMatrix() • Change the current matrix stack: • glMatrixMode(Glenum mode) • GL_MODELVIEW, GL_PROJECTION, GL_TEXTURE

  8. Modelview transformation • Modeling transformation • Model local coordinates → world coordinates • Viewing transformation • world coordinates → eye coordinates

  9. Viewing Transformation Another change of coordinate systems Maps points from world space into eye space Viewing position is transformed to the origin Viewing direction is oriented along some axis A viewing volume is defined Combined with modeling transformation to form the modelview matrix in OpenGL

  10. Camera View • Camera coordinate system • The camera is located at the origin • The camera’s optical axis is along one of the coordinate axes (-z in OpenGL convention) • The up axis (y axis) is aligned with the camera’s up direction • We can greatly simplify the clipping and projection steps in this frame • The viewing transformation can be expressed using the rigid body transformations discussed before

  11. Viewing Transformation Steps Rotate Translate Viewing transformation should align the world and camera coordinate frames We can transform the world frame to the camera frame with a rotation followed a translation

  12. Intuitive Camera Specification • How to specify a cameragluLookAt (eyex, eyey, eyez, centerx, centery, centerz, upx, upy, upz) • (eyex, eyey, eyez): Coordinates of the camera (eye) location in the world coordinate system • (centerx, centery, centerz): the look-at point, which should appear in the center of the camera image, specifies the viewing direction • (upx, upy, upz): an up-vector specifies the camera orientation by defining a world space vector that should be oriented upwards in the final image • This intuitive specification allows us to specify an arbitrary camera path by changing only the eye point and leaving the look-at and up vectors untouched • Or we could pan the camera from object to object by leaving the eye-point and up-vector fixed and changing only look-at point

  13. Example void display() { glClear(GL_COLOR_BUFFER | GL_DEPTH_BUFFER_BIT); glColor3f(0.0, 1.0, 0.0); glLoadIdentity(); gluLookAt(0.0, 0.0, 0.0, 0.0, 0.0, -1.0, 1.0, 1.0, 0.0); glutWireTeapot(0.5); glFlush(); }

  14. Tutorials • Change your camera view based on key inputs • ‘q’ : eyex +0.1, ‘i’ : eyex -0.1 • ‘w’ : eyey + 0.1, ‘o’ : eyey -0.1 • ‘e’ : eyez +0.1, ‘p’ : eyez -0.1 • ‘a’ : centerx +0.1, ‘j’ : centerx -0.1 • ‘s’ : centery +0.1, ‘k’ : centery -0.1 • ‘d’ : centerz +0.1, ‘l’ : centerz -0.1 • ‘z’ : upx +0.1, ‘b’ : upx -0.1 • ‘x’ : upy +0.1, ‘n’ : upy -0.1 • ‘c’ : upz +0.1, ‘m’ : upz -0.1

  15. Tutorials Set camera view to following figures

  16. The Matrix for glLookAt • (u, v, w, eye) forms the viewing coordinate system • w = eye – look • u = up × w • v = w × u • The matrix that transforms coordinates from world frame to viewing frame. • dx = - eye · u • dy = - eye · v • dz = - eye · w

  17. Setup Camera Since viewing transformation is a rotation and translation transformation. We can use glRotatef() and glTranslatef() instead of gluLookAt() In the previous example (view a scene at origin from (10, 0, 0) ), we can equivalently use glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glRotatef(45, 0, 0, 1); Since the viewing transformation is applied after modeling transformations, it should be set before modeling transformations.

  18. Set Viewing Transformation Furthermore, glTranslatef() and glRotatef() can be used to define custom viewing control. Example void display() { … glRotatef(angle, axisx, axisy, axisz); glTranslatef(xpos, ypos, zpos); … }

  19. Projection Transformations • The projection transformation maps all of our 3-D coordinates onto our desired viewing plane. • Greatly simplified by using the camera (viewing) frame. • projection matrices do not transform points from our affine space back into the same space. • Projection transformations are not affine and we should expect projection matrices to be less than full rank

  20. Orthographic Projection • The simplest form of projection • simply project all points along lines parallel to the z-axis (x, y, z)->(x, y, 0) • Here is an example of an parallel projection of our scene. Notice that the parallel lines of the tiled floor remain parallel after orthographic projection

  21. Orthographic Projection The projection matrix for orthographic projection is simple: Notice the units of the transformed points are still the same as the model units. We need to further transform them to the screen space. OpenGL functions for orthographic projection gluOrtho2D(left, right, bottom, top), glOrtho(left, right, bottom, top, near, far)

  22. Perspective Projection • Perspective projection is important for making images appear realistic. • causes objects nearer to the viewer to appear larger than the same object would appear farther away • Note how parallel lines in 3D space may appear to converge to a single point when viewed in perspective.

  23. Viewing Frustum and Clipping The right picture shows the view volume that is visible for a perspective projection window, called viewing frustum It is determined by a near and far cutting planes and four other planes Anything outside of the frustum is not shown on the projected image, and doesn’t need to be rendered The process of remove invisible objects from rendering is called clipping

  24. OpenGL Perspective Projection Set viewing frustum and perspective projection matrix glFrustum(left,right,bottom,top,near,far) left and rightare coordinates of left and right window boundaries in the near plane bottom and top are coordinates of bottom and top window boundaries in the near plane near and farare positive distances from the eye along the viewing ray to the near and far planes Projection actually maps the viewing frustum to a canonical cube the preserves depth information for visibility purpose.

  25. The OpenGL Perspective Matrix Matrix M maps the viewing frustum to a NDC (canonical cube) We are looking down the -z direction

  26. Near/Far and Depth Resolution • It may seem sensible to specify a very near clipping plane and a very far clipping plane • Sure to contain entire scene • But, a bad idea: • OpenGL only has a finite number of bits to store screen depth • Too large a range reduces resolution in depth - wrong thing may be considered “in front” • Always place the near plane as far from the viewer as possible, and the far plane as close as possible

  27. Perspective Projection If the viewing frustum is symmetrical along the x and y axes. It can be set using gluPerspective() gluPerspective(θ,aspect,n,f) θ: the field of view angle aspect: the aspect ratio of the display window (width/height)

  28. Set a view int main( int argc, char* argv[] ) { … glutReshapeFunc( reshape ); … } void reshape(int width, int height) { glViewport(0, 0, width, height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); double aspect = width/double(height); gluPerspective(45, aspect, 1, 1024); }

  29. Next time • Lighting and Texture mapping

More Related