OpenGL (II). How to Draw a 3-D object on Screen?

29
OpenGL (II)
  • date post

    22-Dec-2015
  • Category

    Documents

  • view

    221
  • download

    0

Transcript of OpenGL (II). How to Draw a 3-D object on Screen?

Page 1: OpenGL (II). How to Draw a 3-D object on Screen?

OpenGL (II) OpenGL (II)

Page 2: OpenGL (II). How to Draw a 3-D object on Screen?

How to Draw a 3-D object on Screen?

Page 3: OpenGL (II). How to Draw a 3-D object on Screen?

Drawing a Scene

Apply modeling and viewing transformations Apply projection transformation Clip if it lies outside the viewing volume Apply viewport transformation to display on

the screen

Page 4: OpenGL (II). How to Draw a 3-D object on Screen?

Transformations

Modeling Transformation Viewing Transformation Projection Transformation Viewport Transformation

Page 5: OpenGL (II). How to Draw a 3-D object on Screen?

Clip Coordinates

ModelView

Matrix

Projection

Matrix

Perspective

Division

Viewport

Transformation

Eye Coordinates

Normalized Device Coordinates

Windows Coordinates

Vertex

Object Local Coordinates

Stages of Vertex Transformations

Page 6: OpenGL (II). How to Draw a 3-D object on Screen?

Transformations in OGL

Recall that once you define the attributes (such as color), all the subsequent objects are drawn using those attributes.

Same rule applied for transformations Specify a transformation, and then this

transformation is automatically applied to every object that is drawn, until the transformation is set again.

Important: set transformations prior to drawing

Page 7: OpenGL (II). How to Draw a 3-D object on Screen?

Transformations in OGL

Transformations have different purposes We discuss only two of OGL’s matrix types

(the third is Texture matrix) Modelview matrix for moving objects or

change of coordinates Projection matrix for projection

Project objects from the world window to the viewport, and mapping the viewport to the graphics display window.

Page 8: OpenGL (II). How to Draw a 3-D object on Screen?

Transformations in OGL For each matrix type, OGL maintains a stack of

matrices Stack: last in, first out

To operate on a specific matrix type,call glMatrixMode(mode) Different modes: GL_MODELVIEW, GL_PROJECTION

Once the matrix mode is set, performvarious operations on the stack.

Page 9: OpenGL (II). How to Draw a 3-D object on Screen?

Matrix Stacks (Con’t)

There are several routines for manipulating matrix stacks glPushMatrix() glPopMatrix() glLoadMatrix() glMultMatrix() glLoadIdentity()

To avoid destroying the contents of the Modelview matrix, save the contents of the Modelview matrix and restore its contents after we are done.

Page 10: OpenGL (II). How to Draw a 3-D object on Screen?

Modeling Transformation

Positions and orients the objects Rotation: glRotate{fd}(angle, x, y, z) Translation: glTranslate{fd}(x, y, z) Scaling: glScale{fd}(x, y, z)

Page 11: OpenGL (II). How to Draw a 3-D object on Screen?

Modeling Transformation (Con’t)

World space

Model space

Model space

Page 12: OpenGL (II). How to Draw a 3-D object on Screen?

Transformation Example In OGL, whenever you draw, the points are automatically

transformed using the current Modelview matrix. Common way of object transformations in OGL

Push the matrix stack. Apply all desired transformations Draw your objects (the transformations will be applied

automatically) Pop the matrix stack.

Page 13: OpenGL (II). How to Draw a 3-D object on Screen?

Post-multiplication

M M

M

M

MT

M

MTR

glPushMatrix()

glTranslate()

glRotate()

Page 14: OpenGL (II). How to Draw a 3-D object on Screen?

Order of Transformations

Specify the transformations in the reverse order of the way you conceptualize them

Specify

Conceptualize

M1M2v

Page 15: OpenGL (II). How to Draw a 3-D object on Screen?

Order of Transformations (Con’t) Example 1: TRv

11000

0100

0045cos45sin

0045sin45cos

1000

0100

0010

0.2001

z

y

x

glTranslatef(2.0,0.0,0.0)glRotatef(45.0,0.0,0.0,1.0)

Page 16: OpenGL (II). How to Draw a 3-D object on Screen?

Order of Transformations (Con’t) Example 2: RTv

11000

0100

0010

0.2001

1000

0100

0045cos45sin

0045sin45cos

z

y

x

glRotatef(45.0,0.0,0.0,1.0)glTranslatef(2.0,0.0,0.0)

Page 17: OpenGL (II). How to Draw a 3-D object on Screen?

Transformation Example First attempt

Rotation command glRotatef(angle_in_degree, x, y, z); Rotation is performed about the origin of the coordinate system.

Translation command glTranslatef(x, y, z);

glPushMatrix(); // save the current matrix

glRotatef(20, 0, 0, 1); // rotate by 20 degrees CCW

glRectf(x-2, y-2, x+2, y+2); // draw the rectangle

glPopMatrix(); // restore the old matrix

glPushMatrix(); // save the current matrix

glRotatef(20, 0, 0, 1); // rotate by 20 degrees CCW

glTranslatef(x, y, 0); // apply translation

glRectf(-2, -2, 2, 2); // draw the rectangle

glPopMatrix(); // restore the old matrix

equivalent

Page 18: OpenGL (II). How to Draw a 3-D object on Screen?

Transformation Example Correct way

glPushMatrix(); // save the current matrix

glTranslatef(x, y, 0); // apply translation

glRotatef(20, 0, 0, 1); // rotate by 20 degrees CCW

glRectf(-2, -2, 2, 2); // draw the rectangle

glPopMatrix(); // restore the old matrix

Page 19: OpenGL (II). How to Draw a 3-D object on Screen?

OpenGL viewing

Modelview transformation Modeling transformation:

local coordinates world coordinates Viewing transformation:

world coordinates eye coordinates

Page 20: OpenGL (II). How to Draw a 3-D object on Screen?

Modelview Matrix

Pulling the camera back from the object (viewing transformation) moving the object away from the camera (modeling transformation)

Thus, both viewing and modeling transformations are stored in the modelview matrix stack

Page 21: OpenGL (II). How to Draw a 3-D object on Screen?

Viewing Transformation

Position and aim the camera gluLookAt(eyex, eyey, eyez, centerx, centery,

centerz, upx, upy, upz) Default location at origin looking down the

negative z-axis

x

y

z

Page 22: OpenGL (II). How to Draw a 3-D object on Screen?

Viewing Transformation (Con’t)

Center position

Eye position

Up vector

Page 23: OpenGL (II). How to Draw a 3-D object on Screen?

OpenGL viewing gluLookAt(eye.x, eye.y, eye.z, center.x, center.y,

center.z, up.x, up.y, up.z) Viewing direction: center – eye Up is the upward direction

Viewing direction and up vector eye coordinate system X axis points to the right of viewer Y axis points upward Z axis points to the back of viewer

Generate a matrix, which is postmultiplied to the top-of-the-stack matrix on the Modelview stack Thus, must be called before any modeling transformations

Page 24: OpenGL (II). How to Draw a 3-D object on Screen?

OpenGL viewing

Default OpenGL viewing (if no gluLookAt is specified) Eye is at origin of world space Looking down the negative z axis of world

space Up vector is positive y axis The viewing transformation matrix is identity

matrix (i.e. eye coordinate system = world coordinate system)

Page 25: OpenGL (II). How to Draw a 3-D object on Screen?

Viewing Transformation (Con’t)

Default location gluLookAt(4.0, 2.0, 1.0, 2.0, 4.0, -3.0, 2.0, 2.0, -1.0)

Page 26: OpenGL (II). How to Draw a 3-D object on Screen?

OpenGL projection

glOrtho(), gluPerspective() or glFrustum() Produce a matrix which is stored in the projection

matrix stack All geometry objects are already transformed to the eye

coordinate system before projection transformation is applied

The parameters of these functions are with respect to the eye coordinate system

The parameters define 6 clipping planes To simplify clipping, the viewing space is transformed

into a canonical view volume (all coordinates in [-1, +1])

Page 27: OpenGL (II). How to Draw a 3-D object on Screen?

OpenGL orthographic projection

glOrtho(left, right, bottom, top, near, far) left, right, bottom, top are coordinates in eye space

left, right are the x-coordinate limits bottom, top are the y-coordinate limits

near, far are signed distances from the eye to the near and far clipping planes (e.g., near = 2, far = 15 mean the clipping planes are at z=-2 and z= -15)

Page 28: OpenGL (II). How to Draw a 3-D object on Screen?

OpenGL perspective projection The center of projection and the portion of the

projection plane that map to the final image form an infinite pyramid. The sides of the pyramid are clipping planes.

All of the clipping planes bound the viewing frustum. In OpenGL, PP = near plane

Page 29: OpenGL (II). How to Draw a 3-D object on Screen?

OpenGL perspective projectionglFrustum(left, right, bottom, top, near, far) View frustum may not be centered along view vector Less often used than gluPerspective()

gluPerspective(fov_y, aspect_ratio, near, far) fov_y is vertical field-of-view in degrees aspect ratio = width / height near, far are distances from eye to two clipping planes

must be positive Keep them close so as to maximize depth precision