Lecture Notes Viewing

download Lecture Notes Viewing

of 17

Transcript of Lecture Notes Viewing

  • 8/2/2019 Lecture Notes Viewing

    1/17

    Viewing

    This chapter aims at covering fundamental concepts Perspective projection

    Parallel projection

    view reference coordinate system view volumes

    Normalization

    Transformations in OpenGL

    Viewing determines how a 3D model is mapped to a 2D image, and requires

    Synthetic camera model

    viewing parameters In classical viewing - we pick up the object, position it and view it

    In computer graphics objects are positioned in a fixed frame. The viewer moves to

    the appropriate position in order to achieve the desired view

    Important Concepts required understanding viewing:

    Projection plane (viewplane)Projectors - straight projection rays

    Center of projection

    Types of Projections:

    Perspective Projection: Similar tocamera and human vision. Projectors are notparallel to the projection plane. They appear to converge at a point called Center of

    Projection (COP). Distance from COP (Center of Projection) to VP (view-plane) is finite

    Parallel Projection: Projectors are parallel to the plane. Distance from COP to ViewPlane is infinite. Projectors are assumed to meet at infinity. Result is determined by

    direction of projection (DOP).

  • 8/2/2019 Lecture Notes Viewing

    2/17

    Perspective Projection Lines not parallel to viewplane converge to a vanishing point

    Principal vanishing point - for lines parallel to a principal axis Important characteristic: nr of principal vanishing points

    Reduction ofsize

    Used in animation Two-Point Vanishing

    Parallel Projection COP is at infinity

    Alignment of view plane with axes

    Alignment of view plane with DOP

    Clipping Planes rationale:

    viewplane window clipssides of images limit depth of view byfrontand back clipping planes

    applications:

    depth cueing

    focusing on objects of interest culling artifacts (near and far)

    parameters

    distances along n-axis

  • 8/2/2019 Lecture Notes Viewing

    3/17

    planes parallel to viewplane

    for perspective projection

    negative distances (behind COP) objects are seen upside down nothing in the plane of the COP can be seen

    View Volumes

    Perspective view volume is a truncated pyramid - frustum Parallel view volume rectangular solid

  • 8/2/2019 Lecture Notes Viewing

    4/17

    Transformation and Projection in OpenGL

    CTM (current transformation matrix) operates on all geometry. There are two

    components: model-view matrix, that addresses modeling transformations and camera position

    projection matrix deals with 3D to 2D projection

    OpenGL transformations can be applied to either of the matrices. Matrix must be selected

    explicitly.

    void glMatrixMode( mode ); /* mode is GL_MODELVIEW or

    GL_PROJECTION */

    It is normal to leave in model-view mode by default

    Transformation and Viewing Commands

    We can manipulate the matrices explicitly as:

    void glLoadIdentity();

    void glLoadMatrix{fd}( const TYPE *mat );void glMultMatrix{fd}( const TYPE *mat );

    Note that matrix is in column-major order, i.e. m11, m21, m31, m41, m12, m22, ... m44.

    Implicit construction: orienting scene relative to viewer:

    void gluLookAt( GLdouble ex, GLdouble ey, GLdouble ez,

    GLdouble cx, GLdouble cy, GLdouble cz,

  • 8/2/2019 Lecture Notes Viewing

    5/17

    GLdouble ux, GLdouble uy, GLdouble uz );

    Where (ex, ey, ez) is position of eye within the World Coordinate System (cx, cy, cz) is point being looked at

    (ux, uy, uz) which way is up

    The relative transformations are:

    void glTranslate{fd}( TYPE x, TYPE y, TYPE z );void glRotate{fd}( TYPE ang, TYPE x, TYPE y, TYPE z );

    void glScale{fd}( TYPE x, TYPE y, TYPE z );

    Perspective Projection

    Projection matrix can be defined using transformations. It is much simpler to use pre-

    packaged operators

    void glFrustum( GLdouble left, GLdouble right, GLdouble bottom, GLdoubletop, GLdouble near, GLdouble far );

    void gluPerspective( GLdouble fovy, GLdouble aspect, GLdouble near,GLdouble far );

    Orthographic Projection

    Orthographic parameters define rectangular view volume. By default OpenGL works

    with Orthographic projection mode. If we are working with 2D operation the function

    used is

    void gluOrtho2D( GLdouble left, GLdouble right, GLdouble bottom, GLdouble top);

    For 3D object operations the function used is:

    void glOrtho( GLdouble left, GLdouble right ,GLdouble bottom, GLdouble top,

    GLdouble near, GLdouble far );

  • 8/2/2019 Lecture Notes Viewing

    6/17

    Window to Viewport Transformation

    This is a 2D transformation required to perform a transformation from a window to a

    viewport of different size.

    Normalized Projection Coordinates

  • 8/2/2019 Lecture Notes Viewing

    7/17

    Viewport Transformation

    Projection transformations map scene into canonical view volume (CVV)

    OpenGL maps into full CVV given by functionglOrtho ( -1.0 , 1.0 , -1.0 , 1.0 , -1.0 , 1.0 ) ;

    Viewport transformation locates viewport within output window. Distortion

    may occur if aspect ratios of view volume & viewport differ. Viewport and projectionmatrices may need to be reset after window is resized. The API used is:

    void glViewport( GLint x, GLint y, GLsizei wd, GLsizei ht );

    Bringing all the operations together

    Set up the projection parameters and viewport in the initialisation routine

    glViewport( x, y, wd, ht );

    glMatrixMode( GL_PROJECTION );

    glLoadIdentity(); /* assuming a simple projection */

  • 8/2/2019 Lecture Notes Viewing

    8/17

    glFrustum( ... );

    glMatrixMode(GL_MODELVIEW );

    Set camera position at start of display

    glLoadIdentity(); /* assuming no special requirements */gluLookAt( vx, vy, vz, ox, oy, oz, 0.0, 1.0, 0.0 );

    Thereafter apply modeling transformations

    glTranslatef( ... );

    glRotatef( ... );

    etc;

    We should to change viewport and/or projection matrix in reshape callback

    Viewing Transformations

    Viewing pipeline: mapping view volume to device (screen) coordinates. We need toderive the transformation matrix. There are two cases: parallel and perspective

    WC: world coordinates

    VRC: view reference coordinates NPC: normalized projection coordinates used in the canonical view volume (CVV)

    Each mapping corresponds to matrix

    Matrices may be combined to accelerate process

    Overview of the Problem

  • 8/2/2019 Lecture Notes Viewing

    9/17

    Initially world coordinate system is given in which the view reference point, the viewnormal and the view up vector are defined. In the view reference coordinates the

    projection reference point; the location of the window in the view plane; and the distance

    of the front and back clipping planes from the projection reference point are mentioned.It is required to find transformations that yield a coordinate system such that the front

    bottom left is at (-1, -1, -1) and the back top right is at (1, 1, 1), called a canonical view

    volume (CVV) as shown in the figure below.

    Mathematical calculations:

    Orthographic and Oblique Projection

    Orthographic

    View plane normal parallel to the direction of projection

    Oblique

  • 8/2/2019 Lecture Notes Viewing

    10/17

    View plane normal not parallel to the direction of projection. It is not

    implemented by OpenGL; the required operation can be done using glMulMatrix

    function. The steps are as shown in the figure. The steps are

    Shear followed by orthogonal projection.

    View Reference Coordinate System

    The first stage of transforming the world coordinate system from world coordinates toview reference coordinates is a translation of the view reference point to the origin of the

    world system. The translation can then be applied to each model.

    The second stage of the transformation is to rotate the axes of the view reference

    coordinate system into the axes of the world coordinate system. Applying this

    transformation after the translation in the previous slide allows the models to betransformed into view reference coordinates.

    First determine the u, v and n in world coordinates.

  • 8/2/2019 Lecture Notes Viewing

    11/17

    Form the rows of the rotation matrix out of the components of the 3 vectors.

    View Reference Coordinates: OpenGL Commands

    In OpenGL the view reference coordinates are part of the model view matrix. There are

    2 ways of setting up the coordinates. In GL the commands for translation and rotation can

    be used:void glTranslatef(GLfloat x, GLfloat y, GLfloat z)

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

    Alternatively in the utility toolkit GLU there is:

    void gluLookAt(GLdouble eyex, GLdouble eyey, GLdouble eyez,GLdouble centerx, GLdouble centery, GLdouble centerz,

    GLdouble upx, GLdouble upy, GLdouble upz)

    This eye coordinates specify a look-from point, the centre coordinates a look-at point and

    the up coordinates the head-up vector. The Matrix stack currently used is chosen with:void glMatrixMode()

    Parallel Projection: Bringing DOP parallel to Z (if oblique projection)

    First the direction of projection (DOP)must be made to lie parallel with the n axis. This is

    achieved by a shear.

    CW = Center of Window

    If the projection is orthogonal, then the PRP is on the n axis and the shear is zero.

  • 8/2/2019 Lecture Notes Viewing

    12/17

    Translate Centre of Front Clipping Plane to Origin

    This is a simple translation of the window centre to the z axis.

    Scale into Canonical View Volume

    The final stage is to scale the coordinates so that the volume extends form (-1, -1, 1) to

    (1, 1, -1).

    The x, y and z axis are scaled by half the width and height and depth respectively.

  • 8/2/2019 Lecture Notes Viewing

    13/17

    The OpenGL Commands

    OpenGL only implements orthographic projections, if you need other oblique projectionsyou must form the M0 matrix yourself and use glMultMatrix() to modify the projection

    matrix.

    GL provides the function:

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

    The matrix it produces is:

    Parallel Projection: OpenGL Example

    The following code sets up an orthogonal projection and then returns to model viewmode. The glLoadIdentity() function is needed to replace the existing matrix with an

    identity matrix as the glOrtho() function premultiplies what ever is at the top of the

    matrix stack.

    glMatrixMode (GL_PROJECTION);glPushMatrix(); /* optional, preserves a previous matrix */

    glLoadIdentity (); /* sets top of stack to id matrix */

    glOrtho(Xmin, Xmax, Ymin, Ymax, nearClip, farClip);

    glMatrixMode(GL_MODELVIEW);

    Perspective Projection 1: Scale and Shear View volume

    A shear must be applied to bring the centre line of the view volume parallel to the z axis,

    the x and y coordinates scaled so that the sides of the frustrum lie on the lines x = z

    and y = z.

  • 8/2/2019 Lecture Notes Viewing

    14/17

    Perspective Projection 1: Scale and Shear View volume (cont)

    Asymmetry is corrected by a shear and the sides are fixed by scaling. For the x axis thecombined operation is:

    Width = Xmax Xmin and asymmetry = Xmax + XminIf the frustrum is symmetric Xmax = Xminso the asymmetry is zero and width is 2Xma

    Perspective Projection 2: Simple Perspective

    This is carried out in 2 stages: first scale the x and y coordinates, since the scale must beinversely proportional to the z coordinate this is a shear along the z axis.

  • 8/2/2019 Lecture Notes Viewing

    15/17

    Using Similar Triangles concepts

    So the transformation is:

    When vector is normalized

    Projection 2: Simple Perspective (cont)

    The vector is renormalised after the transformation is applied. So

    Note that depth data is lost as z coordinates are compressed onto plane z = d.

  • 8/2/2019 Lecture Notes Viewing

    16/17

    Perspective Projection: OpenGL Perspective

    Easy part put projection plane at -1 so transformation becomes

    We need the to preserve depth information so that we can carry out hidden surface

    removal also we want the sides of the frustrum to be at . Given the sides are already at

    Z we need a transformation such that: X= Z, Y= ZX= 1 Y= 1

    N = Distance to near clipping Plane, N>0F = Distance to far clipping plane, F>0

    The full OpenGL perspective matrix is given by the composition of P1 and P2.

    Perspective Projection: OpenGL Commands

    The OpenGL perspective matrix can be generated by using the GL command:

    void glFrustum(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top,

    GLdouble near, GLdouble far);left, right = max and min of x

    top, bottom = max and min of Y

  • 8/2/2019 Lecture Notes Viewing

    17/17

    There is a simplified version, for symmetric viewports, in the utility library GLU:

    Map into Viewport

    The final stage is to map the window on the view plane into the view port on the screen.To do this a transformation is applied that maps the bottom left corner of the window into

    the pixel location of the bottom left of the viewport. The height and width of the window

    are also transformed to mach the viewports pixel height and width.

    GL provides the command:

    void glViewport(GLint x, GLint y, GLsizei width, GLsizei height)

    If the aspect ratio of the viewport is different from the window on the view plane then

    distortions will occur.