Introduction to OpenGL Prof. Dr.-Ing. Lars Linsen School of Engineering and Science Bremen 320621: Advanced Visualization Lab
1. What is OpenGL?
Graphics Programming Graphical user interface (GUI) Windowing system X Integrated in OS: Microsoft Windows, Mac OS Microsoft Direct3D OpenGL (Open Graphics Library) OpenGL 2.0 (and higher) GLSL (GL Shading Language) Cg (nvidia) HLSL (high level shader language, DirectX) GPGPU programming: CUDA (nvidia), CTM (ATI), OpenCL 320621: Advanced Visualization Lab 3
What is OpenGL? OpenGL is a software interface for 3D computer graphics Originally developed by SGI (IRIS GL) since 1992 under control of ARB (Architecture Review Board) OpenGL specification is hardware-independent, window system independent and operating system independent. Different implementations (as hardware and/or software) C library 320621: Advanced Visualization Lab 4
OpenGL Architecture Polynomial Evaluator Per Vertex Operations & Primitive Assembly CPU Display List Rasterization Per Fragment Operations Frame Buffer Pixel Operations Texture Memory 320621: Advanced Visualization Lab 5
OpenGL as a Renderer Geometric primitives points, lines, and polygons Image Primitives images and bitmaps Separate pipeline for images and geometry linked through texture mapping Rendering depends on state colors, materials, light sources, etc. 320621: Advanced Visualization Lab 6
Controlling current state Setting State glpointsize( size ); gllinestipple( repeat, pattern ); glshademodel( GL_SMOOTH ); Enabling Features glenable( GL_LIGHTING ); gldisable( GL_TEXTURE_2D ); 320621: Advanced Visualization Lab 7
OpenGL Command Formats glvertex3fv( v ) Number of components 2 - (x,y) 3 - (x,y,z) 4 - (x,y,z,w) Data Type b - byte ub - unsigned byte s - short us - unsigned short i - int ui - unsigned int f - float d - double Vector omit v for scalar form glvertex2f( x, y ) 320621: Advanced Visualization Lab 8
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 320621: Advanced Visualization Lab 9
OpenGL and Related APIs application program OpenGL Motif widget or similar GLX, AGL or WGL X, Win32, Mac O/S GLUT GLU GL software and/or hardware 320621: Advanced Visualization Lab 10
Preliminaries Headers Files #include <GL/gl.h> #include <GL/glu.h> #include <GL/glut.h> Libraries Enumerated Types OpenGL defines numerous types for compatibility GLfloat, GLint, GLenum, etc. 320621: Advanced Visualization Lab 11
2. Getting Started
Getting started with OpenGL Installation of Mesa3D: http://www.mesa3d.org 320621: Advanced Visualization Lab 13
Getting started with OpenGL Installation of Mesa3D: http://www.mesa3d.org 320621: Advanced Visualization Lab 14
Setting up GLUT To use GLUT its header file has to be included in the source: #include <GL/glut.h> The program has to be linked against the GLUT library For GCC just compile with -lglut For MSVC and other of the sort add glut to the libraries in the project options 320621: Advanced Visualization Lab 15
GLUT Basics Application Structure Configure and open window Initialize OpenGL state Register input callback functions render resize input: keyboard, mouse, etc. Enter event processing loop 320621: Advanced Visualization Lab 16
Initializing GLUT void glutinit(int argc, char **argv); Initializes GLUT library. Should be called before any other routine. Processes window system specific command line arguments. void glutinitdisplaymode(unsigned int mode); Sets the GLUT display mode for windows created with glutcreatewindow() The display mode is a bitwise or combination of GLUT_RGBA or GLUT_INDEX, GLUT_SINGLE or GLUT_DOUBLE, and any of the buffer-enabling flags GLUT_DEPTH, GLUT_STENCIL, or GLUT_ACCUM. 320621: Advanced Visualization Lab 17
Initializing GLUT void glutinitwindowsize(int width, int height); width and height of the window void glutinitwindowposition(int x, int y); Sets the coordinates of the top-left corner int glutcreatewindow(char *name); Creates a window with the name name and returns a unique window identifying integer (handle). Useful when rendering is done in multiple windows of the same application. 320621: Advanced Visualization Lab 18
Handling events with GLUT Callback functions are registered to the corresponding event, i.e., when the respective event takes place, the function is called to handle it. Window events: void glutdisplayfunc(void (*func)(void)); The function given as argument is called whenever the window has to be redrawn void glutreshapefunc(void (*func)(int width, int height)); The function is called when the window is resized. Width and height are the new width and height of the resized window. When no function is specified (NULL), a default one is assigned. 320621: Advanced Visualization Lab 19
Handling Events with GLUT Input events void glutkeyboardfunc(void (*func)(unsigned int key, int x, int y); The function is called when a key is pressed. Key is the ASCII of the pressed key, x and y are the mouse coordinates (window relative) at the moment the key is pressed void glutspecialfunc(void (*func)(int key, int x, int y)); Triggered when a special key is pressed (F1..F12, arrows, Insert, Home, etc.). void glutmousefunc(void (*func)(int button, int state, int x, int y)); Triggered when a mouse button is pressed or released. Button is left, right or middle, state is down or up, x and y are as above. void glutmotionfunc(void (*func)(int x, int y)); Specifies the function that is called when the mouse is moved while one or more buttons are pressed. 320621: Advanced Visualization Lab 20
Handling Events with GLUT Redisplaying void glutpostredisplay(void); Posts a message to the queue that redisplaying is required. At the first opportunity, the function is called. Idling void glutidlefunc(void (*func)(void)); Function is called when nothing is happening. Sometimes this function is set to the displaying function and timed to force a certain FPS 320621: Advanced Visualization Lab 21
Running the Application Entering the process loop void glutmainloop(void); 320621: Advanced Visualization Lab 22
First example: Main function (1) 320621: Advanced Visualization Lab 23
Main function (2) 320621: Advanced Visualization Lab 24
Reshape function 320621: Advanced Visualization Lab 25
Keyboard function 320621: Advanced Visualization Lab 26
Display function (1) 320621: Advanced Visualization Lab 27
Display function (2) 320621: Advanced Visualization Lab 28
Display function (3) 320621: Advanced Visualization Lab 29
3. Viewing 320621: Advanced Visualization Lab 30
Camera analogy 3D is just like taking a photograph (lots of photographs!) camera viewing volume tripod model 320621: Advanced Visualization Lab 31
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 320621: Advanced Visualization Lab 32
Programming transformations Prior to rendering, view, locate, and orient: eye/camera position 3D geometry Manage the matrices including matrix stack Combine (composite) transformations 320621: Advanced Visualization Lab 33
Transformation pipeline v e r t e x Modelview Matrix Projection Matrix CPU CPU Perspective Division Poly. Poly. object eye clip normalized device DL DL Pixel Pixel Per Per Vertex Vertex Texture Texture Raster Raster Viewport Transform Frag Frag window FB FB Modelview Modelview Projection other calculations here material color shade model (flat) polygon rendering mode polygon culling clipping 320621: Advanced Visualization Lab 34
Coordinate systems and transformations 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) 320621: Advanced Visualization Lab 35
Homogeneous coordinates each vertex is a column vector x y v r = z w w is usually 1.0 all operations are matrix multiplications directions (directed line segments) can be represented with w = 0.0 320621: Advanced Visualization Lab 36
3D transformations A vertex is transformed by 4 x 4 matrices all affine operations are matrix multiplications all matrices are stored column-major in OpenGL matrices are always post-multiplied product of matrix and vector is Mv v M = m m m m 0 1 2 3 m m m m 4 5 6 7 m m m m 8 9 10 11 m m m m 12 13 14 15 320621: Advanced Visualization Lab 37
Specifying transformations Specify Current Matrix Stack glmatrixmode( GL_MODELVIEW or GL_PROJECTION ) Programmer has two styles of specifying transformations specify matrices (glloadmatrix, glmultmatrix) specify operation (gltranslate, glrotate,, glortho) 320621: Advanced Visualization Lab 38
Projection Transformation Shape of viewing frustum Perspective projection gluperspective( fovy, aspect, znear, zfar ) glfrustum( left, right, bottom, top, znear, zfar ) Orthographic parallel projection glortho( left, right, bottom, top, znear, zfar ) gluortho2d( left, right, bottom, top ) calls glortho with z values near zero 320621: Advanced Visualization Lab 39
Orthogonal projection void glortho(gldouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far); Creates a matrix for an orthographic parallel view and multiplies it by the current matrix 320621: Advanced Visualization Lab 40
Applying projection transformations Typical use (orthographic projection) glmatrixmode( GL_PROJECTION ); glloadidentity(); glortho( left, right, bottom, top, znear, zfar ); 320621: Advanced Visualization Lab 41
Perspective projection void glfrustum(gldouble left, Gldouble right, Gldouble bottom, Gldouble top, Gldouble near, Gldouble far); Creates a matrix for the frustrum and multiplies the current matrix by it. Near and far are the distances to the clipping planes 320621: Advanced Visualization Lab 42
Perspective projection More intuitive: void gluperspective(gldouble fovy, Gldouble aspect, Gldouble near, Gldouble far); fovy is the field of view in x-z plane. Aspect is the aspect ratio width/height 320621: Advanced Visualization Lab 43
Viewing transformations Position the camera/eye in the scene place the tripod down; aim camera To fly through a scene change viewing transformation and redraw scene glulookat( eye x, eye y, eye z, aim x, aim y, aim z, up x, up y, up z ) up vector determines unique orientation careful of degenerate positions tripod 320621: Advanced Visualization Lab 44
Viewing transformation 320621: Advanced Visualization Lab 45
Modeling transformations Move object gltranslate{fd}( x, y, z ) Rotate object around arbitrary axis glrotate{fd}( angle, x, y, z ) angle is in degrees Dilate (stretch or shrink) or mirror object glscale{fd}( x, y, z ) 320621: Advanced Visualization Lab 46
Viewing and modeling Moving camera is equivalent to moving every object in the world towards a stationary camera Viewing transformations are equivalent to several modeling transformations 320621: Advanced Visualization Lab 47
Viewport Transformations Viewport usually same as window size can be used to see part of the screen to which is rendered viewport aspect ratio should be same as projection transformation or resulting image may be distorted glviewport( x, y, width, height ) 320621: Advanced Visualization Lab 48
Matrix Operations Specify Current Matrix Stack glmatrixmode( GL_MODELVIEW or GL_PROJECTION ) Other Matrix or Stack Operations glloadidentity() glpushmatrix() glpopmatrix() 320621: Advanced Visualization Lab 49
Matrix stacks The matrices manipulated by all operations discussed so far are the ones that sit on the top of the matrix stack. When a matrix is pushed onto the stack, the current topmost matrix is duplicated, so that first and second matrices contain the same entries. When popping a matrix, the topmost matrix is deleted and the second one becomes the topmost one. 320621: Advanced Visualization Lab 50
Matrix stacks - some applications Modelview matrices E.g.: Save the current position, go somewhere else, draw something, return to the previous position, go elsewhere, draw the same thing scaled by a factor of 2, return to the initial position, The stack may contain at least 32 matrices (depends on the OpenGL implementation) Projection matrices E.g. useful when a text box needs to be displayed (since text is best viewed in orthogonal projection and realistic applications are using perspective projections). At least 2 matrices (again depends on implementation) 320621: Advanced Visualization Lab 51
4. Object Representation
OPenGL primitive types Points: 320621: Advanced Visualization Lab 53
OPenGL primitive types Line segments & sequences thereof: 320621: Advanced Visualization Lab 54
OpenGL primitive types Convex simple polygons & meshes: 320621: Advanced Visualization Lab 55
Using OpenGL primitives Primitives are specified using glbegin( primtype ); glend(); primtype is any of the primitive types and determines how vertices are combined. GLfloat red, green, blue; Glfloat coords[3]; glcolor3f( red, green, blue ); glbegin( primtype ); for ( i = 0; i < nverts; ++i ) { glvertex3fv( coords ); } glend(); 320621: Advanced Visualization Lab 56
Using OpenGL Primitives 320621: Advanced Visualization Lab 57
Using OpenGL Primitives glbegin(gl_polygon); glvertex2f(0.0, 0.0); glvertex2f(0.0, 3.0); glvertex2f(4.0, 3.0); glvertex2f(6.0, 1.5); glvertex2f(4.0, 0.0); glend(); 320621: Advanced Visualization Lab 58
GLUT objects void glutwiresphere(gldouble radius, GLint slices, GLint stacks); void glutsolidsphere(gldouble radius, GLint slices, GLint stacks); void glutwirecube(gldouble size); void glutsolidcube(gldouble size); void glutwiretorus(gldouble innerradius, GLdouble outerradius, GLint nsides, GLint rings); void glutsolidtorus(gldouble innerradius, GLdouble outerradius, GLint nsides, GLint rings); 320621: Advanced Visualization Lab 59
GLUT objects 320621: Advanced Visualization Lab 60
GLUT objects 320621: Advanced Visualization Lab 61
GLUT objects void glutwireicosahedron(void); void glutsolidicosahedron(void); void glutwireoctahedron(void); void glutsolidoctahedron(void); void glutwiretetrahedron(void); void glutsolidtetrahedron(void); void glutwiredodecahedron(gldouble radius); void glutsoliddodecahedron(gldouble radius); 320621: Advanced Visualization Lab 62
GLUT objects void glutwirecone(gldouble radius, GLdouble height, GLint slices, GLint stacks); void glutsolidcone(gldouble radius, GLdouble height, GLint slices, GLint stacks); void glutwireteapot(gldouble size); void glutsolidteapot(gldouble size); 320621: Advanced Visualization Lab 63
GLUT objects 320621: Advanced Visualization Lab 64
5. Displaying
Rasterization Poly. Poly. Per Per Vertex Vertex CPU CPU DL DL Raster Raster Frag Frag FB FB Texture Texture Pixel Pixel 320621: Advanced Visualization Lab 66
Double Buffering 1 2 4 1 2 4 Front Buffer 8 16 8 16 Back Buffer Display 320621: Advanced Visualization Lab 67
Animation Using Double Buffering Request a double buffered color buffer glutinitdisplaymode( GLUT_RGB GLUT_DOUBLE ); Clear color buffer glclear( GL_COLOR_BUFFER_BIT ); Render scene Request swap of front and back buffers glutswapbuffers(); Repeat steps 2-4 for animation 320621: Advanced Visualization Lab 68
An Updated Program Template (cont.) void drawscene( void ) { GLfloat vertices[] = { }; GLfloat colors[] = { }; glclear( GL_COLOR_BUFFER_BIT); glbegin( GL_TRIANGLE_STRIP ); /* calls to glcolor*() and glvertex*() */ glend(); glutswapbuffers(); } 320621: Advanced Visualization Lab 69
Antialiasing Removing the Jaggies glenable( mode ) GL_POINT_SMOOTH GL_LINE_SMOOTH GL_POLYGON_SMOOTH alpha value computed by computing sub-pixel coverage available in both RGBA and colormap modes 320621: Advanced Visualization Lab 70
Full Scene Antialiasing : Jittering the view Each time we move the viewer, the image shifts Different aliasing artifacts in each image Averaging images using accumulation buffer averages out these artifacts 320621: Advanced Visualization Lab 71
Blending Combine pixels with what s in already in the framebuffer glblendfunc( src, dst ) v C r = src v C f + dst v C p Fragment (src) Framebuffer Pixel (dst) Blending Equation Blended Pixel 320621: Advanced Visualization Lab 72
Accumulation Buffer operations glaccum( op, value ) within the accumulation buffer: GL_ADD, GL_MULT from read buffer: GL_ACCUM, GL_LOAD transfer back to write buffer: GL_RETURN glaccum(gl_accum, 0.5) multiplies each value in write buffer by 0.5 and adds to accumulation buffer 320621: Advanced Visualization Lab 73
Accumulation Buffer Problems of compositing into color buffers limited color resolution clamping loss of accuracy Accumulation buffer acts as a floating point color buffer accumulate into accumulation buffer transfer results to frame buffer 320621: Advanced Visualization Lab 74
Multi-pass Rendering Blending allows results from multiple drawing passes to be combined together enables more complex rendering algorithms Example of bump-mapping done with a multi-pass OpenGL algorithm 320621: Advanced Visualization Lab 75
Depth Buffering 1 2 4 1 2 4 Color Buffer 8 16 8 16 Depth Buffer Display 320621: Advanced Visualization Lab 76
Depth Buffering Using OpenGL Request a depth buffer glutinitdisplaymode( GLUT_RGB GLUT_DOUBLE GLUT_DEPTH ); Enable depth buffering glenable( GL_DEPTH_TEST ); Clear color and depth buffers glclear( GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT ); Render scene Swap color buffers 320621: Advanced Visualization Lab 77
6. Colors & Shading
Specifying Geometric Primitives 320621: Advanced Visualization Lab 79
How OpenGL Simulates Lights Phong lighting model Computed at vertices Lighting contributors Surface material properties Light properties Lighting model properties 320621: Advanced Visualization Lab 80
Surface Normals Normals define how a surface reflects light glnormal3f( x, y, z ) Current normal is used to compute vertex s color Use unit normals for proper lighting scaling affects a normal s length glenable( GL_NORMALIZE ) or glenable( GL_RESCALE_NORMAL ) 320621: Advanced Visualization Lab 81
Material Properties Define the surface properties of a primitive glmaterialfv( face, property, value ); GL_DIFFUSE GL_SPECULAR GL_AMBIENT GL_EMISSION GL_SHININESS Base color Highlight Color Low-light Color Glow Color Surface Smoothness separate materials for front and back 320621: Advanced Visualization Lab 82
Light Properties gllightfv( light, property, value ); light specifies which light multiple lights, starting with GL_LIGHT0 glgetintegerv( GL_MAX_LIGHTS, &n ); properties colors position and type attenuation 320621: Advanced Visualization Lab 83
Light Colors Light color properties GL_AMBIENT GL_DIFFUSE GL_SPECULAR 320621: Advanced Visualization Lab 84
Types of Lights OpenGL supports two types of Lights Local (Point) light sources Infinite (Directional) light sources Type of light controlled by w coordinate w = 0 w 0 Infinite Light directed along Local Light positioned at ( x y z) ( ) x y z w w w 320621: Advanced Visualization Lab 85
Attenuation Light attenuation decrease light intensity with distance GL_CONSTANT_ATTENUATION GL_LINEAR_ATTENUATION GL_QUADRATIC_ATTENUATION f i = k c + k l 1 d + k q d 2 320621: Advanced Visualization Lab 86
Turning on the Lights Flip each light s switch glenable( GL_LIGHTn ); Turn on the power glenable( GL_LIGHTING ); 320621: Advanced Visualization Lab 87
Light & Material 320621: Advanced Visualization Lab 88
Advanced Lighting Features Spotlights localize lighting affects GL_SPOT_DIRECTION GL_SPOT_CUTOFF GL_SPOT_EXPONENT 320621: Advanced Visualization Lab 89
Shading Setting State glshademodel( GL_SMOOTH GL_FLAT); 320621: Advanced Visualization Lab 90
8. Textures 320621: Advanced Visualization Lab 91
Texture Mapping Poly. Poly. Per Per Vertex Vertex CPU CPU DL DL Pixel Pixel Texture Texture Raster Raster Frag Frag FB FB Apply a 1D, 2D, or 3D image to geometric primitives 320621: Advanced Visualization Lab 92
Texture Mapping and the OpenGL Pipeline Images and geometry flow through separate pipelines that join at the rasterizer complex textures do not affect geometric complexity vertices geometry pipeline rasterizer image pixel pipeline 320621: Advanced Visualization Lab 93
Applying Textures Three steps specify texture read or generate image assign to texture enable texturing assign texture coordinates to vertices specify texture parameters wrapping, filtering 320621: Advanced Visualization Lab 94
Texture Objects Like display lists for texture images one image per texture object may be shared by several graphics contexts Generate texture names glgentextures( n, *texids ); 320621: Advanced Visualization Lab 95
Texture Objects (cont.) Create texture objects with texture data and state Bind textures before using glbindtexture( target, id ); 320621: Advanced Visualization Lab 96
Specify Texture Image Define a texture image from an array of texels in CPU memory glteximage2d( target, level, components, w, h, border, format, type, *texels ); 320621: Advanced Visualization Lab 97
Mapping a Texture Based on parametric texture coordinates gltexcoord*() specified at each vertex t 0, 1 Texture Space a 1, 1 Object Space (s, t) = (0.2, 0.8) A b 0, 0 1, 0 c s (0.4, 0.2) B C (0.8, 0.4) 320621: Advanced Visualization Lab 98
Example // Loading a Texture, specifying properties glgentextures(1, &texname); glbindtexture(gl_texture_2d, texname); gltexparameteri(gl_texture_2d, GL_TEXTURE_WRAP_S, GL_REPEAT); gltexparameteri(gl_texture_2d, GL_TEXTURE_WRAP_T, GL_REPEAT); glteximage2d(gl_texture_2d, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, Image_Buffer); // later while describing polygons... gltexcoord2f(0.0, 0.0); glvertex3f(-2.0, -1.0, 0.0); gltexcoord2f(0.0, 1.0); glvertex3f(-2.0, 1.0, 0.0); 320621: Advanced Visualization Lab 99
Example 320621: Advanced Visualization Lab 100
Applying Textures specify textures in texture objects set texture filter set texture function set texture wrap mode set optional perspective correction hint bind texture object enable texturing supply texture coordinates for vertex coordinates can also be generated 320621: Advanced Visualization Lab 101
Texture Application Methods Filter Modes minification or magnification special mipmap minification filters Wrap Modes clamping or repeating (tiling) Texture Functions how to mix primitive s color with texture s color blend, modulate or replace texels 320621: Advanced Visualization Lab 102
Filter Modes Example: gltexparameteri( target, type, mode ); Texture Polygon Magnification Texture Polygon Minification 320621: Advanced Visualization Lab 103
Mipmapped Textures Mipmap allows for prefiltered texture maps of decreasing resolutions Lessens interpolation errors for smaller textured objects Declare mipmap level during texture definition glteximage*d( GL_TEXTURE_*D, level, ) GLU mipmap builder routines glubuild*dmipmaps( ) OpenGL 1.2 introduces advanced LOD controls 320621: Advanced Visualization Lab 104
Wrapping Mode Example: gltexparameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP ) gltexparameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ) t texture s GL_REPEAT wrapping GL_CLAMP wrapping 320621: Advanced Visualization Lab 105
Texture Functions Controls how texture is applied gltexenv{fi}[v]( GL_TEXTURE_ENV, prop, param ) GL_TEXTURE_ENV_MODE modes GL_MODULATE GL_BLEND GL_REPLACE Set blend color with GL_TEXTURE_ENV_COLOR 320621: Advanced Visualization Lab 106
Perspective Correction Hint Texture coordinate and color interpolation either linearly in screen space or using depth/perspective values (slower) Noticeable for polygons on edge glhint( GL_PERSPECTIVE_CORRECTION_HINT, hint ) where hint is one of GL_DONT_CARE GL_NICEST GL_FASTEST 320621: Advanced Visualization Lab 107
Done.