OpenGL Tutorial - PowerPoint PPT Presentation

1 / 61
About This Presentation
Title:

OpenGL Tutorial

Description:

Edit variables in gluLookAt( eye, center, up ) Translation. Moving the center. Rotation ... Notice: The light color is white. for both diffuse and specular ... – PowerPoint PPT presentation

Number of Views:175
Avg rating:3.0/5.0
Slides: 62
Provided by: Goog264
Category:

less

Transcript and Presenter's Notes

Title: OpenGL Tutorial


1
OpenGL Tutorial
2
What is it?
  • Low level graphics library
  • Platform independent
  • Support for primitives such as points, lines,
    polygons, images and bitmaps

3
Used for?
4
Visualization
5
Features
  • 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

6
GLU and GLUT
  • GLUT OpenGL Utility Toolkit
  • Window to draw in
  • Support for higher level shapes
  • GLU OpenGL Utility Library
  • Support functions (camera, tessellations, nurbs)

7
Initialize - GLUT
  • glutInit(int argc, char argv)
  • glutInitDisplayMode(int mode)
  • glutInitWindowPosition(int x, int y)
  • glutInitWindowSize(int width, int height)
  • glutCreateWindow(char window_name)
  • .
  • glutMainLoop()

8
Display - GLUT
  • glutDisplayFunc(void (func)display)
  • .
  • glutPostRedisplay(void)
  • .
  • glutSwapBuffers(void)

9
Interaction - 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))

10
Put 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

11
Put it all together - GLUT
  • void display(void)
  • glClear(GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT)
  • drawBox()
  • glutSwapBuffers()
  • Example

12
OpenGL Pipeline
13
State 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

14
State Drawing
Example
15
State Drawing
16
State 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

17
Viewing
u, v T 1/V P M x y z w T
18
Camera 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)

19
Camera Set up
  • Camera position
  • gluLookAt(eye, center, up)
  • eye, center and up are (x,y,z) vectors
  • Viewport
  • glViewport(x, y, width, height)

20
Camera - Example
21
Matrix Modes
  • glLoadIdentity()
  • Or use glMultMatrix(4x4 identity matrix)
  • glMatrixMode(GL_PERSPECTIVE)
  • viewing volume
  • glMatrixMode(GL_MODELVIEW)
  • transforms the geometry

22
Camera 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 ..

23
Transformations
  • Translate
  • glTranslatef(x,y,z)
  • Rotate
  • glRotatef(angle, axis(x,y,z))
  • Scale
  • glScalef(x,y,z)
  • Example

24
Reversing - 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)))
25
Matrix 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
26
Mix Projection Matrices
  • glMatrixMode(GL_PROJECTION)
  • glPushMatrix()
  • glLoadIdentity()
  • glOrtho()
  • displayText()
  • glPopMatrix()

Mix 2d with 3d!
27
Other 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)

28
Mouse 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)

29
Color
  • Color Index
  • Lookup table
  • RGBA
  • glColor3(r,g,b) or
  • glColor4(r,g,b,a)
  • (glEnable(GL_BLEND)
  • Can use dithering

30
Shade
  • 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()
31
Shade
  • 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()
32
Lighting
  • Ambient
  • Diffuse
  • Specular
  • Material

33
Lighting
34
Lighting
35
Lighting
  • Specular highlights

36
Lighting
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
37
Lighting
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
38
Lighting
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()
39
Lighting
  • 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!

40
Blending
  • 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

41
Blending
  • 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

42
Blending
(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
43
Anti-aliasing
44
Anti-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)

45
Fog
46
Fog
  • 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)

47
Display 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)

48
Pixel Data
  • glReadPixels( )
  • Read from framebuffer
  • x, y, width, height, format, type, pixels
  • glDrawPixels( )
  • Write to framebuffer
  • x, y, width, height, type
  • glCopyPixels( )
  • Read Draw

49
Texture Mapping
50
Texture Mapping
  • How to build one?

51
Texture 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)
52
Texture Mapping
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
GL_CLAMP)
53
Texture Mapping
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
GL_REPEAT)
54
Texture Mapping
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
GL_CLAMP)
55
Texture Mapping
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
GL_CLAMP) glTexParameteri(GL_TEXTURE_2D,
GL_TEXTURE_WRAP_T, GL_CLAMP)
56
Texture 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)
57
Framebuffer
  • Color
  • Front-left, Front-right
  • Back-left, Back-right
  • Auxillary
  • Depth
  • Acculumation
  • Stencil

58
Operations on Framebuffer
  • Scissor test
  • Alpha test
  • Stencil test
  • Depth test
  • Blending
  • Dithering
  • Logical operation

Depth of Field Jitter
59
GL 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

60
References
  • 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

61
Special Thanks to Mayank Singh
Write a Comment
User Comments (0)
About PowerShow.com