opengl: introduction yanci zhang game programming practice
TRANSCRIPT
![Page 1: OpenGL: Introduction Yanci Zhang Game Programming Practice](https://reader035.vdocuments.site/reader035/viewer/2022062803/56649f1e5503460f94c360a4/html5/thumbnails/1.jpg)
OpenGL: OpenGL: IntroductionIntroduction
Yanci ZhangYanci Zhang
Game Programming PracticeGame Programming Practice
![Page 2: OpenGL: Introduction Yanci Zhang Game Programming Practice](https://reader035.vdocuments.site/reader035/viewer/2022062803/56649f1e5503460f94c360a4/html5/thumbnails/2.jpg)
Overview of OpenGLOverview of OpenGL OpenGL vs. Direct XOpenGL vs. Direct X Rendering pipelineRendering pipeline Coordinate systemCoordinate system
OutlineOutline
Game Programming PracticeGame Programming Practice
![Page 3: OpenGL: Introduction Yanci Zhang Game Programming Practice](https://reader035.vdocuments.site/reader035/viewer/2022062803/56649f1e5503460f94c360a4/html5/thumbnails/3.jpg)
OpenGL = Open Graphics LibraryOpenGL = Open Graphics Library Graphics rendering API Produce high-quality color images composed of 3D geometric
objects and images Hardware independentHardware independent Cross platformCross platform
What is OpenGL?What is OpenGL?
Game Programming PracticeGame Programming Practice
![Page 4: OpenGL: Introduction Yanci Zhang Game Programming Practice](https://reader035.vdocuments.site/reader035/viewer/2022062803/56649f1e5503460f94c360a4/html5/thumbnails/4.jpg)
Bases for many advanced data structures in game Bases for many advanced data structures in game programmingprogramming
Typical applicationsTypical applications Scene graph State graph Decision tree Kd-tree, quad tree …
Basic FunctionBasic Function
Game Programming PracticeGame Programming Practice
![Page 5: OpenGL: Introduction Yanci Zhang Game Programming Practice](https://reader035.vdocuments.site/reader035/viewer/2022062803/56649f1e5503460f94c360a4/html5/thumbnails/5.jpg)
Rendering basic primitives, like points, lines, triangles…Rendering basic primitives, like points, lines, triangles… Matrix operationsMatrix operations Local illuminationLocal illumination Texture mappingTexture mapping Pixel operationsPixel operations ……
What Can OpenGL Do?What Can OpenGL Do?
Game Programming PracticeGame Programming Practice
![Page 6: OpenGL: Introduction Yanci Zhang Game Programming Practice](https://reader035.vdocuments.site/reader035/viewer/2022062803/56649f1e5503460f94c360a4/html5/thumbnails/6.jpg)
Create windowsCreate windows Handle window eventsHandle window events Response to user inputResponse to user input Scene managementScene management ……
What Can Not OpenGL What Can Not OpenGL Do?Do?
Game Programming PracticeGame Programming Practice
![Page 7: OpenGL: Introduction Yanci Zhang Game Programming Practice](https://reader035.vdocuments.site/reader035/viewer/2022062803/56649f1e5503460f94c360a4/html5/thumbnails/7.jpg)
OpenGL is only graphics libraryOpenGL is only graphics library DirectX handles graphics, audio, user inputDirectX 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 PracticeGame Programming Practice
![Page 8: OpenGL: Introduction Yanci Zhang Game Programming Practice](https://reader035.vdocuments.site/reader035/viewer/2022062803/56649f1e5503460f94c360a4/html5/thumbnails/8.jpg)
OpenGLOpenGL Industry standard maintained by OpenGL Architectural Review
Board (ARB) Stable function interface Cross platform Very clean, easy to learn
DirectXDirectX Microsoft’s product Instable function interface Only support Windows
OpenGL vs. DirectX OpenGL vs. DirectX 2/2
Game Programming PracticeGame Programming Practice
![Page 9: OpenGL: Introduction Yanci Zhang Game Programming Practice](https://reader035.vdocuments.site/reader035/viewer/2022062803/56649f1e5503460f94c360a4/html5/thumbnails/9.jpg)
InputInput: : scene objects, lighting, camerascene objects, lighting, camera Most of the data is vertex list
OutputOutput: : pixels stored in framebufferpixels stored in framebuffer QuestionQuestion: : how to convert 3D vertex list to 2D pixels?how to convert 3D vertex list to 2D pixels? OpenGL is designed to fulfill this taskOpenGL is designed to fulfill this task
OpenGL PipelineOpenGL Pipeline
OpenGL Pipeline
Game Programming PracticeGame Programming Practice
![Page 10: OpenGL: Introduction Yanci Zhang Game Programming Practice](https://reader035.vdocuments.site/reader035/viewer/2022062803/56649f1e5503460f94c360a4/html5/thumbnails/10.jpg)
Main task: transformation and lightingMain task: transformation and lighting Transformation:Transformation:
Model-View transformation: translation, rotation, scaling Projection transformation: perspective, parallel
LightingLighting Fixed-pipeline implements per-vertex lighting Lighting is normally delayed to fragment processing in
programmable pipeline
Vertex ProcessingVertex Processing
Game Programming PracticeGame Programming Practice
![Page 11: OpenGL: Introduction Yanci Zhang Game Programming Practice](https://reader035.vdocuments.site/reader035/viewer/2022062803/56649f1e5503460f94c360a4/html5/thumbnails/11.jpg)
Assemble vertices into primitivesAssemble vertices into primitives Lines/Curves Triangles/Polygons/Surfaces
Primitive AssemblyPrimitive Assembly
Game Programming PracticeGame Programming Practice
![Page 12: OpenGL: Introduction Yanci Zhang Game Programming Practice](https://reader035.vdocuments.site/reader035/viewer/2022062803/56649f1e5503460f94c360a4/html5/thumbnails/12.jpg)
Don’t render invisible objectsDon’t render invisible objects ClippingClipping
Remove primitives outside of the camera’s view frustum
Backface cullingBackface culling Remove triangles facing away from camera Usually cuts down $ of triangles by about
50%
Clipping and CullingClipping and Culling
Game Programming PracticeGame Programming Practice
![Page 13: OpenGL: Introduction Yanci Zhang Game Programming Practice](https://reader035.vdocuments.site/reader035/viewer/2022062803/56649f1e5503460f94c360a4/html5/thumbnails/13.jpg)
Convert a primitive into a set of fragmentsConvert 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
RasterizationRasterization
Game Programming PracticeGame Programming Practice
![Page 14: OpenGL: Introduction Yanci Zhang Game Programming Practice](https://reader035.vdocuments.site/reader035/viewer/2022062803/56649f1e5503460f94c360a4/html5/thumbnails/14.jpg)
Assemble fragments into final framebufferAssemble fragments into final framebuffer Hidden-surface removal:Hidden-surface removal:
Some fragments may occlude parts of others Z-buffer sorts pixels by distance Handle transparency
Other operationsOther operations
Fragment Processing Fragment Processing
Game Programming PracticeGame Programming Practice
![Page 15: OpenGL: Introduction Yanci Zhang Game Programming Practice](https://reader035.vdocuments.site/reader035/viewer/2022062803/56649f1e5503460f94c360a4/html5/thumbnails/15.jpg)
Using vertex list to represent a set rendering primitivesUsing vertex list to represent a set rendering primitives 14 primitives supported by OpenGL14 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 PrimitivesRendering Primitives
Game Programming PracticeGame Programming Practice
![Page 16: OpenGL: Introduction Yanci Zhang Game Programming Practice](https://reader035.vdocuments.site/reader035/viewer/2022062803/56649f1e5503460f94c360a4/html5/thumbnails/16.jpg)
Polygons must obey following rulesPolygons must obey following rules Simple polygon Convex polygon Coplanar vertices
Triangle satisfies all the above rulesTriangle satisfies all the above rules Most common rendering primitive
Restriction on PolygonsRestriction on Polygons
Game Programming PracticeGame Programming Practice
![Page 17: OpenGL: Introduction Yanci Zhang Game Programming Practice](https://reader035.vdocuments.site/reader035/viewer/2022062803/56649f1e5503460f94c360a4/html5/thumbnails/17.jpg)
Specifying Vertex Specifying Vertex 1/21/2
Game Programming PracticeGame Programming Practice
![Page 18: OpenGL: Introduction Yanci Zhang Game Programming Practice](https://reader035.vdocuments.site/reader035/viewer/2022062803/56649f1e5503460f94c360a4/html5/thumbnails/18.jpg)
How to convert 3D objects to 2D image?How to convert 3D objects to 2D image? Just like taking a photograph!Just like taking a photograph!
Camera Analogy Camera Analogy 1/21/2
camera
tripod model
viewingvolume
Game Programming PracticeGame Programming Practice
![Page 19: OpenGL: Introduction Yanci Zhang Game Programming Practice](https://reader035.vdocuments.site/reader035/viewer/2022062803/56649f1e5503460f94c360a4/html5/thumbnails/19.jpg)
Projection transformationsProjection transformations Adjust the lens of the camera
Viewing transformationsViewing transformations Tripod–define position and orientation of the viewing volume
in the world Modeling transformationsModeling transformations
Moving the model Viewport transformationsViewport transformations
Enlarge or reduce the physical photograph
Camera Analogy Camera Analogy 2/22/2
Game Programming PracticeGame Programming Practice
![Page 20: OpenGL: Introduction Yanci Zhang Game Programming Practice](https://reader035.vdocuments.site/reader035/viewer/2022062803/56649f1e5503460f94c360a4/html5/thumbnails/20.jpg)
Steps in Forming an ImageSteps in Forming an Image Specify geometry (world coordinates) Specify camera (camera coordinates) Project (clip coordinates) Map to viewport (screen coordinates)
Each step uses transformationsEach step uses transformations Every transformation is equivalent to a change in Every transformation is equivalent to a change in
coordinate systems coordinate systems
Coord. Sys. and Coord. Sys. and TransformationsTransformations
Game Programming PracticeGame Programming Practice
![Page 21: OpenGL: Introduction Yanci Zhang Game Programming Practice](https://reader035.vdocuments.site/reader035/viewer/2022062803/56649f1e5503460f94c360a4/html5/thumbnails/21.jpg)
Coord. Sys. and Coord. Sys. and TransformationsTransformations
Object Object CoordinatesCoordinates
World World CoordinatesCoordinates
Camera Camera CoordinatesCoordinates
Clip Clip CoordinatesCoordinates
Screen Screen CoordinatesCoordinates
Model Model TransformationTransformation
World World TransformationTransformation
Project Project TransformationTransformation
Viewport Viewport TransformationTransformation
Game Programming PracticeGame Programming Practice
![Page 22: OpenGL: Introduction Yanci Zhang Game Programming Practice](https://reader035.vdocuments.site/reader035/viewer/2022062803/56649f1e5503460f94c360a4/html5/thumbnails/22.jpg)
Affine Transformations Affine Transformations 1/21/2
DefinitionDefinition
Using a single matrix multiplication to represent affine Using a single matrix multiplication to represent affine transformation by using augmented matrix and transformation by using augmented matrix and augmented vectoraugmented vector
Game Programming PracticeGame Programming Practice
![Page 23: OpenGL: Introduction Yanci Zhang Game Programming Practice](https://reader035.vdocuments.site/reader035/viewer/2022062803/56649f1e5503460f94c360a4/html5/thumbnails/23.jpg)
Preserving geometryPreserving geometry lines, polygons, quadrics
Affine = line preservingAffine = line preserving Rotation, translation, scaling Projection Concatenation (composition)
Affine Transformations Affine Transformations 2/22/2
Game Programming PracticeGame Programming Practice
![Page 24: OpenGL: Introduction Yanci Zhang Game Programming Practice](https://reader035.vdocuments.site/reader035/viewer/2022062803/56649f1e5503460f94c360a4/html5/thumbnails/24.jpg)
Each vertex is a column vector Each vertex is a column vector ww is usually is usually 1.01.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 multiplicationsAll operations are matrix multiplications Directions can be represented with Directions can be represented with w = w = 0.00.0
Homogeneous Homogeneous CoordinatesCoordinates
w
z
y
x
v
Game Programming PracticeGame Programming Practice
![Page 25: OpenGL: Introduction Yanci Zhang Game Programming Practice](https://reader035.vdocuments.site/reader035/viewer/2022062803/56649f1e5503460f94c360a4/html5/thumbnails/25.jpg)
A vertex is transformed by 4 x 4 matricesA 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 Transformations3D Transformations
v
M
151173
141062
13951
12840
mmmm
mmmm
mmmm
mmmm
M
Game Programming PracticeGame Programming Practice
![Page 26: OpenGL: Introduction Yanci Zhang Game Programming Practice](https://reader035.vdocuments.site/reader035/viewer/2022062803/56649f1e5503460f94c360a4/html5/thumbnails/26.jpg)
Two waysTwo ways Specify matrices (glLoadMatrix, glMultMatrix) Specify operation (glRotate, glOrtho)
Obtain the desired matrix by a sequence of simple Obtain the desired matrix by a sequence of simple transformations that can be concatenated togethertransformations that can be concatenated together
Specifying Specifying TransformationsTransformations
Game Programming PracticeGame Programming Practice
![Page 27: OpenGL: Introduction Yanci Zhang Game Programming Practice](https://reader035.vdocuments.site/reader035/viewer/2022062803/56649f1e5503460f94c360a4/html5/thumbnails/27.jpg)
Manage the matricesManage the matrices OpenGL provides matrix stacks for each type of transformation
Specify current matrix stackSpecify current matrix stackglMatrixMode(glMatrixMode(GL_MODELVIEW / GL_PROJECTION / GL_MODELVIEW / GL_PROJECTION /
GL_TEXTUREGL_TEXTURE)) Operations on matrix stackOperations 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 StackMatrix Stack
Game Programming PracticeGame Programming Practice
![Page 28: OpenGL: Introduction Yanci Zhang Game Programming Practice](https://reader035.vdocuments.site/reader035/viewer/2022062803/56649f1e5503460f94c360a4/html5/thumbnails/28.jpg)
Object coordinates to world coordinatesObject coordinates to world coordinates Call Call glMatrixModel(glMatrixModel(GL_MODELVIEWGL_MODELVIEW)) first ! first ! Move objectMove object glTranslate{fd}( x, y, z )glTranslate{fd}( x, y, z )
Rotate object around arbitrary axis Rotate object around arbitrary axis (x,y,z)(x,y,z) glRotate{fd}( angle, x, y, z )glRotate{fd}( angle, x, y, z )
Stretch or shrink objectStretch or shrink object glScale{fd}( x, y, z )glScale{fd}( x, y, z )
Modeling Modeling Transformations Transformations 1/31/3
obj
obj
obj
obj
world
world
world
world
w
z
y
x
w
z
y
x
modelM
Game Programming PracticeGame Programming Practice
![Page 29: OpenGL: Introduction Yanci Zhang Game Programming Practice](https://reader035.vdocuments.site/reader035/viewer/2022062803/56649f1e5503460f94c360a4/html5/thumbnails/29.jpg)
Different transform order may produce different resultsDifferent transform order may produce different results Rotate then translate Translate then rotate
Modeling Modeling Transformations Transformations 2/32/3
Game Programming PracticeGame Programming Practice
![Page 30: OpenGL: Introduction Yanci Zhang Game Programming Practice](https://reader035.vdocuments.site/reader035/viewer/2022062803/56649f1e5503460f94c360a4/html5/thumbnails/30.jpg)
StepsSteps Suppose the current matrix is C Specify a model transform matrix M1 by:
glMultMatrix() glRotate(), glTranslate()…
Use CM1 to replace the current matrix on the top of matrix stack
Specify another model transform matrix M2
Now the current matrix is CM1M2
Transform vertex v: v’ = CM1M2v
You must specify the transform in reverse order! You must specify the transform in reverse order!
Modeling Modeling Transformations Transformations 3/33/3
Game Programming PracticeGame Programming Practice
![Page 31: OpenGL: Introduction Yanci Zhang Game Programming Practice](https://reader035.vdocuments.site/reader035/viewer/2022062803/56649f1e5503460f94c360a4/html5/thumbnails/31.jpg)
World coordinates to camera coordinatesWorld coordinates to camera coordinates Position the camera/eye in the scenePosition the camera/eye in the scene
Place the tripod down and aim cameragluLookAt(gluLookAt(eyeeyexx, eye, eyeyy, eye, eyezz, , aimaimxx, aim, aimyy, aim, aimzz, , upupxx, up, upyy, up, upzz))
Multiplies itself onto the current matrix up vector determines unique orientation
Viewing Viewing TransformationsTransformations
tripod
obj
obj
obj
obj
world
world
world
world
eye
eye
eye
eye
w
z
y
x
w
z
y
x
w
z
y
x
modelviewview MMM
Game Programming PracticeGame Programming Practice
![Page 32: OpenGL: Introduction Yanci Zhang Game Programming Practice](https://reader035.vdocuments.site/reader035/viewer/2022062803/56649f1e5503460f94c360a4/html5/thumbnails/32.jpg)
Camera coordinates to clip coordinatesCamera coordinates to clip coordinates Perspective projectionPerspective projection
gluPerspective( fovy, aspect, zNear, zFar )gluPerspective( fovy, aspect, zNear, zFar )
glFrustum( left, right, bottom, top, zNear, zFar )glFrustum( left, right, bottom, top, zNear, zFar ) Feature: objects in the distance appear smaller than objects
close by
Projection Projection Transformation Transformation 1/21/2
Game Programming PracticeGame Programming Practice
![Page 33: OpenGL: Introduction Yanci Zhang Game Programming Practice](https://reader035.vdocuments.site/reader035/viewer/2022062803/56649f1e5503460f94c360a4/html5/thumbnails/33.jpg)
Orthographic parallel projectionOrthographic parallel projectionglOrtho( left, right, bottom, top, zNear, zFar )glOrtho( left, right, bottom, top, zNear, zFar )
gluOrtho2D( left, right, bottom, top ) gluOrtho2D( left, right, bottom, top ) Feature: ignores perspective effect to allow accurate
measurements
Projection Projection Transformation Transformation 2/22/2
Game Programming PracticeGame Programming Practice
![Page 34: OpenGL: Introduction Yanci Zhang Game Programming Practice](https://reader035.vdocuments.site/reader035/viewer/2022062803/56649f1e5503460f94c360a4/html5/thumbnails/34.jpg)
ViewportViewport 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 Viewport TransformationTransformation
Game Programming PracticeGame Programming Practice
![Page 35: OpenGL: Introduction Yanci Zhang Game Programming Practice](https://reader035.vdocuments.site/reader035/viewer/2022062803/56649f1e5503460f94c360a4/html5/thumbnails/35.jpg)
Why only one ModelView matrix stack instead of two Why only one ModelView matrix stack instead of two separated Model and View matrix stacks?separated Model and View matrix stacks?
Moving camera = Moving every object in the world Moving camera = Moving every object in the world towards a stationary cameratowards a stationary camera
Viewing transformations = Several modeling Viewing transformations = Several modeling transformationstransformations
Viewing and ModelingViewing and Modeling
Game Programming PracticeGame Programming Practice
![Page 36: OpenGL: Introduction Yanci Zhang Game Programming Practice](https://reader035.vdocuments.site/reader035/viewer/2022062803/56649f1e5503460f94c360a4/html5/thumbnails/36.jpg)
Important: Important: OpenGL performs matrices multiplication in OpenGL performs matrices multiplication in reverse order if multiple transforms are applied to a reverse order if multiple transforms are applied to a vertexvertex 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 OrderTransform Order
Game Programming PracticeGame Programming Practice