CS 248 OpenGL Help Session CS248 Presented by Sean Walker

Slides:



Advertisements
Similar presentations
Computer Graphics - Graphics Programming -
Advertisements

OpenGL CMSC340 3D Character Design & Animation. What is OpenGL? A low-level graphics library specification designed for use with the C and C++ provides…
Computer Graphics CSCE 441
Graphics Pipeline.
Computer Graphics(Fall 2003) COMS 4160, Lecture 7: OpenGL 3 Ravi Ramamoorthi Many slides courtesy Greg Humphreys.
MAT 594CM S2010Fundamentals of Spatial ComputingAngus Forbes Overview Today: - Make sure everyone is set up with an OpenGL environment - OpenGL basics:
CS 248 OpenGL Help Session CS248 Presented by Ian Buck Stanford University Nov. 10, 2002.
OpenGL (Graphics Library) Software Interface to graphics software Allows to create interactive programs that produce color images of moving 3D objects.
CS 248 OpenGL Help Session CS248 Presented by Zak Middleton, Billy Chen Stanford University Nov. 8, 2002.
#4: OpenGL Implementation & Project 2 CSE167: Computer Graphics TAs: Alex Kozlowski & Cameron Chrisman UCSD, Winter 2006.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Programming with OpenGL Part 2: Complete Programs Ed Angel Professor of Computer Science,
OpenGL and Projections
Computer Graphics (Fall 2003) COMS 4160, Lecture 6: OpenGL 2 Ravi Ramamoorthi Many slides courtesy Greg Humphreys.
30/1/2006Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)1 CSC345: Advanced Graphics & Virtual Environments Lecture 1: Introduction.
Interactive 3D Graphics and Virtual Reality Introduction to OpenGL concepts Session 2.
Surface Rendering With OpenGL CS460 Project by Rui Yu 11/30/03.
OpenGL Tutorial Jason Lawrence COS426 Precept Notes 2/16/04.
Lighting & Material. Example 1/5 #include ” glut.h ” void display(); void reshape(int, int); void lighting(); int main(int argc, char** argv) { glutInit(&argc,
Introduction to OpenGL M. Ramanathan STTP CAD 2011Introduction to OpenGL.
Introduction to OpenGL Jian Huang This set of slides are extracted from the Interactive OpenGL Programming course given by Dave Shreine, Ed Angel and Vicki.
OpenGL A Brief Overview. What is OpenGL?  It is NOT a programming language.  It is a Graphics Rendering API consisting of a set of function with a well.
Reference1. [OpenGL course slides by Rasmus Stenholt]
CS380 LAB I OpenGL Donghyuk Kim Reference1. [OpenGL course slides by Rasmus Stenholt] Reference2. [
Computer Graphics Bing-Yu Chen National Taiwan University.
Programming in OpenGL Ryan Holmes CSE 570 February 12 th, 2003.
CAP4730: Computational Structures in Computer Graphics Introduction to OpenGL.
2 COEN Computer Graphics I Introductions n Brad Grantham lecturer lab dude n Dave Shreiner lecturer slave driver.
Lecture 3 OpenGL.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Programming with OpenGL Part 1: Background Ed Angel Professor of Computer Science, Electrical.
Introduction to OpenGL 1. 2 OpenGL A Graphics rendering API introduced in 1992 by Silicon Graphics Inc Provide the low-level functions to access graphics.
1. OpenGL/GLU/GLUT  OpenGL v4.0 (latest) is the “core” library that is platform independent  GLUT v3.7 is an auxiliary library that handles window creation,
CD2012 Principles of Interactive Graphics Lecture 01 Introduction Abir Hussain (Rome: 6.33,Tel , Web:
Lecture 6: 3D graphics Concepts 1  Principles of Interactive Graphics  CMSCD2012  Dr David England, Room 718,  ex 2271 
Interactive Computer Graphics CS 418 MP1: Dancing I TA: Zhicheng Yan Sushma S Kini Mary Pietrowicz Slides Taken from: “An Interactive Introduction to OpenGL.
Computing & Information Sciences Kansas State University CIS 536/636 Introduction to Computer Graphics Lecture 4 of 41 William H. Hsu Department of Computing.
OpenGL Color and Lighting 2003 Spring Keng Shih-Ling.
Computer Graphics Bing-Yu Chen National Taiwan University.
Introduction to OpenGL and GLUT. What’s OpenGL? An Application Programming Interface (API) A low-level graphics programming API – Contains over 250 functions.
OpenGL Lighting Jian-Liang Lin 2002 Hidden-Surface Removal -1 Original Code: while (1) { get_viewing_point_from_mouse_position(); glClear(GL_COLOR_BUFFER_BIT);
Draw a Simple Object. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending.
Viewing and Transformation. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture.
Chapters 5 2 March Classical & Computer Viewing Same elements –objects –viewer –projectors –projection plane.
NoufNaief.net TA: Nouf Al-harbi.
Introduction to OpenGL & HW1 Announcement 劉軒銘, 網媒所 碩二 ICG 2012 Fall.
Computing & Information Sciences Kansas State University Lecture 12 of 42CIS 636/736: (Introduction to) Computer Graphics CIS 636/736 Computer Graphics.
Chap 3 Viewing and Transformation
CSCE 441: Computer Graphics
OpenGL Basic Drawing 2003 Spring Keng Shih-Ling
Lecture 2 Review OpenGL Libraries Graphics Overview Rendering Pipeline OpenGL command structure.
Lecture 7 Midterm Review. OpenGL Libraries gl: Basic OpenGL library, e.g. primitives. glu: OpenGL Utility library, a set of functions to create texture.
1 Programming with OpenGL Part 2: Complete Programs.
OpenGL API 2D Graphic Primitives Angel Angel: Interactive Computer Graphics5E © Addison-Wesley
31/1/2006Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)1 CSC345: Advanced Graphics & Virtual Environments Lecture 2: Introduction.
OpenGL Basic Drawing Jian-Liang Lin A Smidgen of OpenGL Code #include main() { InitializeAWindowPlease(); glClearColor (0.0, 0.0, 0.0, 0.0); glClear.
Introduction to OpenGL (INF 250) Veronika Solteszova et al., UiB Dept. of Informatics,
Computer Graphics I, Fall Programming with OpenGL Part 2: Complete Programs.
CS380 LAB II OpenGL Donghyuk Kim Reference1. [OpenGL course slides by Rasmus Stenholt] Reference2. [
Computer Graphics (Fall 2003) COMS 4160, Lecture 5: OpenGL 1 Ravi Ramamoorthi Many slides courtesy Greg Humphreys.
Jason Lawrence COS426 Precept Notes 2/16/04
Computer Graphics Lecture 33
Reference1. [OpenGL course slides by Rasmus Stenholt]
Programming with OpenGL Part 2: Complete Programs
OpenGL API 2D Graphic Primitives
Programming with OpenGL Part 2: Complete Programs
OpenGL (Open Graphics Library) Mr. B.A.Swamy Assistant Professor Dept of CSE.
גרפיקה ממוחשבת: מבוא ל-OpenGL
Lighting and Shading Lab 8:.
Introduction to OpenGL
Programming with OpenGL Part 2: Complete Programs
Programming with OpenGL Part 2: Complete Programs
Presentation transcript:

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

OpenGL is a software interface to graphics hardware. 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.

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

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)

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

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

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

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

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 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 Multiple types of buffers Clearing 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 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 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) 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 supports 2 basic shading models: flat and smooth. glShadeModel(GL_FLAT); glShadeModel(GL_SMOOTH);

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

OpenGL: Texturing Loading your data Setting texture state 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: 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 0,0 s (x0,y0,z0) (x2,y2,z2) Tutorial: Texture pg 403

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 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”

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

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

Development Headers On Windows: On Linux: 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.

Questions?