1 / 114

Computer Graphics CSC 830

Computer Graphics CSC 830. Lecture notes 2 OpenGL. What is OpenGL?. It is NOT a language. It is a Graphics Rendering API (Application Programmer’s Interface) that is a set of function with well defined interface.

kata
Download Presentation

Computer Graphics CSC 830

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computer GraphicsCSC 830 Lecture notes 2 OpenGL

  2. What is OpenGL? • It is NOT a language. • It is a Graphics Rendering API (Application Programmer’s Interface) that is a set of function with well defined interface. • Whenever we say that a program is OpenGL-based or OpenGL applications, we mean that it is written in some programming language (such as C/C++ or Java) that makes calls to one or more of OpenGL libraries. • OpenGL is Not Object-Oriented. OpenGL API does not make use of features such as overloading that are available in object-oriented languages. -> I will show examples with C language bindings.

  3. A History of OpenGL • Was SGI’s Iris GL – basis for “Open”GL • “Open” standard allowing for wide range hardware platforms • OpenGL v1.0 (1992) • OpenGL v1.1 (1995) • OpenGL v1.5 (latest) • Governed by OpenGL Architecture Review Board (ARB) which has members from such as Silicon Graphics, IBM, and NVIDIA. • “Mesa” – an Open source (http://www.mesa3d.org)

  4. Useful Websites and Books • Official Site http://www.opengl.org • Non official sites • http://nehe.gamedev.net/ • BOOKS • OpenGL : A Primer • OpenGL Red Book & • OpenGL Blue Book

  5. Useful Links • Online Reference manual sites • GL and GLU http://www.mevis.de/~uwe/opengl/opengl.html • GLUT http://pyopengl.sourceforge.net/documentation/manual/reference-GLUT.html

  6. Three View of OpenGL • Programmer’s view • Specifying a set of objects to render • Describing properties of these objects • Defining how these objects should be viewed • State machine • Keeps states that affects appearances of input ie. States determines how the inputs are processed. • Change state (such as color) by using state changing functions • OpenGL uses Rendering Pipeline Model • Models -> Transformer -> Clipper -> Projector -> Rasterizer -> Image

  7. OpenGL API Functions • OpenGL contains over 200 functions • Primitive functions : define the elements (eg. A point, line, polygon, etc) • Attribute functions : control the appearance of primitives (eg. colors, line types, light source, textures.) • Viewing functions : determine the properties of camera. Transformation • Windowing functions: not part of core OpenGL • Other functions

  8. Window Management • OpenGL is meant to be platform independent. i.e. OpenGL is window and operating system independent. • OpenGL does not include any functions for window management, user interaction, and file I/O. • Host environment is responsible for window management.

  9. Window Management API • We need additional libraries to handle window management • GLX/AGL/WGL - glue between OpenGL and windowing systems • GLUT - OpenGL Window Interface/Utility toolkit • AUX - OpenGL Utility Toolkit

  10. OpenGL API Hierarchy

  11. OpenGL Division of Labor • GL - “core” library of OpenGL that is platform independent • GLU - an auxiliary library that handles a variety of graphics accessory functions • GLUT/AUX - utility toolkits that handle window managements

  12. Libraries and Headers All are presented in the C language

  13. Learning OpenGL with GLUT • GLUT is a Window Manager (handles window creation, user interaction, callbacks, etc) • http://www.opengl.org/resources/libraries/glut/spec3/spec3.html • Platform Independent • Makes it easy to learn and write OpenGL programs without being distracted by your environment • All of our discussions will be presented in C/C++ language

  14. Include Header files Include the necessary header files in your code #include <GL/gl.h> // “core”, the only thing is required #include <GL/glu.h> // handles accessory functions #include <GL/glut.h> // handles window managements void main( int argc, char **argv ) { ….. } Only the “core” library (opengl32.lib, gl.h) are required

  15. Link Libraries Link the necessary Libraries to your code • Link GL library • Link opengl32.lib (PC), or -lgl (UNIX) • Link GLU library • Link glu32.lib (PC), or -lglu (UNIX) • Link GLUT library • Link glut32.lib (PC), or -lglut (UNIX)

  16. GLshort A[10]; short A[10]; GLdouble B; double B; Programming Convention : OpenGL Data Types To make it easier to convert OpenGL code from one platform to another, OpenGL defines its own data types that map to normal C data types

  17. Programming Convention : OpenGL Data Types

  18. Programming Convention : OpenGL Function Naming OpenGL functions all follow a naming convention that tells you which library the function is from, and how many and what type of arguments that the function takes <Library prefix><Root command><Argument count><Argument type>

  19. glColor3f(…) gl library Root command, # of arguments, type of arguments Programming Convention : OpenGL Function Naming f: the argument is float type i: the argument is integer type v: the argument requires a vector gl means OpenGL glu means GLU glut means GLUT

  20. Programming Convention : OpenGL Function Naming • Multiple forms of OpenGL functions to support the variety of data types • glVertex3i(ix, iy, iz) • glVertex3f(x, y, z) • glVertex2i(ix, iy) • glVertex2f(x, y) • .. • We shall use the notation glVertex*() to refer to all the forms of the vertex function

  21. Basic OpenGL Coding Framework • Configure GL (and GLUT) - Open window, Display mode, …… • Initialize OpenGL state - background color, light, View positions, …… • Register callback functions - Render, Interaction (keyboard, mouse), …… • Event processing loop - glutMainLoop()

  22. A Sample Program void main (int argc, char **argv) { glutInit (&argc, argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutInitWindowSize (500, 500); glutCreateWindow (“My First Program"); myinit (); glutDisplayFunc ( display ); glutReshapeFunc ( resize ); glutKeyboardFunc ( key ); glutMainLoop (); } 1 2 3 4

  23. 1: Initializing & Creating Window Set up window/display you’re going to use void main (int argc, char **argv) { glutInit (&argc, argv); // GLUT initialization glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); // display model glutInitWindowSize (500, 500); // window size glutCreateWindow (“My First Program"); // create window …… }

  24. GLUT Initializing Functions • Standard GLUT initialization • void glutInit (int *argc, char ** argv) • Display model • void glutInitDisplayMode (unsigned int mode) • Define color model : GLUT_RGB or GLUT_INDEX • Define buffering: GLUT_SINGLE | GLUT_DOUBLE • Window size and position • void glutInitWindowSize (int width, int height) • void glutInitWindowPosition(int x, int y) • - top-left corner of the screen in pixel • Create window • int glutCreateWindow (char *title);

  25. 2: Initializing OpenGL State Set up whatever state you’re going to use void myinit(void) { glClearColor(1.0, 1.0, 1.0, 1.0); // background color glColor3f(1.0, 0.0, 0.0); // line color glMatrixMode(GL_PROJECTION); // followings set up viewing glLoadIdentity(); gluOrtho2D(0.0, 500.0, 0.0, 500.0); glMatrixMode(GL_MODELVIEW); }

  26. Event Loops and Callback Functions • Interactive programs react to the events such as mouse or keyboard events and window events. • Callback Function - Routine to call when something happens (eg. window resize, redraw, user input, etc) • GLUT uses a callback mechanismto do its event processing

  27. GLUT Callback Functions • Contents of window need to be refreshed glutDisplayFunc() • Window is resized or moved glutReshapeFunc() • Key action glutKeyboardFunc() • Mouse button action glutMouseFunc() • Mouse moves while a button is pressed glutMotionFunc() • Mouse moves regardless of mouse button state glutPassiveMouseFunc() • Called when nothing else is going on glutIdleFunc()

  28. 3: Register Callback Functions Set up any callback function you’re going to use void main (int argc, char **argv) { …… glutDisplayFunc ( display ); // display callback glutReshapeFunc ( resize ); // window resize callback glutKeyboardFunc ( key ); // keyboard callback …… }

  29. Rendering Callback It’s here that does all of your OpenGL rendering void display( void ) { int k; glClear(GL_COLOR_BUFFER_BIT); for( k=0; k<5000; k++) …… }

  30. Window Resize Callback It’s called when the window is resized or moved void resize(int w, int h) { …… display(); }

  31. Keyboard Input Callback It’s called when a key is struck on the keyboard void key( char mkey, int x, int y ) { switch( mkey ) { case ‘q’ : exit( EXIT_SUCCESS ); break; …… } }

  32. 4. Event Process Loop This is where your application receives events, and schedules when callback functions are called void main (int argc, char **argv) { …… glutMainLoop(); }

  33. 2D Geometric Primitives • Primitives – fundamental entities such as point and polygons • Basic types of geometric primitives • Points • Line segments • Polygons

  34. GL_POINTS GL_LINES GL_LINE_STRIP GL_LINE_LOOP GL_POLYGON GL_TRIANGLES GL_TRIANGLE_FAN 2D Geometric Primitives GL_QUADS All geometric primitives are specified by vertices

  35. Geometry Commands • glBegin(GLenum type) marks the beginning of a vertex-data list that describes a geometric primitives • glEnd (void) • marks the end of a vertex-data list • glVertex*(…) • specifies vertex for describing a geometric object

  36. Specifying Geometric Primitives glBegin( type ); glVertex*(…); …… glVertex*(…); glEnd(); type determines how vertices are combined

  37. GL_POINTS GL_LINES GL_LINE_STRIP GL_LINE_LOOP GL_POLYGON GL_TRIANGLES GL_TRIANGLE_FAN Types GL_QUADS

  38. Types GL_POINTS GL_LINES : each successive pair for a ling segment GL_LINE_STRIP: vertices defining a sequence of line segments GL_LINE_LOOP: GL_LINE_STRIP + the last vertex connects to the first GL_POLYGON : sequence of vertices of polygon, filled GL_QUADS: each successive group of four vertices for a quadrilaterals GL_TRIANGLES: each successive group of three vertices for a triangle GL_TRIANGLE_FAN: first three vertices for the first triangle and each subsequent vertex with the first vertex and the previous vertex for the next triangle

  39. Attribute : Line void glLineWidth(GLfloat width) • Set the width in pixel. Default is 1.0 void glLineStripple(GLint factor, GLushort pattern)

  40. Rectangles • glRect*() – defines 2D filled rectangle aligned with the axes. void glRect{sifd} (TYPE x1, TYPE y1, TYPE x2, TYPE y2);

  41. Example void drawSquare () { glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_POLYGON); glVertex2f ( 0.0, 0.0 ); glVertex2f ( 1.0, 0.0 ); glVertex2f ( 1.1, 1.1 ); glVertex2f ( 0.0, 1.0 ); glEnd(); glFlush(); // force the renderer to output the results }

  42. OpenGL Color • There are two color models in OpenGL • RGB Color (True Color) • Indexed Color (Color map)

  43. RGB Color Model • R, G, B components are stored for each pixel

  44. RGB Color Red Green Blue

  45. How Many Colors? Color number = For example: 4-bit color = 16 colors 8-bit color = 256 colors 24-bit color = 16.77 million colors

  46. How Much Memory? Buffer size = width * height *color depth For example: If width = 640, height = 480, color depth = 24 bits Buffer size = (640 * 480 * 2) bytes If width = 640, height = 480, color depth = 32 bits Buffer size = (640 * 480 * 4) bytes

  47. Alpha Component Alpha value A value indicating the pixels opacity 0 usually represents totally transparent and the 1 represents completely opaque Alpha buffer Hold the alpha value for every pixel Alpha values are commonly represented in 8 bits, in which case transparent to opaque ranges from 0 to 255

  48. RGB Color Commands • glColor*(…) specifies vertex colors • glClearColor(r, g, b, a) • sets current color for cleaning color buffer

  49. Example void drawLine (GLfloat *color) { glColor3fv ( color ); glBegin(GL_LINE); glVertex2f ( 0.0, 0.0 ); glVertex2f ( 1.0, 0.0 ); glEnd(); }

  50. Example void drawLine (GLfloat *color) { glBegin(GL_LINE); glColor3f(1.0,0.0,0.0 ); glVertex2f ( 0.0, 0.0 ); glColor3f(0.0,0.0,1.0); glVertex2f ( 1.0, 0.0 ); glEnd(); }

More Related