OpenGL - PowerPoint PPT Presentation

1 / 54
About This Presentation
Title:

OpenGL

Description:

Computer Graphics Group - Katholieke Universiteit Leuven. Computer ... glMaterialfv(GL_FRONT, GL_DIFFUSE, green); glMaterialfv(GL_FRONT, GL_SPECULAR, white) ... – PowerPoint PPT presentation

Number of Views:96
Avg rating:3.0/5.0
Slides: 55
Provided by: barta5
Category:
Tags: opengl | green | toons

less

Transcript and Presenter's Notes

Title: OpenGL


1
(No Transcript)
2
What is OpenGL?
  • OpenGL software interface to graphics hardware
  • hardware independent
  • same code for NVIDIA, ATI,
  • platform independent
  • same code for linux, mac, windows,
  • ? 150 commands
  • to produce interactive (3D) graphics

? http//www.opengl.org/
3
What is OpenGL NOT?
  • no commands for
  • creating and managing windows
  • dealing with user input (mouse, keyboard,)
  • Luckily there is GLUT
  • no high-level commands for
  • describing 3D objects
  • only small set of geometric primitives
  • points, lines, polygons,

4
What does it look like?
  • main()
  • InitializeAWindowPlease()
  • glClearColor (0.0, 0.0, 0.0, 0.0)
  • glClear (GL_COLOR_BUFFER_BIT)
  • glColor3f (1.0, 1.0, 1.0)
  • glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0)
  • glBegin(GL_POLYGON)
  • glVertex3f (0.25, 0.25, 0.0)
  • glVertex3f (0.75, 0.25, 0.0)
  • glVertex3f (0.75, 0.75, 0.0)
  • glVertex3f (0.25, 0.75, 0.0)
  • glEnd()
  • glFlush()
  • UpdateTheWindowAndCheckForEvents()

5
OpenGL as a State Machine
  • OpenGL State Machine
  • states/modes remain until you change them
  • e.g. glColor3f(1.0,1.0,1.0)
  • often modes enabled and disabled
  • e.g. glEnable(GL_LIGHTING)
  • glDisable(GL_LIGHTING)
  • you can query the state of each variable
  • using the glGet() commands
  • not needed often though

6
OpenGL Rendering Pipeline
7
OpenGL Related Libraries
  • The OpenGL Utility Library (GLU)
  • provided as part of OpenGL
  • prefix glu
  • e.g. gluSphere()
  • The OpenGL Utility Toolkit (GLUT)
  • window system-independent toolkit
  • window management
  • keyboard, mouse input
  • prefix glut
  • e.g. glutInitWindowSize()

8
Include files
  • include ltGL/gl.hgt
  • include ltGL/glu.hgt
  • or if using GLUT
  • include ltGL/glut.hgt
  • on windows often necessary
  • include ltwindows.hgt
  • include ltGL/glut.hgt

on Mac include ltOpenGL/gl.hgt include
ltGLUT/glut.hgt
9
Lets start with GLUT
  • or how to make a window

10
GLUT Main
  • int main(int argc, char argv)
  • glutInit()
  • glutInitDisplayMode (GLUT_SINGLE GLUT_RGB)
  • glutInitWindowSize (Vx_max-Vx_min,
    Vy_max-Vy_min)
  • glutCreateWindow (argv0)
  • init ()
  • glutDisplayFunc(display)
  • glutReshapeFunc(reshape)
  • glutMouseFunc(mouse)
  • glutKeyboardFunc(keyboard)
  • glutMainLoop()
  • return 0

11
GLUT Init
  • void init(void)
  • glClearColor (0.0, 0.0, 0.0, 0.0)
  • glEnable()

12
GLUT Display
  • void display(void)
  • glClear (GL_COLOR_BUFFER_BIT)
  • glColor3f(1.0,1.0,1.0)
  • glBegin(GL_LINES)
  • glVertex2d(100.0, 100.0)
  • glVertex2d(400.0, 100.0)
  • glEnd()
  • glFlush ()

13
GLUT Reshape
  • void reshape (int w, int h)
  • glViewport (0, 0, (GLsizei) w, (GLsizei) h)
  • glMatrixMode (GL_PROJECTION)
  • glLoadIdentity ()
  • gluOrtho2D(Vx_min, Vx_max, Vy_min, Vy_max)
  • glMatrixMode (GL_MODELVIEW)

14
GLUT Mouse
  • void mouse(int button, int state, int x, int y)
  • switch (button)
  • case GLUT_LEFT_BUTTON
  • if (state GLUT_DOWN)
  • printf("left mouse click\n")
  • break
  • default
  • break

15
GLUT Keyboard
  • void keyboard(unsigned char key, int x, int y)
  • switch (key)
  • case r'
  • glutPostRedisplay()
  • break
  • case 27 / Escape key /
  • exit(0)
  • break
  • default
  • break

16
GLUT Summary
  • Creating windows / handling input
  • Where to get?
  • windows http//www.xmission.com/nate/glut.html
  • linux should be there, or in mesa demos
  • Drawbacks?
  • C API
  • not maintained anymore
  • but

17
Alternatives/Extensions to GLUT
  • GLT OpenGL C Toolkit
  • http//www.nigels.com/glt/
  • GlutMaster C interface to GLUT
  • multiple windows
  • class for mouse manipulation
  • http//www.nigels.com/glt/
  • GLVU C/OpenGL/GLUT based viewer
  • http//www.cs.unc.edu/walk/software/glvu/
  • Many more out there

18
Back to OpenGL
19
OpenGL Conventions
  • Functions in OpenGL start with gl
  • Functions starting with glu are utility functions
    (i.e., gluLookAt())
  • Functions starting with glx are for interfacing
    with the X Windows system
  • Function names indicate argument /type
  • Functions ending with f take floats
  • Functions ending with i take ints, functions that
    end with v take an array, with b take byte, etc.
  • Ex glColor3f() takes 3 floats, but glColor4fv()
    takes an array of 4 floats

20
The Buffers
  • A buffer is a memory area in the graphics
    hardware for some special purposes.
  • An OpenGL system can manipulate the four buffers
  • Color buffers (front-left, front-right,
    back-left, back-right, and any number of
    auxiliary color buffers)
  • Depth buffer (Z-Buffer)
  • Stencil buffer
  • Accumulation buffer

21
OpenGL Specifying Geometry
  • Geometry in OpenGL consists of a list of vertices
    in between calls to glBegin() and glEnd()
  • A simple example telling GL to render a triangle
  • glBegin(GL_POLYGON)
  • glVertex3f(x1, y1, z1)
  • glVertex3f(x2, y2, z2)
  • glVertex3f(x3, y3, z3)
  • glEnd()
  • Usage glBegin(geomtype) where geomtype is
  • Points, lines, polygons, triangles,
    quadrilaterals, etc...

22
OpenGL More Examples
  • Example GL supports quadrilaterals
  • glBegin(GL_QUADS)
  • glVertex3f(-1, 1, 0)
  • glVertex3f(-1, -1, 0)
  • glVertex3f(1, -1, 0)
  • glVertex3f(1, 1, 0)
  • glEnd()
  • This type of operation is called immediate-mode
    rendering each command happens immediately

23
OpenGL Front/Back Rendering
  • Each polygon has two sides, front and back
  • OpenGL can render the two differently
  • The ordering of vertices in the list determines
    which is the front side
  • When looking at the front side, the vertices go
    counterclockwise
  • This is basically the right-hand rule
  • Note that this still holds after perspective
    projection

24
OpenGL Drawing Triangles
  • You can draw multiple triangles between
    glBegin(GL_TRIANGLES) and glEnd()
  • float v13, v23, v33, v43
  • ...
  • glBegin(GL_TRIANGLES)
  • glVertex3fv(v1) glVertex3fv(v2)
    glVertex3fv(v3)
  • glVertex3fv(v1) glVertex3fv(v3)
    glVertex3fv(v4)
  • glEnd()
  • Each set of 3 vertices forms a triangle
  • What do the triangles drawn above look like?
  • How much redundant computation is happening?

25
OpenGL Triangle Strips
  • An OpenGL triangle strip primitive reduces this
    redundancy by sharing vertices
  • glBegin(GL_TRIANGLE_STRIP)
  • glVertex3fv(v0)
  • glVertex3fv(v1)
  • glVertex3fv(v2)
  • glVertex3fv(v3)
  • glVertex3fv(v4)
  • glVertex3fv(v5)
  • glEnd()
  • triangle 0 is v0, v1, v2
  • triangle 1 is v2, v1, v3 (why not v1, v2, v3?)
  • triangle 2 is v2, v3, v4
  • triangle 3 is v4, v3, v5 (again, not v3, v4, v5)

v2
v4
v0
v5
v1
v3
26
OpenGL Triangle Fan
  • The GL_TRIANGLE_FAN primitive is another way to
    reduce vertex redundancy

v4
v3
v5
v0
v2
v6
v1
27
OpenGL Drawing Other Primitives
  • You can draw other primitives using
  • GL_POINTS
  • GL_LINES
  • GL_LINE_STRIP
  • GL_LINE_LOOP
  • GL_QUADS

28
OpenGL Specifying Color
  • Can specify other properties such as color
  • To produce a single aqua-colored triangle
  • glColor3f(0.1, 0.5, 1.0)
  • glVertex3fv(v0) glVertex3fv(v1)
    glVertex3fv(v2)
  • To produce a Gouraud-shaded triangle
  • glColor3f(1, 0, 0) glVertex3fv(v0)
  • glColor3f(0, 1, 0) glVertex3fv(v1)
  • glColor3f(0, 0, 1) glVertex3fv(v2)
  • In OpenGL, colors can also have a fourth
    component ? (transparency)
  • Generally want ? 1.0 (opaque)

29
OpenGL Specifying Normals
  • Calling glColor() sets the color for vertices
    following, until the next call to glColor()
  • Calling glNormal() sets the normal vector for the
    following points, till next glNormal()
  • So flat-shaded Phong lighting requires
  • glNormal3f(Nx, Ny, Nz)
  • glVertex3fv(v0)glVertex3fv(v1)glVertex3fv(v2)
  • While smooth shading requires
  • glNormal3f(N0x, N0y, N0z) glVertex3fv(v0)
  • glNormal3f(N1x, N1y, N1z) glVertex3fv(v1)
  • glNormal3f(N2x, N2y, N2z) glVertex3fv(v2)
  • (Of course, lighting requires additional setup
    glShadeModel())

30
OpenGL Specifying Normals
  • Normals should by default be normalized!
  • otherwise you should set glEnable(GL_NORMALIZE)

31
Lighting
  • The Phong lighting model

32
OpenGL Lighting
  • OpenGL binds our Phong lighting coefficients (ka,
    kd, ks, nshiny) into materials
  • Calling glMaterialfv() sets the current material
    (actually just a single attribute)
  • Example
  • float green 0, 1, 0, 1
  • float white 1, 1, 1, 1
  • glMaterialfv(GL_FRONT, GL_DIFFUSE, green)
  • glMaterialfv(GL_FRONT, GL_SPECULAR, white)

33
OpenGL Lighting
  • OpenGL supports at least 8 lights, with
    parameters set by the glLight() call
  • float l_amb .1, .1, .1, 1.0
  • float l_diff 1, 0, 0, 1
  • float l_spec 1, 1, 1, 1
  • float l_pos 10, 100, 30, 0
  • glLightfv(GL_LIGHT0, GL_AMBIENT, l_amb)
  • glLightfv(GL_LIGHT0, GL_DIFFUSE, l_diff)
  • glLightfv(GL_LIGHT0, GL_SPECULAR, l_spec)
  • glLightf(GL_LIGHT0, GL_SHININESS, 50.0)
  • glLightfv(GL_LIGHT0, GL_POSITION, l_pos)
  • What might the 4th coordinate in l_pos be for?

34
OpenGL Lighting
  • Dont forget to enable lighting and each light
  • glEnable(GL_LIGHTING)
  • glEnable(GL_LIGHT0)
  • Can set the lighting model too
  • Intensity of the ambient light source
  • Whether to treat eye point as infinitely far away
  • Whether to perform lighting calculations for both
    sides of polygons
  • All these things have reasonable default values
  • man glLightModel for details

35
OpenGL Display Lists
  • OpenGL can compile a series of rendering
    commands into a display list...
  • list glGenLists(1)
  • glNewList(list, GL_COMPILE)
  • glBegin(GL_TRIANGLES)
  • glVertex3fv(v0)
  • / draw lots of triangles /
  • glVertex3fv(vN)
  • glEnd()
  • glEndList()
  • which can be rendered with a single call
  • glCallList(list)

36
OpenGL Display Lists
  • Display lists can contain
  • Geometry
  • Color, material, and texture specifications
  • Matrix transforms
  • Other display lists!
  • Display lists are not only handy, they usually
    increase performance

37
OpenGL Matrix Manipulation
  • OpenGL keeps two matrices that vertices are
    multiplied by upon calling glVertex()
  • The modelview matrix combines all modeling
    transforms and the viewing transform
  • The projection matrix performs the projection
    (usually a perspective projection)
  • Various commands affect the current matrix
  • You need to specify which matrix is current
  • E.g., glMatrixMode(GL_MODELVIEW) or
    glMatrixMode(GL_PROJECTION)
  • glLoadIdentity() replaces the contents of the
    current matrix with the identity matrix

38
OpenGL Modeling Transforms
  • Some OpenGL commands generate transformation
    matrices
  • glTranslatef(Tx, Ty, Tz)
  • glRotatef(angleDegrees, Ax, Ay, Az)
  • glScalef(Sx, Sy, Sz)
  • The resulting matrix concatenates to the right of
    the current matrix

39
OpenGL Modeling Transforms
  • Example
  • glMatrixMode(GL_MODELVIEW)
  • glLoadIdentity()
  • glTranslatef()
  • glRotatef()
  • glScalef()
  • Result the modelview matrix is set to
  • I T R T R
  • which then multiplies all following vertices
  • Thus transformations appearing last in the
    program have the first effect on the geometry

40
OpenGL Viewing Transforms
  • No camera concept in OpenGL
  • Viewing transforms are treated the same way
  • Ex gluLookAt() computes a lookat matrix and
    concatenates it with the current matrix
  • gluLookAt(eyeX, eyeY, eyeZ,
  • centerX, centerY, centerZ,
  • upX, upY, upZ)
  • Again, this matrix postmultiplies the current
    matrix

41
OpenGL Projection Transforms
  • The projection matrix is generally used for the
    perspective projection matrix
  • Why do you suppose OpenGL separates the modelview
    and projection matrices?
  • gluOrtho2D() creates a matrix for projecting 2D
    coordinates onto the screen and multiplies the
    current projection matrix by it.
  • gluOrtho2D(double left, double right,
  • double bottom, double top)

42
Selecting a polygon withthe mouse
  • Use gluUnProject() to find world coordinates for
    mouse click on near and far clipping planes
    (pnear and pfar).
  • Intersect every triangle in the scene with the
    line that passes through pnear and pfar.

43
Selecting a polygon withthe mouse
  • Use glProject() to determine which intersected
    triangle is closest to viewer (smallest z-value).
  • See also glGetIntegerv(), glGetDoublev(), and
    unproject.c

44
The Scene Graph
  • Instancing means using the same geometry for
    multiple objects
  • Example 4 wheels on car
  • How might we use display lists for instancing?
  • Compile geometry (say a car tire, centered about
    the origin) into a display list
  • Set up matrices viewing transform modeling
    transform(s) to put origin at front left corner
    of car
  • Call display list for tire
  • Set up matrices, this time putting origin at
    front right of car
  • Call display list for tire Etc
  • Why is this inefficient?

45
The Scene Graph
  • Answer because many objects in a scene typically
    share multiple transformations
  • The scene graph captures transformations and
    object-object relationships in a DAG

World
Legend
Robot
Objects
Body
Head
Instancing(i.e, a matrix)
Arm
Trunk
Leg
Eye
Mouth
46
The Scene Graph
  • Traverse the scene graph in depth-first order,
    concatenating and undoing transforms
  • For example, to render the robot
  • Apply robot ?head matrix
  • Apply head ?mouth matrix
  • Render mouth
  • Un-apply head ?mouth matrix
  • Apply head ?left eye matrix
  • Render eye
  • Un-apply head ?left eye matrix
  • Apply head ?right eye matrix
  • Render eye
  • Un-apply head ?right eye matrix
  • Un-apply robot ?head matrix
  • Apply robot ?body matrix

How should we implement thisun-apply business?
47
The Scene Graph in OpenGL
  • OpenGL maintains a matrix stack of modeling and
    viewing transformations

Robot
Visited
Head
Body
Unvisited
Leg
Arm
Trunk
Eye
Mouth
MatrixStack
Active
Foot
48
OpenGL The Matrix Stack
  • The user can save the current transformation
    matrix by pushing it onto the stack with
    glPushMatrix()
  • The user can later restore the most recently
    pushed matrix with glPopMatrix()
  • These commands really only make sense when in
    GL_MODELVIEW matrix mode

49
OpenGL Matrix Stack Example
  • glMatrixMode(GL_MODELVIEW)
  • glLoadIdentity()
  • glTranslatef()
  • // save translation matrix
  • glPushMatrix()
  • glRotatef()
  • // render something translated rotated
  • glCallList(foo)
  • // restore pushed matrix, undoing rotation
  • glPopMatrix()
  • // render something else, no rotation
  • glCallList(bar)

50
OpenGL Texturing
  • Texture creation
  • int ids1
  • glGenTextures(1,ids)
  • glBindTexture (GL_TEXTURE_2D, ids0)
  • glPixelStorei (GL_UNPACK_ALIGNMENT, 1)
  • glTexParameteri (GL_TEXTURE_2D,
    GL_TEXTURE_WRAP_S, GL_REPEAT)
  • glTexParameteri (GL_TEXTURE_2D,
    GL_TEXTURE_WRAP_T, GL_REPEAT)
  • glTexParameteri (GL_TEXTURE_2D,
    GL_TEXTURE_MAG_FILTER, GL_LINEAR)
  • glTexParameteri (GL_TEXTURE_2D,
    GL_TEXTURE_MIN_FILTER, GL_LINEAR)
  • glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,
    GL_MODULATE)
  • glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
    imageWidth, imageHeight, 0, GL_RGBA,
    GL_UNSIGNED_BYTE, imageData)

51
OpenGL Texturing
  • Texture usage
  • glEnable (GL_TEXTURE_2D) / enable texture
    mapping /
  • glBindTexture (GL_TEXTURE_2D, ids0) / bind to
    our texture /
  • glBegin (GL_QUADS)
  • glTexCoord2f (0.0f,0.0f) / lower left
    corner of image /
  • glVertex3f (-10.0f, -10.0f, 0.0f)
  • glTexCoord2f (1.0f, 0.0f) / lower right
    corner of image /
  • glVertex3f (10.0f, -10.0f, 0.0f)
  • glTexCoord2f (1.0f, 1.0f) / upper right
    corner of image /
  • glVertex3f (10.0f, 10.0f, 0.0f)
  • glTexCoord2f (0.0f, 1.0f) / upper left
    corner of image /
  • glVertex3f (-10.0f, 10.0f, 0.0f)
  • glEnd ()

52
Now, what do we know?
  • GLUT
  • OpenGL primitives points, lines, triangles,
    and colors, normals
  • Lights, materials
  • Render a scene efficiently using Display Lists
    and the matrix stack
  • Apply textures

53
Where to Find Documentation?
  • http//www.opengl.org
  • The OpenGL Reference Manual
  • (Blue Book)
  • javadoc-like reference
  • http//www.rush3d.com/reference/opengl-bluebook-1.
    0/
  • The OpenGL Programming Guide
  • (Red Book)
  • lots of useful examples
  • http//www.rush3d.com/reference/opengl-redbook-1.1
    /
  • Many tutorials online http//nehe.gamedev.net,

54
So, thats it?
  • No! Much more is possible!
  • Extensions for OpenGL
  • GPU programming
  • Maybe in a later session
Write a Comment
User Comments (0)
About PowerShow.com