1 / 30

CS 248 OpenGL Help Session

This session provides an overview of OpenGL, covering topics such as basic primitives and rendering, transformations and viewing, GLUT and the interaction/display loop, buffers, lighting, and texturing. Development tips are also provided.

hirst
Télécharger la présentation

CS 248 OpenGL Help Session

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 248OpenGL Help Session CS248 Presented by Sean Walker (adapted from Ian Buck’s slides – see ’03 web page) Stanford University Nov. 5, 2004

  2. 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, 4th Edition ver. 1.4(aka “The Red Book”) unless noted otherwise.

  3. What is OpenGL? OpenGL is a software interface to graphics hardware. Application 3-D world Simulation User Interface Graphics Hardware (rasterizer, texturing, lighting, transformations, etc.) OpenGL Geometry, vertices, normals, colors, texture, etc.

  4. 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

  5. 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);

  6. 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();

  7. Primitive Types in glBegin(Ch.2, p.44) • Points GL_POINTS • Lines 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 Nate Robins' Tutorial: shapes (show page 45)

  8. Transformations and Viewing(Ch.3) OpenGL has 3 different matrix modes: • GL_MODELVIEW • GL_PROJECTION • GL_TEXTURE Choose the matrix with: glMatrixMode(…);

  9. 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

  10. OpenGL: Projection Matrix • Sets up a perspective projection. (page 127) • glFrustrum (...); • gluPerspective (fovy, aspect, near, far); • glOrtho (...); • gluLookAt (...); (often applied to modelview matrix)

  11. 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

  12. 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

  13. 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(); // Start GLUT return 0; } * Your code here

  14. 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!

  15. 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 );

  16. 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);

  17. 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

  18. 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); ...

  19. 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

  20. OpenGL: Shading • OpenGL supports 2 basic shading models: flat and smooth. glShadeModel(GL_FLAT);glShadeModel(GL_SMOOTH);

  21. 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

  22. OpenGL: Texturing

  23. 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.

  24. 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); (x3,y3,z3) (x1,y1,z1) t 0,1 1,1 1,1 + 0,0 1,0 s 0,0 (x0,y0,z0) (x2,y2,z2) Tutorial: Texture pg 403

  25. 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

  26. 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 • Useful for overlaying textures or other effects ? + = New color Color in framebuffer (r’,g’,b’,a’) (r1,g1,b1,a1) (r0,g0,b0,a0) “source” “destination”

  27. OpenGL: Fog • Simulate atmospheric effects • glFog (): Sets fog parameters • glEnable (GL_FOG); Tutorial: fog

  28. 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

  29. 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 <GL/gl.h> #include <GL/glu.h> #include <GL/glut.h> • 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.

  30. Questions?

More Related