CS 248 OpenGL Help Session CS248 Presented by Zak Middleton, Billy Chen Stanford University Nov. 8, 2002.

Slides:



Advertisements
Similar presentations
OpenGL CMSC340 3D Character Design & Animation. What is OpenGL? A low-level graphics library specification designed for use with the C and C++ provides…
Advertisements

Graphics Pipeline.
Computer Graphics(Fall 2003) COMS 4160, Lecture 7: OpenGL 3 Ravi Ramamoorthi Many slides courtesy Greg Humphreys.
OpenGL Son of the Survival Guide. Last Time on OpenGL Windowing … glut Rendering Primatives Transformations Projections State Management.
MAT 594CM S2010Fundamentals of Spatial ComputingAngus Forbes Overview Today: - Make sure everyone is set up with an OpenGL environment - OpenGL basics:
What is OpenGL? Low level 2D and 3D Graphics Library Competitor to Direct3D (the rendering part of DirectX) Used in: CAD, virtual reality, scientific.
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.
#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.
CS 248 OpenGL Help Session CS248 Presented by Sean Walker
Surface Rendering With OpenGL CS460 Project by Rui Yu 11/30/03.
CS 4731: Computer Graphics Lecture 17: Texturing Emmanuel Agu.
3D Rendering with JOGL Introduction to Java OpenGL Graphic Library By Ricardo Veguilla
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.
3D coordinate systems X Y Z Right-Hand Coordinate System X Y Z Left-Hand Coordinate System OpenGL uses this! Direct3D uses this!
Computer Graphics Texture Mapping Eriq Muhammad Adams
OpenGL Shading Language (Advanced Computer Graphics) Ernest Tatum.
2IV60 Computer Graphics set 10: Texture mapping Jack van Wijk TU/e.
Computer Graphics Ben-Gurion University of the Negev Fall 2012.
Lecture 3 OpenGL.
CS380 LAB IV OpenGL Jonghyeob Lee Reference1. [OpenGL course slides by Rasmus Stenholt] Reference2. [
Homogeneous Form, Introduction to 3-D Graphics Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Monday, October 20,
Ch 2 Graphics Programming page 1 CSC 367 Coordinate Systems (2.1.2) Device coordinates, or screen coordinates (pixels) put limitations on programmers and.
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,
1 Figures are extracted from Angel's book (ISBN x) The Human Visual System vs The Pinhole camera Human Visual System Visible Spectrum Pinhole.
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.
Modeling with OpenGL Practice with OpenGL transformations.
Computer Science Term 1, 2006 Tutorial 2 Assignment 3 – The Virtual World.
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.
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
OpenGL Basic Drawing 2003 Spring Keng Shih-Ling
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 Graphics Programming. Graphics API.
Introduction to OpenGL (INF 250) Veronika Solteszova et al., UiB Dept. of Informatics,
Computer Graphics I, Fall Programming with OpenGL Part 2: Complete Programs.
OpenGL LAB III.
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.
Texture Mapping CEng 477 Introduction to Computer Graphics.
Computer Graphics Lecture 34. OpenGL Programming II Taqdees A. Siddiqi
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.
Lab 3 Geometric Drawing Lab 3 Geometric Drawing.
Introduction to OpenGL
OpenGL program.
Programming with OpenGL Part 2: Complete Programs
Programming with OpenGL Part 2: Complete Programs
Programming with OpenGL Part 2: Complete Programs
Presentation transcript:

CS 248 OpenGL Help Session CS248 Presented by Zak Middleton, Billy Chen Stanford University Nov. 8, 2002

2 Overview Basic primitives and rendering in OpenGL Basic primitives and rendering in OpenGL Transformations and viewing Transformations and viewing GLUT and the interaction / display loop GLUT and the interaction / display loop More primitives and rendering More primitives and rendering Development tips Development tips Note: all page references refer to the OpenGL Programming Guide, 3 rd Edition ver. 1.2 (aka “The Red Book”) unless noted otherwise.

3 Getting Started… OpenGL is a cross platform 3D graphics library that takes advantage of specialized graphics hardware. OpenGL is a cross platform 3D graphics library that takes advantage of specialized graphics hardware. Read the Red Book! It’s a great resource and is very readable. 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. OpenGL is a state machine: polygons are affected by the current color, transformation, drawing mode, etc. Two scenes rendered with a shading language developed at Stanford.

4 Specifying Object Vertices (Ch.2 p.42) Every object is specified by vertices Every object is specified by vertices glVertex3f (2.0, 4.1, 6.0); // specifies a vertex at the x, y, z coordinate (2.0, 4.1, 6.0). // The “3f” means 3 floating point coordinates. Other examples: glVertex2i (4, 5); // 2 integers for x and y. z = 0. glVertex3fv (vector); // float vector[3] = {5.0, 3.2, 5.0}; Current color affects any vertices Current color affects any vertices glColor3f (0.0, 0.5, 1.0); // no Red, half-intensity Green, full-intensity Blue Vertices are specified only between glBegin(mode) and glEnd(), usually in a counter-clockwise order for polygons. 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();

5 Primitive Types in glBegin (Ch.2, p.44) PointsGL_POINTS PointsGL_POINTS LinesGL_LINES, GL_LINE_STRIP, GL_LINE_LOOP LinesGL_LINES, GL_LINE_STRIP, GL_LINE_LOOP TrianglesGL_TRIANGLES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN TrianglesGL_TRIANGLES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN QuadsGL_QUADS, GL_QUAD_STRIP QuadsGL_QUADS, GL_QUAD_STRIP PolygonsGL_POLYGON PolygonsGL_POLYGON (show page 45) glBegin(GL_LINES); [lots of glVertex calls]; glEnd(); glBegin(GL_QUADS); [lots of glVertex calls]; glEnd();

6 Transformations and Viewing (Ch.3) OpenGL has 3 different matrix modes: GL_MODELVIEW GL_PROJECTION GL_TEXTURE For example, choose to act on the projection matrix with: glMatrixMode(GL_PROJECTION); The Modelview matrix is used for your object transformations. The Modelview matrix is used for your object transformations. The Projection matrix sets up the perspective transformation. It is usually set once at the beginning of your program. The Projection matrix sets up the perspective transformation. It is usually set once at the beginning of your program. The Texture matrix can be used to warp textures (not commonly used). The Texture matrix can be used to warp textures (not commonly used).

7 OpenGL: Modelview matrix Transforms the viewpoint and objects within the scene. Transforms the viewpoint and objects within the scene. Example: glMatrixMode(GL_MODELVIEW); // set the current matrix Example: glMatrixMode(GL_MODELVIEW); // set the current matrix glLoadIdentity(); // load the identity matrix glTranslatef(10.5, 0, 0); // translate 10.5 units along x-axis glRotatef(45, 0, 0, 1); // rotate 45 degrees CCW around z-axis DrawCube(); // cube is defined centered around origin Where will this end up? Where will this end up? Answer: on the x-axis, rotated 45 degrees CCW. First image on page 107, fig 3-4. Remember that the operations are right multiplied, so the transformation just before DrawCube() takes effect first. Answer: on the x-axis, rotated 45 degrees CCW. First image on page 107, fig 3-4. Remember that the operations are right multiplied, so the transformation just before DrawCube() takes effect first. You can use gluLookAt(…) (page 119) in addition to rotations and translations to affect the viewpoint. You can use gluLookAt(…) (page 119) in addition to rotations and translations to affect the viewpoint.

8 OpenGL: Projection Matrix Sets up a perspective projection. (page 123) Sets up a perspective projection. (page 123) A few available options: A few available options: glFrustrum (...); // sets up a user defined viewing frustrum gluPerspective (fovy, aspect, near, far); // calculates viewing frustrum for you, given field-of-view in degrees, aspect ratio, and near and far clipping planes. glOrtho (...); // creates orthographic (parallel) projection. Useful for 2D rendering. Example: glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(64, (float)windowWidth / (float)windowHeight, 4, 4096); Example: glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(64, (float)windowWidth / (float)windowHeight, 4, 4096);

9 GLUT – OpenGL Utility Toolkit (Appendix D) GLUT is a library that handles system events and windowing across multiple platforms, and also provides some nice utilities. We strongly suggest you use it. Find it from the proj3 web page. GLUT is a library that handles system events and windowing across multiple platforms, and also provides some nice utilities. We strongly suggest you use it. Find it from the proj3 web page. 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 any rendering states (your code). RegisterCallbacks(); // Set up event callbacks (your code, coming up). glutMainLoop(); // Transfer control to GLUT. Doesn’t return. return 0; }

10 Setting Up Rendering States OpenGL is a state machine: polygons are affected by the current color, transformation, drawing mode, etc.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.Enable and disable features such as lighting, texturing, and alpha blending. glEnable (GL_LIGHTING); // enable lighting (disabled by default) Forgetting to enable something is a common source of bugs! Make sure you enable any features that you need (list of defaults is in Appendix B).Forgetting to enable something is a common source of bugs! Make sure you enable any features that you need (list of defaults is in Appendix B).

11 GLUT Event Callbacks Register functions that are called when certain events occur. Register functions that are called when certain events occur.Examples: glutDisplayFunc( Display );// called when its time to draw glutKeyboardFunc( Keyboard );// receives key input glutReshapeFunc( Reshape );// called when window reshapes glutMouseFunc( Mouse );// called when button changes glutPassiveMotionFunc( PassiveFunc );// mouse moves, no buttons glutMotionFunc( MouseDraggedFunc );// mouse moves, some buttons glutIdleFunc( Idle );// called whenever idle

12 OpenGL – Depth Buffer, Double Buffer Buffers store color and depth Buffers store color and depth Allows Hidden Surface Removal, so there is proper ordering of objects in 3D space. This will be discussed later in the course. 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(); // called at the end of rendering Clearing the 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); Clearing the 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);

13 GLUT – Code Demo

14 OpenGL: Normals and Lighting OpenGL can simulate lighting for you, given some information on the geometry. Specify vertex normals as you specify geometry. OpenGL can simulate lighting for you, given some information on the geometry. Specify vertex normals as you specify geometry. Normal vectors should be of unit length (normalized) in most cases. // each vertex has a different normal here glColor3f (0.8, 1.0, 0.5); glBegin(GL_TRIANGLES); glNormal3fv (n0); glVertex3fv (v0); glNormal3fv (n1); glVertex3fv (v1); glNormal3fv (n2); glVertex3fv (v2); glEnd(); // all vertices have the same normal here glBegin(GL_TRIANGLES); glNormal3fv (n0); glVertex3fv (v0); glVertex3fv (v1); glVertex3fv (v2); glEnd(); Normal vectors should be of unit length (normalized) in most cases. // each vertex has a different normal here glColor3f (0.8, 1.0, 0.5); glBegin(GL_TRIANGLES); glNormal3fv (n0); glVertex3fv (v0); glNormal3fv (n1); glVertex3fv (v1); glNormal3fv (n2); glVertex3fv (v2); glEnd(); // all vertices have the same normal here glBegin(GL_TRIANGLES); glNormal3fv (n0); glVertex3fv (v0); glVertex3fv (v1); glVertex3fv (v2); glEnd();

15 OpenGL: Lighting (Ch.5 p.173) glEnable (GL_LIGHTING); glEnable (GL_LIGHTING); OpenGL supports a minimum of 8 lights. OpenGL supports a minimum of 8 lights. glEnable (GL_LIGHT0);... glEnable (GL_LIGHT7); Lights have a position, type, and color, among other things. Lights have a position, type, and color, among other things. Position: Position: float light0Position[4] = {1.0, 0.0, 4.0, 1.0}; glLightfv (GL_LIGHT0, GL_POSITION, light0Position); Types of lights are point light, directional light, and spotlight. The fourth component of position (1.0 above) determines the type. 0 is for directional lights, 1 is for point/spot lights. (page 187) Types of lights are point light, directional light, and spotlight. The fourth component of position (1.0 above) determines the type. 0 is for directional lights, 1 is for point/spot lights. (page 187) Color has a few components: Ambient, Diffuse, Specular. Read about them in the text. Color has a few components: Ambient, Diffuse, Specular. Read about them in the text.

16 OpenGL: Lighting (cont.) OpenGL supports 2 basic shading models: flat and smooth. OpenGL supports 2 basic shading models: flat and smooth. glShadeModel(GL_FLAT);glShadeModel(GL_SMOOTH); Lighting calculations can be expensive, so investigate other options (ie lightmaps) if needed. Lighting calculations can be expensive, so investigate other options (ie lightmaps) if needed.

17 OpenGL: Material Properties (Ch.5) You can specify different material properties for different polygons, changing the effect of lights. You can specify different material properties for different polygons, changing the effect of lights. Use glMaterial*(GLenum face, GLenum pname, TYPE param); Some properties ( pname ), page 202: Some properties ( pname ), page 202: 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) Color plate 17 in the book shows a few examples. Color plate 17 in the book shows a few examples.

18 OpenGL: Texturing

19 OpenGL: Texturing

20 OpenGL: Texturing

21 OpenGL: Texturing

22 OpenGL: Texturing Loading your data Loading your data this can come from an image: ppm, tiff create at run time final result is always an array Setting texture state Setting texture state creating texture names, scaling the image/data, building Mipmaps, setting filters, etc. Mapping the texture to the polygon Mapping the texture to the polygon specify s,t coordinates for polygon vertices

23 OpenGL: Texturing Loading your data Loading your data this can come from an image: ppm, tiff libtiff, libppm, etc. remember the ordering of color channels and bits per channel! ie: RGBA, or AGBR, 32 bits or 8 bits? You can tell OpenGL how to read your data by setting certain texture state (see next slide) create at run time procedural textures, 3D textures, adding specular highlights final result is always an array

24 OpenGL: Texturing Setting texture state Setting texture state create texture names glGenTextures(int num, int* texNames) glBindTexture(GL_TEXTURE_2D, texName); Tell OpenGL how to read your array glPixelStorei(GL_UNPACK_SWAP_BYTES, int num); glPixelStorei(GL_UNPACK_ALIGNMENT, int num); Scale your array to be 2 n +2(b), b = {0,1} if you have a border or not gluScaleImage(GL_RGBA, w0, h0, GL_UNSIGNED_BYTE, img, w1, h1, GL_UNSIGNED_BYTE, imgScaled) gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, w0, h0, GL_RGBA, GL_UNSIGNED_BYTE, img);

25 OpenGL: Texturing Setting texture state (cont) Setting texture state (cont) Tell OpenGL what to do when the s,t values are not within [0,1]x[0,1] range. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); GL_CLAMP: any values larger than 1.0 are clamped to 1.0 GL_REPEAT: wrap larger values to the beginning of the texture (see OpenGL book, pg 411) Set the filters for minification/magnification glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); other parameters: GL_LINEAR, other mipmap options

26 OpenGL: Texturing Setting texture state (cont) Setting texture state (cont) Tell OpenGL about your data array (image, etc.) glTexImage2D(GL_TEXTURE_2D, int lod, int num_components, width, height, border, format_of_data_pixel, size_of_each_channel, img_array) If you used to gluBuild2DMipmaps scale your image and create a multi-resolution pyramid of textures, then you do NOT need to use glTexImage2D. The gluBuild2DMipmaps command will already tell OpenGL about your array.

27 OpenGL: Texturing Mapping the texture to the polygon 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: 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) glTexCoord2f(s,t); +

28 OpenGL: Texturing Let’s look at code! Let’s look at code!

29 OpenGL: Texturing Advanced Texture techniques Advanced Texture techniques Multitextures automatic texture generation Let OpenGL determine texture coordinates for you Environment Mapping Texture matrix stack

30 OpenGL: Alpha Blending When enabled, OpenGL uses the alpha channel to blend a new fragment’s color value with a color in the framebuffer When enabled, OpenGL uses the alpha channel to blend a new fragment’s color value with a color in the framebuffer New color Color in framebuffer += ? (r1,g1,b1,a1)(r0,g0,b0,a0) (r’,g’,b’,a’) r’ = a1*r1 + (1-a1)*r0 “source”“destination” glEnable(GL_BLEND); glBlendFunc(GL_ONE, GL_ZERO); …draw green square … glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); …draw brown square with alpha = 0.5…

31 OpenGL: Alpha Blending AND Textures Alpha blending with multiple textures Alpha blending with multiple textures one way to do multi-pass rendering number of “texture passes” over a polygon is independent of the maximum number of multi- texture units on the graphics card GeForce 2 has only 2 texture units! slower because geometry is sent n times to the card for n texture passes demo and code if you want to see it

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

33 Questions?