introduction to opengl introduction to opengl
play

Introduction to OpenGL Introduction to OpenGL Graphics API Window - PDF document

Falko Kuester BERKELEY DAVIS IRVINE LOS ANGELES RIVERSIDE SAN DIEGO SAN FRANCISCO SANTA BARBARA SANTA CRUZ ECE 104 ECE 104 Fundamentals of Computer Graphics Fundamentals of Computer Graphics OpenGL Slides courtesy


  1. Falko Kuester BERKELEY • DAVIS • IRVINE • LOS ANGELES • RIVERSIDE • SAN DIEGO • SAN FRANCISCO SANTA BARBARA • SANTA CRUZ ECE 104 ECE 104 Fundamentals of Computer Graphics Fundamentals of Computer Graphics OpenGL Slides courtesy of Dave Shreine, Ed Angel and Vicki Shreiner (SIGGRAPH 2001). Introduction to OpenGL Introduction to OpenGL � Graphics API � Window system independent � Operating system independent � Geometric and image primitives 1

  2. Falko Kuester OpenGL Architecture OpenGL Architecture Per Vertex Polynomial Operations & Evaluator Primitive Assembly Display Per Fragment Frame CPU Rasterization List Operations Buffer Texture Memory Pixel Operations OpenGL as a Renderer OpenGL as a Renderer � Geometric primitives – points, lines, triangles, quadrilaterals, polygons � Image Primitives – images and bitmaps – separate pipeline for images and geometry � Rendering depends on state – Colors – Materials – Lights 2

  3. Falko Kuester Related APIs Related APIs � AGL, GLX, WGL – glue between OpenGL and windowing systems � GLU (OpenGL Utility Library) – part of OpenGL – NURBS, tessellators, quadric shapes, etc. � GLUT (OpenGL Utility Toolkit) – portable windowing API – not officially part of OpenGL OpenGL and Related APIs OpenGL and Related APIs application program OpenGL Motif GLUT widget or similar GLX, AGL GLU or WGL GL X, Win32, Mac O/S software and/or hardware 3

  4. Falko Kuester Preliminaries Preliminaries � Libraries � Headers Files – #include <GL/gl.h> – #include <GL/glu.h> – #include <GL/glut.h> GLUT Basics GLUT Basics Application Structure � 1. Configure and open window 2. Initialize OpenGL state 3. Register input callback functions • render • resize • input: keyboard, mouse, etc. 4. Enter event processing loop 4

  5. Falko Kuester GLUT Display Modes GLUT Display Modes GLUT_RGBA Select an RGBA mode window. This is the default if neither GLUT_RGBA nor GLUT_INDEX are specified. GLUT_RGB same as GLUT_RGBA. GLUT_INDEX Select color index window mode. This overrides GLUT_RGBA. GLUT_SINGLE Select a single buffered window. This is the default. GLUT_DOUBLE Select a double buffered window. This overrides GLUT_SINGLE. GLUT_ACCUM Select a window with an accumulation buffer. GLUT_ALPHA Select a window with an alpha component to the color buffer(s). GLUT_DEPTH Select a window with a depth buffer. GLUT_STENCIL Select a window with a stencil buffer. GLUT_MULTISAMPLE Select a window with multismapling support. GLUT_STEREO Select a stereo window. GLUT_LUMINANCE Select a stereo window with a "luminance" color model Sample Program Sample Program void main( int argc, char** argv ) { int mode = GLUT_RGB|GLUT_DOUBLE; glutInitDisplayMode( mode ); glutCreateWindow( argv[0] ); init(); glutDisplayFunc( display ); glutReshapeFunc( resize ); glutKeyboardFunc( key ); glutIdleFunc( idle ); glutMainLoop(); } 5

  6. Falko Kuester OpenGL Initialization OpenGL Initialization � Configure the state machine void init( void ) { glClearColor( 0.0, 0.0, 0.0, 1.0 ); glClearDepth( 1.0 ); glEnable( GL_LIGHT0 ); glEnable( GL_LIGHTING ); glEnable( GL_DEPTH_TEST ); } GLUT Callback Functions GLUT Callback Functions � Routine to call when something happens – window resize or redraw – user input – Animation � Register callbacks with GLUT glutDisplayFunc( display ); glutIdleFunc( idle ); glutKeyboardFunc( keyboard ); 6

  7. Falko Kuester Rendering Callback Rendering Callback � Do all of your drawing here glutDisplayFunc( display ); void display( void ) { glClear( GL_COLOR_BUFFER_BIT ); glBegin( GL_TRIANGLE_STRIP ); glVertex3fv( v[0] ); glVertex3fv( v[1] ); glVertex3fv( v[2] ); glVertex3fv( v[3] ); glEnd(); glutSwapBuffers(); } Idle Callbacks Idle Callbacks � Use for animation and continuous update glutIdleFunc( idle ); void idle( void ) { t += dt; glutPostRedisplay(); } 7

  8. Falko Kuester User Input Callbacks User Input Callbacks � Process user input glutKeyboardFunc( keyboard ); void keyboard( char key, int x, int y ) { switch( key ) { case ‘q’ : case ‘Q’ : exit( EXIT_SUCCESS ); break; case ‘r’ : case ‘R’ : rotate = GL_TRUE; break; } } Elementary Rendering Elementary Rendering � Geometric Primitives � OpenGL State Machine � OpenGL Buffers 8

  9. Falko Kuester OpenGL Geometric Primitives OpenGL Geometric Primitives � All geometric primitives are specified by vertices GL_LINES GL_LINES GL_POLYGON GL_POLYGON GL_LINE_STRIP GL_LINE_STRIP GL_LINE_LOOP GL_LINE_LOOP GL_POINTS GL_POINTS GL_TRIANGLES GL_TRIANGLES GL_QUADS GL_QUADS GL_QUAD_STRIP GL_QUAD_STRIP GL_TRIANGLE_FAN GL_TRIANGLE_FAN GL_TRIANGLE_STRIP GL_TRIANGLE_STRIP Simple Example Simple Example void drawRhombus( GLfloat color[] ) { glBegin( GL_QUADS ); glColor3fv( color ); glVertex2f( 0.0, 0.0 ); glVertex2f( 1.0, 0.0 ); glVertex2f( 1.5, 1.118 ); glVertex2f( 0.5, 1.118 ); glEnd(); } 9

  10. Falko Kuester OpenGL Command Formats OpenGL Command Formats glVertex3fv( glVertex3fv( v v ) ) Data Type Vector Number of Number of Data Type Vector components components b - b - byte byte omit “v” for omit “v” for ub - ub - unsigned byte unsigned byte scalar form scalar form 2 2 - - (x,y) (x,y) s - - short short s 3 3 - - (x,y,z) (x,y,z) us us - - unsigned short unsigned short 4 - 4 - (x,y,z,w) (x,y,z,w) glVertex2f( x, y ) glVertex2f( x, y ) i - - int int i ui - ui - unsigned int unsigned int f - f - float float d - - double double d Specifying Geometric Primitives Specifying Geometric Primitives � Primitives are specified using glBegin( primType glBegin( primType ); ); glEnd(); glEnd(); – primType determines how vertices are combined GLfloat red, greed, blue; GLfloat red, greed, blue; Glfloat coords[3]; Glfloat coords[3]; glBegin( primType glBegin( primType ); ); for ( i = 0; i < nVerts; ++i ) { for ( i = 0; i < nVerts; ++i ) { glColor3f( red, green, blue ); glColor3f( red, green, blue ); glVertex3fv( coords ); glVertex3fv( coords ); } } glEnd(); glEnd(); 10

  11. Falko Kuester OpenGL Color Models OpenGL Color Models Per Poly. Per Poly. Vertex Vertex � RGBA or Color Index Raster Frag FB CPU DL Raster Frag FB CPU DL Texture Texture Pixel Pixel color index mode Red Green Blue 0 Display 1 1 2 2 4 8 3 �� ��� 16 24 123 219 74 �� 25 26 ��� RGBA mode Shapes Tutorial Shapes Tutorial 11

  12. Falko Kuester Controlling Rendering Appearance Controlling Rendering Appearance � From Wireframe to Texture Mapped OpenGL’s State Machine OpenGL’s State Machine � All rendering attributes are encapsulated – rendering styles – shading – lighting – texture mapping 12

  13. Falko Kuester Manipulating OpenGL State Manipulating OpenGL State � Appearance is controlled by current state for each ( primitive to render ) { update OpenGL state render primitive } � Manipulating vertex attributes is most common way to manipulate state glColor*() / glIndex*() glColor*() / glIndex*() glNormal*() glNormal*() glTexCoord*() glTexCoord*() Controlling current state Controlling current state � Setting State glPointSize( size glPointSize( size ); ); glLineStipple( repeat repeat , , pattern pattern ); ); glLineStipple( glShadeModel( GL glShadeModel( GL _ _ SMOOTH SMOOTH ); ); � Enabling Features glEnable( GL glEnable( GL _ _ LIGHTING LIGHTING ); ); glDisable( GL_TEXTURE_2D GL_TEXTURE_2D ); ); glDisable( 13

  14. Falko Kuester Transformations in OpenGL Transformations in OpenGL � Modeling � Viewing – Viewpoint (camera location and orientations) – Projection � Animation � Map to screen Camera Analogy Camera Analogy � Real-time photography viewing volume camera model tripod 14

  15. Falko Kuester Camera Analogy and Transformations Camera Analogy and Transformations � Projection transformations – adjust the lens of the camera � Viewing transformations – tripod–define position and orientation of the viewing volume in the world � Modeling transformations – moving the model � Viewport transformations – enlarge or reduce the physical photograph Coordinate Systems and Transforms Coordinate Systems and Transforms � Steps in Forming an Image – specify geometry (world coordinates) – specify camera (camera coordinates) – project (window coordinates) – map to viewport (screen coordinates) � Each step uses transformations � Every transformation is equivalent to a change in coordinate systems (frames) 15

  16. Falko Kuester Affine Transformations Affine Transformations � Want transformations which preserve geometry – lines, polygons, quadrics � Affine = line preserving – Rotation, translation, scaling – Projection – Concatenation (composition) Homogeneous Coordinates Homogeneous Coordinates – each vertex is a column vector – w is usually 1.0 – all operations are matrix multiplications – directions (directed line segments) can be represented with w = 0.0   x   y r   = v   z   w   16

Recommend


More recommend