Transcript S2K Arial, Bold, 37 points, 105% line spacing
CS 248 OpenGL Help Session CS248 Presented by Sean Walker (adapted from Ian Buck’s slides – see ’03 web page) Stanford University Nov. 5, 2004
Overview
• • • • • • •
What is OpenGL?
Basic primitives and rendering in OpenGL Transformations and viewing GLUT and the interaction / display loop Buffers, Lighting, and Texturing Other features Development tips
Note: all page references refer to the
OpenGL Programming Guide, 4 th
(aka “The Red Book”) unless noted otherwise.
Edition ver. 1.4
2
What is OpenGL?
OpenGL is a software interface to graphics hardware
.
Application 3-D world Simulation User Interface OpenGL Geometry, vertices, normals, colors, texture, etc.
Graphics Hardware (rasterizer, texturing, lighting, transformations, etc.) 3
OpenGL Basics
•
Read the Red Book! It’s a great resource and is very readable.
•
OpenGL is a state machine: polygons are affected by the current color, transformation, drawing mode, etc.
•
Everything in the OpenGL specification is supported in every implementation
4
Specifying Object Vertices
(Ch.2 p.42, Ch.4 p.171)
•
Every object is specified by vertices
•
glVertex3f (2.0, 4.1, 6.0);
•
glVertex2i (4, 5);
•
glVertex3fv (vector);
•
Current color affects any vertices
•
glColor3f (0.0, 0.5, 1.0);
•
glColor4ub (0, 128, 255, 0);
•
glColor3dv (color);
5
Specifying Object Vertices
(Ch.2 p.42)
•
Vertices are specified only between glBegin(mode) and glEnd() , usually in a counter-clockwise order for polygons.
glBegin (GL_TRIANGLES); glVertex2i (0, 0); glVertex2i (2, 0); glVertex2i (1, 1); glEnd();
6
Primitive Types in glBegin
(Ch.2, p.44)
• • • • •
Points Lines GL_POINTS GL_LINES, GL_LINE_STRIP, GL_LINE_LOOP Triangles GL_TRIANGLES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN Quads GL_QUADS, GL_QUAD_STRIP Polygons GL_POLYGON
(show page 45) Nate Robins' Tutorial: shapes 7
Transformations and Viewing
(Ch.3) OpenGL has 3 different matrix modes:
•
GL_MODELVIEW
• •
GL_PROJECTION GL_TEXTURE Choose the matrix with: glMatrixMode(…);
8
OpenGL: Modelview matrix
• •
Transforms objects within the scene.
glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(10.5, 0, 0); glRotatef(45, 0, 0, 1); DrawCube(); Remember that the operations are right multiplied, so the transformation just before DrawCube() takes effect first.
Tutorial: transformation 9
OpenGL: Projection Matrix
•
Sets up a perspective projection.
(page 127)
•
glFrustrum (...);
•
gluPerspective (fovy, aspect, near, far);
•
glOrtho (...);
•
gluLookAt (...);
(often applied to modelview matrix) 10
OpenGL: Projection Matrix
Example:
glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(64, (float)windowWidth / (float)windowHeight, 4, 4096); gluLookAt(0.0, 0.0, 2.0, // camera position 0.0, 0.0, 0.0, // target position 0.0, 0.0, 2.0); // up vector
Tutorial: projection 11
GLUT – OpenGL Utility Toolkit
(Appendix D)
•
GLUT is a library that handles system events and windowing across multiple platforms
• • •
Includes some nice utilities We strongly suggest you use it Find it at: http://graphics.stanford.edu/courses/cs248-04/proj3/index.html
12
GLUT – Starting Up
int main (int argc, char *argv[]) { glutInit(&argc, argv); glutInitDisplayMode (GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA); glutInitWindowSize (windowWidth, windowHeight); glutInitWindowPosition (0, 0); glutCreateWindow (“248 Video Game!"); SetStates(); // Initialize rendering states* RegisterCallbacks(); // Set event callbacks* } glutMainLoop(); return 0; // Start GLUT * Your code here
13
Setting Up Rendering States
•
OpenGL is a state machine: polygons are affected by the current color, transformation, drawing mode, etc.
•
Enable and disable features such as lighting, texturing, and alpha blending.
•
glEnable (GL_LIGHTING);
•
glDisable (GL_FOG);
•
Forgetting to enable something is a common source of bugs!
14
GLUT Event Callbacks
• •
Register functions that are called when certain events occur.
Examples: glutDisplayFunc( Display ); glutKeyboardFunc( Keyboard ); glutReshapeFunc( Reshape ); glutMouseFunc( Mouse ); glutPassiveMotionFunc( PassiveFunc ); glutMotionFunc( MouseDraggedFunc ); glutIdleFunc( Idle );
15
OpenGL Buffers
•
Multiple types of buffers
• Color buffers (front/back, left/right) • • • Depth buffer (hidden surface removal) Stencil buffer (allows masking or stenciling) Accumulation buffer (antialiasing, depth of field) •
Clearing buffers: // Clear to this color when screen is cleared.
glClearColor (0.0, 0.0, 0.0, 0.0); // Clear color and depth buffers.
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
16
OpenGL Double Buffering
• •
Double buffering:
• Draw on
back
buffer while
front
buffer is being displayed.
• When finished drawing, swap the two, and begin work on the new back buffer.
•
glutSwapBuffers(); Primary purpose: eliminate flicker
17
OpenGL: Normals and Lighting
• • •
OpenGL can do lighting computations for you Normal vectors should be of unit length (normalized) in most cases. Normal vector kept as state – each vertex is assigned the most recently set normal vector ...
glNormal3fv (n0); glVertex3fv (v0); glVertex3fv (v1); glVertex3fv (v2); ...
18
OpenGL: Lighting
(Ch.5 p.178)
• • • •
glEnable (GL_LIGHTING); OpenGL supports a minimum of 8 lights.
glEnable (GL_LIGHT0); ... glEnable (GL_LIGHT7); Lights have a position, type, and color, among other things (more details in text).
Types of lights are point light, directional light, and spotlight. (page 191)
Tutorial: lightposition 19
OpenGL: Shading
•
OpenGL supports 2 basic shading models: flat and smooth.
glShadeModel(GL_FLAT); glShadeModel(GL_SMOOTH);
20
OpenGL: Material Properties
(Ch.5)
• •
Material properties are associated with each polygon (corresponding light properties)
•
glMaterial*(GLenum face, GLenum pname, TYPE param); Some properties ( pname ), page 206:
• GL_AMBIENT: Ambient color of material • • • GL_DIFFUSE: Diffuse color of material GL_SPECULAR: Specular component (for highlights) GL_SHININESS: Specular exponent (intensity of highlight) Tutorial: lightmaterial 21
OpenGL: Texturing
22
OpenGL: Texturing
•
Loading your data
• This can come from an image: ppm, tiff • Or create at run time • Final result is always an array •
Setting texture state
• Creating texture names with “binding”, scaling the image/data, building Mipmaps, setting filters, etc.
23
OpenGL: Texturing
•
Mapping the texture to the polygon
• specify (s,t) texture coordinates for (x,y,z) polygon vertices • texture coordinates (s,t)are from 0,1:
glTexCoord2f(s,t);
t (x3,y3,z3) 0,1 (x1,y1,z1) 1,1 1,1 + 0,0 s 0,0 (x0,y0,z0) 1,0 (x2,y2,z2) Tutorial: Texture pg 403 24
OpenGL: Advanced Texturing
•
Advanced texturing techniques
• Mipmapping • Multitextures • Automatic texture generation • Let OpenGL determine texture coordinates for you • • Environment Mapping Texture matrix stack • Fragment Shaders • Custom lighting effects 25
OpenGL: Alpha Blending
Ch 6, pg 225
• When enabled, OpenGL uses the alpha channel to blend a new fragment’s color value with a color in the framebuffer New color (r1,g1,b1,a1) “source” + Color in framebuffer (r0,g0,b0,a0) “destination” = ?
(r’,g’,b’,a’) • Useful for overlaying textures or other effects 26
OpenGL: Fog
Simulate atmospheric effects
•
glFog (): Sets fog parameters
•
glEnable (GL_FOG);
Tutorial: fog 27
OpenGL: Other Features
• • • •
Display Lists (ch 7): Speed up your game!
Quadrics (ch 11): Pre-made objects
• Also look at GLUT’s objects
Evaluators (ch 12): Bezier curves and surfaces Selection (ch 13): Clicking on game objects with a mouse
28
Development
• • • •
On Windows:
• Download the GLUT libraries (linked off the proj3 webpage). • You want to link your project with: opengl32.lib, glut32.lib, and glu32.lib.
This is under Project->Settings->Link in MS Visual Studio.
On Linux:
• GLUT is already installed on the graphics lab PCs.
• In your Makefile, compile with flags: -L/usr/lib -lGL -lGLU –lglut
Headers #include
Call glutReportErrors() once each display loop for debugging.
• This will report any errors that may have occurred during rendering, such as an illegal operation in a glBegin/glEnd pair.
29
Questions?
30