Introduction to OpenGL (part 1) - PowerPoint PPT Presentation

donat
introduction to opengl part 1 n.
Skip this Video
Loading SlideShow in 5 Seconds..
Introduction to OpenGL (part 1) PowerPoint Presentation
Download Presentation
Introduction to OpenGL (part 1)

play fullscreen
1 / 112
Download Presentation
Introduction to OpenGL (part 1)
243 Views
Download Presentation

Introduction to OpenGL (part 1)

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Introduction to OpenGL(part 1) Ref: OpenGL Programming Guide (The Red Book) Fall 2013 revised

  2. Part 1 Introduction Geometry Viewing and Rendering Pipeline Light & Material Part 2 Display List Alpha Channel Polygon Offset Part 3 Image Texture Mapping Part 4 Framebuffers Selection & Feedback Topics Fall 2013 revised

  3. OPEN GL Introduction Fall 2013 revised

  4. Introduction • 2D/3D graphics API created by SGI (1992) • 250 function calls in C • Cross platform: MacOS, Windows, Linux, Unix, Playstation 3 • Specifications controlled by OpenGL ARB (architecture review board) • Currently (as of Aug 2007): OpenGL 2.1 Aug 2012: OpenGL 4.3 Fall 2013 revised

  5. GL Version Query #include <gl/glew.h> #include <gl/glut.h> #include <iostream> using namespace std; int main (int argc, char** argv) { glutInit (&argc,argv); glewInit(); glutCreateWindow ("t"); cout << "GL vendor: " << glGetString (GL_VENDOR) << endl; cout << "GL renderer: " << glGetString (GL_RENDERER) << endl; cout << "GL version: " << glGetString (GL_VERSION) << endl; cout << "GLSL version: " << glGetString (GL_SHADING_LANGUAGE_VERSION) << endl; // cout << "GL extension: " << glGetString (GL_EXTENSIONS) << endl; } Fall 2013 revised

  6. Client An application issuing graphics commands SERVER Interpret and process commands; Maintain graphics contexts Client-Server Execution Model • client • The application program issuing graphic commands • server • OpenGL that interprets and displays • Can be the same or other computers over the network Fall 2013 revised

  7. GLU (utility) part of OpenGL implementation useful groupings of OpenGL commands GLUT (toolkit) window management handling input events drawing 3-D primitives managing background (idle) process running program (main loop) Interface Codes PUI, MUI, GLUI, … OpenGL Related APIs Fall 2013 revised

  8. OpenGL and Related APIs application program OpenGL Motif widget or similar GLUT GLX, AGLor WGL GLU GL X, Win32, Mac O/S software and/or hardware Fall 2013 revised

  9. File/New as Win32 console application GLUT Installation glut32.dll in Windows/System glut32.lib in lib/ glut.h in include/GL OpenGL on VC6 Fall 2013 revised

  10. VC7 : Visual Studio .NET 2003 VC7 with glut (ref) Fall 2013 revised

  11. VC9 : Visual Studio 2008 VC9 with glut (ref) Fall 2013 revised

  12. GLUT for Win32 (url) This is what you need to run This contains a lot of example codes, from simple to sophisticated Fall 2013 revised

  13. Online Resources • API references • (1) • Redbook • Ver 1.1 (HTML) • Tutorials • (see course homepage) Fall 2013 revised

  14. Example 1: double.c • Study objectives: • Compiling OpenGL program • Basics of double-buffered animation • The order the subroutines execute • Concept of state variables (glColor) • Commands: • Glut basics • glClear • glFlush:force all previous issued commands to begin execution; required for single buffered application Fall 2013 revised

  15. Double-Buffered Animation Fall 2013 revised

  16. Ex: double.c void reshape(int w, int h) { glViewport (0, 0, (GLsizei) w, (GLsizei) h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-50.0, 50.0, -50.0, 50.0, -1.0, 1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } void mouse(int button, int state, int x, int y) { switch (button) { case GLUT_LEFT_BUTTON: if (state == GLUT_DOWN) glutIdleFunc(spinDisplay); break; case GLUT_MIDDLE_BUTTON: case GLUT_RIGHT_BUTTON: if (state == GLUT_DOWN) glutIdleFunc(NULL); break; default: break; } } void init(void) { glClearColor (0.0, 0.0, 0.0, 0.0); glShadeModel (GL_FLAT); } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize (250, 250); glutInitWindowPosition (100, 100); glutCreateWindow (argv[0]); init (); glutDisplayFunc(display); glutReshapeFunc(reshape); glutMouseFunc(mouse); glutMainLoop(); return 0; } #include <GL/glut.h> #include <stdlib.h> static GLfloat spin = 0.0; void display(void) { glClear(GL_COLOR_BUFFER_BIT); glPushMatrix(); glRotatef(spin, 0.0, 0.0, 1.0); glColor3f(1.0, 1.0, 1.0); glRectf(-25.0, -25.0, 25.0, 25.0); glPopMatrix(); glutSwapBuffers(); } void spinDisplay(void) { spin = spin + 2.0; if (spin > 360.0) spin = spin - 360.0; glutPostRedisplay(); } Fall 2013 revised

  17. Graphics Reference • Time a function • Frame rate (FPS) computation • Time-based animation Fall 2013 revised

  18. OPEN GL Geometry Fall 2013 revised

  19. OpenGL Geometry Primitives Fall 2013 revised

  20. Defining Primitives • Each primitive • Bounded by glBegin and glEnd • Defined by the following commands: • Vertex location (glVertex): local (object) coordinates • Most other OpenGL commands are for state-specification: (normal, color, texture coordinates, etc.) Fall 2013 revised

  21. Examples • Commands between Begin and End • Allowable OpenGL commands (next page); others are not allowable • non-OpenGL commands are not restricted Fall 2013 revised

  22. Valid Commands Between glBegin & glEnd Fall 2013 revised

  23. Examples of illegal polygons Restrictions on polygons • simple (no self intersection) • planar: no guarantee of correct rendering for nonplanar “polygons” • convex • no holes Fall 2013 revised

  24. glPointSize (GLfloat) glLineWidth (GLfloat) glLineStipple (factor, pattern) Advanced use of line stipple for neon signs Drawing Styles Fall 2013 revised

  25. glPolygonMode Related: glFrontFace: specifies whether polygons with CW winding, or CCW winding, in window coordinates, are taken to be front-facing glCullFace: specifies whether front- or back-facing facets are candidates for culling [stippled polygon: a simple “texture”] 1 4 F 2 3 glFrontFace (GL_CCW); Drawing Styles (cont) Fall 2013 revised

  26. Concave Polygons • Break concave ones into convex pieces • Manually doing so (as shown next page) • GLU has utility for tessellation ! • [we’ll do so after displaylist] • Use glEdgeFlag () to outline polygons Fall 2013 revised

  27. glEdgeFlag (Glboolean flag) • Indicates whether a vertex should be considered as initializing a boundary edge of a polygon. If flag of a vertex is set GL_TRUE (default), the vertices created are considered to precede boundary edges until this function is called again with flag being GL_FALSE. Hence, v1-v2 is disabled Fall 2013 revised

  28. v1:(-20,20) V4:(-10,-10) V2:(-20,-20) V3:(20,-20) Example of glEdgeFlag Use the same code; change display style only! Fall 2013 revised

  29. [More on TRIANGLE|QUAD_STRIP] • Faces constructed (note: consistent CW/CCW) • Winding according to first face • (v0,v1,v2), (v2,v1,v3),(v2,v3,v4),… • (v0,v1,v3,v2),(v2,v3,v5,v4),(v4,v5,v6,v7) • Normal assignment • Per vertex: as before • Per face: set at strip vertex (the last defining vertex of the face) • Stripification: make strips out of triangular mesh (ref) Fall 2013 revised

  30. Setting normal vectors normal convention: outward-pointing unit vectors should always supply normalized vectors Related call: glEnable (GL_NORMALIZE); enabling system to normalize normal vectors after transforms Geometry construction: avoid T-section HSR (hidden surface removal) with z-buffer (depth buffer) Ex: drawing icosahedron Scale esp. Fall 2013 revised

  31. glScalef(.3,.3,.3); glScalef(3,3,3); Original size Diffuse intensity  n·l Without glEnable (GL_NORMALIZE) Fall 2013 revised

  32. Primitives in GLUT Platonic Solids Other Solids Sphere Cone Cube (Utah) Teapot Fall 2013 revised

  33. Glut Primitives (Torus) r R Fall 2013 revised

  34. Depth Buffer Related Functions • glClear (GL_DEPTH_BIT) • set all pixels to the largest possible distance (default: 1.0) • glClearDepth (1.0) --- default • Depth range: [zNear, zFar]  [0.0, 1.0] • 1.0: set depth to zFar • glDepthFunc: • (fragment) pass the test if its z value has the specified relationship to the value already in the depth buffer • default: GL_LESS • glEnable (GL_DEPTH_TEST) • More on Chap 10 (Framebuffers) Fall 2013 revised

  35. Vertex Arrays The default way to specify geometry in OpenGL-ES • Boost efficiency by • Reducing number of API calls • Reducing repetitive processing on shared vertices • Three steps of using VA: • Enable arrays • glEnableClientState/glDisableClientState • Specify data for the arrays • glVertexPointer, glColorPointer, glNormalPointer, … • Dereference and rendering • glArrayElement(i) • glDrawElements(mode, count, type, indices) • glArrayElements(mode, first, count) Fall 2013 revised

  36. Vertex Array (cont) Dereferencing Enabling and Setting Fall 2013 revised

  37. Vertex Array (cont) Fall 2013 revised

  38. Fall 2013 revised

  39. Fall 2013 revised

  40. The data of vertex array are stored on client side glEnableClientState • GL_COLOR_ARRAY, GL_EDGE_FLAG_ARRAY, GL_INDEX_ARRAY, GL_NORMAL_ARRAY, GL_TEXTURE_COORD_ARRAY, and GL_VERTEX_ARRAY are accepted. Fall 2013 revised

  41. Graphics References • Rendering • Light-material interaction • Hidden surface removal and Z-buffer Fall 2013 revised

  42. Depth Related • OpenGL depth buffer value clamped to [0,1] • 0 at near; 1 at far • glClearDepth(); • Set depth clear value • glGetFloatv (GL_DEPTH_CLEAR_VALUE); • Query current depth clear value • glGetIntegerv (GL_DEPTH_BITS); • 24 [for my platform] • glClear (GL_DEPTH_BUFFER_BIT); • Clear depth buffer Fall 2013 revised

  43. Z-buffer in OpenGL/GLUT MUST do these three things for Z-buffer to work! Fall 2013 revised

  44. Mathematics Reference • Transformation matrices • [Homogeneous coordinates] Fall 2013 revised

  45. OPEN GL Viewing Fall 2013 revised

  46. VIEWING • The camera analogy • Modeling • Viewing • Projection • Viewport Fall 2013 revised

  47. Rendering Pipeline (OpenGL) • stages of vertex transformation Fall 2013 revised

  48. Two Transformation Matrices • ModelView: geometry transformation and viewpoint placement (dual) • Projection: orthographic and perspective Fall 2013 revised

  49. Transform object coords to world coords modeling transformations: glTranslate glRotate glScale glMultMatrix each command generates a 4x4 matrix the current matrix (the one on top of stack) is then post-multiplied by the transform Example order of interpretation: INML v Modeling Transformation Fall 2013 revised

  50. glRotate (30, 0, 0, 1); glTranslate (10, 0, 0); drawPlant( ); glTranslate (10, 0, 0); glRotate (30, 0, 0, 1); drawPlant( ); Hence, ... Fall 2013 revised