Documente Academic
Documente Profesional
Documente Cultură
Michael Tao
Event-Driven Programming
I GLUT
C++ Quick Introduction
OpenGL
I Commands
I Hierarchical Programming
int main ()
{
while ( true )
{
...
if ( event . happened () )
{
doEventCode () ;
}
...
}
}
GLUT
I Used in this class!
QT
I My Favorite!
GTK
...
}
void rende rFunctio n ()
{
glClear ( G L _ C O L O R _ B UF F E R _ B I T | G L _D EP TH _ BU FF ER ) ;
glClearColor (0 ,0 ,0 ,1) ;
drawStuff () ;
g lu tS wa p Bu ff er s () ;
}
struct Foo () {
Foo ( int a_ =0) : a ( a_ ) {}
int f () { return 1;}
int a ;
static int g () { return 3;}
}
class Bar () {
public :
Bar () : myfoo ( new Foo (4) ) {}
~ Bar () { delete myfoo ;}
Foo * myfoo ;
int g () { return -1;}
}
Foo foo ;
Bar * bar_ptr = new Bar () ;
bar_ptr - > myfoo . a = foo . f () ;
foo . a = bar_ptr - > g () ;
int a = Foo :: g () ;
Pointers
Similar to what you see in most other languages so far
I Pass by value / pass by reference
I Except we explicitly declare when to do what
const provides security over modification
EnableClientState
DisableClientState
CCCC
ColorPointer
Vertex
MapGrid
CCCC
IndexPointer Array
CCCC
NormalPointer Control
VertexPointer
InterLeavedArrays
CCCC
CCCC
CCCC Evaluator
ArrayElement CCCC
CCCC
EvalMesh
EvalPoint
Grid
Application
Control
DrawElements
DrawArrays CCCC
CCCC
CCCC CCC
CCC
CCCC
Map
CCC
Evaluation
CCCC EvalCoord
CCC The OpenGL graphics system diagram, Version 1.1. Copyright 1996 Silicon Graphics, Inc. All rights reserved.
CCCC
CCCC CCC
CCCC CCC
CCC
CCCC
Map
CCCC CCC
Enable/Disable
CCCC CCC
CCCC CCC
CCC
CCCC Current
CCC
CC CCC
EdgeFlag Edge
CC CCC
Flag
Enable/Disable
t 0
CC
CC CCC
CCC
CC CCC
TexCoord1 TexGen
CC CCC
OBJECT_LINEAR
CC CCC
TexGen b A*b
A
CC CCC
TexCoord2 r 0 EYE_LINEAR
CC CCC
TexGen
CC CCC
SPHERE_MAP
Texture
TexCoord3 q 1
CC CCC Matrix
CC Current
CCC TexGen
Stack
Vertices
TexCoord4 Texture
CC
CC
Coordinates
CC
CC
CC CC CC
Evaluators
CC
Color3 A 1 CC
CC CC
CC CC &
CC
CC CC
Enable/Disable
CC CC
Vertex Arrays
Convert
CC
Current
CC
ColorMaterial
CC CC
CC
CC
Color4 RGBA Material
RGBA to float
CC CC C CC
Color
CC CC C CC
Material
Texture Coordinate
CC CC C CC
Parameters
LightModel
Generation
CC CC C CC
Control Input
C&
Conversion C
C C
Current Begin/End
Convert
Index
CC CC C C
Color Light Light Material Light Model
index to float
CC CC C C
Index Enable/Disable Parameters Parameters Parameters
Current
CC
CC CC
CC C C
C C
Values Lighting
CC CC C C
Enable/Disable
CC CC C C
Clamp to
Primitive
C C
RGBA Lighting Equation [0,1]
Assembly
C
Convert
Current
CC CC
Normal3 normal coords b M*b Normalize Mask to
Normal M
CC CC
to float
Color Index Lighting Equation [0,2n−1]
CC
CC CC
CC CC
CC
Matrix
CC CC
M−T
Vertex2
z 0 Enable/Disable Control
CC CC
FrontFace
RasterPos2 M (Lighting) Clipping, Perspective,
CC CC and
Vertex3
CC CC Rasteriz−
CC CC Viewport Application
w 1
RasterPos3
CC CC
ation Texturing, Per−Fragment Operations
Vertex4 OBJECT M EYE Fog,
b M*b Feedback
RasterPos4 COORDINATES M COORDINATES and
& Frame Buffer
Antialiasing
Selection &
Pixels Frame Buffer Control
Rectangle
Rect
Generation
Primitives Fragments
Model View
Matrix
Stack
Key to OpenGL Operations
Enable/Disable
(Antialiasing/Stipple)
MatrixMode ClipPlane
Matrix FrontFace PolygonOffset
PushMatrix PolygonMode
Control CullFace
PopMatrix
b
M
M−Tb
LoadIdentity Polygon
Polygon Polygon
LoadMatrix Rasterization
Culling Mode
Projection Viewport
Clip
Matrix DepthRange Enable/Disable
M*N ShadeModel Planes
M Stack (Antialiasing)
N LineStipple
LineWidth
Polygon
MultMatrix POLYGONS Polygon
b M M*b View Volume
Clipping Divide Line
Clipping
Translate Flatshading Vertex Segment
Line Apply
Scale LINE Line Coordinates Rasterization
Matrix b M*b View Volume Viewport
SEGMENTS Clipping by TexParameter
Rotate Clipping Enable/Disable
Generators (Vertex w
Frustum Enable/Disable
Only) Point (Antialiasing) Enable/Disable Enable/Disable Enable/Disable
Ortho POINTS Point Enable/Disable Enable/Disable StencilOp Enable/Disable Enable/Disable Enable/Disable Enable/Disable
b M*b View Volume PointSize TexEnv Fog
RASTER POS. Culling Scissor AlphaFunc StencilFunc DepthFunc BlendFunc LogicOp
Culling
Coverage
Point Texel Texture Pixel Alpha Depth
Fog (antialiasing) Scissor Stencil Blending
Rasterization Generation Application Ownership Test Buffer Dithering Logic Op
Application Test Test (RGBA only)
Current Test (RGBA only) Test
Raster
Position
RenderMode
Clear
Clear
Control
Notes: Selection Feedback
Bitmap
Selection
Rasterization
1. Commands (and constants) are shown without the Encoding Control Encoding Masking
Readback
indeterminate. Control
4. This diagram is schematic; it may not directly
CopyPixels
correspond to any actual OpenGL implementation. ReadPixels
Pack
ReadBuffer
CopyTexImage
Pixels
CopyTexSubImage
glBegin ( GL_TRIANGLES ) ;
glColor4f (1.0 f ,1.0 f ,1.0 f ,1.0 f ) ;
glVertex3d (.5 ,.25 ,0.0) ;
glVertex3d (.25 ,.5 ,0.0) ;
glVertex3d (.25 ,.25 ,0.0) ;
glEnd () ;
glBegin ( GL_LINES ) ;
glColor3f (1.0 f ,0.0 f ,0.0 f ) ; glVertex3d (1.0 ,0.0 ,0.0) ;
glColor3f (0.0 f ,1.0 f ,0.0 f ) ; glVertex3d (0.0 ,1.0 ,0.0) ;
glMatrixMode ( GL_PROJECTION ) ;
glLoa dIdentit y () ;
glFrustum ( left , right , bottom , top , nearVal , farVal ) ; // option 1
glOrtho ( left , right , bottom , top , nearVal , farVal ) ; // option 2
gluPe rspectiv e ( fovy , aspect , zNear , zFar ) ; // option 3
glMatrixMode ( GL_MODELVIEW ) ;
glLoa dIdentit y () ;
gluLookAt ( eyeX , eyeY , eyeZ , centerX , centerY , centerZ , upX , upY , upZ ) ;
glRotatef ( angle ,x ,y , z ) ;
glTranslate (x ,y , z ) ;
glScale (x ,y , z ) ;
glMatrixMode ( GL_MODELVIEW ) ;
glLoa dIdentit y () ;
glPushMatrix () ; w o r l d T oH o u s e S p a c e () ; // house space
glPushMatrix () ; ho u s e T oD o o r S p a c e () ; // door space
glPushMatrix () ; d o o r T o Do o r n o b S p a c e () ; // doornob space
renderDoornob () ; // doornob space
glPopMatrix () ; // Door space
renderFrame () ; // Door space
glPopMatrix () // house space
... render rest of house
another example
void renderForest ()
{
glMatrixMode ( GL_MODELVIEW ) ;
glLoa dIdentit y () ;
for ( int i = 0; i <100; ++ i )
{
glPushMatrix () ;
glTranslatef (i ,0.0 ,0.0) ; // Push ourselves to row i
for ( int j = 0; j <100; ++ j )
{
glPushMatrix () ;
glTranslatef (0.0 , j ,0.0) ; // Push ourselves to row j
renderTree () ; // draw tree at position i , j
glPopMatrix () ;
}
glPopMatrix () ;
}
}
Questions?