International Master Program «Biometrics» Workshop sessions Workshop 3D capture, modelling and augmented reality Capture 3D, modélisation et réalité augmentée Jean-Marc Vézien (LIMSI-CNRS, équipe VENISE) 3D Modelling and Augmented Reality - Jean-Marc Vézien 1
Workshop 3D capture, modelling and augmented reality Capture 3D, modélisation et réalité augmentée An introduction to 3D modelling and OpenGL 3D Modelling and Augmented Reality - Jean-Marc Vézien 2
Computer Graphics is about animation ( films ) Major driving force now 3D Modelling and Augmented Reality - Jean-Marc Vézien 3
And games! 3D Modelling and Augmented Reality - Jean-Marc Vézien 4
And a lot of serious apps Medical imaging 3D Modelling and Augmented Reality - Jean-Marc Vézien 5
Scientific Visualisation Pymol.org 3D Modelling and Augmented Reality - Jean-Marc Vézien 6
Graphics/Rendering Pipeline Graphics processes generally execute sequentially Pipelining the process means dividing it into stages Different hardware resources assigned for each stage real-time 3 stages: 3D Modelling and Augmented Reality - Jean-Marc Vézien 7
Application stage Entirely done in software by the CPU Read Data - the world geometry database, - User s input by mice, trackballs, trackers, or sensing gloves In response to the user s input, the application stage changes the view or scene 3D Modelling and Augmented Reality - Jean-Marc Vézien 8
Geometry Stage Modeling: shapes Model Transformation Transformation: viewing Hidden Surface Elimination Rasterization Stage Shading: reflection and lighting 3D Modelling and Augmented Reality - Jean-Marc Vézien 9
Rasterization Stage Geometry Stage Rasterization and Sampling Texture Mapping Image Composition Intensity and Colour Quantization Framebuffer/Display 3D Modelling and Augmented Reality - Jean-Marc Vézien 10
Pipeline illustration The final scene Images courtesy of Picture Inc. 3D Modelling and Augmented Reality - Jean-Marc Vézien 11
Geometry Pipeline Loaded 3D Models Model Transformation Transformation: viewing Hidden Surface Elimination Shading: reflection and lighting Imaging 3D Modelling and Augmented Reality - Jean-Marc Vézien 12
Preparing Shape Models Designed by polygons, parametric curves/surfaces, implicit surfaces and etc. Defined in its own coordinate system 3D Modelling and Augmented Reality - Jean-Marc Vézien 13
Model Transformation Put Objects into the scene by applying translation, scaling and rotation Linear transformation (homogeneous) The location of all the vertices are updated by this transformation 3D Modelling and Augmented Reality - Jean-Marc Vézien 14
Model Transformation Rotate Translate Scale 3D Modelling and Augmented Reality - Jean-Marc Vézien 15
Perspective Projection Create a picture of the scene viewed from the camera Perspective transformation to convert the 3D coordinates to 2D coordinates of the screen Objects far away appear smaller, closer objects appear bigger 3D Modelling and Augmented Reality - Jean-Marc Vézien 16
Perspective Projection 3D Modelling and Augmented Reality - Jean-Marc Vézien 17 M(X,Y,Z) o m(x,y) f z x Focal point Focal plane PM Z Y X f f w wy wx m 1 0 1 0 0 0 0 0 0 0 0
Hidden Surface Removal Objects occluded by other objects must not be drawn! Also called occlusion culling (OC) or visible surface determination (VSD) Step 1: Remove all polygons outside of viewing frustum (culling) Step 2: Remove all polygons that are facing away from the viewer : N.V < 0 Step 3: Draw the visible faces in an order so the object looks right. Usually a sorting process in depth (the closer wins) 3D Modelling and Augmented Reality - Jean-Marc Vézien 18
Frustum Culling (Step 1) Camera has a pyramid of view Quickly discard everything out of view 3D Modelling and Augmented Reality - Jean-Marc Vézien 19
Shading Compute the colour p of each pixels : object s colour (reflectance f) lighting condition (light source l) The camera position (position of L and N relative to camera) Simplest model: Lambertian (diffuse) p l. f.cos p Add up light sources (+ occlusions!) 3D Modelling and Augmented Reality - Jean-Marc Vézien 20
Shading : Constant Shading - Ambient Objects painted by own colour What children do: instinctive intrinsic representation 3D Modelling and Augmented Reality - Jean-Marc Vézien 21
Ex: Flat Shading Object coloured based on its own colour and the lighting condition One colour for one face (normal based) Crude rendering for complex objects 3D Modelling and Augmented Reality - Jean-Marc Vézien 22
Gouraud shading Lighting calculation per vertex (average of color normals) Interpolate between vertex 3D Modelling and Augmented Reality - Jean-Marc Vézien 23
Specular highlights added Surface = diffuse + mirror (specularities) Illustration of the Phong Reflection Model. Created by the uploaded, Brad Smith, August 7, 2006. Ld = [rd, gd,bd]max(0,(n L)) La = [ra, ga,ba] Ls = [rs, gs,bs] max(0, (V R)) p Total Radiance for a light L = A(La + Ld + Ls) A = 1/(a + br + cr 2 ) distance fall-off 3D Modelling and Augmented Reality - Jean-Marc Vézien 24
Phong shading Gouraud Shading Highlights (Phong) 3D Modelling and Augmented Reality - Jean-Marc Vézien 25
Imaging pipeline Geometry Pipelin e Rasterization and Sampling Texture Mapping Image Composition Intensity and Colour Quantization Framebuffer/Display 3D Modelling and Augmented Reality - Jean-Marc Vézien 26
Rasterization Converts the vertex information output by the geometry pipeline into pixel information needed by the video display 1962: Bresenham! function line(x0, x1, y0, y1) int deltax := x1 - x0 int deltay := y1 - y0 real error := 0 real deltaerr := abs (deltay / deltax) // Assume deltax!= 0 (line is not vertical), // note that this division needs to be done in a way // that preserves the fractional part int y := y0 for x from x0 to x1 plot(x,y) error := error + deltaerr if error 0.5 then y := y + 1 error := error - 1.0 Aliasing: distortion artifacts produced when representing a high-resolution signal at a lower resolution. Anti-aliasing : technique to remove aliasing 3D Modelling and Augmented Reality - Jean-Marc Vézien 27
Anti-aliasing Aliased polygons ( edges (jagged Anti-aliased polygons Must low-pass filter the input signal OR Oversample it (create higher frequencies) 3D Modelling and Augmented Reality - Jean-Marc Vézien 28
Anti-aliasing Each pixel is subdivided (sub-sampled) in n regions, Each sub-pixel has a color Compute the average color value 3D Modelling and Augmented Reality - Jean-Marc Vézien 29
Texture mapping Paint objects with images (wallpaper) A texture map is applied (mapped) to the surface of a shape or polygon. Every vertex in a polygon is assigned a 2D texture coordinate Texture coordinates are interpolated on the polygon surface with rasterization algorithm. A surface color can be computed for each pixel Note: possible warping Modulated with lighting as previously seen 3D Modelling and Augmented Reality - Jean-Marc Vézien 30
Texture mapping 3D Modelling and Augmented Reality - Jean-Marc Vézien 31
OpenGL 3D Modelling and Augmented Reality - Jean-Marc Vézien 32
What is OpenGL? A software interface to graphics hardware Graphics rendering API (Low Level) + pipeline High-quality color images composed of geometric and image primitives Window system independent Operating system independent Doom3 3D Modelling and Augmented Reality - Jean-Marc Vézien 33
OpenGL : A brief history Silicon Graphics (SGI) revolutionized the graphics workstation by implementing the pipeline in hardware (1982) To access the system, application programmers used a library called GL relatively simple to program 3D interactive applications The success of GL lead to OpenGL (1992), a platform-independent API that was: Easy to use Close enough to the hardware to get excellent performance Focus on rendering Omitted windowing and input to avoid window system dependencies 3D Modelling and Augmented Reality - Jean-Marc Vézien 34
Control Architectural Review Board (ARB) Members include SGI, Microsoft, Nvidia, HP, 3DLabs, IBM, etc. Rapidly evolving (4.0 in 2011) ; evolution reflects new hardware capabilities Allows for platform specific features through extensions 3D Modelling and Augmented Reality - Jean-Marc Vézien 35
OpenGL Basics Rendering Typically execution of OpenGL commands Converting geometric/mathematical object descriptions into frame buffer values OpenGL can render: Geometric primitives: lines, points, polygons, etc Bitmaps and Images Images and geometry linked through texture mapping Graphics Pipeline 3D Modelling and Augmented Reality - Jean-Marc Vézien 36
Transformation Pipeline Position object w.r.t. camera Project object coordinates 3D Modelling and Augmented Reality - Jean-Marc Vézien 37
Objects Made up of sets of polygons Which are made up of lines Which are made of points No curved surfaces +Y Just a shell Not a solid object Everything is a set of points (vertex) In local coordinate system Polygons +Z +X 3D Modelling and Augmented Reality - Jean-Marc Vézien 38
OpenGL Geometric Primitives The geometry is specified by vertices. There are 10 primitive types: 3D Modelling and Augmented Reality - Jean-Marc Vézien 39
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(); } 3D Modelling and Augmented Reality - Jean-Marc Vézien 40
Polygon Issues OpenGL will only display polygons correctly that are: Simple: edges cannot cross Convex: all points on line segment between two points in a polygon are also in the polygon Flat: all vertices are in the same plane User program can check if above true OpenGL will produce output if these conditions are violated but it may not be what is desired Triangles satisfy all conditions triangulation algorithms 3D Modelling and Augmented Reality - Jean-Marc Vézien 41
OpenGL Command Format glvertex3fv 3D Modelling and Augmented Reality - Jean-Marc Vézien 42
Vertices and Primitives Primitives are specified between glbegin( primtype ); glend(); primtype determines how vertices are combined glend(); GLfloat red, green, blue; Glfloat coords[nverts][3]; /*Initialize coords and colors somewhere in program*/ glbegin( primtype ); for ( i = 0; i < nverts; ++i ) { glcolor3f( red, green, blue ); glvertex3fv( coords[i] ); } 3D Modelling and Augmented Reality - Jean-Marc Vézien 43
An Example void drawparallelogram( 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(); } 3D Modelling and Augmented Reality - Jean-Marc Vézien 44
Vertices and Primitives Points: GL_POINTS Individual points Point size can be altered: glpointsize (float size) glbegin(gl_points); glcolor3fv( color ); glvertex2f( P0.x, P0.y ); glvertex2f( P1.x, P1.y ); glvertex2f( P2.x, P2.y ); glvertex2f( P3.x, P3.y ); glvertex2f( P4.x, P4.y ); glvertex2f( P5.x, P5.y ); glvertex2f( P6.x, P6.y ); glvertex2f( P7.x, P7.y ); glend(); 3D Modelling and Augmented Reality - Jean-Marc Vézien 45
Vertices and Primitives Lines: GL_LINES Pairs of vertices interpreted as individual line segments Can specify line width using gllinewidth (float width) glbegin(gl_lines); glcolor3fv( color ); glvertex2f( P0.x, P0.y ); glvertex2f( P1.x, P1.y ); glvertex2f( P2.x, P2.y ); glvertex2f( P3.x, P3.y ); glvertex2f( P4.x, P4.y ); glvertex2f( P5.x, P5.y ); glvertex2f( P6.x, P6.y ); glvertex2f( P7.x, P7.y ); glend(); Lines are useful for rays 3D Modelling and Augmented Reality - Jean-Marc Vézien 46
Vertices and Primitives Line Strip: GL_LINE_STRIP series of connected line segments 3D Modelling and Augmented Reality - Jean-Marc Vézien 47
Vertices and Primitives Line Loop: GL_LINE_LOOP Line strip with a segment added between last and first vertices 3D Modelling and Augmented Reality - Jean-Marc Vézien 48
Vertices and Primitives Polygon : GL_POLYGON boundary of a simple, convex polygon 3D Modelling and Augmented Reality - Jean-Marc Vézien 49
Vertices and Primitives Triangles : GL_TRIANGLES triples of vertices interpreted as triangles VERY USED! 3D Modelling and Augmented Reality - Jean-Marc Vézien 50
Vertices and Primitives Triangle Strip : GL_TRIANGLE_STRIP linked strip of triangles v0 v1 v2 v3 v4 v5 v6 v7 3D Modelling and Augmented Reality - Jean-Marc Vézien 51
Vertices and Primitives Triangle Fan : GL_TRIANGLE_FAN linked fan of triangles v1 v2 v3 v4 v0 v5 3D Modelling and Augmented Reality - Jean-Marc Vézien 52
Vertices and Primitives Quads : GL_QUADS quadruples of vertices interpreted as four-sided polygons 3D Modelling and Augmented Reality - Jean-Marc Vézien 53
Vertices and Primitives Between glbegin/ glend, these opengl commands are allowed: glvertex*() : set vertex coordinates glcolor*() : set current color glindex*() : set current color index glnormal*() : set normal vector coordinates (Light.) gltexcoord*() : set texture coordinates (Texture) 3D Modelling and Augmented Reality - Jean-Marc Vézien 54
Example glbegin(gl_triangles); for (int i=0; i<ntris; i++) { glcolor3f(tri[i].r0,tri[i].g0,tri[i].b0); // Color of vertex glnormal3f(tri[i].nx0,tri[i].ny0,tri[i].nz0);// Normal glvertex3f(tri[i].x0,tri[i].y0,tri[i].z0); // Position glcolor3f(tri[i].r2,tri[i].g2,tri[i].b2); glnormal3f(tri[i].nx2,tri[i].ny2,tri[i].nz2); glvertex3f(tri[i].x2,tri[i].y2,tri[i].z2); } glend(); // Sends all the vertices/normals to OpenGL library 3D Modelling and Augmented Reality - Jean-Marc Vézien 55
OpenGL: Camera Two things to specify: Physical location of camera in the scene (MODELVIEW matrix in OpenGL). Where is the camera? Which direction is it pointing? What is the orientation of the camera? Projection properties of the camera (PROJECTION matrix in OpenGL): Depth of field? Field of view in the x and y directions? 3D Modelling and Augmented Reality - Jean-Marc Vézien 56
OpenGL: Camera Coordinate-systems are represented as matrices in OpenGL. Camera = a coordinate-system centered at its image plane. Camera Matrix = specify the physical properties of the camera. 3D Modelling and Augmented Reality - Jean-Marc Vézien 57
OpenGL: MODELVIEW eye= z Camera up = y y p Point p in world is: (MODELVIEW * p) in camera coord-sys!!! x center = (0,0,0) x World glmatrixmode(gl_modelview); // Specify matrix mode glloadidentity(); // Clear modelview matrix //GLU API (included with OpenGL) glulookat(eyex,eyey,eyez,centerx, centery,centerz,upx,upy,upz); z 3D Modelling and Augmented Reality - Jean-Marc Vézien 58
OpenGL: GL_MODELVIEW World coord-system: Camera coord-system: 3D Modelling and Augmented Reality - Jean-Marc Vézien 59
OpenGL: GL_PROJECTION Goal: set intrinsic (optical) properties of camera: glmatrixmode(gl_projection); glloadidentity(); gluperspective(fovy, aspect, near, far); Everything before near and after far is discarded 3D Modelling and Augmented Reality - Jean-Marc Vézien 60
OpenGL: GL_PROJECTION (C td) Camera viewport: window on the scene far gluperspective(fovy, aspect, near, far); near x center fov aspect = w/h z y z y h far Note: symetric camera only! x w near 3D Modelling and Augmented Reality - Jean-Marc Vézien 61
OpenGL: Setting Camera Assume window is width x height: void SetCamera() { glviewport(0, 0, width, height); /* Set camera position */ glmatrixmode(gl_modelview); glloadidentity(); glulookat( m_veye[0], m_veye[1], m_veye[2], m_vref[0], m_vref[1], m_vref[2], m_vup[0], m_vup[1], m_vup[2]); /* Set projection frustrum */ glmatrixmode(gl_projection); glloadidentity(); gluperspective(m_fyfov, width / height, m_fnear, m_ffar); } Typical: 45 16/9 0.1 1000.0 3D Modelling and Augmented Reality - Jean-Marc Vézien 62
OpenGL: Lighting Set light: gllightfv(light_num, property, value); Light_num : id of light (GL_LIGHTx) Property: one of the 3 components for each light: diffuse (lambertian) component: GL_DIFFUSE specular component : GL_SPECULAR ambient light: GL_AMBIENT Value: GLfloat[4] (between 0 and 1): RGBA. 3D Modelling and Augmented Reality - Jean-Marc Vézien 63
OpenGL: Lighting Set material (for objects): glmaterialfv(side, property, value); Side : which side (GL_FRONT, GL_BACK, GL_FRONT_AND_BACK) Property: one of the 3 properties: diffuse albedo: GL_AMBIENT_AND_DIFFUSE specular albedo: GL_SPECULAR shininess (in phong s model): GL_SHININESS Value: GLFloat[] 3D Modelling and Augmented Reality - Jean-Marc Vézien 64
OpenGL: Lighting example GLfloat mat_diffuse[4] = {0.75, 0.75, 0.75, 1.0}; GLfloat mat_specular[4] = {0.55, 0.55, 0.55, 1.0}; GLfloat mat_shininess[1] = {80}; glmaterialfv(gl_front_and_back, GL_AMBIENT_AND_DIFFUSE,mat_diffuse); glmaterialfv(gl_front,gl_specular,mat_specular); glmaterialfv(gl_front,gl_shininess,mat_shininess); GLfloat light0_ambient[4] = { 0.0, 0.0, 0.0, 1.0}; GLfloat light0_color[4] = { 0.4, 0.4, 0.4, 1.0 }; gllightfv(gl_light0, GL_AMBIENT, light0_ambient); gllightfv(gl_light0, GL_DIFFUSE, light0_color); gllightfv(gl_light0, GL_SPECULAR, light0_color); glenable(gl_light0); glenable(gl_lighting); 3D Modelling and Augmented Reality - Jean-Marc Vézien 65
OpenGL: Shading model There are two shading modes in OpenGL: Flat shading: entire face is the color computed at the 0 th vertex. glshademodel(gl_flat); Gouraud ( smooth ) shading: color computed at each vertex and interpolated across polygon. glshademodel(gl_smooth); 3D Modelling and Augmented Reality - Jean-Marc Vézien 66
OpenGL: Flat Shading glshademodel(gl_flat); 3D Modelling and Augmented Reality - Jean-Marc Vézien 67
OpenGL: Flat Shading glshademodel(gl_smooth); 3D Modelling and Augmented Reality - Jean-Marc Vézien 68
OpenGL: Lighting is Optional! Lighting is an explicit option : glenable(gl_lighting); gldisable(gl_lighting); When disabled, the color of each vertex is directly glcolor3f(r,g,b). Do not need lighting when debugging your renderer. 3D Modelling and Augmented Reality - Jean-Marc Vézien 69
Texture Mapping y x z t geometry screen image s 3D Modelling and Augmented Reality - Jean-Marc Vézien 70
Texture mapping: basic principle Three steps to applying a texture 1. Specify the texture Read or generate image Assign to texture Enable texturing 2. Assign texture coordinates to vertices Proper mapping function is left to application 3. Specify texture parameters wrapping, filtering 3D Modelling and Augmented Reality - Jean-Marc Vézien 71
Texture Example The texture is a 256 x 256 image that has been mapped to a rectangular polygon which is viewed in perspective 1. Define a texture image from an array of texels (texture elements) in CPU memory Glubyte my_texels[512][512][4]; Fill it as any other pixel array Scan Via application code Enable texture mapping glenable(gl_texture_2d) OpenGL supports 1-4 dimensional texture maps 3D Modelling and Augmented Reality - Jean-Marc Vézien 72
Assign Image as a Texture glteximage2d( target, level, components, w, h, border, format, type, texels ); Ex: target: type of texture, e.g. GL_TEXTURE_2D level: used for mipmapping (discussed later) components: elements per texel w, h: width and height of texels in pixels border: used for smoothing (discussed later) format and type: describe texels texels: pointer to texel array glteximage2d(gl_texture_2d, 0, 3, 512, 512, 0, GL_RGB, GL_UNSIGNED_BYTE, my_texels); 3D Modelling and Augmented Reality - Jean-Marc Vézien 73
Converting A Texture Image OpenGL requires texture dimensions to be powers of 2 If dimensions of image are not powers of 2: gluscaleimage( format, w_in, h_in, type_in, *data_in, w_out, h_out, type_out, *data_out ); data_in = source image data_out = destination image Image interpolated and filtered during scaling 3D Modelling and Augmented Reality - Jean-Marc Vézien 74
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) 3D Modelling and Augmented Reality - Jean-Marc Vézien 75
Mapping a Texture : example glbegin(gl_polygon); glcolor3f(r0, g0, b0); glnormal3f(u0, v0, w0); gltexcoord2f(s0, t0); glvertex3f(x0, y0, z0); glcolor3f(r1, g1, b1); glnormal3f(u1, v1, w1); gltexcoord2f(s1, t1); glvertex3f(x1, y1, z1);.. glend(); Note that we can use vertex arrays to increase efficiency 3D Modelling and Augmented Reality - Jean-Marc Vézien 76
Interpolation OpenGL uses bilinear interpolation to find proper texels from specified texture coordinates Distorsions can occur! good selection of tex coordinates poor selection of tex coordinates texture stretched over trapezoid showing effects of bilinear interpolation 3D Modelling and Augmented Reality - Jean-Marc Vézien 77
Texture Parameters OpenGL has a variety of parameters that determine how texture is applied gltexparameteri( target, type, mode ) Wrapping parameters determine what happens of s and t if outside the (0,1) range Filter modes allow to use area averaging instead of point samples Mipmapping = use textures at multiple resolutions Environment parameters determine how texture mapping interacts with shading 3D Modelling and Augmented Reality - Jean-Marc Vézien 78
Wrapping Mode Two modes: clamping or wrapping GL_CLAMP : if s,t > 1 use 1, if s,t <0 use 0 t GL_WRAP : use s,t modulo 1 gltexparameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP ) gltexparameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ) texture s GL_REPEAT wrapping GL_CLAMP wrapping 3D Modelling and Augmented Reality - Jean-Marc Vézien 79
Magnification and Minification More than one texel can cover a pixel (minification) or more than one pixel can cover a texel (magnification) Texture Magnification Polygon Texture Polygon Minification 3D Modelling and Augmented Reality - Jean-Marc Vézien 80
Filter Modes Minification and magnification can use point sampling (nearest texel) or linear filtering ( 2 x 2 filter) to obtain texture values: gltexparameteri(gl_texture_2d, GL_TEXTURE_MAG_FILTER, GL_NEAREST); gltexparameteri(gl_texture_2d, GL_TEXTURE_MIN_FILTER, GL_LINEAR); Note that linear filtering requires a border of an extra texel for filtering at edges (border = 1) 3D Modelling and Augmented Reality - Jean-Marc Vézien 81
Mipmapped Textures Mipmapping allows for prefiltered texture maps of decreasing resolutions Lessens interpolation errors for smaller textured objects Declare mipmap level at texture definition glteximage2d( GL_TEXTURE_*D, level, ) GLU mipmap builder routines will build all the textures from a given image glubuild2dmipmaps( ) 3D Modelling and Augmented Reality - Jean-Marc Vézien 82
Example Simple point sampling Linear filtering Mipmapped point sampling Mipmapped linear 3D Modelling and Augmented Reality - Jean-Marc Vézien 83
Texture Functions Controls how texture is applied gltexenv{fi}[v]( GL_TEXTURE_ENV_MODE, prop, param ) GL_TEXTURE_ENV_MODE modes GL_MODULATE: modulates with computed shading GL_BLEND: blends with an environmental color GL_REPLACE: use only texture color GL(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); Set blend color with GL_TEXTURE_ENV_COLOR 3D Modelling and Augmented Reality - Jean-Marc Vézien 84
Perspective Correction Hint Texture coordinate and color interpolation are computed: either linearly in screen space or using depth/perspective values (slower) Noticeable for polygons on edge glhint( GL_PERSPECTIVE_CORRECTION_HINT, hint ) hint is one of GL_DONT_CARE GL_NICEST GL_FASTEST 3D Modelling and Augmented Reality - Jean-Marc Vézien 85
Generating Texture Coordinates OpenGL can generate texture coordinates automatically Specify a plane gltexgen{ifd}[v]() Generate texture coordinates based upon distance from the plane Generation modes: GL_OBJECT_LINEAR GL_EYE_LINEAR GL_SPHERE_MAP (used for environmental maps) 3D Modelling and Augmented Reality - Jean-Marc Vézien 86
Texture Objects Texture is part of the OpenGL state Different textures for different objects moving large data from processor memory to texture memory Recent versions of OpenGL have texture objects: one image per texture object Texture memory can hold multiple texture objects 3D Modelling and Augmented Reality - Jean-Marc Vézien 87
Other Texture Features Environmental Maps Take image of environment with wide angle lens Use this texture to generate a spherical map Use automatic texture coordinate generation on box-like container for scene Multi-texturing Apply a sequence of textures through cascaded texture units 3D Modelling and Augmented Reality - Jean-Marc Vézien 88
OpenGL and GLUT 3D Modelling and Augmented Reality - Jean-Marc Vézien 89
OpenGL and GLUT GLUT (OpenGL Utility Toolkit) An auxiliary library Portable windowing API Easier to show the output of your OpenGL application Not officially part of OpenGL Handles: Window creation, OS system calls Mouse buttons, movement, keyboard, etc Callbacks 3D Modelling and Augmented Reality - Jean-Marc Vézien 90
How to install GLUT? Download GLUT http://www.opengl.org/resources/libraries/glut.html Copy the files to following folders: glut.h VC/include/gl/ glut32.lib VC/lib/ glut32.dll windows/system32/ Header Files: #include <GL/glut.h> #include <GL/gl.h> Include glut automatically includes other header files 3D Modelling and Augmented Reality - Jean-Marc Vézien 91
Application Structure: GLUT Basics Configure and open window Initialize OpenGL state Register input callback functions render resize input: keyboard, mouse, etc. Enter event processing loop 3D Modelling and Augmented Reality - Jean-Marc Vézien 92
Sample Program #include <GL/glut.h> #include <GL/gl.h> void main(int argc, char** argv) { int mode = GLUT_RGB GLUT_DOUBLE; glutinitdisplaymode( mode ); glutinitwindowsize( 500,500 ); glutcreatewindow( Simple ); init(); glutdisplayfunc( display ); glutkeyboardfunc( key ); glutmainloop(); } 3D Modelling and Augmented Reality - Jean-Marc Vézien 93
Sample Program #include <GL/glut.h> #include <GL/gl.h> void main(int argc, char** argv) { int mode = GLUT_RGB GLUT_DOUBLE; glutinitdisplaymode( mode ); glutinitwindowsize( 500,500 ); glutcreatewindow( Simple ); init(); glutdisplayfunc( display ); glutkeyboardfunc( key ); glutmainloop(); } Specify the display Mode RGB or color Index, single or double Buffer 3D Modelling and Augmented Reality - Jean-Marc Vézien 94
Sample Program #include <GL/glut.h> #include <GL/gl.h> void main(int argc, char** argv) { int mode = GLUT_RGB GLUT_DOUBLE; glutinitdisplaymode( mode ); glutinitwindowsize( 500,500 ); glutcreatewindow( Simple ); init(); glutdisplayfunc( display ); glutkeyboardfunc( key ); glutmainloop(); } Create a window Named simple with resolution 500 x 500 3D Modelling and Augmented Reality - Jean-Marc Vézien 95
Sample Program #include <GL/glut.h> #include <GL/gl.h> void main(int argc, char** argv) { int mode = GLUT_RGB GLUT_DOUBLE; glutinitdisplaymode( mode ); glutinitwindowsize( 500,500 ); } glutcreatewindow( Simple ); init(); glutdisplayfunc( display ); glutkeyboardfunc( key ); glutmainloop(); Your OpenGL initialization code (Optional) 3D Modelling and Augmented Reality - Jean-Marc Vézien 96
Sample Program #include <GL/glut.h> #include <GL/gl.h> void main(int argc, char** argv) { int mode = GLUT_RGB GLUT_DOUBLE; glutinitdisplaymode( mode ); glutinitwindowsize( 500,500 ); glutcreatewindow( Simple ); } init(); glutdisplayfunc( display ); glutkeyboardfunc(key); glutmainloop(); Register your call back functions 3D Modelling and Augmented Reality - Jean-Marc Vézien 97
glutmainloop() #include <GL/glut.h> #include <GL/gl.h> int main(int argc, char** argv) { int mode = GLUT_RGB GLUT_DOUBLE; glutinitdisplaymode(mode); glutinitwindowsize(500,500); glutcreatewindow( Simple ); init(); glutdisplayfunc(display); } Sample Program glutkeyboardfunc(key); glutmainloop(); The program goes into an infinite loop waiting for events 3D Modelling and Augmented Reality - Jean-Marc Vézien 98
OpenGL Initialization Set up whatever state you re going to use Specify camera, window (= viewport), lights void init( void ) { glclearcolor (0.0, 0.0, 0.0, 0.0); glviewport(0, 0, width, height); glmatrixmode(gl_projection); glloadidentity(); glortho(-10, 10, -10, 10, -10, 20); glmatrixmode(gl_modelview); glloadidentity(); } glenable( GL_LIGHT0 ); glenable( GL_LIGHTING ); glenable( GL_DEPTH_TEST ); 3D Modelling and Augmented Reality - Jean-Marc Vézien 99
GLUT Callback functions GLUT is Event-driven: Programs that use windows Input/Output events Wait until an event happens and then execute some pre-defined functions according to the user s input Events = key press, mouse button press and release, window resize, redraw, etc. Your OpenGL program will be in infinite loop 3D Modelling and Augmented Reality - Jean-Marc Vézien 100
GLUT Callback Functions Callback function : Routine to call when an event happens Window resize or redraw User input (mouse, keyboard) Animation (render many frames) Register callbacks with GLUT: glutdisplayfunc( my_display_func ); glutidlefunc( my_idle_func ); glutkeyboardfunc( my_key_events_func ); glutmousefunc ( my_mouse_events_func ); 3D Modelling and Augmented Reality - Jean-Marc Vézien 101
GLUT s Event Queue MainLoop() Event queue Keyboard. Mouse Window Mouse_callback() {. { Keypress_callback() {. { window_callback() {. { 3D Modelling and Augmented Reality - Jean-Marc Vézien 102
Events in GLUT Event Example OpenGL Callback Function Keypress KeyDown Mouse Motion Window System KeyUp leftbuttondown leftbuttonup With mouse press Without Moving Resizing Idle Timer Software What to draw glutkeyboardfunc glutmousefunc glutmotionfunc glutpassivemotionfunc glutreshapefunc glutidlefunc gluttimerfunc glutdisplayfunc 3D Modelling and Augmented Reality - Jean-Marc Vézien 103
Rendering Callback Callback function where all the drawing is done Every GLUT program must have a display callback glutdisplayfunc( my_display_func ); /* this part is in main.c */ void my_display_func (void ) { glclear( GL_COLOR_BUFFER_BIT ); glbegin( GL_TRIANGLE ); glvertex3fv( v[0] ); glvertex3fv( v[1] ); glvertex3fv( v[2] ); glend(); glflush(); } 3D Modelling and Augmented Reality - Jean-Marc Vézien 104
Idle Callback Called when not doing anything else Use for animation and continuous update Can use gluttimerfunc or timed callbacks for animations glutidlefunc( idle ); void idle( void ) { /* change something */ t += dt; glutpostredisplay(); } 3D Modelling and Augmented Reality - Jean-Marc Vézien 105
User Input Callbacks Process user input glutkeyboardfunc( my_key_events ); void my_key_events (char key, int x, int y ) { switch ( key ) { case q : case Q : exit ( EXIT_SUCCESS); break; case r : case R : rotate = GL_TRUE; break; } } 3D Modelling and Augmented Reality - Jean-Marc Vézien 106
Mouse Callback Captures mouse press and release events glutmousefunc( my_mouse ); void mymouse(int button, int state, int x, int y) { if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) { } } 3D Modelling and Augmented Reality - Jean-Marc Vézien 107
References 1. http://www.opengl.org/documentation/spec.html 2. http://www.opengl.org/documentation/* 3. http://www.cs.rit.edu/~jdb/cg1/openglintro.pdf 4. Many, many others 3D Modelling and Augmented Reality - Jean-Marc Vézien 108
THE END 3D Modelling and Augmented Reality - Jean-Marc Vézien 109