Title: OpenGL Functions
1OpenGL Functions
2Outline
- Multiple GLUT Display Windows
- Attributes of Graphics Primitives
- OpenGL Geometric Transformations
- Demo
3Multiple GLUT Display Windows
- GLUT Display-Window Identifier
- Managing Multiple GLUT Display Windows
- GLUT Subwindows
4GLUT Display-Window Identifier
int main (int argc, char argv) glutInit(argc
, argv) glutInitDisplayMode(GLUT_DOUBLE
GLUT_RGB) glutInitWindowSize(250,
250) glutInitWindowPosition(100,
100) glutCreateWindow(argv0) init() glu
tDisplayFunc(display) glutMouseFunc(mouse) gl
utMainLoop() return 0
5GLUT Display-Window Identifier
int main (int argc, char argv) glutInit(argc
, argv) glutInitDisplayMode(GLUT_DOUBLE
GLUT_RGB) glutInitWindowSize(250,
250) glutInitWindowPosition(100, 100) int
windowID glutCreateWindow(argv0) init()
glutDisplayFunc(display) glutMouseFunc(mouse)
glutMainLoop() return 0
6GLUT Display-Window Identifier
- Each window is assigned a display-window
identifier - Positive integer start with the value 1
- Delete a display window
WindowID glutCreatWindow (A Display Window)
Display-Window Identifier
glutDestroyWindow (windowID)
7Example
glutInit(argc, argv) glutInitDisplayMode(GLUT_SI
NGLE GLUT_RGB ) glutInitWindowPosition(50,
50) glutInitWindowSize(300, 300) int windowID
glutCreateWindow( "Example") init() glutDispla
yFunc( display ) int windowID2
glutCreateWindow( "Example 2") init() glutDispl
ayFunc( display2 ) glutPositionWindow (50, 400)
8Current GLUT Display Window
- Specify display-window to the current display
window - The last display window
- The one you select
- Query the current display window
- ? 0 no display windows or the current display
window was destroyed
glutSetWindow (windowID)
currentWindowID glutGetWindow()
9Relocating and Resizing a GLUT Display Window
- Reset the screen location
- Reset the size
- Expand to fill the screen
glutPositionWindow (xNewTopLeft, yNewTopleft)
glutReshapeWindow (dwNewWidth, dwNewHeight)
glutFullScreen()
- The aspect ration may change and object may be
distorted - glutReshapeFunc (winReshapeFcn) // GLUT routine
10Managing Multiple GLUT Display Windows
- Several routines for manipulating a display window
glutSetWindowTitle(New Window Name)
glutSetWindow (windowID) glutPopWindow () //
in front of all other windows
glutSetWindow (windowID) glutPushWindow ()
// push to the back
glutHideWindow ()
glutShowWindow ()
11GLUT Subwindows
- Within a selected display window
- Can set up any number of second-level display
windows, called subwindows - Screen-cursor shape
glutCreatSubWindow (windowID, xBottomLeft,
yBottomLeft, width, height)
GLUT_CURSOR_UP_DOWN GLUT_CURSOR_CYCLE GLUT_CURSOR_
WAIT CLUT_CURSOR_DESTORY
glutSetCursor (shape)
12Result Two Subwindows
? The parent window should also be initiated and
defined the glutDisplayFunc.
13Attributes of Graphics Primitives
- OpenGL State Variables
- OpenGL Color Functions
- Attributes of Primitives
- Fill-Area
14Attributes of Graphics Primitives
- Attribute parameter
- A parameter that affects the way a primitive is
to be displayed, ex color, size. - State system / State machine
- A graphics system that maintains a list for the
current values of attributes
15OpenGL State Variables
- All OpenGL state parameters have default values.
- Changing the attribute settings
- Only affects those primitives that are specified
after the OpenGL state is changed - Lines blue or orange, dotted or dashed, and fat
or thin.
16Color and Gray Scale
- Color information can be stored in two ways
- 1. RGB color codes
- 2. Color table
17Color and Gray Scale
- Gray scale
- Using RGB color functions to set shades of gray
- R value G value B value
- Other color parameters
- Printers ? cyan, magenta, yellow
- Intensity ? light energy radiating in a direction
over a period of time - Luminance ? characterize the perceived brightness
18OpenGL Color Functions
- Set color display mode
-
- 1. OpenGL RGB and RGBA mode
-
- 2. OpenGL Color-Index Mode
glutInitDisplayMode ( GLUT_SINGLE GLUT_RGB )
GLUT_RGBA
GLUT_INDEX
glColor (colorComponents)
// Select current color
glIndexi(196) glutSetColor (index, red, green,
blue)
// Select current color
// Specify color into a table
19OpenGL Color Functions
- OpenGL color blending
- Combining the color of overlapping objects
- Blending an object with the background
- Color-blending function
glEnable(GL_BLEND)
glDisable(GL_BLEND)
Destination
? Blending methods can be performed only in RGB
or RGBA mode.
20OpenGL Color Functions
(SrRsDrRd, SrGsDgGd, SbBsDbBd, SaAsDaAd)
glBlendFunc (sFactor, dFactor)
Default GL_ONE GL_ZERO
(1.0, 1.0, 1.0, 1.0)
(0.0, 0.0, 0.0, 0.0)
Ex. glBlendFunc(GL_SRC_ALPHA,GL_ONE)
Ex implement it by using glColor4f (0.8, 0.2,
0.2, 0.4)
Destination
21Point Attributes
- Attributes
- Color
- Size a square block of pixels
- Function
glPointSize(size)
22Line Attributes
- Attributes
- Color
- Width
- Style
- Solid lines, dashed lines, and dotted lines
glColor()
glLineWidth(width)
23Line Attributes
- Pixel mask
- A pattern of binary digits
- For example, 11111000
24Line Attributes
- OpenGL line-style Function
glLineStippel(repeatFactor, pattern)
How many times each bit is to be repeat
16-bit integer Default 0xFFFF ? Solid
line Lower-order bits are applied first
glEnable(GL_LINE_STIPPLE)
25OpenGL Fill-area
- Filling convex polygons in four steps
- 1. Define a fill pattern
- 2. Invoke the polygon-fill routine
- 3. Activate the polygon-fill feature of OpenGL
- 4. Describe the polygons to be filled
26OpenGL Fill-area
- OpenGL wire-frame methods
glColor3f( 1.0, 0.0, 0.0 ) //red glPolygonMode(
GL_FRONT, GL_FILL ) // GL_FILL, GL_LINE,
GL_POINT glBegin( GL_POLYGON ) //draw solid
polygon glVertex2i( 10, 10 ) glVertex2i(
100, 10 ) glVertex2i( 150, 50 )
glVertex2i( 100, 100 ) glVertex2i( 50, 80 )
glVertex2i( 10, 10 ) glEnd()
27OpenGL Geometric Transformations
- Manipulating the Matrix Stacks
- OpenGL Viewing-Transformation Function
- Orthogonal-projection
- Perspective-projection
28Basic OpenGL Geometric Transformations
- Translation matrix
- Rotation matrix
- Scaling matrix
glTranslate (tx, ty, tz)
glTranslatef (25.0, -10.0, 0.0)
glRotate (theta, vx, vy, vz)
glRotatef (90.0, 0.0, 0.0, 1.0)
glScale (sx, sy, sz)
glScalef (2.0, 3.0, 1.0)
29OpenGL Matrix Operations
- Specify the modelview mode
- Modelview matrix store and combine the geometric
transformations - Define a 4 by 4 modelview matrix as the current
matrix - A call to a transformation routine
- ? Generate a matrix that is multiplied by the
current matrix for that mode
glMatrixMode (GL_MODELVIEW) // defaule
30Manipulating the Matrix Stacks
- Modelview and Projection Matrix Stacks
- Useful for constructing hierarchical models
a b c d e f g h i j k l m n o p
a b c d e f g h i j k l m n o p
a b c d e f g h i j k l m n o p
current matrix copy
a b c d e f g h i j k l m n o p
a b c d e f g h i j k l m n o p
Modelview Matrix stack (32 4x4 matrices)
Projection Matrix stack (2 4x4 matrices)
- OpenGL may support more than 32 matrices for
Modelview - More than 2 for Projection
? Query the number of matrices
glGetIntegerv(GL_MAX_MODELVIEW_STACK_DEPTH) glGet
Integerv(GL_MAX_PROJECTION_STACK_DEPTH)
31Manipulating the Matrix Stacks
- Draw a car body
- Pushing and popping
- Other
// remember where your are
void glPushMatrix (void)
void glPopMatrix (void)
// go back to where you were
void glLoadIdentity ()
32OpenGL Three-Dimensional Viewing Function
- OpenGL Utility library (GLU) includes functions
- Specifying the three-dimensional viewing
parameters - Setting up a symmetric perspective-projection
transformation - OpenGL Viewing-Transformation Function
- The viewing matrix is combined with any geometric
transformations
glMatrixMode (GL_MODELVIEW)
33OpenGL Viewing-Transformation Function
- Specifying viewing parameters
gluLookAt (eyex, eyey, eyez, cenerx, centery,
centerz,
upx, upy, upz)
The origin of the Viewing reference frame
View-up vector
Viewing Reference Position
Default gluLookAt(0.0, 0.0, 0.0, 0.0, 0.0,
-100, 0.0., 1.0,
0.0)
? Projection Reference Point
Viewing-coordinate Origin ? View Plane
Near clipping plane
34OpenGL Viewing-Transformation Function
- Once designate the viewing parameters
- A matrix is formed and concatenated with the
current matrix - Before you issue the transformation commands
(parallel and perspective)
glMatrixMode (GL_PROJECTION) glLoadIdentity()
//affect the projection matrix
35OpenGL Orthogonal-Projection Function
- Set the projection mode
- Orthogonal-projection (Parallel Projection)
glMatrixMode (GL_Projection)
glOrtho (left, right, bottom, top, near, far)
top
left
far
right
Toward the viewpoint
near
bottom
36OpenGL Symmetric Perspective-Projection Function
- OpenGL general perspective-projection function
- Symmetric-perspective
- Oblique-perspective projective
glFrustum( left, right, bottom, top, near, far)
top
left
right
bottom
near
far
37OpenGL Symmetric Perspective-Projection Function
- Advanced
- More intuitive to use
glPerspective( fovy, aspect, near, far)
Field of view angle
Aspect ration (w/h)
- dnear and dfar cant be behind the viewing
position - Must be assigned position numerical values
w
fovy the angle between the top and bottom
clipping planes, range 0.0, 180.0
h
fovy
near
far
38OpenGL Viewport and Display Windows
- Viewport Transformation
- The viewport is the rectangle region of the
window where the image is drawn - Defining the viewport
- The window system is responsible for opening a
window on the screen - By default, the viewport is set to the entire
pixel rectangle of the window thats open
39OpenGL Viewport and Display Windows
Void glViewport( GLint x, Glint y,
GLsizei width, GLsizei height)
yscreen
Video Screen
gluPerspective ( fovy, 1.0, near,
far) glViewport ( 0, 0, 400, 400)
gluPerspective ( fovy, 1.0, near,
far) glViewport ( 0, 0, 400, 200) // Distorted
Display Window
gluPerspective ( fovy, 2.0, near,
far) glViewport ( 0, 0, 400, 200)
xscreen
40Demo