Unit III Graphics Programming Using Open GL Kaustubh
Unit –III Graphics Programming Using Open. GL Kaustubh Satpute Department of Computer Science and Engineering DMIETR, Wardha
Syllabus • • • Graphics Programming using OPENGL: Why Open. GL, Features in Open. GL, Open. GL operations, Abstractions in Open. GL – GL, GLU & GLUT, 3 D viewing pipeline and viewing matrix specifications, • a few examples and demos of Open. GL programs, • Animations in Open. GL
Graphics Processing Unit (GPU)
History of Open. GL • In the 1980's developing software was a challenge. Especially when you wanted to program for a wide range of graphics hardware • The software developers had to write custom drivers for different Interface and devices. • In the early 90's Silicon Graphics Inc. (SGI) was a leader in 3 D graphics for workstations. • They used an API called IRIS GL(Integrated Raster Imaging System Graphical Library) for their workstations. • Competing vendors i. e. Sun Microsystems, IBM and Hewlett-Packard were also bringing 3 D hardware on the market. They used another API called PHIGS(Programmers Hierarchical Graphics System ). In functionality and capability IRIS GL was superior to PHIGS • New 3 D hardware on the market, SGI's market share became smaller • SGI made a new API based on IRIS GL called Open. GL • In 1992, SGI led the creation of the Open. GL architectural review board (ARB)[Founding Companies SGI, Microsoft, IBM, DEC and Intel]
Open. GL? • The Open Graphics Library (Open. GL) is a standard specification defining a cross-language, cross-platform Application Programming Interface (API). Open. GL provides the programmer an interface to the graphics hardware. Open. GL is a powerful, low-level rendering and modelling software library. • With Open. GL it is possible to produce 2 D and 3 D graphics applications. Open. GL is widely used in video games, CAD, scientific applications, etc, on many different platforms. • Many commercial games, such as the ID Software's Quake series, use Open. GL for their graphics engines.
Open. GL Basic Features: Basic features: Ø Drawing primitives Ø Transformations Ø Color Ø Lighting Ø Display Lists Advanced features: Ø Texture mapping Ø Vertex Arrays Ø Blending effects Ø Frame buffer manipulation
3 D Graphics Rendering Pipeline
Open. GL Rendering Pipeline
Display Lists: -All data, whether it describes geometry or pixels, can be saved in a display list for current or later use. When a display list is executed, the retained data is sent from the display list just as if it were sent by the application in immediate mode. Evaluators: -All geometric primitives are eventually described by vertices. Parametric curves and surfaces may be initially described by control points and polynomial functions called basis functions. Evaluators provide a method to derive the vertices used to represent the surface from the control points. The method is a polynomial mapping, which can produce surface normal, texture coordinates, colors, and spatial coordinate values from the control points. Per-Vertex Operations: - For vertex data, next is the per-vertex operations stage, which converts the vertices into primitives. Some vertex data (for example, spatial coordinates) are transformed by 4 x 4 floating-point matrices. Spatial coordinates are projected from a position in the 3 D world to a position on your screen. If advanced features are enabled, this stage is even busier. If texturing is used, texture coordinates may be generated and transformed here. If lighting is enabled, the lighting calculations are performed using the transformed vertex, surface normal, light source position, material properties, and other lighting information to produce a color value.
Primitive Assembly: - Clipping, a major part of primitive assembly, is the elimination of portions of geometry which fall outside a half-space, defined by a plane. Point clipping simply passes or rejects vertices; line or polygon clipping can additional vertices depending upon how the line or polygon is clipped. 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 rasterization step. Pixel Operations: - While geometric data takes one path through the Open. GL rendering pipeline, pixel data takes a different route. 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. The results are clamped and then either written into texture memory or sent to the rasterization step. If pixel data is read from the frame buffer, pixel-transfer operations (scale, bias, mapping, and clamping) are performed. Then these results are packed into an appropriate format and returned to an array in system memory. There are special pixel copy operations to copy data in the frame buffer to other parts of the frame buffer or to the texture memory. A single pass is made through the pixel transfer operations before the data is written to the texture memory or back to the frame buffer.
Texture Assembly: -An Open. GL application may wish to apply texture images onto geometric objects to make them look more realistic. If several texture images are used, it's wise to put them into texture objects so that you can easily switch among them. Some Open. GL implementations may have special resources to accelerate texture performance. There may be specialized, high-performance texture memory. If this memory is available, the texture objects may be prioritized to control the use of this limited and valuable resource. Rasterization: - Rasterization is the conversion of both geometric and pixel data into fragments. Each fragment square corresponds to a pixel in the frame buffer. Line and polygon stipples, line width, point size, shading model, and coverage calculations to support anti-aliasing are taken into consideration as vertices are connected into lines or the interior pixels are calculated for a filled polygon. Color and depth values are assigned for each fragment square. Fragment Operations: - Before values are actually stored into the frame buffer, a series of operations are performed that may alter or even throw out fragments. All these operations can be enabled or disabled.
Open. GL Hierarchy Several levels of abstraction are provided Ø GL(Graphics Library) : - Open. GL on Windows and GL on most Unix/Linux System. Lowest level: vertex, matrix manipulation • Primitives - points, line, polygons • Shading and Colour • Translation, rotation, scaling • Viewing, Clipping, Texture • Hidden surface removal Ø GLU( Open. GL Utility Library ): -Provides functionality in Open. GL core but avoids having to rewrite code, Helper functions for shapes, transformations • Viewing –perspective/orthographic • Image scaling, polygon tessellation • Sphere, cylinders, quadratic surfaces Ø GLUT(Open. GL Utility Library ): - Provides functionality common to all window systems such as Open a window, Initialize Open. GL State, Get input from mouse and keyboard, Menus, Event-driven. Highest level: Window and interface management • Windowing toolkit (key, mouse handler, window events)
Open. GL and Related APIs application program Open. GL Motif widget or similar GLUT GLX, AGL or WGL X, Win 32, Mac O/S GLU GL software and/or hardware AGL, GLX, WGL: glue between Open. GL and windowing systems GLU (Open. GL Utility Library) : part of Open. GL , NURBS, tessellators, quadric shapes, GLUT (Open. GL Utility Toolkit): portable windowing API, not officially part of Open. GL
Preliminaries Headers Files • #include <GL/gl. h> • #include <GL/glut. h> Libraries Enumerated Types • Open. GL defines numerous types for compatibility – GLfloat, GLint, GLenum, etc.
Open. GL Commands & GLU Routine Types Open. GL Commands • Primitives • Coordinate Transformation • Coloring and Lighting • Clipping • Rasterization • Pixel Operations • Texture Mapping • Fog • Frame Buffer Operations • Evaluators • Selection and Feedback • Display Lists • Modes and Execution • State Queries GLU Routines • Texture Images • Coordinate Transformation • Polygon Tessellation • Quadric Objects • NURBS Curves and Surfaces • Error Handling GLX Routines • Initialization • Controlling Rendering
Open. GL Command Notation void gl. Some. Function {3} {bsifd} {v} (arguments; ( gl. Vertex 3 fv(. . . ) Number of components 2 - (x, y) 3 - (x, y, z) 4 - (x, y, z, w) Data Type b ub s us i ui f d - byte unsigned byte short unsigned short int unsigned int float double Vector omit v for scalar form gl. Vertex 2 f( x, y )
Some Open. GL Command Primitives Coordinate Transformation Specify vertices or rectangles: Transform the current matrix: void gl. Begin (GLenum mode); void gl. Rotate{fd} (TYPE angle, TYPE x, TYPE y, TYPE z); void gl. End (void); void gl. Translate{fd} (TYPE x, TYPE y, TYPE z); void gl. Vertex 2{sifd}{v} (TYPE x, TYPE y); void gl. Vertex 3{sifd}{v} (TYPE x, TYPE y, TYPE z); void gl. Scale{fd} (TYPE x, TYPE y, TYPE z); void gl. Vertex 4{sifd}{v} (TYPE x, TYPE y, TYPE z, void gl. Mult. Matrix{fd} (const TYPE *m); TYPE w); void gl. Frustum (GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, void gl. Rect{sifd} (TYPE x 1, TYPE y 1, TYPE x 2, GLdouble far); TYPE y 2); void gl. Ortho (GLdouble left, GLdouble right, void gl. Rect{sifd}v (const TYPE *v 1, const GLdouble bottom, GLdouble top, GLdouble near, TYPE *v 2); GLdouble far); Specify polygon edge treatment: Replace the current matrix: void gl. Edge. Flag (GLboolean flag); void gl. Load. Matrix{fd} (const TYPE *m); void gl. Edge. Flagv (const GLboolean *flag); void gl. Load. Identity (void); Manipulate the matrix stack: void gl. Matrix. Mode (GLenum mode); void gl. Push. Matrix (void); void gl. Pop. Matrix (void);
Why GLUT Requires • We begin with the basic elements of how to create a window. Open. GL was intentionally designed to be independent of any Specific window system. As a result, a number of the basic window operations are not provided in Open. GL. • Therefore, a separate library called GLUT or Open. GL Utility Toolkit was created to provide these functions. • Basically, GLUT provides the necessary tools for requesting windows to be created and providing interaction with I/O devices
Window Management Five routines perform tasks necessary to initialize a window. glut. Init(int *argc, char **argv) initializes GLUT and processes any command line arguments . glut. Init() should be called before any other GLUT routine. glut. Init. Display. Mode(unsigned int mode) specifies whether to use an RGBA or color-index color model. You can also specify whether you want a single- or double-buffered window. For example, if you want a window with double buffering, the RGBA color model, and a depth buffer, you might call glut. Init. Display. Mode(GLUT_DOUBLE|GLUT_RGB| GLUT_DEPTH).
Window Management glut. Init. Window. Position(int x, int y) specifies the screen location for the upper-left corner of your window. glut. Init. Window. Size(int width, int size) specifies the size, in pixels, of your window. int glut. Create. Window(char *string) creates a window with an Open. GL context. It returns a unique identifier for the new window. Be warned: Until glut. Main. Loop() is called the window is not yet displayed
Program Structure • Most Open. GL programs have a similar structure that consists of the following functions –main(): • defines the callback functions • opens one or more windows with the required properties • enters event loop (last executable statement) –init(): sets the state variables • viewing • Attributes –callbacks • Display function • Input and window functions
A Typical Event-Driven Program Initialization Main Loop Event Handler Input Processing Procedures Background Processing Procedure
main. c #include <GL/glut. h> includes gl. h int main(int argc, char** argv) { glut. Init(&argc, argv); glut. Init. Display. Mode(GLUT_SINGLE|GLUT_RGB); glut. Init. Window. Size(500, 500); glut. Init. Window. Position(0, 0); define window properties glut. Create. Window( simple ); glut. Display. Func(mydisplay); init(); display callback set Open. GL state glut. Main. Loop(); } enter event loop 23
init. c black clear color void init() { gl. Clear. Color (0. 0, 1. 0); fill with white gl. Color 3 f(1. 0, 1. 0); } opaque window gl. Matrix. Mode (GL_PROJECTION); gl. Load. Identity (); gl. Ortho(-1. 0, -1. 0, 1. 0); viewing volume 24
mydisplay. c void mydisplay() { gl. Clear(GL_COLOR_BUFFER_BIT); gl. Begin(GL_POLYGON); gl. Vertex 2 f(-0. 5, -0. 5); gl. Vertex 2 f(-0. 5, 0. 5); gl. Vertex 2 f(0. 5, -0. 5); gl. End(); gl. Flush(); } 25
Features of Open. GL Ø 3 D Transformations - Rotations, scaling, translation, perspective Ø Colour models - Values: R, G, B, alpha. Ø Lighting - Flat shading, Gouraud shading, Phong shading Ø Rendering -Texture mapping Ø Modelling - non-uniform rational B-spline (NURB) curves, surfaces Ø Others - atmospheric fog, alpha blending, motion blur
How to Install & run Open. GL Program Using Open. GL & GLUT in Code: : Blocks Download Code: : Blocks http: //www. sci. brooklyn. cuny. edu/~goetz/codeblocks/ Download the GLUT bin file (first download link) from: http: //www. deannicholls. co. uk/tutorials/show/cpp_glut After you download and open the GLUT bin zip file, you will need to: Copy glut 32. dll to c: windowssystem, Copy glut 32. lib to c: program filesmingwlib, and Copy glut. h to c: program filesmingwincludeGL. These are default locations, your paths may be different. But basically, you place the. lib and. h files in the location of your compiler (in this case mingw). The. dll file goes in the windows system directory. Now you are ready to start Code: : Blocks and make a new project
References Open. GL Reference Book Ø http: //www. glprogramming. com/red/index. html Ø http: //www. glprogramming. com/blue/index. html Ø https: //helpindex. wordpress. com/2011/07/27/lin e-generation-simple-dda-using-opengl-programsource-code/ Ø http: //www. ntu. edu. sg/home/ehchua/programmi ng/opengl/cg_basicstheory. html
Thank You
- Slides: 29