cs 248 opengl help session
Skip this Video
Download Presentation
CS 248 OpenGL Help Session

Loading in 2 Seconds...

play fullscreen
1 / 30

CS 248 OpenGL Help Session - PowerPoint PPT Presentation

  • Uploaded on

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

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about 'CS 248 OpenGL Help Session' - walda

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
cs 248 opengl help session

CS 248OpenGL Help Session


Presented by Sean Walker

(adapted from Ian Buck’s slides – see ’03 web page)

Stanford University

Nov. 5, 2004

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

what is opengl
What is OpenGL?

OpenGL is a software interface to graphics hardware.


3-D world


User Interface

Graphics Hardware

(rasterizer, texturing,

lighting, transformations,



Geometry, vertices, normals, colors, texture, etc.

opengl basics
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
specifying object vertices ch 2 p 42 ch 4 p 171
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);
specifying object vertices ch 2 p 42
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);


primitive types in glbegin ch 2 p 44
Primitive Types in glBegin(Ch.2, p.44)
  • Points GL_POINTS
  • Polygons GL_POLYGON

Nate Robins' Tutorial: shapes

(show page 45)

transformations and viewing ch 3
Transformations and Viewing(Ch.3)

OpenGL has 3 different matrix modes:


Choose the matrix with: glMatrixMode(…);

opengl modelview matrix
OpenGL: Modelview matrix
  • Transforms objects within the scene.



glTranslatef(10.5, 0, 0);

glRotatef(45, 0, 0, 1);


  • Remember that the operations are right multiplied, so the transformation just before DrawCube() takes effect first.

Tutorial: transformation

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

(often applied to modelview matrix)

opengl projection matrix11
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

glut opengl utility toolkit appendix d
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:


glut starting up
GLUT – Starting Up

int main (int argc, char *argv[])

{glutInit(&argc, argv);


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

setting up rendering states
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!
glut event callbacks
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 );

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

opengl double buffering
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
opengl normals and lighting
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);


opengl lighting ch 5 p 178
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

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


opengl material properties ch 5
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

opengl texturing23
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.
opengl texturing24
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);














Tutorial: Texture

pg 403

opengl advanced texturing
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
opengl alpha blending ch 6 pg 225
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






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

Tutorial: fog

opengl other features
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
  • 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




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