Introduction to C & C++ Lecture 12 OpenGL JJCAO 2 OpenGL Applications Modelling & Creative Content Creation Modelling, Animation, Video & Creative Content Creation applications that use OpenGL CAD CAD/CAM, Interior Design and Architectural applications that use Ope nGL

Developer Toolkits & Libraries, Game Engines, High Level 3D A PIs Tools, Libraries, Game Engines and High Level 3D APIs for developing OpenGL applications and games Games Freeware and commercial games that use the OpenGL API 3 Windows OpenGL Applications (cont) Virtual Reality Tools, Viewers & Internet 3D Streaming VRML/Web3D, Internet 3D Streaming & Virtual Reality Tools that u se OpenGL

Utilities: Screensavers, File Converters, Benchmarks Screen-Savers, Format Converters, Interface & Performance Utiliti es that use OpenGL Simulation & Visualization Simulation and Visualization applications that use OpenGL Scientific, Data Analysis & Geographic Mapping Scientific and Data Analysis applications that use OpenGL Modelling & Creative Content Creation A list of Modelling, Animation, Video & Creative Content Creation ap plications that use OpenGL can be found in: 3ds max (Professional 3D modelling, animation and rendering)

ImageModeler (Automatic creation of 3D models with textures, from still pictures) Lightwave 3D (3D modelling, animation, rendering) Cinema 4D (Modelling, ray tracing & animation) Maya (Character animation, modelling, F/X, rendering) Z-Brush 4

CAD 5 CAD/CAM, Interior Design and Architectural applications that use OpenGL AutoDest Pro/E CATIA SolidWorks, Delmia, ACIS 6 Developer Toolkits & Libraries, Game Engines & 3D APIs

Tools, Libraries, Game Engines and High Level 3D APIs for developing OpenGL applications and games Fly3D (Game engine in C++ with lots of advanced features) fltk (Fast, Light GUI Toolkit for OpenGL) libQGLViewer VTK (open-source, freely available software system for 3D comput er graphics, image processing and visualization) Volume rendering and image display

from the visible woman dataset. Fluid flow around the space shuttle CT scan from the visible woman dataset. An isosurface of the skin is clipped with a sphere to reveal the underlying bone structure. Games in OpenGL A list of some famous games include Quake (id's first true 3d FPS was Quake, GLQuake is the 3d acc elerated version)

Doom3 (Sequel to Doom) Half Life (FPS based on the QuakeII engine) Tribes 1 & 2 (FPS team online game ) 7 Virtual Reality Tools, Viewers & Internet 3D Streaming

VRML/Web3D, Internet 3D Streaming & Virtual Reality Tools that us e OpenGL OpenVRML (VRML97 runtime library) Visualizador3D (Virtual reality viewer for real time walkthroughs with textures, collision and gravity) Cosmo Player (VRML browser) OpenWorlds (C++ toolkit for integrating VRML into any new or e xistingapplications) Amaya (Web Browser/Editor for HTML, XHTML, CSS, MathML, SV G and XML)

8 9 Simulation & Visualization Simulation and Visualization applications that use OpenGL HyperPipe (Professional Flight Simulation Software) Capture 3.0 (A theatre/TV/event/architectural lighting design and visualization software) 3DShadows (Visualization of different real-time shadow-algorithm s) Di-Guy (Virtual animated humans) FreeVol3D (A free 3D medical visualization software for CT and M R DICOM volume data)

Scientific, Data Analysis & Geographic Mapping Scientific and Data Analysis applications that use OpenGL GraphingCalc (Mathematical 2D/3D visualization program) Graphis (2D/3D Data visualization and analysis tool) 3DSurface Viewer (Draw surfaces defined by mathematical expressions) OpenRT-3D (Real time OpenGL 2D and 3D cognitive reactio n time experiments) WinTrak Pro (Easy-to-use program for tracking satellites in r

eal-time on PC) Equipotential surfaces (3D Electromagnetism software for v isualization of equipotential surfaces and field lines) 10 What is OpenGL Low-level A software interface to graphics hardware that consists of a bout 250 distinct functions System-independent Designed as a streamlined, hardware-independent interfac e to be implemented on many different hardware platforms Client-Server abstraction

Client is the program which sends commands to the server Server (graphics card) produces pixels on the screen 12 Characteristics of OpenGL Function calls No data structures Abstract canvas Window system independent State machine current color, current model transforma tion,

13 OpenGL Command Syntax Constants begin with GL_ and are in capital letters GL_LIGHTING, GL_SMOOTH, etc Commands have prefix gl and initial capital letters for each word glEnable(), glDisable(), etc Some commands conatin extra letters which indicate the number and type of variables glColor3b(), glColor3i(), glColor3f(), etc

OpenGL Rendering Pipeline 1.Defines objects mathematically. 2.Arranges objects in space relative to a viewpoi nt. 3.Calculates the color of the objects. 4.Rasterizes the objects. 14 15 Key Stages in the OpenGL Rendering Pipeline Display Lists All data, whether it describes geometry or pixels, can be sav ed in a display list for current or later use

Evaluators All geometric primitives are eventually described by vertices Evaluators provide a method to derive the vertices used to r epresent the surface from the control points Per-Vertex Operations For vertex data, next is the "per-vertex operations" stage, w hich converts the vertices into primitives 16 Key Stages in the OpenGL Rendering Pipeline (cont) Primitive Assembly The results of this stage are complete geometric primitives, w

hich are the transformed and clipped vertices with related col or, depth, and sometimes texture-coordinate values and guid elines for the rasterization step Pixel Operations Pixels from an array in system memory are first unpacked fro m one of a variety of formats into the proper number of comp onents Next the data is scaled, biased, and processed by a pixel map Results are clamped and then either written into texture mem ory or sent to the rasterization step 17 Key Stages in the OpenGL Rendering Pipeline (cont)

Texture Assembly An OpenGL application may wish to apply texture images onto geo metric objects to make them look more realistic Rasterization Rasterization is the conversion of both geometric and pixel data int o fragments Each fragment square corresponds to a pixel in the framebuffer Fragment Operations Before values are actually stored into the framebuffer, a series of o perations are performed that may alter or even throw out fragmen ts All these operations can be enabled or disabled Brief History of OpenGL

It was promoted by SGI (& Microsoft, half-heartedly), is now promo ted/supported by NVIDIA, ATI, etc. It doesnt change every year (like DirectX, its main competitor) 1983 IRIS GL ships with SGI IRIS 1000 terminal 1987 SGI and Pixar consider joint API development 1991 OpenGL ARB created 1992 OpenGL 1.0 completed (June 30) 1995 OpenGL 1.1 released (vertex array, texture objects, new texenv modes) 1997 Fahrenheit agreement between SGI and Microsoft 1998 OpenGL 1.2 released (3D textures, separate specular, imaging) 1999 OpenGL 1.2.1 released (multi-texture) 2001 OpenGL 1.3 released (compressed texture, cube maps, multisample, dot3) 2002 OpenGL 1.4 (mip-map generation, shadows, point parameters) 2003 OpenGL 1.5 (vertex buffer objects, occlusion query) ARB extensions: OpenG L Shading language, ARB_vertex_program, ARB_fragment_program 2004 OpenGL 2.0

19 OpenGL Related Libraries GLUT (GL Utility Toolkit)

A simple windowing API for OpenGL Callback style programming GLU (GL Utilities) Several routines that perform tasks like setting up matric es for viewing operations and projections and many more FSG (Fahrenheit Scene Graph) OO toolkit that provides objects and methods for creating interactive 3D graphics animations OpenGL Geometric Drawing Primitives OpenGL geometric primitives can create a set of points, a line, or a polygon from vertices OpenGL support ten primitives A drawing primitive must start with

glBegin(); And finish with glEnd(); Between them the primitive glBegin(GL_POLYGON); glVertex2f(-0.5, -0.5); glVertex2f(-0.5, 0.5); glVertex2f( 0.5, 0.5); glVertex2f( 0.5, -0.5); glEnd(); OpenGL Geometric Drawing Primitives (con t) 22

Front/Back Rendering Each polygon has two sides, front and back OpenGL can render the two differently The ordering of vertices in the list determi nes which is the front side: When looking at the front side, the vertices go co unterclockwise This is basically the right-hand rule Note that this still holds after perspective projecti on OpenGL Colouring OpenGL maintains a current colour (in RGBA mode) and a current colour index (in color-index mode). Unless you're using a more complic

ated colouring model such as lighting or texture mapping, each object is dr awn using the current colour (or color index) In RGBA mode, use the glColor*() The glColor*() command accept floating-point data types (range between 0.0 an d 1.0) glBegin(GL_TRIANGLES); glColor3F(1.0f,0.0f,0.0f); glVertex3f( 0.0f, 1.0f, 0.0f); glColor3F(0.0f,1.0f,0.0f); glVertex3f(1.0f,-1.0f, 0.0f); glColor3F(0.0f,0.0f,1.0f); glVertex3f( 1.0f,1.0f, 0.0f); glEnd(); glColor3F(0.5f,0.5f,1.0f); glBegin(GL_QUADS); // Draw A Quad glVertex3f(-1.0f, 1.0f, 0.0f);

glEnd(); In color-index mode, use the glIndex*() command to select a single valued colour index as the current colour index void glIndex{sifd ub}(TYPE c); OpenGL Geometric Drawing Primitives (con t) Nate_Robins_tutorials: Shapes Draw a complicated 3D Object

void DrawMeshWire( CMesh *m ) glBegin( GL_LINES ); for ( int i = 0; i < m->numFaces * 3; i+=3 ) { glVertex3f( m->vertex[m->faces[i]*3], m->vertex[m->faces[i]*3+1], m->vertex[m->faces[i]*3+2] ); glVertex3f( m->vertex[m->faces[i+1]*3], m->vertex[m->faces[i+1]*3 +1], m->vertex[m->faces[i+1]*3+2] ); glVertex3f( m->vertex[m->faces[i+1]*3], m->vertex[m->faces[i+1]*3 +1], m->vertex[m->faces[i+1]*3+2] ); } glEnd();

Camera Analogy OpenGL coordinate system has different origin (lower-left corner) from the window system (up per-left corner) The transformation process to produce the des ired scene for viewing is analogous to taking a photograph with a camera The steps with a camera (or a computer) migh t be the following: Set up your tripod and pointing the camera at the s cene (viewing transformation). Arrange the scene to be photographed into the desi red composition (modelling transformation) Choose a camera lens or adjust the zoom (projectio n transformation) Determine how large you want the final photograph

to be (viewport transformation) 26 27 Stages of Vertex Transformation To specify viewing, modelling, and projection transfo rmations, you construct a 4x4 matrix M, which is the n multiplied by the coordinates of each vertex v in th e scene to accomplish the transformation (v'=Mv) Modeling Transformations The three OpenGL routines for modeling transformations are: glTranslate*(),

glScale*() void glRotate{fd}(TYPE angle, TYPE x, TYPE y, TYPE z); glRotatef(45.0, 0.0, 0.0, 1.0) These routines transform an object (or coordinate syste m, if you're thinking of it that way) by moving, rotating, stretc hing, shrinking, or reflecting it All three commands are equivalent to producing an appropri ate translation, rotation, or scaling matrix, and then calling g lMultMatrix*() with that matrix as the argument OpenGL automatically computes the matrices for you Copy the current matrix and push it onto a stack: glPushMatrix() Discard the current matrix and replace it with whatevers on t op of the stack:

glPopMatrix() 28 Modeling Transformations (cont) 29 Each of these postmultiplies the current matrix E.g., if current matrix is C, then C=CS The current matrix is either the modelview matrix or the projection matrix (also a texture matrix, wont disc uss) Set these with glMatrixMode(), e.g.: glMatrixMode(GL_MODELVIEW);

glMatrixMode(GL_PROJECTION); WARNING: common mistake ahead! Be sure that you are in GL_MODELVIEW mode before makin g modeling or viewing calls! Ugly mistake because it can appear to work, at least for a w hile Modeling Transformations (cont) Nate_Robins_tutorials: 31 OpenGL Viewing Transformation Viewing transformation is analogous to positioning and aiming a cam

era A viewing transformation changes the position and orientation of the Vie wpoint Before the viewing transformation can be specified, the current matrix is set to the identity matrix by using glLoadIdentity(); You can manufacture a viewing transformation in any of several ways Use one or more modeling transformation commands (that is, glTranslate*() and glRotate*()) Use the Utility Library routine gluLookAt() to define a line of sight. This routine encapsulates a series of rotation and translation commands. Create your own utility routine that encapsulates rotations and translations 32

OpenGL Projection Transformation Specifying the projection transformation is like choosing a lens for a camera The purpose of the projection transformation is to define a viewing volume, which is used in two ways. The viewing volume determines how an object is projected onto the s creen (that is, by using a perspective or an orthographic projection), and Defines which objects or portions of objects are clipped out of t he final image Need to establish the appropriate mode for constructing the viewing transformation, or in other words select the projection mode glMatrixMode(GL_PROJECTION); This designates the projection matrix as the current matrix, which is

originally set to the identity matrix Perspective Projection 33 The characteristic of perspective projection is that the farther an object is from the camera, the smaller it appears in the final image This occurs because the viewing volume for a perspective projection is a frustum of a pyramid This method of projection is commonly used for animation, visual simulation, and a ny other applications that strive for some degree of realism because it's similar to how our eye (or a camera) works The command to define a frustum, glFrustum(), calculates a perspective projection matrix and multiplies the current projection matrix (typically the identity matrix) by it

void glFrustum(GLdouble left, GLdouble right, GLdouble bottom, Gldouble top, GLdouble ne ar, GLdouble far); Advanced Perspective Projection 34 glFrustum() isn't intuitive to use so can use gluPerspective() which creates a viewin g volume of the same shape as glFrustum() does, but rather than specifying corners of the near clipping plane, you specify the angle of the field o f view in the y direction and the aspect ratio of the width to height (x/y). These two parameters are enough to determine an untruncated pyramid along the li ne of sight You also specify the distance between the viewpoint and the near and far clipping planes, there by truncating the pyramid.

Note that gluPerspective() is limited to creating frustums that are symmetric in both t he x- and y-axes along the line of sight void gluPerspective(GLdouble fovy, GLdouble aspect, GLdouble near, GLdouble far); Orthographic Projection 35 With an orthographic projection, the viewing volume is a rectangular p arallelepiped Size of the viewing volume doesn't change from one end to the other, so dista nce from the camera doesn't affect how large an object appears Ortographic projection is used for applications such as creating archite ctural blueprints and computer-aided design, where it's crucial to main

tain the actual sizes of objects and angles between them void gluOrtho2D (GLdouble left, GLdouble right, GLdouble bottom, GLdouble top); void glOrtho (GLdouble left, GLdouble right, GLdouble bottom, Gldouble top, GLdouble near, GLdouble far); Projection & Viewpoint (cont) Nate_Robins_tutorials: OpenGL Lighting Provides a limited variety of light sources We can have point sources, spotlights and ambient sources Each source has separate diffuse, specular and ambient RGB parameters Materials are modeled in a complementary manner

For each surface separate ambient, diffuse and specular components must be u sed Lighting calculations must be enabled and each light source must be enabl ed individually glEnable(GL_LIGHTING); glEnable(LIGHT1); Enabling lighting makes OpenGL to do the shading calculations Once lighting is enabled, colours assigned by glColor() are no longer valid 37 38

Specifying a Light Source Light sources have a number of properties, such as colour, position, a nd direction The OpenGL function to create a light source is void glLight{if}(GLenum light, GLenum param, TYPE value); The directional light source allows to associate three different colour-r elated parameters with any particular light GL_AMBIENT, GL_DIFFUSE, and GL_SPECULAR The positional light source need to define the location (GL_LOCATION), and the colour (ambient, diffuse and specular) Also can have a positional light source act as a spotlight Specifying Material Properties

39 Material properties match the lighting properties A material has reflectivity properties for each type of light The basic function for setting material properties is: void glMaterial{if}(GLenum face, GLenum name, TYPE value); Diffuse and Ambient Reflection The GL_DIFFUSE and GL_AMBIENT parameters set with glMaterial*() affect the c olour of the diffuse and ambient light reflected by an object Specular Reflection Specular reflection from an object produces highlights. OpenGL allows you to set the effect that the material has on reflected light (wit

h GL_SPECULAR) and control the size and brightness of the highlight (with GL_S HININESS Emission By specifying an RGBA color for GL_EMISSION, you can make an object appear to be giving off light of that color Light & Material Nate_Robins_tutorials: 41 Light & Material (cont) OpenGL Shading

42 Lighting calculations are made on a vertex-by-vertex ba sis OpenGL computes vertex colours to create shading effe ct. flat shading (single color): glShadeModel(GL_FLAT); With flat shading the color of vertices is duplicated across all th e primitive vertices smooth shading (many different colours): glShadeModel(GL_ SMOOTH); In smooth shading the colour at each vertex is treated individu ally

OpenGL Terrain Generator 43 An example of OpenGL terrain generator devel oped by Antnio Ramires Fernandes can be fo und in: http://www.lighthouse3d.com/opengl/appstools/tg/ Terrain generation from an image, computing normals and simulating both directional and p ositional lights 44 Laboratory Sessions Assignment: Building the solar system

You will need to write from scratch a com plete OpenGL programme that renders a Sun with an orbiting planet and a moon o rbiting the planet 45 Assignment Basic Implementation The basic implementation includes the following: Add a sphere representing the sun planet Make the sun planet to rotate around itself Add another sphere representing the earth Make the earth planet to rotate around itself Make the earth planet to rotate around sun Add another sphere representing the moon

Make the moon planet to rotate around itself Make the moon planet to rotate around the earth Control the camera position using the keyboard Control the camera position using widget menus Add a light source Add shading to the planets Add material properties to the planets (you have to check this ou t yourselves) 46 Assignment Advanced Implementation Recommended Implementation Add more planets, e.g. if you are quick enough you could create the complete solar system

Add more light sources (OpenGL supports up to 8 lights) Have planets counter rotating Add more moons to planets Add stars to the planetary system Add spaceships Reference Fotis Liarokapis [Nate Robins] [Nehe] [Red book] Building Virtual Environments with OpenGL http://www.morrowland.com/apron/tut_gl.php http://nehe.gamedev.net/ OpenGL Programming Guide: The Official Guide to Learning OpenGL, Latest Version

47 48