Title: OpenGL Tutorial
1OpenGL Tutorial
2What is it?
- Low level graphics library
- Platform independent
- Support for primitives such as points, lines,
polygons, images and bitmaps
3Used for?
4Visualization
5Features
- Geometric and raster primitives
- RGBA or color index mode
- Display list or immediate mode
- Viewing and modeling transformations
- Lighting and Shading
- Hidden surface removal (Depth Buffer)
- Alpha Blending (Translucency)
- Anti-aliasing
- Texture Mapping
- Atmospheric Effects (Fog, Smoke, Haze)
- Feedback and Selection
- Stencil Planes
- Accumulation Buffer
- Depth Cueing
- Wireframes
- Motion blur
6GLU and GLUT
- GLUT OpenGL Utility Toolkit
- Window to draw in
- Support for higher level shapes
- GLU OpenGL Utility Library
- Support functions (camera, tessellations, nurbs)
7Initialize - GLUT
- glutInit(int argc, char argv)
- glutInitDisplayMode(int mode)
- glutInitWindowPosition(int x, int y)
- glutInitWindowSize(int width, int height)
- glutCreateWindow(char window_name)
- .
- glutMainLoop()
8Display - GLUT
- glutDisplayFunc(void (func)display)
- .
- glutPostRedisplay(void)
- .
- glutSwapBuffers(void)
9Interaction - GLUT
- glutReshapeFunc(void (func)(int w, int h))
- glutKeyboardFunc(void (func)(unsigned char key,
int x, int y) - glutMouseFunc(void (func)(int button, int state,
int x, int y)) - glutMotionFunc(void (func)(int x, int y))
- glutIdleFunc(void (func)(void))
10Put it all together - GLUT
- int main(int argc, char argv)
-
- glutInit(argc, argv)
- glutInitDisplayMode(GLUT_DOUBLE GLUT_RGB
GLUT_DEPTH) - glutCreateWindow("red 3D lighted cube")
- glutReshapeFunc(Reshape)
- glutKeyboardFunc(Key)
- glutDisplayFunc(display)
- init()
- glutMainLoop()
- return 0
-
- Example
11Put it all together - GLUT
- void display(void)
-
- glClear(GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT)
- drawBox()
- glutSwapBuffers()
-
- Example
12OpenGL Pipeline
13State Drawing
- Clearing the window
- glClearColor(0.0, 0.0, 0.0, 0.0)
- glClear(GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT)
- .
- glFlush() or glFinish()
- Drawing glBegin(.) glEnd()
- GL_POINTS,
- GL_LINES, GL_LINE_STRIP, GL_LINE_LOOP
- GL_TRIANGLES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN
- GL_QUADS, GL_QUAD_STRIP
- GL_POLYGON
14State Drawing
Example
15State Drawing
16State Drawing
- State management querying
- glEnable(..) glDisable(..)
- glIsEnabled(..)
- glGetBooleanv(GLenum pname, GLboolean params)
- glGetFloatv(..), glGetDoublev(..),
glGetPointerv(..) - glPushAttrib(..) and glPopAttrib(..)
- Other vertex attributes
- glColor3(r,g,b) or glColor4(r,g,b,a)
- glNormal()
- glTexCoord()
- .many more
17Viewing
u, v T 1/V P M x y z w T
18Camera Set up
- Perspective Projection
- glFrustum(left, right, bottom, top, near, far)
- gluPerspective(fovy, aspect, near, far)
- Orthographic Projection
- glOrtho(left, right, bottom, top, near, far)
- gluOrtho2D(left, right, bottom, top)
19Camera Set up
- Camera position
- gluLookAt(eye, center, up)
- eye, center and up are (x,y,z) vectors
- Viewport
- glViewport(x, y, width, height)
20Camera - Example
21Matrix Modes
- glLoadIdentity()
- Or use glMultMatrix(4x4 identity matrix)
- glMatrixMode(GL_PERSPECTIVE)
- viewing volume
- glMatrixMode(GL_MODELVIEW)
- transforms the geometry
22Camera Set up Matrices
- glMatrixMode(GL_PERSPECTIVE)
- glLoadIdentity()
- gluPerspective(fovy, aspect, near, far)
- glMatrixMode(GL_MODELVIEW)
- glLoadIdentity()
- gluLookAt(eye_x, eye_y, eye_z, center_x,
center_y, center_z, up_x, up_y, up_z) - .. Start drawing ..
23Transformations
- Translate
- glTranslatef(x,y,z)
- Rotate
- glRotatef(angle, axis(x,y,z))
- Scale
- glScalef(x,y,z)
- Example
24Reversing - order of code
- glMatrixMode(GL_MODELVIEW)
- glLoadIdentity()
-
- glMultMatrix(N)
- glMultMatrix(M)
- glMultMatrix(L)
- glBegin(GL_POINTS)
- glVertex3f(v)
- glEnd()
I, N, M, L 4 x 4 matrix Reverse the order
I(N(M(Lv)))
25Matrix Stack
- glMatrixMode(GL_MODELVIEW)
- glLoadIdentity()
-
- glMultMatrix(N)
- glMultMatrix(M)
- for(int i 0 i lt 10 i)
-
- glPushMatrix()
- glMultMatrix(Li)
- drawBox()
- glPopMatrix()
-
10 boxes with unique affine transformations
26Mix Projection Matrices
- glMatrixMode(GL_PROJECTION)
- glPushMatrix()
- glLoadIdentity()
- glOrtho()
- displayText()
- glPopMatrix()
Mix 2d with 3d!
27Other stuff
- glClipPlane(plane, equation)
- glEnable / glDisable (GL_CLIP_PLANEi)
- gluUnProject()
- input projection matrix, modelview matrix,
viewport, winx, winy - Returns object position (x,y,z)
- gluProject()
- Input object (x,y,z) along with modelview matrix,
projection matrix and viewport - Returns the window (x, y, depth)
28Mouse Controlled Camera
- Edit variables in gluLookAt( eye, center, up )
- Translation
- Moving the center
- Rotation
- Moving the eye, keeping the center the same
- Zoom
- Move the camera along the vector (eye center)
29Color
- Color Index
- Lookup table
- RGBA
- glColor3(r,g,b) or
- glColor4(r,g,b,a)
- (glEnable(GL_BLEND)
- Can use dithering
30Shade
- glShadeModel -
- GL_SMOOTH
- GL_FLAT
void triangle(void) glBegin (GL_TRIANGLES) glCo
lor3f (1.0, 0.0, 0.0) glVertex2f (5.0,
5.0) glColor3f (0.0, 1.0, 0.0) glVertex2f
(25.0, 5.0) glColor3f (0.0, 0.0,
1.0) glVertex2f (5.0, 25.0) glEnd()
31Shade
- glShadeModel -
- GL_SMOOTH
- GL_FLAT
void triangle(void) glBegin (GL_TRIANGLES) glCo
lor3f (1.0, 0.0, 0.0) glVertex2f (5.0,
5.0) glColor3f (0.0, 1.0, 0.0) glVertex2f
(25.0, 5.0) glColor3f (0.0, 0.0,
1.0) glVertex2f (5.0, 25.0) glEnd()
32Lighting
- Ambient
- Diffuse
- Specular
- Material
33Lighting
34Lighting
35Lighting
36Lighting
GLfloat ambient 0.0, 0.0, 0.0, 1.0 GLfloat
diffuse 1.0, 1.0, 1.0, 1.0 GLfloat
specular 1.0, 1.0, 1.0, 1.0 GLfloat
position 0.0, 3.0, 3.0, 0.0 glLightfv(GL_
LIGHT0, GL_AMBIENT, ambient) glLightfv(GL_LIGHT0,
GL_DIFFUSE, diffuse) glLightfv(GL_LIGHT0,
GL_POSITION, position) glEnable(GL_LIGHTING) gl
Enable(GL_LIGHT0) glEnable(GL_AUTO_NORMAL) glEna
ble(GL_NORMALIZE)
GL_POSITION (x,y,z,w) If w 0.0 Directional
(vector) If w 1.0 Positional
37Lighting
GLfloat ambient 0.0, 0.0, 0.0, 1.0 GLfloat
diffuse 1.0, 1.0, 1.0, 1.0 GLfloat
specular 1.0, 1.0, 1.0, 1.0 GLfloat
position 0.0, 3.0, 3.0, 0.0 glLightfv(GL_
LIGHT0, GL_AMBIENT, ambient) glLightfv(GL_LIGHT0,
GL_DIFFUSE, diffuse) glLightfv(GL_LIGHT0,
GL_POSITION, position) glEnable(GL_LIGHTING) gl
Enable(GL_LIGHT0) glEnable(GL_AUTO_NORMAL) glEna
ble(GL_NORMALIZE)
Notice The light color is white for both diffuse
and specular light
38Lighting
void renderTeapot(GLfloat x, GLfloat y, GLfloat
ambr, GLfloat ambg, GLfloat ambb, GLfloat difr,
GLfloat difg, GLfloat difb, GLfloat specr,
GLfloat specg, GLfloat specb, GLfloat
shine) GLfloat mat4 glPushMatrix() glTransl
atef(x, y, 0.0) mat0 ambr mat1 ambg
mat2 ambb mat3 1.0 glMaterialfv(GL_FRON
T, GL_AMBIENT, mat) mat0 difr mat1
difg mat2 difb glMaterialfv(GL_FRONT,
GL_DIFFUSE, mat) mat0 specr mat1 specg
mat2 specb glMaterialfv(GL_FRONT,
GL_SPECULAR, mat) glMaterialf(GL_FRONT,
GL_SHININESS, shine 128.0) glCallList(teapotLis
t) glPopMatrix()
39Lighting
- Attenuation
- GL_CONSTANT_ATTENUATION
- GL_LINEAR_ATTENUATION
- GL_QUADRATIC_ATTENUATION
- Spotlights
- GL_SPOT_CUTOFF
- GL_SPOT_DIRECTION
- Moving light with camera
- Light position (0,0,0,1)
- Set up light position before calling gluLookAt!
40Blending
- glEnable(GL_BLEND)
- glBlendFunc()
- (source_color source_factor)
(destination_color destination_factor) - do this for R,G,B
- GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA
41Blending
- glEnable(GL_BLEND)
- glBlendFunc()
- (source_color source_factor)
(destination_color destination_factor) - do this for R,G,B
- GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA
42Blending
(0, 0, 0, 0)
GL_ZERO
(1, 1, 1, 1)
GL_ONE
(Rd, Gd, Bd, Ad)
GL_DST_COLOR
(Rs, Gs, Bs, As)
GL_SRC_COLOR
(1, 1, 1, 1)-(Rd, Gd, Bd, Ad)
GL_ONE_MINUS_DST_COLOR
(1, 1, 1, 1)-(Rs, Gs, Bs, As)
GL_ONE_MINUS_SRC_COLOR
(As, As, As, As)
GL_SRC_ALPHA
(1, 1, 1, 1)-(As, As, As, As)
GL_ONE_MINUS_SRC_ALPHA
(Ad, Ad, Ad, Ad)
GL_DST_ALPHA
(1, 1, 1, 1)-(Ad, Ad, Ad, Ad)
GL_ONE_MINUS_DST_ALPHA
(f, f, f, f) fmin(As, 1-Ad)
GL_SRC_ALPHA_SATURATE
43Anti-aliasing
44Anti-aliasing
- How to?
- glEnable(GL_DEPTH_TEST)
- glEnable(GL_BLEND)
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
- Lines glEnable(GL_LINE_SMOOTH)
- Polygon glEnable(GL_POLYGON_SMOOTH)
45Fog
46Fog
- glEnable(GL_FOG)
-
- GLfloat fogColor4 0.5, 0.5, 0.5, 1.0
- fogMode GL_EXP
- glFogi (GL_FOG_MODE, fogMode)
- glFogfv (GL_FOG_COLOR, fogColor)
- glFogf (GL_FOG_DENSITY, 0.35)
- glHint (GL_FOG_HINT, GL_DONT_CARE)
- glFogf (GL_FOG_START, 1.0)
- glFogf (GL_FOG_END, 5.0)
-
47Display Lists
- Initialize
- Gluint list_name glGenLists(1)
- Build List
- glNewList(list_name, GL_COMPILE)
- draw_some_geometric_objects()
- glEndList()
- Call List (for display)
- glLoadMatrix(M)
- glCallList(list_name)
48Pixel Data
- glReadPixels( )
- Read from framebuffer
- x, y, width, height, format, type, pixels
- glDrawPixels( )
- Write to framebuffer
- x, y, width, height, type
- glCopyPixels( )
- Read Draw
49Texture Mapping
50Texture Mapping
51Texture Mapping How to?
Gluint tex_handle glGenTextures(1,
tex_handle) glBindTexture(GL_TEXTURE_2D,
tex_handle) glTexParameteri(GL_TEXTURE_2D,
GL_TEXTURE_WRAP_S, GL_REPEAT) glTexParameteri(GL_
TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT) glTexPa
rameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
GL_LINEAR) glTexParameteri(GL_TEXTURE_2D,
GL_TEXTURE_MIN_FILTER, GL_LINEAR) glTexImage2D(G
L_TEXTURE_2D, 0, GL_RGBA, width, height,
0, GL_RGBA, GL_UNSIGNED_BYTE, pixels)
52Texture Mapping
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
GL_CLAMP)
53Texture Mapping
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
GL_REPEAT)
54Texture Mapping
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
GL_CLAMP)
55Texture Mapping
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
GL_CLAMP) glTexParameteri(GL_TEXTURE_2D,
GL_TEXTURE_WRAP_T, GL_CLAMP)
56Texture Mapping
Mapping texels pixels
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILT
ER, GL_LINEAR) glTexParameteri(GL_TEXTURE_2D,
GL_TEXTURE_MIN_FILTER, GL_LINEAR)
57Framebuffer
- Color
- Front-left, Front-right
- Back-left, Back-right
- Auxillary
- Depth
- Acculumation
- Stencil
58Operations on Framebuffer
- Scissor test
- Alpha test
- Stencil test
- Depth test
- Blending
- Dithering
- Logical operation
Depth of Field Jitter
59GL Errors?
- define CHECK_GL_ERROR() check_gl_error(__FILE__,
__LINE__) - int check_gl_error(char file, int line)
-
- GLenum glErr
- int retCode 0
- glErr glGetError()
- while (glErr ! GL_NO_ERROR)
-
- printf(\nGL Error d, glErr)
- cout ltlt "(" ltlt gluErrorString(glErr) ltlt ")
- cout ltlt "File " ltlt file ltlt " at line " ltlt line
ltlt endl - retCode 1
-
- return retCode
60References
- OpenGL http//www.opengl.org/
- OpenGL Specifications - http//www.opengl.org/docu
mentation/specs/ - GLUT - http//www.opengl.org/resources/libraries/g
lut/ - UI Glui - http//glui.sourceforge.net/
- Example code
- http//www.sgi.com/products/software/opengl/exampl
es/index.html - http//www.xmission.com/nate/tutors.html
- http//www.opengl.org/resources/faq/technical/
- Other helpful resources
- http//msdn.microsoft.com/en-us/library/ms970772.a
spx
61Special Thanks to Mayank Singh