opengl - github pages › cs148 › assets › slides › 05-opengl.pdf · hierarchical modeling cs...
TRANSCRIPT
CS 148: Summer 2016Introduction of Graphics and ImagingZahid Hossain
OpenGL
http://www.pling.org.uk/cs/cgv.html
SoFar:TheoryofRasterization
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 2
Observation
• Applytransformation• Barycentric Interpolation• Rasterize• ComputeLightandShading(Moreonitlater)• LookupTextures(Moreonitlater)• Andlotmore….
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 3
Observation
• Applytransformation• Barycentric Interpolation• Rasterize• ComputeLightandShading(Moreonitlater)• LookupTextures(Moreonitlater)• Andlotmore….
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 4
To Millions of Triangles
and Billions of Fragments
Observation
• Applytransformation• Barycentric Interpolation• Rasterize• ComputeLightandShading(Moreonitlater)• LookupTextures(Moreonitlater)• Andlotmore….
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 5
To Millions of Triangles
and Billions of Fragments
SIMD(Single Instruction Multiple Data)
GraphicsHardware
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 6
2560 cores! (upto 1733 MHz, 8GB Mem)http://www.geforce.com/hardware/10series/geforce-gtx-1080
GTX1080
Advice
Leaveimplementationoflow-levelfeaturestotheexperts.
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 7
Advice
Leaveimplementationoflow-levelfeaturestotheexperts.
Why?
• Abstractawayhardwaredifferences
• Rasterizationshouldbefast
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 8
Introducing…
Industry Standard API for Computer Graphics(Cross Platform, since 1992)
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 9
NottheOnlyOne
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 10
RelatedAPIsintheOpenGLFamily
GoogleMaps
AngryBirdsbyRovio
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 11
RelatedAPIsintheOpenGLFamily
GoogleMaps
AngryBirdsbyRovio
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 12
OpenGL1.x
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 13
OpenGL1.x:Why?
•Easytolearn!•Littlecode•Instructional
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 14
SimpleFirstProgram
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 15
WhatOpenGLIsNot
OpenGLisnotawindowingsystem
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 16
IntroducingGLUT(GLUtilityToolkit)
• Simplecross-platformwindowingAPI• glutInitDisplay(), glutInitWindowSize(..)
• Bindings:C,C++,Fortran,Ada,…• Features:• Multiplewindows,menus• Keyboard/mouse/otherinput• Assortedcallbacks:idle,timers• Basicfontsupport‘• glutSolidTeapot, glutSolidSphere, glutSolidCube, …
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 17
IntroducingGLUT(GLUtilityToolkit)
• Simplecross-platformwindowingAPI• glutInitDisplay(), glutInitWindowSize(..)
• Bindings:C,C++,Fortran,Ada,…• Features:• Multiplewindows,menus• Keyboard/mouse/otherinput• Assortedcallbacks:idle,timers• Basicfontsupport‘• glutSolidTeapot, glutSolidSphere, glutSolidCube, …
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 18
Other options: glfw, SDL
IntroducingGLU(GLUtility)• High-levelgraphicscommands• Not includedinOpenGLES• Someinterestingfeatures:• Mappingbetweenworldandscreencoordinates• Texturingsupport• Tessellationandothergeometricutilities• OpenGLerrorcodelookup• Moreprimitives:spheres,cylinders,disks,…• Camerasupport:gluLookAt, gluOrtho2D, …
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 19
SimpleFirstProgram
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 20
Projection Matrix !(GL_PROJECTION)
SimpleFirstProgram
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 21
Camera and Model Matrices Combined !
(GL_MODELVIEW)
gluLookAt(eye,at,up)
defines thecamera/viewingmatrix!
whereistheviewpoint?
whereisitpointed?
whichwayisup?
eyeat
up
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 22
gluPerspective(fovy,aspect,near,far)
TheRedbook, fig.3-14(p.155)
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 23
TransformationRecall
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 24
TransformationRecall
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 25
NDC à Viewport Transformation à Final Image
glViewport(x,y,width,height)
(x,y)atlowerleft
height
width
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 26
Primitive[prim-i-tiv]:
AsmallpieceofgeometrythatcanberenderedinOpenGL;e.g.triangles,lines,pointsetc.
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 27
OpenGLPrimitiveTypes
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 28
http://www.opengl2go.net/wp-content/uploads/2015/10/gl-primitives-with-background.png
OpenGLPrimitives(Triangles)
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 29
1 2
3
4 5
6
OpenGLPrimitives(TriangleStrip)
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 30
1
2
3
4
5
OpenGLPrimitives(TriangleStrip)
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 31
1
2
3
4
5
Immediate Mode(Deprecated)
Color:glColor3f(red,green,blue)
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 32
v1
v2
v3
GL,GLU,andGLUTnotationsgl...
e.g.,glColor3f(...)coreOpenGLfunction
glu...e.g.,gluLookAt(...)
OpenGLutilityfunction,makescommontaskseasier(defined intermsofgl...functions)
glut...e.g.,glutSolidTeapot(...)
GLUTfunctions
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 33
GL,GLU,andGLUTnotations
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 34
glVertex3f(...)...3f takes3floats...3d takes3doubles...3i takes3integers
...2f takes2floats
...4f takes4floats(etc)
ComposingTransformationsglLoadMatrixf(A);glMultMatrixf(B);glMultMatrixf(C);
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 35
ConvenienceFunctions
• glTranslatef(tx, ty, tz)
• glRotatef(degrees, x, y, z)
• glScalef(sx, sy, sz)
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 36
HierarchicalModeling
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 37
translate(0,4)drawTorso()pushMatrix()
translate(1.5,0)rotateX(leftHipRotate)drawThigh()pushMatrix()
translate(0,-2)rotateX(leftKneeRotate)drawLeg()...
popMatrix()popMatrix()pushMatrix()
translate(-1.5,0)rotateX(rightHipRotate)// Draw the right side...
...x
y
CurrentMatrix =translate(0,4)translate(1.5,0)rotateX(leftHipRotate)translate(0,-2)rotate(leftKneeRotate)
MatrixStack
translate(0,4)translate(0,4)translate(1.5,0)rotateX(leftHipRotate)
OpenGLMatrixStack
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 38
OpenGL1.xPipeline(Simplified)
http://upload.wikimedia.org/wikipedia/commons/b/bb/Pipeline_OpenGL_%28en%29.pngCS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 39
OpenGL1.xPipeline(Unsimplified)
http://www.opengl.org/documentation/specs/version1.1/state.pdf
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 40
OpenGL1.xPipeline(Unsimplified)
http://www.opengl.org/documentation/specs/version1.1/state.pdf
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 41
PiecesofthePipeline
Stores“subroutines”
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 42
GLuint boxList;boxList = glGenLists(1);glNewList(boxList, GL_COMPILE);
// draw boxglEndList(boxList);…glCallList(boxList);
PiecesofthePipeline
Stores“subroutines”
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 43
PiecesofthePipeline
Constructgeometricobjects
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 44
PiecesofthePipeline
Changegeometry
Storeprimitiveshapes
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 45
PiecesofthePipeline
Changegeometry
Storeprimitiveshapes
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 46
Includes Clipping
PiecesofthePipeline
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 47
Fragment[frag-muhnt]:
Thedatanecessarytogenerateasinglepixel’sworthofaprimitive.
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 48
PiecesofthePipeline
Modifyandcombineper-pixel
information
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 49
PiecesofthePipeline
Prepareimagetobedisplayed
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 50
OpenGLStateMachine
ChangeStateDrawDraw
ChangeStateDraw
ChangeStateChangeState
Draw
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 51
OpenGLStateMachine
glColor3f(…)
glEnable(…)
glLineStipple(…)
glGetFloatv(…)
glIsEnabled(…)
glGetLineStipple(…)
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 52
SetState GetState
OpenGLStateMachine
glColor3f(…)
glEnable(…)
glLineStipple(…)
glGetFloatv(…)
glIsEnabled(…)
glGetLineStipple(…)
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 53
Efficiently managing state changes is a major implementation challenge
SetState GetState
OUT OF DATE.
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 54
OUT OF DATE.
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 55
VertexLighting
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 56
Normal
Avectorperpendiculartoasurface;constantoveraplane
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 57
https://en.wikipedia.org/wiki/Normal_(geometry)
SpecifyingNormals
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 58
1
2
3
1
2
3
VertexLighting
• DiffuseTerm
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 59
VertexLighting
• SpecularTerm
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 60
Camera
VertexLighting
• SpecularTerm
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 61
Camera
VertexLighting
• SpecularTerm
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 62
Camera
AmbientLight
VertexLighting
• SpecularTerm
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 63
Camera
AmbientLight
VertexLighting
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 64
EnableaLight
Setupmaterial
VertexLighting
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 65
EnableaLight
Setupmaterial
Note: w=0 is directional light
glutSolidTeapot specifies the normal in this case
VertexLighting:Types
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 66
http://www.computing.northampton.ac.uk/~gary/csy3019/images3d/lightSources.gif
Vertex&IndexBuffers
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 67
Vertex/IndexBuffer
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 68
8 vertices only
Vertex/IndexBuffer
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 69
Drawing with triangles: 36 vertices!
8 vertices only
Vertex/IndexBuffer
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 70
8 vertices only
Vertices={v0,v1,..V7}
Indices={0,1,2,0,2,3,1,5,6,1,6,2…...}v0
v1
v2
v3
v5
v6
v7
Vertex/IndexBuffer
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 71
NextGenerationAPIs
• Vulkan (NextgenOpenGL)• DirectX12(Microsoft)• Metal(Apple)
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 72
NextGenerationAPIs
• Vulkan (NextgenOpenGL)• DirectX12(Microsoft)• Metal(Apple)
CS 148: Introduction to Computer Graphics and Imaging (Summer 2016) – Zahid Hossain 73
CS 148: Summer 2016Introduction of Graphics and ImagingZahid Hossain
OpenGL
http://www.pling.org.uk/cs/cgv.html