opengl e-manual: unreal/theredbook
TRANSCRIPT
![Page 1: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/1.jpg)
OpenGLOpenGLE-Manual: http://fly.cc.fer.hr/~unreal/theredbook/
![Page 2: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/2.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
OpenGL
• What it is:– Software interface to graphics hardware– about 120 C-callable routines for 3D graphics– Hardware independent
• What it is not:– Not a windowing system (no window creation)– Not a UI system (no keyboard and mouse routines)– Not a 3D modeling system (Open Inventor, VRML,
Java3D)
![Page 3: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/3.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
OpenGL Functionality
• Simple geometric objects (e.g. lines, polygons, rectangles, etc.)
• Transformations, viewing, clipping
• Hidden line & hidden surface removal
• Color, lighting, texture
• Bitmaps, fonts, and images
• Immediate- & Retained- mode graphics
![Page 4: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/4.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
OpenGL Convention
• Functions:– prefix gl + capital first letter (e.g. glClearColor)
• Constants:– prefix GL + all capitals (e.g. GL_COLOR_BUFER_B
IT)
![Page 5: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/5.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
OpenGL Convention
• Many variations of the same functions– glColor[2,3,4][b,s,i,f,d,ub,us,ui](v)
• [2,3,4]: dimension• [b,s,i,f,d,ub,us,ui]: data type• (v): optional pointer (vector) representation
Example:glColor3i(1, 0, 0)orglColor3f(1.0, 1.0, 1.0)orGLfloat color_array[] = {1.0, 1.0, 1.0};glColor3fv(color_array)
![Page 6: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/6.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
OpenGL Basic Concepts
• OpenGL as a state machine
• Graphics primitives going through a “pipeline” of rendering operations
• OpenGL controls the state of the pipeline with many state variables (fg & bg colors, line thickness, texture pattern, eyes, lights, surface material, etc.)
• Binary state: glEnable & glDisable
• Query: glGet[Boolean,Integer,Float,Double]
![Page 7: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/7.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
Rendering Pipeline
viewingtransform
graphicsprimitives
modelingtransform clipping
shading &texture
projectionimages inInternal buffer
viewporttransform
images onscreen
Transformmatrix
Eye, lookat,headup
Parallel orPersepctivevolume
Material,lights, surface color
Viewportlocation
![Page 8: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/8.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
Points, Lines, Polygons
• Specified by a set of vertices– glVertex[2,3,4][s,i,f,d](v) (TYPE coords)
• Polygons:– simple, convex, no holes
• Grouped together by glBegin() & glEnd()
glBegin(GL_POLYGON)
glVertex3f( …)
glVertex3f( …)
glVertex3f( …)
glEnd
![Page 9: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/9.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
Primitives
• There is debate, similar to CISC vs. RISC, over how many primitives to support OpenGL takes the middle ground • Primitives specified by vertex calls (glVertex*) bracketed by glBegin(type) and glEnd() • Line-based primitives: Line segments (GL_LINES), Polylines (GL_LINE_STRIP)
![Page 10: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/10.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
Polygons
• Polygons are objects with a line loop border and an interior
• Polygons must be simple (no crossing edges), convex (the line segment between two interior points does not leave the polygon) and flat (planar)
• Triangles always obey these properties and are often better supported in hardware
![Page 11: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/11.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
Polygon Types
• Polygons (GL_POLYGON) successive vertices define line segments, last vertex connects to first
• Triangles and Quadrilaterals (GL_TRIANGLES, GL_QUADS) successive groups of 3 or 4 interpreted as triangles or quads
• Strips and Fans (GL_TRIANGLE_STRIP, GL_QUAD_STRIP, GL_TRIANGLE_FAN) joined triangles or quads that share vertices
![Page 12: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/12.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
Other Primitives
GL_TRIANGLE_STRIP012, 213, 234, 435, …
GL_QUAD_STRIP0132, 2354, 4576, …
![Page 13: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/13.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
Points, Lines, Polygons Details
• Points:– size: glPointSize(GLfloat size)
• Lines:– width: glLineWidth(GLfloat width)– stippled lines:
glLineStipple(GLint factor, GLushort pattern)
glEnable(GL_LINE_STIPPLE)
![Page 14: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/14.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
Points, Lines, Polygons Details
• glPolygonMode(face, mode)– face: GL_FRONT, GL_BACK, GL_FRONT_AND_B
ACK– mode: GL_POINT, GL_LINE, GL_FILL– default: both front and back as filled
![Page 15: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/15.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
Points, Lines, Polygons Details
• face culling– glEnable(GL_CULL_FACE)– glCullFace(mode)
• mode: GL_FRONT, GL_BACK, GL_FRONT_AND_BACK• outside: back-facing polygon not visible• inside: front-facing polygon not visible
![Page 16: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/16.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
Geometric Transform
• Step 1: Modeling transform– A global “world” coordinate system where one
constructs and manipulates models
glTranslate[f,d](x,y,z)glRotate[f,d](angle,x,y,z)glScale[f,d](x,y,z)
![Page 17: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/17.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
Geometric Transform• Step 2: Viewing transform
– Select the eye pos, look-at dir, head-up dir, and view volume
![Page 18: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/18.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
Geometric Transform• Step 3: Clipping
– Remove primitives that are not in the view volume
![Page 19: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/19.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
Geometric Transform
• Step 4: Projection– Map from 3D into 2D
![Page 20: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/20.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
Geometric Transform
• Step 5: Viewport transform– Map 2D images onto screen
![Page 21: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/21.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
Transform in OpenGL
• OpenGL uses stacks to maintain transformation matrices (MODELVIEW stack is the most important)
• You can load, push and pop the stack
• The current transform is applied to all graphics primitive until it is changed
![Page 22: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/22.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
General Transform Commands
• Specify current matrix– glMatrixMode(GLenum mode)
• GL_MODELVIEW, GL_PROJECTION, GL_TEXTURE
• Initialize current matrix– glLoadIdentity(void)– glLoadMatrix[f,d](const TYPE *m)
![Page 23: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/23.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
General Transform Commands
• Concatenate current matrix– glMultMatrix(const TYPE *m)– C = CM
• Caveat: OpenGL matrices are stored in column major
• Best use utility functions glTranslate, glRotate, glScale
![Page 24: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/24.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
Step 1: Modeling TransformglTranslate[f,d](x,y,z)
glRotate[f,d](angle,x,y,z)
glScale[f,d](x,y,z)
Order is important
![Page 25: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/25.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
Step 1: Modeling Transform
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMultiMatrixf(T);
glMultiMatrixf(R);
draw_the_object(v);
v’ = ITRv
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();glMultiMatrixf(R);glMultiMatrixf(T);draw_the_object(v);v’ = IRTv
![Page 26: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/26.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
Modeling Transform (global)
• Usually, think there is a global “world” coordinate system where– all objects are defined– rotation, translation, scaling of objects in the w
orld system– order is reverse (backward)
![Page 27: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/27.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
Two Different Views
• As a global system• object moves but coordinates stay the same• apply in the reverse order
• As a local system• object moves and coordinates move with it• applied in the forward order
![Page 28: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/28.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
glLoadIdentity();glMultiMatrixf(T);glMultiMatrixf(R);draw_the_object(v);•Global view
•Roate object•Then translate
•Local view•Translate object•Then rotate
![Page 29: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/29.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
glLoadIdentity();glMultiMatrixf(R);glMultiMatrixf(T);draw_the_object(v);Global view
Translate objectThen rotate
Local viewRotate objectThen translate
![Page 30: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/30.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
glLoadIdentity();glRotate(90,0,0,1)glTranslate(1,0,0);glRotate(45,0,0,1)glTranslate(1,0,0);draw_the_object(v);
)2
11,
2
1(
)2
11,
2
1(
(0,1)
(0,0) (1,0)(0,0)
)2
1,
2
1(
)2
1,
2
11(
![Page 31: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/31.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
glLoadIdentity();glRotate(90,0,0,1)glTranslate(0,1,0);glScale(2,0.5,0)glTranslate(1,0,0);draw_the_object(v);
Caveats: scale in local view may distort coordinate systems!!
(-1,1)
(1,1) (-1,1)
(0,-0.5)
(-1,-1)
(1,0)(0.5,0)(-0.5,0) (-0.5,0)
(-1,-0.5)
A movement of 2 instead of 1!
![Page 32: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/32.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
More on Rotation
11000
0sin0cos
0010
0cos0sin
1
`
`
`
z
y
x
z
y
x
11000
0100
00cossin
00sincos
1
`
`
`
z
y
x
z
y
x
11000
0cossin0
0sincos0
0001
1
`
`
`
z
y
x
z
y
x
![Page 33: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/33.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
)()()()()( zyxyz RRRRR
![Page 34: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/34.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
Hierarchical TransformUsed very frequently for building complex objects in a mod
ular mannerSubroutine callsBe able to push and pop transform matrices as neededOpenGL provides two stacks
at least 32 4x4 model view matricesat least 2 4x4 projection matrices
glLoadMatrix(), glMultMatrix(), glLoadIdentity() affect top-most (current) one
![Page 35: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/35.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
Hierarchical Transform
glPushMatrix(void)topmost matrix is copied (top and second-from-top)
glPopMatrix(void)topmost is gone (second-from-top becomes top)
Very important
For OpenGL beginnerTransformation orderingTransformation grouping
![Page 36: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/36.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
Hierarchical Transform
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(T);
draw_ car_body();
glPushMatrix();
glTranslatef(T1);
draw_wheel();
glPopMatrix();
glPushMatrix();
glTranslatef(T2);
draw_wheel();
glPopMatrix();
![Page 37: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/37.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
Hierarchical Transform
![Page 38: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/38.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
Viewing Transform
Default: eyes at origin, looking along -Z
Important parameters:–where is the observer (camera)?
• origin of the viewing system– What is the look-at direction?
• -z direction– What is the head-up direction?
• y direction
![Page 39: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/39.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
Viewing TransformgluLookAt (GLdouble eyex, eyey, eyez,
GLdouble centerx, centery, centerz,
GLdouble upx, upy, upz)
![Page 40: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/40.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
Viewing Transform eye and center: local w(z) direction up and local w(z): local v(y) direction local v(y) and w(z) directions: local u(x) direction
![Page 41: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/41.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
Viewing Transform - gluLookAt
Occur after modeling transformUsually involves only translation + rotation (no scal
ing)Best done by gluLookAt functionCan also be done using glTranslate + glRotate (need
to think about moving the camera instead of object in opposite way)
![Page 42: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/42.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
Viewing Transform - the hard way
Use gluLookAt if possibleThink in an object-centered way (forward)Camera is at the origin pointing along -zRotate and translate objects to expose the right view
![Page 43: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/43.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
glMatrixMode glLoadIdentity glRotateZ(-roll) glRotateX(-pitch) glRotateY(-heading) glTranslate(-px,-py,-pz) // Other modeling transform glBegin draw_car(); glEnd
(px,py,pz)
![Page 44: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/44.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
Shading and Texturing
A BIG topic in graphicsFor photo realistic renderingTwo aspects: geometry (location and orientation)
and appearance (color, shading, texture)Here we concentrate on geometry only
![Page 45: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/45.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
Perspective Projection
glMatrixMode(GL_PROJECTION);glLoadIdentity();glFustrum(GLDouble left, right, bottom, top, near, f
ar);
![Page 46: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/46.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
Perspective ProjectiongluPerspective(GLdouble fovy, aspect, near, far) -- f
or symmetric view volume
![Page 47: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/47.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
Perspective Projection
h
waspect
near
h
)2
(tan2 1
![Page 48: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/48.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
Example
![Page 49: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/49.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
Parallel (Orthographical) Projection
No perspective foreshortening
– sizes and angles can be measured and comparedUseful for engineering drawing
– top, front, side views
![Page 50: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/50.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
Parallel (Othographic) Projection
glOrtho(GLdouble left, right, bottom, top, near, far)
![Page 51: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/51.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
Clipping
Get rid of the things that are not seen
To do things efficiently require some mathematical twiddling
![Page 52: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/52.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
Viewport Transform
glViewport(GLint x, y, GLsizei width, height); The internal buffer is mapped to the rectangle specified by (x,
y) lower left corner of size width and height
![Page 53: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/53.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
Viewport TransformMultiple buffers can be mapped to a single windo
w (if they have different viewports)Distortion may occur if viewport does not have the
right aspect ratiogluPerspective(fovy, 1.0, near, far)glViewport(0,0,400,400)
gluPerspective(fovy, 1.0, near, far)glViewport(0,0,400,200)
![Page 54: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/54.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
Other OpenGL Basics
Create a drawing buffer (not a screen window)Clear bufferDraw to bufferLink buffer to screen window displayInteraction (expose, resize, mouse, keyboard input,
etc).
![Page 55: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/55.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
OpenGL BuffersRectangular arrays of pixelsColor buffers: front-left, front-right, back-left, back-right
At least one, Color indexed or RGBAStereoscopic systems have left and rightDoubled buffered systems have front and back
![Page 56: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/56.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
OpenGL Buffers Color Indexed Buffer
![Page 57: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/57.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
OpenGL Buffers RGBA Buffer
![Page 58: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/58.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
Other OpenGL Buffers
• Depth buffers:– for determining hidden surface effects– Z buffer
• Stencil buffers:– acts like a cardboard stencil (“windshield effect”)
– 15 bits Z buffer + 1 bit stencil buffer or– 24 bits Z buffer + 8 bit stencil buffer
• Accumulation buffers:– for accumulating multiple images into one (e.g.
for anti-aliasing, motion blur)
![Page 59: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/59.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
OpenGL Buffer Operations
• Draw– glDrawBuffer(GLenum mode)
enabled for writing or clearing GL_FRONT, GL_BACK, GL_RIGHT, GL_LEFT
GL_FRONT_RIGHT, GL_FRONT_LEFT
GL_BACK_RIGHT, GL_BACK_LEFT
GL_AUXI, GL_FRONT_AND_BACK,
GL_NONE
![Page 60: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/60.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
OpenGL Buffer Operations
• Clear– glClear[Color,Index,Depth,Stencil,Accum]
• glClearColor(0.0,0.0,0.0,0.0);
• glClearDepth(1.0);
• Set clear color, depth values
– glClear(GLbitfield mask)• GL_COLOR_BUFFER_BIT
• GL_DEPTH_BUFFER_BIT
• GL_STENCIL_BUFFER_BIT
• GL_ACCUM_BUFFER_BIT
![Page 61: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/61.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
OpenGL Misc. Functions
• Forced completion– glFlush(void)
• asynchronous
– glFinish(void)• synchronous
– One of them should be called at the end of each frame
![Page 62: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/62.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
Color
• FLAT vs. SMOOTH
glShadeModel(GL_FLAT);
glShadeModel(GL_SMOOTH)
• Color Specification (affects subsequent primitives)
glBegin(GL_TRIANGLES);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(1.0, 0.0, 0.0);
glColor3f(0.0, 1.0, 0.0);
glVertex3f(0.0, 1.0, 0.0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(0.0, 0.0, 1.0);
glEnd();
![Page 63: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/63.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
Depth (z-buffer)
• For Hidden Surface Removal
glutInitDisplayMode(GLUT_DEPTH | ….);
glEnable(GL_DEPTH_TEST);
glClear(GL_DEPTH_BUFFER_BIT | ….);
![Page 64: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/64.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
Shading
• Need gradient of color to realistically represent 3-D objects
• Simulate the interaction between light sources and surfaces
• Recursive interaction of light between surfaces embodied in an integral rendering equation
• Unsolvable in practice so use approximations
• Consider rays from light sources that eventually strike the viewing plane
![Page 65: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/65.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
Light and Matter
• The reflectance, absorption and transmission properties of a surface interact with light to determine appearance
• Surface Types:– Specular (shiny) - reflected light is scattered in a narrow range of
angles. Perfectly specular (mirror) reflect in a single direction– Diffuse (matt) - reflected light is scattered in all directions. Perfect
ly diffuse appears the same to all viewers– Translucent - allows light to penetrate, be refracted and emergeels
ewhere. Examples: glass, water
![Page 66: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/66.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
Light Sources
• Each source has an RGB intensity (luminance) Types:
– Ambient - uniform lighting scattered in all directions and identical throughout the scene– Point Sources - emit light in all directions from a point. Intensity often scaled inversely with distance. Create un- realistically stark shadows– Spotlights - emit light in a limited range of angles(a cone). Intensity tails off from the cone centre to its edge– Distant Light Sources - replace location of light source with a constant direction for efficiency
![Page 67: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/67.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
Lighting and shading
• Phong model, Gouraud shading• Three steps: – Enable lighting – Specify the lights – Specify the materials• Advanced user: – local/infinite viewpoint – two sided lighting
![Page 68: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/68.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
Simple lighting
•Enable lighting: glEnable(GL_LIGHTING); glEnable(GL_LIGHT0);•Specify light sources parameters:glLightfv(GL_LIGHT0,GL_AMBIENT, light_ambient);glLightfv(GL_LIGHT0,GL_POSITION, light_pos);glLightfv(GL_LIGHT0,GL_DIFFUSE, light_dif);
•Plus global ambient lightglLightModelfv(GL_LIGHT_MODEL_AMBIENT, l_ambient);
![Page 69: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/69.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
Light Sources in OpenGL
• GLfloat light0_pos[] = {1.0, 2.0, 3.0, 1.0};• GLfloat light1_dir[] = {1.0, 2.0, 3.0, 0.0};• GLfloat lambient[] = {1.0, 0.0, 0.0, 1.0};• glEnable(GL_LIGHTING);• glEnable(GL_LIGHT0);• glLightfv(GL_LIGHT0, GL_POSITION, light0_pos);• glLightfv(GL_LIGHT0, GL_AMBIENT, lambient);• glLightfv(GL_LIGHT0, GL_DIFFUSE, ldiffuse);• glLightfv(GL_LIGHT0, GL_SPECULAR, lspecular);
• Light0 is a point light source (w = 1)• Light1 is a distant light source (w = 0) • ldiffuse and lspecular are similar 4 element arrays to lambient• Must enable lighting and the particular light sources (0-7)• Lighting setup:• glLightfv(source, paramter, pointer_to_array)• glLightf(source, parameter, value)• Variations allow attenuation, one or two sided lighting and spotlight settings
![Page 70: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/70.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
Normal Vectors
• Define normal vectors for each vertex of every polygons• These normal vectors determine the orientation of the polyg
on relative to the light sources. glBegin(GL_TRIANGLES); glNormal3f(0.0, 0.0, 1.0);
glVertex3f(0.0, 0.0, 0.0); glNormal3f(0.0, 0.0, 1.0); glVertex3f(1.0, 0.0, 0.0);
glNormal3f(0.0, 0.0, 1.0); glVertex3f(0.0, 1.0, 0.0); glEnd();
![Page 71: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/71.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
Specifying materials
• One function call for each property: glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,mat_amb);glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE, mat_diff);glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,mat_spec)glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,100.0);glMaterialfv(GL_FRONT_AND_BACK,GL_EMISSION,mat_emi);
![Page 72: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/72.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
Materials in OpenGL
GLfloat mambient[] = {0.2, 0.2, 0.2, 1.0};glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mambient);glMaterialfv(GL_FRONT, GL_DIFFUSE, mdiffuse);glMaterialfv(GL_FRONT, GL_SPECULAR, mspecular);glMaterialfv(GL_FRONT, GL_SHININESS, 100.0);
•mdiffuse and mspecular are similar 4 element arraysto lambient•Material setup: glMaterialfv(face, type, pointer_to_array) glMaterialf(face, type, value)•Variations allow light emmiting surfaces Before each glVertex3f it is necessary to set glNormal3f to ensure correct lighting
![Page 73: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/73.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
![Page 74: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/74.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
Texturequadratic=gluNewQuadric(); // Create A Pointer To The QuadricgluQuadricOrientation(quadratic,GLU_OUTSIDE);gluQuadricNormals(quadratic, GLU_SMOOTH); // Create Smooth NormalsgluQuadricTexture(quadratic, GL_TRUE); // Create Texture
CoordsgluQuadricDrawStyle(quadratic,GLU_FILL);glBindTexture(GL_TEXTURE_2D,texture.texID);gluSphere(quadratic,15.0f,15,15);
void glBindTexture( GLenum target, GLuint texture ) target specifies the target. Must be either GL_TEXTURE_1D or GL_TEXTURE_2D. texture specifies the name of a texture.
void gluSphere(GLUquadricObj *qobj, GLdouble radius, GLint slices, GLint stacks) qobj specifies the quadrics object (created with gluNewQuadric).radius specifies the radius of the sphere. slices specifies the number of subdivisions around the z axis (similar to lines of longitude).stacks specifies the number of subdivisions along the z axis (similar to lines of latitude).
![Page 75: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/75.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
TGALoader
#include "Texture.h“int LoadTextures() { int Status = FALSE; if(LoadTGA(&texture, “xxx.tga)) {
Status = TRUE;glGenTextures(1, &texture.texID);glBindTexture(GL_TEXTURE_2D, texture.texID);glTexImage2D(GL_TEXTURE_2D, 0, 3, texture.width, texture.height,0,GL_RGB, GL_UNSIGNED_BYTE, texture.imageData);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);if (texture[i].imageData) free(texture[i].imageData);
} return Status;}
![Page 76: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/76.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
Helper Libraries
• Remember that OpenGL does not do– windowing, GUI, and modeling
• A real application will need all the above
• At least two choices– GLUT (GL Utility Library) or aux (obsolete)
• simple windowing, GUI and models
![Page 77: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/77.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
OpenGL-Related Libraries
• GLU (prefix glu-)– utility library
• GLUT (prefix glut-)– windowing, input, simple objects (OpenGL1.1
and later, replacing aux- )
![Page 78: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/78.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
OpenGL-Related Libraries
• Open Inventor– objects + methods of interaction
– creating + editing 3D scenes
– data format exchange
![Page 79: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/79.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
GL Utility Library (glut)
• Convenient and easy-to-use
• For– specifying the display mode
– creating window (size and location)
– handling window and input events
– convenient objects
• Replaced aux library after version 1.1
![Page 80: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/80.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
GL Utility Library (glut)
• glutInit(int argc, char **argv)– initialize glut, process command line arguments such as -geo
metry, -display, etc.
• glutInitDisplayMode(unsigned int mode)– Mode for later glutCreateWindow() call– GLUT_RGBA or GLUT_INDEX– GLUT_SINGLE or GLUT_DOUBLE– Associated GLUT_DEPTH, GLUT_STENCIL,
GLUT_ACCUM buffers– default: RGBA & SINGLE
![Page 81: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/81.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
GL Utility Library (glut)
• glutInitWindowPosition(int x, int y)• glutInitWindowSize(int width, int height)
– window location and size
• glutCreateWindow(char *name)– after Init, Displaymode, Position, and Size calls– will not appear until glutMainLoop
![Page 82: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/82.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
GL Utility Library (glut)
• glutDisplayFunc(void (*func) (void))– display function for initial display, de-iconfy
and expose
• glutReshapeFunc(void (*function) (width, height))– called when window is resized or moved
– default glViewport(0,0,width,height)
![Page 83: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/83.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
GL Utility Library (glut)
• glutKeyboardFunc(void *(func) (unsinged int key, int x, int y)• glutMouseFunc(void *(func) (int button, int state, int x, int y))
– button: GLUT_{LEFT,MIDDLE,RIGHT}_BUTTON mode: GLUT_UP, GLUT_DOWN
• glutMotionFunc(void *(func) (int x, int y))– mouse pointer move while one or more mouse buttons is presse
d• glutIdleFunc(void *(func) (int x, int y))
![Page 84: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/84.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
GL Utility Library (glut)
• glut{Wire,Solid}Sphere()• glut{Wire,Solid}Cube()• glut{Wire,Solid}Box()• glut{Wire,Solid}Torus()• glut{Wire,Solid}Cylinder()• glut{Wire,Solid}Cone()• glut{Wire,Solid}Teapot()• glut{Wire,Solid}(Octahedron,Tetrahedron, Dodecahedron,Icosahedron)• centered at the origin
![Page 85: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/85.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
GL Utility Library (glut)
• glutMainLoop (void)– GLUT main loop, never returns
![Page 86: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/86.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
GLUT Example
#include <GL/glut.h>
#include <stdlib.h>
![Page 87: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/87.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
void init(void){ glClearColor (0.0, 0.0, 0.0, 0.0); glShadeModel (GL_FLAT);}void display(void){ glClear (GL_COLOR_BUFFER_BIT); glColor3f (1.0, 1.0, 1.0); glLoadIdentity (); /* clear the matrix */ /* viewing transformation */ gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); glScalef (1.0, 2.0, 1.0); /* modeling transformation*/ glutWireCube (1.0); glFlush ();}
![Page 88: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/88.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
void reshape (int w, int h){ glViewport (0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode (GL_PROJECTION); glLoadIdentity (); glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0); glMatrixMode (GL_MODELVIEW);}void keyboard(unsigned char key, int x, int y){ switch (key) { case 27: exit(0); break; }}
![Page 89: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/89.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
int main(int argc, char** argv){ glutInit(&argc, argv); glutInitDisplayMode (GLUT_SINGLE |GLUT_RGB); glutInitWindowSize (500, 500); glutInitWindowPosition (100, 100); glutCreateWindow (argv[0]); init (); glutDisplayFunc(display); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutMainLoop(); return 0;}
![Page 90: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/90.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
Selection
• Allow a user of your application to pick an object drawn on the screen.
• Once you're in selection mode the contents of the frame buffer don't change until you exit selection mode.
• When you exit, OpenGL returns a list of the primitives that would have intersected the viewing volume.
• The viewing volume is defined by the current modelview and projection matrices.
• Each primitive that intersects the viewing volume causes a selection hit.
• The list of primitives is actually returned as an array of integers called the hit records.
![Page 91: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/91.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
Basic steps
1. Specify the array to be used for the returned hit records with glSelectBuffer().
2. Enter selection mode by specifying GL_SELECT with glRenderMode().
3. Initialize the name stack using glInitNames() and glPushName().
4. Define the viewing volume you want to use for selection. Usually, this is different from the viewing volume you used to draw the scene originally, so you probably want to save and then restore the current transformation state with glPushMatrix() and glPopMatrix().
5. Alternately issue primitive drawing commands to manipulate the name stack so that each primitive of interest has an appropriate name assigned. glLoadName()
6. Exit selection mode by specifying GL_RENDER with glRenderMode() and process the returned selection data (the hit records).
![Page 92: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/92.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
Creating the Name Stack
GLuint selectBuf[BUFSIZE];GLint hits;glSelectBuffer(BUFSIZE, selectBuf)(void) glRenderMode(GL_SELECT);glInitNames();glPushName(-1);glPushMatrix(); /* save the current transformation state */ /* create your desired viewing volume here */ glLoadName(1); drawSomeObject(); glLoadName(2); drawAnotherObject(); glLoadName(3); drawYetAnotherObject(); drawJustOneMoreObject();glPopMatrix ();glFulsh();Hits=glRenderMode(GL_RENDER);
![Page 93: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/93.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
Creating the Name Stack
• Calls to glPushName(), glPopName(), and glLoadName() are ignored if you're not in selection mode.
• It simplifies your code to use these calls throughout your drawing code, and then use the same drawing code for both selection and normal rendering modes.
![Page 94: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/94.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
The Hit Record
Each hit record consists of four items, in order:
1. The number of names on the name stack when the hit occurred.
2. Both the minimum and maximum window-coordinate z values of all vertices of the primitives that intersected the viewing volume since the last recorded hit. These two values, which lie in the range [0,1], are each multiplied by 232-1 and rounded to the nearest unsigned integer.
3. The contents of the name stack at the time of the hit, with the bottommost element first.
![Page 95: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/95.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
Picking
• As an extension of the selection
• Using the special picking matrix by calling gluPickMatrix(x,y,width,height,viewport[])
• Objects that are drawn near the cursor cause selection
![Page 96: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/96.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
#include <GL/gl.h>#include <GL/glu.h>#include <GL/glut.h>int board[3][3]; /* amount of color for each square *//* Clear color value for every square on the board */void myinit(void){ int i, j; for (i = 0; i < 3; i++) for (j = 0; j < 3; j ++) board[i][j] = 0; glClearColor (0.0, 0.0, 0.0, 0.0);}void drawSquares(GLenum mode){ GLuint i, j; for (i = 0; i < 3; i++) { if (mode == GL_SELECT) glLoadName (i); for (j = 0; j < 3; j ++) { if (mode == GL_SELECT) glPushName (j); glColor3f ((GLfloat) i/3.0, (GLfloat) j/3.0, (GLfloat) board[i][j]/3.0); glRecti (i, j, i+1, j+1); // 3D coordinate if (mode == GL_SELECT) glPopName (); } }}
![Page 97: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/97.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
void processHits (GLint hits, GLuint buffer[]){ unsigned int i, j; GLuint ii, jj, names, *ptr; printf ("hits = %d\n", hits); ptr = (GLuint *) buffer; for (i = 0; i < hits; i++) { /* for each hit */ names = *ptr; printf (" number of names for this hit = %d\n", names); ptr++; printf (" z1 is %u;", *ptr); ptr++; printf (" z2 is %u\n", *ptr); ptr++; printf (" names are "); for (j = 0; j < names; j++) { /* for each name */ printf ("%d ", *ptr); if (j == 0) /* set row and column */ ii = *ptr; else if (j == 1) jj = *ptr; ptr++; } printf ("\n"); board[ii][jj] = (board[ii][jj] + 1) % 3; }}
![Page 98: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/98.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
#define BUFSIZE 512void pickSquares(int button, int state, int x, int y){ GLuint selectBuf[BUFSIZE]; GLint hits; GLint viewport[4]; if (button != GLUT_LEFT_BUTTON || state != GLUT_DOWN) return; glGetIntegerv (GL_VIEWPORT, viewport); glSelectBuffer (BUFSIZE, selectBuf); (void) glRenderMode (GL_SELECT); glInitNames(); glPushName(-1); glMatrixMode (GL_PROJECTION); glPushMatrix (); glLoadIdentity (); /* create 5x5 pixel picking region near cursor location */ gluPickMatrix((GLdouble) x, (GLdouble) (viewport[3] - y), 5.0, 5.0, viewport); gluOrtho2D (0.0, 3.0, 0.0, 3.0); drawSquares (GL_SELECT); glPopMatrix (); glFlush (); hits = glRenderMode (GL_RENDER); processHits (hits, selectBuf);}
![Page 99: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/99.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
void display(void){ glClear(GL_COLOR_BUFFER_BIT); drawSquares (GL_RENDER); glFlush();}void myReshape(GLsizei w, GLsizei h){ glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D (0.0, 3.0, 0.0, 3.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity();}int main(int argc, char** argv){ glutInit(&argc, argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGBA); glutInitWindowPosition (100, 100); glutInitWindowSize (100, 100]); glutCreateWindow(argv[0]); myinit (); glutMouseFunc (pickSquares); glutReshapeFunc (myReshape); glutMainLoop(display);}
![Page 100: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/100.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
Picking with Multiple Names and a Hierarchical Model
• Multiple names can also be used to choose parts of a hierarchical object in a scene.
• If you were rendering an assembly line of automobiles, you might want the user to move the mouse to pick the third bolt on the left front tire of the third car in line.
• A different name can be used to identify each level of hierarchy: which car, which tire, and finally which bolt.
![Page 101: OpenGL E-Manual: unreal/theredbook](https://reader033.vdocuments.net/reader033/viewer/2022061522/56649eb05503460f94bb5d77/html5/thumbnails/101.jpg)
CVVR Lab.@ National Dong Hwa Univ. 國立東華大學
Possible name-stack return values
2 z1z2 2 1 //Car 2, wheel 1
1 z1z2 3 //Car 3 body
3 z1z2 1 1 0 //Bolt 0 on wheel 1 on car 1