30
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

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

  • View
    220

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 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

CS 248OpenGL Help Session

CS248Presented by Sean Walker (adapted from Ian Buck’s slides – see ’03 web page)Stanford UniversityNov. 5, 2004

Page 2: 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

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 tipsNote: all page references refer to the OpenGL Programming Guide, 4th Edition ver. 1.4(aka “The Red Book”) unless noted otherwise.

Page 3: 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

3

What is OpenGL?

OpenGL is a software interface to graphics hardware.

Application3-D worldSimulation

User Interface

OpenGLGraphics Hardware

(rasterizer, texturing, lighting, transformations,

etc.) Geometry, vertices, normals, colors,

texture, etc.

Page 4: 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

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

Page 5: 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

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

Page 6: 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

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

Page 7: 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

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

(show page 45) Nate Robins' Tutorial: shapes

Page 8: 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

8

Transformations and Viewing (Ch.3)

OpenGL has 3 different matrix modes:• GL_MODELVIEW

• GL_PROJECTION

• GL_TEXTURE

Choose the matrix with:

glMatrixMode(…);

Page 9: 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

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

Page 10: 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

10

OpenGL: Projection Matrix

• Sets up a perspective projection. (page 127)

•glFrustrum (...);•gluPerspective (fovy, aspect, near, far);

•glOrtho (...);•gluLookAt (...);

(often applied to modelview matrix)

Page 11: 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

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

Page 12: 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

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

Page 13: 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

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 GLUTreturn 0;

}

* Your code here

Page 14: 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

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!

Page 15: 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

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

Page 16: 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

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

Page 17: 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

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

Page 18: 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

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

...

Page 19: 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

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

Page 20: 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

20

OpenGL: Shading

• OpenGL supports 2 basic shading models: flat and smooth.

glShadeModel(GL_FLAT); glShadeModel(GL_SMOOTH);

Page 21: 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

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

Page 22: 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

22

OpenGL: Texturing

Page 23: 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

23

OpenGL: Texturing

• Loading your dataLoading your data• This can come from an image: ppm, tiff

• Or create at run time

• Final result is always an array

• Setting texture stateSetting texture state• Creating texture names with “binding”, scaling the

image/data, building Mipmaps, setting filters, etc.

Page 24: 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

24

OpenGL: Texturing

• Mapping the texture to the polygonMapping 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);

s

t

0,0

1,1

0,0

1,1

(x0,y0,z0)

(x1,y1,z1)

1,0

0,1

(x2,y2,z2)

(x3,y3,z3)

+

Tutorial: Texturepg 403

Page 25: 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

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

Page 26: 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

26

OpenGL: Alpha Blending Ch 6, pg 225

• When enabled, OpenGL uses the alpha When enabled, OpenGL uses the alpha channel to blend a new fragment’s color value channel to blend a new fragment’s color value with a color in the framebufferwith a color in the framebuffer

• Useful for overlaying textures or other effectsUseful for overlaying textures or other effects

New color Color in framebuffer

+ = ?

(r1,g1,b1,a1) (r0,g0,b0,a0)(r’,g’,b’,a’)

“source” “destination”

Page 27: 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

27

OpenGL: Fog

Tutorial: fog

Simulate atmospheric effects

•glFog (): Sets fog parameters

•glEnable (GL_FOG);

Page 28: 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

28

OpenGL: Other Features

• Display Lists (ch 7): Speed up your game!Display Lists (ch 7): Speed up your game!

• Quadrics (ch 11): Pre-made objectsQuadrics (ch 11): Pre-made objects• Also look at GLUT’s objects

• Evaluators (ch 12): Bezier curves and Evaluators (ch 12): Bezier curves and surfacessurfaces

• Selection (ch 13): Clicking on game Selection (ch 13): Clicking on game objects with a mouse objects with a mouse

Page 29: 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

29

Development

• On Windows: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:On Linux:• GLUT is already installed on the graphics lab PCs.

• In your Makefile, compile with flags: -L/usr/lib -lGL -lGLU –lglut

• HeadersHeaders#include <GL/gl.h>#include <GL/glu.h>#include <GL/glut.h>

• Call glutReportErrors() once each display loop for debugging.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.

Page 30: 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

30

Questions?