Viewing in OpenGL
Transcript of Viewing in OpenGL
![Page 1: Viewing in OpenGL](https://reader030.fdocuments.net/reader030/viewer/2022013000/61c9da0c54122247f55d5bd7/html5/thumbnails/1.jpg)
Viewing in OpenGL
Credits: Vicki Shreiner, SIGGRAPH tutorial
![Page 2: Viewing in OpenGL](https://reader030.fdocuments.net/reader030/viewer/2022013000/61c9da0c54122247f55d5bd7/html5/thumbnails/2.jpg)
Camera Analogy and Transformations
Projection transformations adjust the lens of the camera
Viewing transformations tripod–define position and orientation of the viewing volume in
the world
Modeling transformations moving the model
Viewport transformations enlarge or reduce the physical photograph
![Page 3: Viewing in OpenGL](https://reader030.fdocuments.net/reader030/viewer/2022013000/61c9da0c54122247f55d5bd7/html5/thumbnails/3.jpg)
Coordinate Systems and Transformations
Steps in Forming an Image
specify geometry (world coordinates)
specify camera (camera coordinates)
project (window coordinates)
map to viewport (screen coordinates)
Each step uses transformations
Every transformation is equivalent to a change in
coordinate systems (frames)
![Page 4: Viewing in OpenGL](https://reader030.fdocuments.net/reader030/viewer/2022013000/61c9da0c54122247f55d5bd7/html5/thumbnails/4.jpg)
Affine Transformations
Want transformations which preserve geometry
lines, polygons, quadrics
Affine = line preserving
Rotation, translation, scaling
Projection
Concatenation (composition)
![Page 5: Viewing in OpenGL](https://reader030.fdocuments.net/reader030/viewer/2022013000/61c9da0c54122247f55d5bd7/html5/thumbnails/5.jpg)
151173
141062
13951
12840
mmmm
mmmm
mmmm
mmmm
M
3D Transformations
A vertex is transformed by 4 x 4 matrices all affine operations are matrix multiplications
all matrices are stored column-major in OpenGL
matrices are always post-multiplied
product of matrix and vector is v
M
= 1 4 2 5 3 6
![Page 6: Viewing in OpenGL](https://reader030.fdocuments.net/reader030/viewer/2022013000/61c9da0c54122247f55d5bd7/html5/thumbnails/6.jpg)
Specifying Transformations
Programmer has two styles of specifying transformations
specify matrices (glLoadMatrix, glMultMatrix)
specify operation (glRotate, glOrtho)
Programmer does not have to remember the exact matrices
check appendix of Red Book (Programming Guide)
![Page 7: Viewing in OpenGL](https://reader030.fdocuments.net/reader030/viewer/2022013000/61c9da0c54122247f55d5bd7/html5/thumbnails/7.jpg)
Programming Transformations
Prior to rendering, view, locate, and orient:
eye/camera position
3D geometry
Manage the matrices
including matrix stack
Combine (composite) transformations
![Page 8: Viewing in OpenGL](https://reader030.fdocuments.net/reader030/viewer/2022013000/61c9da0c54122247f55d5bd7/html5/thumbnails/8.jpg)
vertex
Modelview
Matrix
Projection
Matrix
Perspective
Division
Viewport
Transform
Modelview
Modelview
Projection
l
l
l
object eye clip normalized
devicewindow
other calculations here material color shade model (flat) polygon rendering mode polygon culling clipping
Transformation
PipelineCPU DL
Poly.Per
Vertex
Raster Frag FB
Pixel
Texture
![Page 9: Viewing in OpenGL](https://reader030.fdocuments.net/reader030/viewer/2022013000/61c9da0c54122247f55d5bd7/html5/thumbnails/9.jpg)
Matrix Operations
Specify Current Matrix StackglMatrixMode( GL_MODELVIEW or GL_PROJECTION )
Other Matrix or Stack OperationsglLoadIdentity(),
glPushMatrix(),
glPopMatrix()
Viewport
usually same as window size
viewport aspect ratio should be same as projection transformation or resulting image may be distorted
glViewport( x, y, width, height )
![Page 10: Viewing in OpenGL](https://reader030.fdocuments.net/reader030/viewer/2022013000/61c9da0c54122247f55d5bd7/html5/thumbnails/10.jpg)
Projection Transformation
Shape of viewing frustum
Perspective projection
gluPerspective( fovy, aspect,
zNear, zFar )
glFrustum( left, right, bottom,
top, zNear, zFar )
Orthographic parallel projection
glOrtho( left, right, bottom, top,
zNear, zFar )
gluOrtho2D( left, right, bottom,
top )
calls glOrtho with z values near zero
![Page 11: Viewing in OpenGL](https://reader030.fdocuments.net/reader030/viewer/2022013000/61c9da0c54122247f55d5bd7/html5/thumbnails/11.jpg)
Applying Projection Transformations
Typical use (orthographic projection)glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( left, right, bottom, top, zNear, zFar );
![Page 12: Viewing in OpenGL](https://reader030.fdocuments.net/reader030/viewer/2022013000/61c9da0c54122247f55d5bd7/html5/thumbnails/12.jpg)
Viewing Transformations
Position the camera/eye in the scene place the tripod down; aim camera
To “fly through” a scene change viewing transformation and
redraw scene
gluLookAt( eyex, eyey, eyez,
aimx, aimy, aimz,
upx, upy, upz )
up vector determines unique orientation
careful of degenerate positions
tripod
![Page 13: Viewing in OpenGL](https://reader030.fdocuments.net/reader030/viewer/2022013000/61c9da0c54122247f55d5bd7/html5/thumbnails/13.jpg)
Projection Tutorial
![Page 14: Viewing in OpenGL](https://reader030.fdocuments.net/reader030/viewer/2022013000/61c9da0c54122247f55d5bd7/html5/thumbnails/14.jpg)
Modeling Transformations
Move objectglTranslate{fd}( x, y, z )
Rotate object around arbitrary axis
glRotate{fd}( angle, x, y, z )
angle is in degrees
Dilate (stretch or shrink) or mirror objectglScale{fd}( x, y, z )
zyx
![Page 15: Viewing in OpenGL](https://reader030.fdocuments.net/reader030/viewer/2022013000/61c9da0c54122247f55d5bd7/html5/thumbnails/15.jpg)
Transformation Tutorial
![Page 16: Viewing in OpenGL](https://reader030.fdocuments.net/reader030/viewer/2022013000/61c9da0c54122247f55d5bd7/html5/thumbnails/16.jpg)
Connection: Viewing and Modeling
Moving camera is equivalent to moving every object in
the world towards a stationary camera
Viewing transformations are equivalent to several
modeling transformations
gluLookAt() has its own command
can make your own polar view or pilot view
![Page 17: Viewing in OpenGL](https://reader030.fdocuments.net/reader030/viewer/2022013000/61c9da0c54122247f55d5bd7/html5/thumbnails/17.jpg)
Polar view and Pilot view
To create a viewing transformation in which the viewer orbits an object, use this sequence (which is known as “polar view”): glTranslated(0, 0, -distance)
glRotated(-twist, 0, 0, 1)
glRotated(-incidence, 1, 0, 0)
glRotated(azimuth, 0, 0, 1)
To create a viewing transformation which orients the viewer (roll, pitch, and heading) at position (x, y, z), use this sequence (known as “pilot view”): glRotated(roll, 0, 0, 1)
glRotated(pitch, 0, 1, 0)
glRotated(heading, 1, 0, 0)
glTranslated(-x, -y, -z)
Image credits: quest.arc.nasa.gov
![Page 18: Viewing in OpenGL](https://reader030.fdocuments.net/reader030/viewer/2022013000/61c9da0c54122247f55d5bd7/html5/thumbnails/18.jpg)
Projection is left handed
Projection transformations (gluPerspective,
glOrtho) are left handed
think of zNear and zFar as distance from view point
Everything else is right handed, including the vertexes to
be rendered
xx
yy
z+
z+
left handed right handed
![Page 19: Viewing in OpenGL](https://reader030.fdocuments.net/reader030/viewer/2022013000/61c9da0c54122247f55d5bd7/html5/thumbnails/19.jpg)
Common Transformation Usage
3 examples of resize/reshape() routine
restate projection & viewing transformations
Usually called when window resized
Registered as callback for glutReshapeFunc()
![Page 20: Viewing in OpenGL](https://reader030.fdocuments.net/reader030/viewer/2022013000/61c9da0c54122247f55d5bd7/html5/thumbnails/20.jpg)
resize(): Perspective & LookAt
void resize( int w, int h )
{glViewport( 0, 0, (GLsizei) w, (GLsizei) h );glMatrixMode( GL_PROJECTION );glLoadIdentity();
gluPerspective( 65.0, (GLdouble) w / h,
1.0, 100.0 );
glMatrixMode( GL_MODELVIEW );glLoadIdentity();
gluLookAt( 0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 );
}
![Page 21: Viewing in OpenGL](https://reader030.fdocuments.net/reader030/viewer/2022013000/61c9da0c54122247f55d5bd7/html5/thumbnails/21.jpg)
resize(): Perspective & Translate
Same effect as previous LookAt
void resize( int w, int h )
{
glViewport( 0, 0, (GLsizei) w, (GLsizei) h );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 65.0, (GLdouble) w/h,
1.0, 100.0 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glTranslatef( 0.0, 0.0, -5.0 );
}
![Page 22: Viewing in OpenGL](https://reader030.fdocuments.net/reader030/viewer/2022013000/61c9da0c54122247f55d5bd7/html5/thumbnails/22.jpg)
resize(): Ortho (part 1)void resize( int width, int height )
{
GLdouble aspect = (GLdouble) width / height;
GLdouble left = -2.5, right = 2.5;
GLdouble bottom = -2.5, top = 2.5;
glViewport( 0, 0, (GLsizei) w, (GLsizei) h );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
… continued …
![Page 23: Viewing in OpenGL](https://reader030.fdocuments.net/reader030/viewer/2022013000/61c9da0c54122247f55d5bd7/html5/thumbnails/23.jpg)
resize(): Ortho (part 2)
if ( aspect < 1.0 ) {
left /= aspect;
right /= aspect;
} else {
bottom *= aspect;
top *= aspect;
}
glOrtho( left, right, bottom, top, near, far );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
}
![Page 24: Viewing in OpenGL](https://reader030.fdocuments.net/reader030/viewer/2022013000/61c9da0c54122247f55d5bd7/html5/thumbnails/24.jpg)
Compositing Modeling Transformations
Problem 1: hierarchical objects
one position depends upon a previous position
robot arm or hand; sub-assemblies
Solution 1: moving local coordinate system
modeling transformations move coordinate system
post-multiply column-major matrices
OpenGL post-multiplies matrices
![Page 25: Viewing in OpenGL](https://reader030.fdocuments.net/reader030/viewer/2022013000/61c9da0c54122247f55d5bd7/html5/thumbnails/25.jpg)
Compositing Modeling Transformations
Problem 2: objects move relative to absolute world origin my object rotates around the wrong origin
make it spin around its center or something else
Solution 2: fixed coordinate system modeling transformations move objects around fixed coordinate
system
pre-multiply column-major matrices
OpenGL post-multiplies matrices
must reverse order of operations to achieve desired effect
glTranslatef(x,y,z);
glRotatef(theta, ax, ay, az);
glTranslatef(-x,-y,-z);
object();
![Page 26: Viewing in OpenGL](https://reader030.fdocuments.net/reader030/viewer/2022013000/61c9da0c54122247f55d5bd7/html5/thumbnails/26.jpg)
Additional Clipping Planes
At least 6 more clipping planes available
Good for cross-sections
Modelview matrix moves clipping plane
clipped
glEnable( GL_CLIP_PLANEi )
glClipPlane( GL_CLIP_PLANEi, GLdouble* coeff )
0 DCzByAx
![Page 27: Viewing in OpenGL](https://reader030.fdocuments.net/reader030/viewer/2022013000/61c9da0c54122247f55d5bd7/html5/thumbnails/27.jpg)
Reversing Coordinate Projection
Screen space back to world space
glGetIntegerv( GL_VIEWPORT, GLint viewport[4] )
glGetDoublev( GL_MODELVIEW_MATRIX, GLdouble
mvmatrix[16] )
glGetDoublev( GL_PROJECTION_MATRIX,
GLdouble projmatrix[16] )
gluUnProject( GLdouble winx, winy, winz,
mvmatrix[16], projmatrix[16],
GLint viewport[4],
GLdouble *objx, *objy, *objz )
gluProject goes from world to screen space
![Page 28: Viewing in OpenGL](https://reader030.fdocuments.net/reader030/viewer/2022013000/61c9da0c54122247f55d5bd7/html5/thumbnails/28.jpg)
Animation and Depth Buffering
![Page 29: Viewing in OpenGL](https://reader030.fdocuments.net/reader030/viewer/2022013000/61c9da0c54122247f55d5bd7/html5/thumbnails/29.jpg)
Animation and Depth Buffering
Discuss double buffering and animation
Discuss hidden surface removal using the depth buffer
![Page 30: Viewing in OpenGL](https://reader030.fdocuments.net/reader030/viewer/2022013000/61c9da0c54122247f55d5bd7/html5/thumbnails/30.jpg)
Double
Buffering
12
48
16
12
48
16Front
Buffer
Back
Buffer
Display
CPU DL
Poly.Per
Vertex
Raster Frag FB
Pixel
Texture
![Page 31: Viewing in OpenGL](https://reader030.fdocuments.net/reader030/viewer/2022013000/61c9da0c54122247f55d5bd7/html5/thumbnails/31.jpg)
Animation Using Double Buffering
1. Request a double buffered color bufferglutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
2. Clear color bufferglClear( GL_COLOR_BUFFER_BIT );
3. Render scene
4. Request swap of front and back buffersglutSwapBuffers();
5. Repeat steps 2 - 4 for animation
![Page 32: Viewing in OpenGL](https://reader030.fdocuments.net/reader030/viewer/2022013000/61c9da0c54122247f55d5bd7/html5/thumbnails/32.jpg)
Depth Buffering and
Hidden Surface Removal
12
48
16
12
48
16Color
Buffer
Depth
Buffer
Display
![Page 33: Viewing in OpenGL](https://reader030.fdocuments.net/reader030/viewer/2022013000/61c9da0c54122247f55d5bd7/html5/thumbnails/33.jpg)
Depth buffering
Technique to determine which primitives in your scene
are occluded by other primitives.
As each pixel in a primitive is rasterized, its distance from
the eyepoint (depth value), is compared with the values
stored in the depth buffer.
If the pixel’s depth value is less than the stored value, the
pixel’s depth value is written to the depth buffer, and its
color is written to the color buffer.
OpenGL depth values range from [0, 1], with one being
essentially infinitely far from the eyepoint. The depth
buffer is cleared to one at the start of a frame.
![Page 34: Viewing in OpenGL](https://reader030.fdocuments.net/reader030/viewer/2022013000/61c9da0c54122247f55d5bd7/html5/thumbnails/34.jpg)
Depth Buffering Using OpenGL
Request a depth bufferglutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE |
GLUT_DEPTH );
Enable depth bufferingglEnable( GL_DEPTH_TEST );
Clear color and depth buffersglClear( GL_COLOR_BUFFER_BIT |
GL_DEPTH_BUFFER_BIT );
Render scene
Swap color buffers
![Page 35: Viewing in OpenGL](https://reader030.fdocuments.net/reader030/viewer/2022013000/61c9da0c54122247f55d5bd7/html5/thumbnails/35.jpg)
An Updated Program Template
void main( int argc, char** argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
glutCreateWindow( “Tetrahedron” );
init();
glutIdleFunc( idle );
glutDisplayFunc( display );
glutMainLoop();
}
![Page 36: Viewing in OpenGL](https://reader030.fdocuments.net/reader030/viewer/2022013000/61c9da0c54122247f55d5bd7/html5/thumbnails/36.jpg)
An Updated Program Template (cont.)
void init( void ){
glClearColor( 0.0, 0.0, 1.0, 1.0 );}
void idle( void ){
glutPostRedisplay();}
![Page 37: Viewing in OpenGL](https://reader030.fdocuments.net/reader030/viewer/2022013000/61c9da0c54122247f55d5bd7/html5/thumbnails/37.jpg)
An Updated Program Template (cont.)
void drawScene( void )
{
GLfloat vertices[] = { … };
GLfloat colors[] = { … };
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glBegin( GL_TRIANGLE_STRIP );
/* calls to glColor*() and glVertex*() */
glEnd();
glutSwapBuffers();
}
![Page 38: Viewing in OpenGL](https://reader030.fdocuments.net/reader030/viewer/2022013000/61c9da0c54122247f55d5bd7/html5/thumbnails/38.jpg)
Gaming Industry
Video gaming industry took in about $25 billion in the US
in 2011 (ESA annual report)
“Directly and indirectly employs more than 120,000
people with an average salary for direct employees of
$90,000.” – M. D. Gallagher, CEO, Entertainment Software
Assoc, 2011
Source: ESA Report 2011