Title: CS380 LAB III OpenGL
1CS380 LAB III OpenGL
- Jonghyeob Lee
- Reference1. OpenGL course slides by Rasmus
Stenholt - Reference2. http//nehe.gamedev.net/
2Goal
- Introduce OpenGL programming
- Help you do CS380 homework by yourself
3Notice
- Use Noah board for your questions
(http//noah.kaist.ac.kr/course/CS380)
4Outline
- Viewing Transformation
- Projection and Orthographic Projection
5Transformation pipeline
- Stages of vertex transformation
Modelview Matrix
Projection Matrix
Viewport Mapping
Object coords
Camera coords
Normalized coords
Window coords
6Transformation 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
7Transformation pipeline
- Save / Restore the current matirx
- glPushMatrix()
- glPopMatrix()
- Change the current matrix stack
- glMatrixMode(Glenum mode)
- GL_MODELVIEW, GL_PROJECTION, GL_TEXTURE
8Modelview transformation
- Modeling transformation
- Model local coordinates ? world coordinates
- Viewing transformation
- world coordinates ? eye coordinates
9Viewing 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
10Camera View
- Camera coordinate system
- The camera is located at the origin
- The cameras optical axis is along one of the
coordinate axes (-z in OpenGL convention) - The up axis (y axis) is aligned with the
cameras 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
11Viewing 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
12Intuitive 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
13Example
- 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()
14Tutorials
- 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
15Tutorials
- Set camera view to following figures
16The 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
17Setup 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.
18Set 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)
-
-
19Projection 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
20Orthographic Projection
- The simplest form of projection
- simply project all points along lines parallel
to the z-axis (x, y, z)-gt(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
21Orthographic 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)
22Perspective 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.
23Viewing 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 doesnt need to be
rendered - The process of remove invisible objects from
rendering is called clipping
24OpenGL 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.
25The OpenGL Perspective Matrix
- Matrix M maps the viewing frustum to a NDC
(canonical cube) - We are looking down the -z direction
26Near/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
27Perspective 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)
28Set 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)
29Next time
- Lighting and Texture mapping