Download presentation
Presentation is loading. Please wait.
Published byStuart Horton Modified over 9 years ago
1
OpenGL: Introduction Yanci Zhang Game Programming Practice
2
Overview of OpenGL OpenGL vs. Direct X Rendering pipeline Coordinate system Outline Game Programming Practice
3
OpenGL = Open Graphics Library Graphics rendering API Produce high-quality color images composed of 3D geometric objects and images Hardware independent Cross platform What is OpenGL? Game Programming Practice
4
Bases for many advanced data structures in game programming Typical applications Scene graph State graph Decision tree Kd-tree, quad tree … Basic Function Game Programming Practice
5
Rendering basic primitives, like points, lines, triangles… Matrix operations Local illumination Texture mapping Pixel operations … What Can OpenGL Do? Game Programming Practice
6
Create windows Handle window events Response to user input Scene management … What Can Not OpenGL Do? Game Programming Practice
7
OpenGL is only graphics library DirectX handles graphics, audio, user input Use OpenGL improperly, system does nothing Use DirectX improperly, system does something beyond expectation OpenGL vs. DirectX OpenGL vs. DirectX 1/2 Game Programming Practice
8
OpenGL Industry standard maintained by OpenGL Architectural Review Board (ARB) Stable function interface Cross platform Very clean, easy to learn DirectX Microsoft’s product Instable function interface Only support Windows OpenGL vs. DirectX OpenGL vs. DirectX 2/2 Game Programming Practice
9
Input: scene objects, lighting, camera Most of the data is vertex list Output: pixels stored in framebuffer Question: how to convert 3D vertex list to 2D pixels? OpenGL is designed to fulfill this task OpenGL Pipeline Game Programming Practice
10
Main task: transformation and lighting Transformation: Model-View transformation: translation, rotation, scaling Projection transformation: perspective, parallel Lighting Fixed-pipeline implements per-vertex lighting Lighting is normally delayed to fragment processing in programmable pipeline Vertex Processing Game Programming Practice
11
Assemble vertices into primitives Lines/Curves Triangles/Polygons/Surfaces Primitive Assembly Game Programming Practice
12
Don’t render invisible objects Clipping Remove primitives outside of the camera’s view frustum Backface culling Remove triangles facing away from camera Usually cuts down $ of triangles by about 50% Clipping and Culling Game Programming Practice
13
Convert a primitive into a set of fragments Each pixel has both RGB color and depth Interpolate vertex color over fragments Fragment might not correspond to pixels on screen: Occluded fragments Rasterization Game Programming Practice
14
Assemble fragments into final framebuffer Hidden-surface removal: Some fragments may occlude parts of others Z-buffer sorts pixels by distance Handle transparency Other operations Fragment Processing Game Programming Practice
15
Using vertex list to represent a set rendering primitives 14 primitives supported by OpenGL Point: GL_POINTS Line: GL_LINES, GL_LINE_STRIP, GL_LINE_LOOP Triangle: GL_TRIANGLES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN Polygon: GL_QUADS, GL_QUAD_STRIP, GL_POLYGON Rendering Primitives Game Programming Practice
16
Polygons must obey following rules Simple polygon Convex polygon Coplanar vertices Triangle satisfies all the above rules Most common rendering primitive Restriction on Polygons Game Programming Practice
17
Specifying Vertex 1/2 Game Programming Practice
18
How to convert 3D objects to 2D image? Just like taking a photograph! Camera Analogy 1/2 camera tripod model viewing volume Game Programming Practice
19
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 Camera Analogy 2/2 Game Programming Practice
20
Steps in Forming an Image Specify geometry (world coordinates) Specify camera (camera coordinates) Project (clip coordinates) Map to viewport (screen coordinates) Each step uses transformations Every transformation is equivalent to a change in coordinate systems Coord. Sys. and Transformations Game Programming Practice
21
Coord. Sys. and Transformations Object Coordinates World Coordinates Camera Coordinates Clip Coordinates Screen Coordinates Model Transformation World Transformation Project Transformation Viewport Transformation Game Programming Practice
22
Affine Transformations 1/2 Definition Using a single matrix multiplication to represent affine transformation by using augmented matrix and augmented vector Game Programming Practice
23
Preserving geometry lines, polygons, quadrics Affine = line preserving Rotation, translation, scaling Projection Concatenation (composition) Affine Transformations 2/2 Game Programming Practice
24
Each vertex is a column vector w is usually 1.0 ( x,y,z,w ) = ( ax,ay,az,aw ) If w is not 1.0, we can recover x,y,z by division by w Only perspective transformation change w All operations are matrix multiplications Directions can be represented with w = 0.0 Homogeneous Coordinates Game Programming Practice
25
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 3D Transformations Game Programming Practice
26
Two ways Specify matrices (glLoadMatrix, glMultMatrix) Specify operation (glRotate, glOrtho) Obtain the desired matrix by a sequence of simple transformations that can be concatenated together Specifying Transformations Game Programming Practice
27
Manage the matrices OpenGL provides matrix stacks for each type of transformation Specify current matrix stack glMatrixMode( GL_MODELVIEW / GL_PROJECTION / GL_TEXTURE ) Operations on matrix stack glLoadIdentity(): glLoadIdentity(): replaces the current matrix with the identity matrix glPushMatrix(): glPushMatrix(): pushes the current matrix stack down by one, duplicating the current matrix glPopMatrix(): glPopMatrix(): pops the current matrix stack, replacing the current matrix with the one below it on the stack glMultMatrix(): glMultMatrix(): multiply the current matrix by input matrix Matrix Stack Game Programming Practice
28
Object coordinates to world coordinates Call glMatrixModel(GL_MODELVIEW) first ! Move object glTranslate{fd}( x, y, z ) glTranslate{fd}( x, y, z ) Rotate object around arbitrary axis (x,y,z) glRotate{fd}( angle, x, y, z ) glRotate{fd}( angle, x, y, z ) Stretch or shrink object glScale{fd}( x, y, z ) glScale{fd}( x, y, z ) Modeling Transformations 1/3 Game Programming Practice
29
Different transform order may produce different results Rotate then translate Translate then rotate Modeling Transformations 2/3 Game Programming Practice
30
Steps Suppose the current matrix is C Specify a model transform matrix M 1 by: glMultMatrix() glRotate(), glTranslate()… Use CM 1 to replace the current matrix on the top of matrix stack Specify another model transform matrix M 2 Now the current matrix is CM 1 M 2 Transform vertex v : v’ = CM 1 M 2 v You must specify the transform in reverse order! Modeling Transformations 3/3 Game Programming Practice
31
World coordinates to camera coordinates Position the camera/eye in the scene Place the tripod down and aim camera gluLookAt(eye x, eye y, eye z, aim x, aim y, aim z, up x, up y, up z ) Multiplies itself onto the current matrix up vector determines unique orientation Viewing Transformations tripod Game Programming Practice
32
Camera coordinates to clip coordinates Perspective projection gluPerspective( fovy, aspect, zNear, zFar ) glFrustum( left, right, bottom, top, zNear, zFar ) Feature: objects in the distance appear smaller than objects close by Projection Transformation 1/2 Game Programming Practice
33
Orthographic parallel projection glOrtho( left, right, bottom, top, zNear, zFar ) gluOrtho2D( left, right, bottom, top ) Feature: ignores perspective effect to allow accurate measurements Projection Transformation 2/2 Game Programming Practice
34
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 ) glViewport( x, y, width, height ) Viewport Transformation Game Programming Practice
35
Why only one ModelView matrix stack instead of two separated Model and View matrix stacks? Moving camera = Moving every object in the world towards a stationary camera Viewing transformations = Several modeling transformations Viewing and Modeling Game Programming Practice
36
Important: OpenGL performs matrices multiplication in reverse order if multiple transforms are applied to a vertex Viewing transform comes first before modeling transform in your code If you want to rotate then translate an object, put glTranslatef() first then glRotatef() Transform Order Game Programming Practice
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.