OpenGL I - PowerPoint

Document Sample
OpenGL I - PowerPoint Powered By Docstoc
					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




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




                                             3
Outline
   Viewing Transformation
   Projection and Orthographic Projection




                                             4
Transformation pipeline
   Stages of vertex transformation
         Modelview   Projection   Viewport
          Matrix       Matrix     Mapping


Object          Camera     Normalized        Window
coords          coords     coords            coords




                                                      5
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


                                                       6
Transformation pipeline
   Save / Restore the current matirx:
     glPushMatrix()
     glPopMatrix()


   Change the current matrix stack:
     glMatrixMode(Glenum
                      mode)
     GL_MODELVIEW, GL_PROJECTION,
      GL_TEXTURE
                                         7
Modelview transformation
   Modeling transformation
       Model local coordinates → world coordinates


   Viewing transformation
       world coordinates → eye coordinates




                                                      8
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

                                  9
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


                                                            10
 Viewing Transformation Steps
    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
Rotate                               Translate




                                                                   11
Intuitive Camera Specification
   How to specify a camera
    gluLookAt (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         12
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();
}




                                                                   13
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


                                                  14
Tutorials
   Set camera view to following figures




                                           15
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        ux   uy   uz   dx 
    frame to viewing frame.      v     vy   vz   dy 
       dx = - eye · u            x                 
       dy = - eye · v
                                  wx   wy   wz   dz 
                                                    
       dz = - eye · w           0     0    0    1

                                                         16
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.


                                                                         17
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);
         …
    }




                                                                        18
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

                                         19
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

                                      20
Orthographic Projection
   The projection matrix for orthographic projection is simple:
                      x  1 0 0 0  x 
                      y   0 1 0 0   y 
                                    
                      z   0 0 0 0   z 
                                     
                      1  0 0 0 1   1 
   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)



                                                                      21
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.




                                                                           22
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


                                   23
OpenGL Perspective Projection
   Set viewing frustum and perspective projection matrix
   glFrustum(left,right,bottom,top,near,far)
   left and right are 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 far are 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.




                                                                      24
The OpenGL Perspective Matrix
   Matrix M maps the viewing frustum to a NDC (canonical cube)
                     2n             r l          
                    r  l   0                 0 
                                     r l
                           2n       t b          
                     0                        0 
                M        t b      t b          
                     0            ( f  n)  2 fn
                            0
                                     f n    f n
                     0               1       0 
                            0                     
   We are looking down the -z direction




                                                                  25
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




                                                                            26
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)




                                      27
Set a view


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




                                                                               28
Next time
   Lighting and Texture mapping




                                   29

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:8
posted:6/8/2012
language:
pages:29