CS 248 Open GL Help Session CS 248

  • Slides: 30
Download presentation
CS 248 Open. GL Help Session CS 248 Presented by Sean Walker (adapted from

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

Overview • What is Open. GL? • Basic primitives and rendering in • •

Overview • What is Open. GL? • Basic primitives and rendering in • • • Open. GL Transformations and viewing GLUT and the interaction / display loop Buffers, Lighting, and Texturing Other features Development tips Note: all page references refer to the Open. GL Programming Guide, 4 th Edition ver. 1. 4 (aka “The Red Book”) unless noted otherwise. 2

What is Open. GL? Open. GL is a software interface to graphics hardware. Application

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

Open. GL Basics • Read the Red Book! It’s a great resource and is

Open. GL Basics • Read the Red Book! It’s a great resource and is very readable. • Open. GL is a state machine: polygons are affected by the current color, transformation, drawing mode, etc. • Everything in the Open. GL specification is supported in every implementation 4

Specifying Object Vertices (Ch. 2 p. 42, Ch. 4 p. 171) • Every object

Specifying Object Vertices (Ch. 2 p. 42, Ch. 4 p. 171) • Every object is specified by vertices • gl. Vertex 3 f (2. 0, 4. 1, 6. 0); • gl. Vertex 2 i (4, 5); • gl. Vertex 3 fv (vector); • Current color affects any vertices • gl. Color 3 f (0. 0, 0. 5, 1. 0); • gl. Color 4 ub (0, 128, 255, 0); • gl. Color 3 dv (color); 5

Specifying Object Vertices (Ch. 2 p. 42) • Vertices are specified only between gl.

Specifying Object Vertices (Ch. 2 p. 42) • Vertices are specified only between gl. Begin(mode) and gl. End(), usually in a counter-clockwise order for polygons. gl. Begin (GL_TRIANGLES); gl. Vertex 2 i (0, 0); gl. Vertex 2 i (2, 0); gl. Vertex 2 i (1, 1); gl. End(); 6

Primitive Types in gl. Begin (Ch. 2, p. 44) • Points • Lines •

Primitive Types in gl. Begin (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) Open. GL has 3 different matrix modes: • GL_MODELVIEW

Transformations and Viewing (Ch. 3) Open. GL has 3 different matrix modes: • GL_MODELVIEW • GL_PROJECTION • GL_TEXTURE Choose the matrix with: gl. Matrix. Mode(…); 8

Open. GL: Modelview matrix • Transforms objects within the scene. gl. Matrix. Mode(GL_MODELVIEW); gl.

Open. GL: Modelview matrix • Transforms objects within the scene. gl. Matrix. Mode(GL_MODELVIEW); gl. Load. Identity(); gl. Translatef(10. 5, 0, 0); gl. Rotatef(45, 0, 0, 1); Draw. Cube(); • Remember that the operations are right multiplied, so the transformation just before Draw. Cube() takes effect first. Tutorial: transformation 9

Open. GL: Projection Matrix • Sets up a perspective projection. (page 127) • gl.

Open. GL: Projection Matrix • Sets up a perspective projection. (page 127) • gl. Frustrum (. . . ); • glu. Perspective (fovy, aspect, near, far); • gl. Ortho (. . . ); • glu. Look. At (. . . ); (often applied to modelview matrix) 10

Open. GL: Projection Matrix Example: gl. Matrix. Mode(GL_PROJECTION); gl. Load. Identity(); glu. Perspective(64, (float)window.

Open. GL: Projection Matrix Example: gl. Matrix. Mode(GL_PROJECTION); gl. Load. Identity(); glu. Perspective(64, (float)window. Width / (float)window. Height, 4, 4096); glu. Look. At(0. 0, 2. 0, // camera position 0. 0, // target position 0. 0, 2. 0); // up vector Tutorial: projection 11

GLUT – Open. GL Utility Toolkit (Appendix D) • GLUT is a library that

GLUT – Open. GL 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/cs 248 -04/proj 3/index. html 12

GLUT – Starting Up int main (int argc, char *argv[]) { glut. Init(&argc, argv);

GLUT – Starting Up int main (int argc, char *argv[]) { glut. Init(&argc, argv); glut. Init. Display. Mode (GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA); glut. Init. Window. Size (window. Width, window. Height); glut. Init. Window. Position (0, 0); glut. Create. Window (“ 248 Video Game!"); Set. States(); // Initialize rendering states* Register. Callbacks(); // Set event callbacks* } glut. Main. Loop(); return 0; // Start GLUT * Your code here 13

Setting Up Rendering States • Open. GL is a state machine: polygons are affected

Setting Up Rendering States • Open. GL 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. • gl. Enable (GL_LIGHTING); • gl. Disable (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.

GLUT Event Callbacks • Register functions that are called when certain • events occur. Examples: glut. Display. Func( Display ); glut. Keyboard. Func( Keyboard ); glut. Reshape. Func( Reshape ); glut. Mouse. Func( Mouse ); glut. Passive. Motion. Func( Passive. Func ); glut. Motion. Func( Mouse. Dragged. Func ); glut. Idle. Func( Idle ); 15

Open. GL Buffers • Multiple types of buffers • • Color buffers (front/back, left/right)

Open. GL 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. gl. Clear. Color (0. 0, 0. 0); // Clear color and depth buffers. gl. Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 16

Open. GL Double Buffering • Double buffering: • Draw on back buffer while front

Open. GL 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. • glut. Swap. Buffers(); • Primary purpose: eliminate flicker 17

Open. GL: Normals and Lighting • Open. GL can do lighting computations for you

Open. GL: Normals and Lighting • Open. GL 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. . . gl. Normal 3 fv gl. Vertex 3 fv. . . (n 0); (v 1); (v 2); 18

Open. GL: Lighting (Ch. 5 p. 178) • gl. Enable (GL_LIGHTING); • Open. GL

Open. GL: Lighting (Ch. 5 p. 178) • gl. Enable (GL_LIGHTING); • Open. GL supports a minimum of 8 lights. gl. Enable (GL_LIGHT 0); . . . gl. Enable (GL_LIGHT 7); • 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

Open. GL: Shading • Open. GL supports 2 basic shading models: flat and smooth.

Open. GL: Shading • Open. GL supports 2 basic shading models: flat and smooth. gl. Shade. Model(GL_FLAT); gl. Shade. Model(GL_SMOOTH); 20

Open. GL: Material Properties (Ch. 5) • Material properties are associated with each polygon

Open. GL: Material Properties (Ch. 5) • Material properties are associated with each polygon (corresponding light properties) • gl. Material*(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

Open. GL: Texturing 22

Open. GL: Texturing 22

Open. GL: Texturing • Loading your data • This can come from an image:

Open. GL: 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

Open. GL: Texturing • Mapping the texture to the polygon • specify (s, t)

Open. GL: 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: gl. Tex. Coord 2 f(s, t); t (x 3, y 3, z 3) (x 1, y 1, z 1) 0, 1 1, 1 + 0, 0 s 0, 0 1, 0 (x 0, y 0, z 0) (x 2, y 2, z 2) Tutorial: Texture pg 403 24

Open. GL: Advanced Texturing • Advanced texturing techniques • Mipmapping • Multitextures • Automatic

Open. GL: Advanced Texturing • Advanced texturing techniques • Mipmapping • Multitextures • Automatic texture generation • Let Open. GL determine texture coordinates for you • Environment Mapping • Texture matrix stack • Fragment Shaders • Custom lighting effects 25

Open. GL: Alpha Blending Ch 6, pg 225 • When enabled, Open. GL uses

Open. GL: Alpha Blending Ch 6, pg 225 • When enabled, Open. GL uses the alpha channel to blend a new fragment’s color value with a color in the framebuffer + = New color (r 1, g 1, b 1, a 1) Color in framebuffer (r 0, g 0, b 0, a 0) “source” “destination” ? (r’, g’, b’, a’) • Useful for overlaying textures or other effects 26

Open. GL: Fog Simulate atmospheric effects • gl. Fog (): Sets fog parameters •

Open. GL: Fog Simulate atmospheric effects • gl. Fog (): Sets fog parameters • gl. Enable (GL_FOG); Tutorial: fog 27

Open. GL: Other Features • Display Lists (ch 7): Speed up your game! •

Open. GL: 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 proj 3

Development • On Windows: • Download the GLUT libraries (linked off the proj 3 webpage). • You want to link your project with: opengl 32. lib, glut 32. lib, and glu 32. 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 -l. GLU –lglut • Headers #include <GL/gl. h> #include <GL/glut. h> • Call glut. Report. Errors() once each display loop for debugging. • This will report any errors that may have occurred during rendering, such as an illegal operation in a gl. Begin/gl. End pair. 29

Questions? 30

Questions? 30