480 likes | 558 Views
Introduction to C & C++. Lecture 12 – OpenGL JJCAO. 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 OpenGL
E N D
Introduction to C & C++ Lecture 12 – OpenGL JJCAO
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 OpenGL • Developer Toolkits & Libraries, Game Engines, High Level 3D APIs • 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
Windows OpenGL Applications(cont) • Virtual Reality Tools, Viewers & Internet 3D Streaming • VRML/Web3D, Internet 3D Streaming & Virtual Reality Tools that use OpenGL • Utilities: Screensavers, File Converters, Benchmarks • Screen-Savers, Format Converters, Interface & Performance Utilities 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 applications 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
CAD CAD/CAM, Interior Design and Architectural applications that use OpenGL • AutoDest • Pro/E • CATIA • SolidWorks, Delmia, ACIS
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 computer graphics, image processing and visualization) Volume rendering and image display from the visible woman dataset. CT scan from the visible woman dataset. An isosurface of the skin is clipped with a sphere to reveal the underlying bone structure. Fluid flow around the space shuttle
Games in OpenGL A list of some famous games include • Quake (id's first true 3d FPS was Quake, GLQuake is the 3d accelerated version) • Doom3 (Sequel to Doom) • Half Life (FPS based on the QuakeII engine) • Tribes 1 & 2 (FPS team online game )
Virtual Reality Tools, Viewers &Internet 3D Streaming VRML/Web3D, Internet 3D Streaming & Virtual Reality Tools that use 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 existingapplications) • Amaya (Web Browser/Editor for HTML, XHTML, CSS, MathML, SVG and XML)
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-algorithms) • Di-Guy (Virtual animated humans) • FreeVol3D (A free 3D medical visualization software for CT and MR 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 reaction time experiments) • WinTrakPro (Easy-to-use program for tracking satellites in real-time on PC) • Equipotential surfaces (3D Electromagnetism software for visualization of equipotential surfaces and field lines)
What is OpenGL • Low-level • A software interface to graphics hardware that consists of about 250 distinct functions • System-independent • Designed as a streamlined, hardware-independent interface 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
Characteristics of OpenGL • Function calls • No data structures • Abstract canvas • Window system independent • State machine • current color, current model transformation, …
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 • Defines objects mathematically. • Arranges objects in space relative to a viewpoint. • Calculates the color of the objects. • Rasterizes the objects.
Key Stages in the OpenGLRendering Pipeline • Display Lists • All data, whether it describes geometry or pixels, can be saved 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 represent the surface from the control points • Per-Vertex Operations • For vertex data, next is the "per-vertex operations" stage, which converts the vertices into primitives
Key Stages in the OpenGLRendering Pipeline (cont) • Primitive Assembly • The results of this stage are complete geometric primitives, which are the transformed and clipped vertices with related color, depth, and sometimes texture-coordinate values and guidelines for the rasterizationstep • Pixel Operations • Pixels from an array in system memory are first unpacked from one of a variety of formats into the proper number of components • Next the data is scaled, biased, and processed by a pixel map • Results are clamped and then either written into texture memory or sent to the rasterization step
Key Stages in the OpenGLRendering Pipeline (cont) • Texture Assembly • An OpenGL application may wish to apply texture images onto geometric objects to make them look more realistic • Rasterization • Rasterizationis the conversion of both geometric and pixel data into fragments • Each fragment square corresponds to a pixel in the framebuffer • Fragment Operations • Before values are actually stored into the framebuffer, a series of operations are performed that may alter or even throw out fragments • All these operations can be enabled or disabled
Brief History of OpenGL It was promoted by SGI (& Microsoft, half-heartedly), is now promoted/supported by NVIDIA, ATI, etc. It doesn’t change every year (like DirectX, it’s 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: OpenGL Shading language, ARB_vertex_program, ARB_fragment_program • 2004 OpenGL 2.0
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 matrices 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 tenprimitives • 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();
Front/Back Rendering • Each polygon has two sides, front and back • OpenGL can render the two differently • The ordering of vertices in the list determines which is the front side: • When looking at the front side, the vertices go counterclockwise • This is basically the right-hand rule • Note that this still holds after perspective projection
OpenGL Colouring • OpenGL maintains a current colour(in RGBA mode) and a current colourindex (in color-index mode). Unless you're using a more complicated colouring model such as lighting or texture mapping, each object is drawn 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 and 1.0) • In color-index mode, use the glIndex*() command to select a singlevaluedcolourindex as the current colour index • void glIndex{sifdub}(TYPE c); 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();
OpenGL Geometric Drawing Primitives (cont) 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 (upper-left corner) • The transformation process to produce the desired scene for viewing is analogous to taking a photograph with a camera • The steps with a camera (or a computer) might be the following: • Set up your tripod and pointing the camera at the scene (viewing transformation). • Arrange the scene to be photographed into the desired composition (modelling transformation) • Choose a camera lens or adjust the zoom (projection transformation) • Determine how large you want the final photograph to be (viewport transformation)
Stages of Vertex Transformation • To specify viewing, modelling, and projection transformations, you construct a 4x4 matrix M, which is then multiplied by the coordinates of each vertex v in the 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 system, if you're thinking of it that way) by moving, rotating, stretching, shrinking, or reflecting it • All three commands are equivalentto producing an appropriate translation, rotation, or scaling matrix, and then calling glMultMatrix*() with that matrix as the argument • OpenGL automaticallycomputes the matrices for you • Copy the current matrix and push it onto a stack: • glPushMatrix() • Discard the current matrix and replace it with whatever’s on top of the stack: • glPopMatrix()
Modeling Transformations (cont) • Each of these postmultipliesthe current matrix • E.g., if current matrix is C, then C=CS • The current matrix is either the modelviewmatrix or the projectionmatrix (also a texture matrix, won’t discuss) • 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 making modeling or viewing calls! • Ugly mistake because it can appear to work, at least for a while…
Modeling Transformations (cont) Nate_Robins_tutorials: Transformation
OpenGL Viewing Transformation • Viewing transformation is analogous to positioning and aiming a camera • A viewing transformation changes the position and orientation of the Viewpoint • 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
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 screen (that is, by using a perspective or an orthographic projection), and • Defines which objects or portions of objects are clipped out of the 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 • 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 any 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 near, GLdouble far);
Advanced Perspective Projection • glFrustum() isn't intuitive to use so can use gluPerspective() which creates a viewing 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 of 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 line of sight • You also specify the distance between the viewpoint and the near and far clipping planes, thereby truncating the pyramid. • Note that gluPerspective() is limited to creating frustums that are symmetric in both the x- and y-axes along the line of sight • void gluPerspective(GLdoublefovy, GLdouble aspect, GLdoublenear, GLdoublefar);
Orthographic Projection • With an orthographic projection, the viewing volume is a rectangular parallelepiped • Size of the viewing volume doesn't change from one end to the other, so distance from the camera doesn't affect how large an object appears • Ortographicprojection is used for applications such as creating architectural blueprints and computer-aided design, where it's crucial to maintain the actual sizes of objects and angles between them • void gluOrtho2D(GLdouble left, GLdouble right, GLdoublebottom, GLdoubletop); • void glOrtho(GLdouble left, GLdouble right, GLdouble bottom, Gldouble top, GLdouble near, GLdouble far);
Projection & Viewpoint (cont) Nate_Robins_tutorials: Projection
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 used • Lighting calculations must be enabled and each light source must be enabled 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
Specifying a Light Source • Light sources have a number of properties, such as colour, position, and direction • The OpenGL function to create a light source is • void glLight{if}(GLenum light, GLenumparam, TYPE value); • The directional light source allows to associate three different colour-related 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 • 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); • Diffuseand Ambient Reflection • The GL_DIFFUSE and GL_AMBIENT parameters set with glMaterial*() affect the colour of the diffuse and ambient light reflected by an object • SpecularReflection • Specular reflection from an object produces highlights. • OpenGL allows you to set the effect that the material has on reflected light (with GL_SPECULAR) and control the size and brightness of the highlight (with GL_SHININESS • 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: lightmaterial
OpenGL Shading • Lighting calculations are made on a vertex-by-vertex basis • OpenGL computes vertex colours to create shading effect. • flat shading (single color): glShadeModel(GL_FLAT);With flat shading the color of vertices is duplicated across all the primitive vertices • smooth shading (many different colours): glShadeModel(GL_SMOOTH);In smooth shading the colour at each vertex is treated individually
OpenGL Terrain Generator • An example of OpenGL terrain generator developed by AntónioRamiresFernandes can be found in: http://www.lighthouse3d.com/opengl/appstools/tg/ • Terrain generation from an image, computing normalsand simulating both directional and positional lights
Laboratory Sessions • Assignment: Building the solar system • You will need to write from scratch a complete OpenGL programme that renders a Sun with an orbiting planet and a moon orbiting the planet
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 out • yourselves)
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