Open GL Basics Open GL Open Graphics Library
Open. GL Basics
Open. GL – Open Graphics Library § What it is a s/w interface to graphics h/w mid-level, device-independent, portable graphics subroutine package developed primarily by SGI 2 D/3 D graphics, lower-level primitives (polygons) does not include low-level I/O management basis for higher-level libraries/toolkits
The camera analogy
Open. GL libraries § GL – gl. h – Opengl 32. lib Provides basic commands for graphics drawing § GLU (Open. GL Utility Library) – glu. h – glu 32. lib Uses GL commands for performing compound graphics like § viewing orientation and projection specification § polygon tessellations, surface rendering etc. § GLUT (Open. GL Utility Toolkit) – glut. h – glut. lib is a window system-independent toolkit for user interaction built on top of Open. GL and WGL (Windows) or GLX (Linux). § System-specific Open. Gl extensions GLX : for X window system (Linux/Unix) WGL: for Windows 95/98/2000/NT AGL : Apple Macintosh system
Open. GL conventions § Functions in Open. GL start with gl Most functions use just gl (e. g. , gl. Color()) Functions starting with glu are utility functions (e. g. , glu. Look. At()) § Note that GLU functions can always be composed entirely from core GL functions Functions starting with glut are from the GLUT library.
Open. GL conventions § Function names indicate argument type and number Functions ending with f take floats Functions ending with i take ints Functions ending with b take bytes Functions ending with ub take unsigned bytes Functions that end with v take an array. § Examples gl. Color 3 f() takes 3 floats gl. Color 4 fv() takes an array of 4 floats
Graphics Pipeline
Step 1: Modeling Transform § Vertices of an object are define in it’s own co-ordinate system (Object Space) § A scene is composed of different objects and some times multiple copy of the same object § Modeling transforms places all the objects in a world coordinate system (World Space) § Model Transforms must be specified before specifying the vertices of an object gl. Rotatef(30, 1, 0, 0); gl. Vertex 3 d(20, 30, 10); Instantiate & Modeling Transform § Basic Modeling Transforms are: Translate : gl. Translate{f|d}(x, y, z) Rotate : gl. Rotate{f|d}( , x, y, z) Scale : gl. Scale{f|d}(x, y, z) Object Space World Space
Step 2: Viewing Transform § A model can be viewed from different angles. § Viewing Transform specify following information about the viewer: eye position head up Look at direction Eye Space
Step 2: Viewing Transform void glu. Look. At(GLdouble eyex, GLdouble eyey, GLdouble eyez, GLdouble centerx, GLdouble centery, GLdouble centerz, GLdouble upx, GLdouble upy, GLdouble upz);
Step 3: Normalize & Clip § Normalize View volume within an unit cube. § Remove Primitives that are not in Normalized view volume Eye Space Clipping Space • After clipping lighting effects and textures are applied to the primitives
Step 4: Projection § Maps 3 D-coordinates to 2 D-image coordinates Image Space Perspective Projection Types Parallel Projection Clipping Space
Step 4: Projection Perspective Projection
Step 4: Projection Perspective Projection
Step 4: Projection Parallel Projection
Step 5: Rasterization § Projected image (vertices) in image space has fractional x and y co-ordinates values § But raster scan device can only display pixels at integer co-ordinates • Some Algorithms: Image Space Rasterization – DDA (Digital Differential Analyzer – Brasenham’s Algorithm – Midpoint Algorithm Screen Space
Step 6: Viewport Transformation § Maps rasterized 2 D images onto graphical device Screen Space Device Space
Step 6: Viewport Transformation § void gl. Viewport(GLint x, GLint y, GLsizei width, GLsizei height); glu. Perspective(fovy, 1. 0, near, far); gl. Viewport(0, 0, 400); glu. Perspective(fovy, 1. 0, near, far); gl. Viewport(0, 0, 400, 200);
Graphics Pipeline Graphics Primitives Modeling Transform transform matrix Object Space World Space Viewing Transform Eye, lookat, headup Eye Clipping Image Space Screen Space Normalize & Clip Parallel or material, Perspectiv lights, color e volume Shading & Texture projection matrix Device Space Projection viewport location Image on Screen Viewport Transform Image in Internal Buffer Scan conversio Rasteri-n zation
Primitives § Primitives: Points, Lines & Polygons § Each object is specified by a set Vertices • Grouped together by gl. Begin & gl. End gl. Begin(type) gl. Vertex*( ) … gl. End( ); • type can have 10 possible values
Primitive Types V 0 V 4 V 3 V 0 V 4 V 0 V 3 V 2 V 1 GL_LINE V 0 V 2 V 1 V 4 V 3 V 1 GL_POINTS V 0 V 3 V 1 GL_LINE_STRIP V 5 V 5 V 4 V 3 V 2 GL_LINE_LOOP Polygon must be: • Simple • No-holes inside • Convex V 2 V 1 GL_POLYGON Simple Non-convex P 1 P 2 Convex Complex
Primitive Types V 8 V 1 V 7 V 6 V 2 V 1 V 3 GL_TRIANGLE V 5 V 3 V 5 V 1 V 4 V 0 V 5 GL_TRIANGLE_STRIP GL_TRIANGLE_FAN Order of Vertex rendering V 0 V 3 V 1 V 0 V 2 V 4 V 6 V 7 V 6 V 2 V 3 V 4 V 2 V 0 V 4 GL_QUAD V 1 V 5 V 3 V 5 V 7 GL_QUAD_STRIP GL_TRIANGLE_STRIP 012, 213, 234, 435 GL_QUAD_STRIP 0132, 2354, 4576 GL_TRIANGLE_FAN 012, 023 , 034, 045
Configuring Open. GL in Visual C++ § Files Required for GLUT: glut 32. dll glut. h glut 32. lib
Sample Program
Specify Canvas Color Must always remember to clear canvas before drawing § gl. Clear. Color( r , g , b , α ) specify the color to clear the canvas to should generally set α to be 0 (i. e. , fully transparent) this is a state variable, and can be done only once § gl. Clear( GL_ COLOR_ BUFFER_ BIT) actually clears the screen gl. Clear clears such as the depth buffer GL_ DEPTH_ BUFFER_ BIT but we’re not using it right now
Redrawing Window § void gl. Flush(void); Forces previously issued Open. GL commands to begin execution It returns before the execution ends. glut. Swap. Buffers() automatically calls gl. Flush() For single buffer display function should end with this command § void gl. Finish(void); Forces previously issued Open. GL commands to complete This command doesn’t return until all effects from previous commands are fully realized. § void glut. Post. Redisplay(void); Causes the currently registered display function to be called at the next available opportunity.
Initializing GLUT § Void glut. Init( int argc, char **argv) initialize glut, process command line arguments such as -geometry, -display etc. § void glut. Init. Display. Mode(unsigned int mode) Mode for later glut. Create. Window() call mode is a bit-wised Ored combination of § Either GLUT_RGBA or GLUT_INDEX § Either GLUT_SINGLE or GLUT_DOUBLE § One or more GLUT_DEPTH, GLUT_STENCIL, GLUT_ACCUM buffers § default: RGBA & SINGLE
Initializing GLUT § void glut. Init. Window. Position(int x, int y) Initial location of window § void glut. Init. Window. Size(int width, int height) Initial size of window § int glut. Create. Window(char *name) Called after Init, Displaymode, Position and Size calls Window will not appear until glut. Main. Loop is called Return value is a unique identifier for the window
Event driven approach § void glut. Main. Loop(void); enters the GLUT event processing loop. should be called at most once in a GLUT program. Once called, this routine will never return. It will call as necessary any callbacks that have been registered. While (TRUE) { e=get. Next. Event(); switch (e) { case (MOUSE_EVENT): call registered Mouse. Func break; case (RESIZE_EVENT): call registered Reshape. Func break; … } Keyboard Callback Keyboard Mouse Display OS Event Queue Main. Loop Each application has its Own event queue Mouse Callback Display Callback
Callback Functions § void glut. Display. Func(void (*func) (void)) Specifies the function that’s called whenever § the window is initially opened § The content of the window is needed to be redrawn § glut. Post. Redisplay() is explicitly called. § void glut. Reshape. Func( void (*func)(int width, int height)); Specifies the function that’s called whenever § The window is resized or moved The function should perform following tasks § Call gl. View. Port(0, 0, width, height); // default behavior § Redefine projection matrix to match aspect ratio of image & view port
Callback Functions § void glut. Keyboard. Func( void (* func)(unsigned int key, int x, int y) ); Specifies the function that’s called whenever § a key that generates an ASCII character is pressed. § The key callback parameter is the generated ASCII value. § The x and y callback parameters indicate the location of the mouse when the key was pressed.
Callback Functions § void glut. Mouse. Func( void (* func)(int button, int state, int x, int y)); Specifies the function that’s called whenever a mouse button is pressed or released. button callback parameter is one of § GLUT_LEFT_BUTTON § GLUT_MIDDLE_BUTTON § GLUT_RIGHT_BUTTON state callback parameter is either § GLUT_UP § GLUT_DOWN The x and y callback parameters indicate the location of the mouse when the event occurred.
Animation( Motion = Redraw+Swap )
Animation( Motion = Redraw+Swap )
Animation( Motion = Redraw+Swap )
Transformation in Open. GL § Open. GL uses 3 stacks to maintain transformation matrices: Model & View transformation matrix stack Projection matrix stack Texture matrix stack § You can load, push and pop the stack § The top most matrix from each stack is applied to all graphics primitive until it is changed Graphics Primitives (P) M Model-View Matrix Stack N Projection Matrix Stack Output N • M • P
Translation – 2 D x’ = x + dx y’ = y + dy
Transformations and Open. GL® § Each time an Open. GL transformation M is called the current MODELVIEW matrix C is altered: gl. Translatef(1. 5, 0. 0); gl. Rotatef(45. 0, 0. 0, 1. 0); Note: v is any vertex placed in rendering pipeline v’ is the transformed vertex from v.
Matrix Operation
Thinking About Transformations ¬ There is a World Coordinate System where: Ø All objects are defined Ø Transformations are in World Coordinate space Two Different Views As a Global System § Objects moves but coordinates stay the same § Think of transformation in reverse order as they appear in code As a Local System § Objects moves and coordinates move with it § Think of transformation in same order as they appear in code
Order of Transformation T • R Global View v Rotate Object v Then Translate gl. Load. Identity(); gl. Multi. Matrixf( T); Local View gl. Multi. Matrixf( R); v Translate Object draw_ the_ object( v Then Rotate v); v’ = ITRv Effect is same, but perception is different
Order of Transformation R • T Global View v Translate Object v Then Rotate gl. Load. Identity(); gl. Multi. Matrixf( R); Local View gl. Multi. Matrixf( T); v Rotate Object draw_ the_ object( v Then Translate v); v’ = ITRv Effect is same, but perception is different
Thank You
- Slides: 44