Download presentation
Presentation is loading. Please wait.
Published byMargaretMargaret Bond Modified over 9 years ago
1
Using OpenGL
2
2 What is OpenGL? A software interface to graphics hardware It is a Graphics Rendering API (Application Programmer’s Interface) that is a set of function with well defined interface. OpenGL is intended for use with computer hardware that is designed and optimized for the display and manipulation of 3D graphics. Software-only implementations of OpenGL are also possible (MESA) 2
3
A History of OpenGL Was SGI’s Iris GL – basis for “Open”GL “Open” standard allowing for wide range hardware platforms OpenGL v1.0 (1992) OpenGL v1.1 (1995) OpenGL v1.5 “Mesa” – an Open source implementation of OpenGL (http://www.mesa3d.org)
4
4 The OpenGL ARB OpenGL Architecture Review Board (ARB) –Control license and conformance test of OpenGL The Khronos Group(after 2006) is industry consortium focused on the creation and maintenance of open media standards. Most ARB members are already members of Khronos. A vendor who wants to create and market an OpenGL implementation must first license OpenGL from The Khronos Group.
5
5 Useful Websites and Books Official Site http://www.opengl.org Non official sites –http://nehe.gamedev.net/ BOOKS –OpenGL Red Book & –OpenGL Blue Book 5
6
6 Useful Links Online Reference manual sites GL and GLU http://www.mevis.de/~uwe/opengl/opengl.html GLUT http://pyopengl.sourceforge.net/documentation/manual/reference- GLUT.html 6
7
7 OpenGL API Functions OpenGL contains over 200 functions –Primitive functions : define the elements (eg. A point, line, polygon, etc) –Attribute functions : control the appearance of primitives (eg. colors, line types, light source, textures.) –Viewing functions : determine the properties of camera. Transformation –Windowing functions: not part of core OpenGL –Other functions 7
8
8 Window Management OpenGL is meant to be platform independent. i.e. OpenGL is window and operating system independent. OpenGL does not include any functions for window management, user interaction, and file I/O. Host environment is responsible for window management. 8
9
9 Related APIs GL –“core” library of OpenGL that is platform independent GLU (OpenGL Utility Library) –part of OpenGL –an auxiliary library that handles a variety of graphics accessory functions –NURBS, tessellators, quadric shapes, etc. AGL, GLX, WGL –glue between OpenGL and windowing systems GLUT (OpenGL Utility Toolkit) –utility toolkits that handle window managements –portable windowing API –not officially part of OpenGL
10
OpenGL API Hierarchy GLUT, JOGL GLU GL GLX, AGL or WGL X, Win32, Mac O/S Java Virtual Machine software and/or hardware application program OpenGL Motif widget or similar
11
Programming Convention : OpenGL Function Naming OpenGL functions all follow a naming convention that tells you which library the function is from, and how many and what type of arguments that the function takes
12
Programming Convention : OpenGL Function Naming glVertex3fv( v ) Number of components 2 - (x,y) 3 - (x,y,z) 4 - (x,y,z,w) Data Type b - byte ub - unsigned byte s - short us - unsigned short i - int ui - unsigned int f - float d - double Vector omit “v” for scalar form glVertex2f( x, y )
13
13 Programming Convention : OpenGL Function Naming Multiple forms of OpenGL functions to support the variety of data types –glVertex3i(ix, iy, iz) –glVertex3f(x, y, z) –glVertex2i(ix, iy) –glVertex2f(x, y) –.. –We shall use the notation glVertex*() to refer to all the forms of the vertex function 13
14
14 OpenGL Data TypeInternal Representation Literal Suffix GLbyte8-bit integerb GLshort16-bit integers GLint, GLsizei32-bit integeri GLfloat, GLclampf32-bit floating pointf GLdouble, GLclampd64-bit floating pointd GLubyte, GLboolean8-bit unsigned integerub GLushort16bit unsigned integerus GLuint, GLenum, GLbitfield 32bit unsigned integerui
15
15 General Structure of an OpenGL Program Configure and open a window Initialize OpenGL’s state Process user events Draw an image
16
16 2D Geometric Primitives Primitives – fundamental entities such as point and polygons Basic types of geometric primitives –Points –Line segments –Polygons 16
17
17 2D Geometric Primitives 17 GL_POINTS GL_LINESGL_LINE_STRIPGL_LINE_LOOP GL_POLYGON GL_QUADS GL_TRIANGLES GL_TRIANGLE_FAN All geometric primitives are specified by vertices
18
18 Geometry Commands glBegin(GLenum type) marks the beginning of a vertex-data list that describes a geometric primitives 18 glEnd (void) marks the end of a vertex-data list glVertex*(…) specifies vertex for describing a geometric object
19
19 Specifying Geometric Primitives glBegin( type ); glVertex*(…); …… glVertex*(…); glEnd(); 19 type determines how vertices are combined
20
20 Types
21
21 Types GL_POINTS GL_LINES : each successive pair for a ling segment GL_LINE_STRIP: vertices defining a sequence of line segments GL_LINE_LOOP: GL_LINE_STRIP + the last vertex connects to the first GL_POLYGON : sequence of vertices of polygon, filled GL_QUADS: each successive group of four vertices for a quadrilaterals GL_TRIANGLES: each successive group of three vertices for a triangle GL_TRIANGLE_FAN: first three vertices for the first triangle and each subsequent vertex with the first vertex and the previous vertex for the next triangle 21
22
22 Example 22 void drawSquare () { glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_POLYGON); glVertex2f ( 0.0, 0.0 ); glVertex2f ( 1.0, 0.0 ); glVertex2f ( 1.1, 1.1 ); glVertex2f ( 0.0, 1.0 ); glEnd(); glFlush();// force the renderer to output the results }
23
23 Winding glFrontFace(GL_CCW);
24
24 Triangle Strips When drawing several connected triangles, you can save a lot of time by drawing a strip of connected triangles The pattern is V0, V1, V2; then V2, V1, V3; then V2, V3, V4; and so on.
25
25 Triangle Fans Produce a group of connected triangles that fan around a central point The first vertex, V0, forms the origin of the fan. After the first three vertices are used to draw the initial triangle, all subsequent vertices are used with the origin (V0) and the vertex immediately preceding it (Vn–1) to form the next triangle.
26
26 How OpenGL Works: The Conceptual Model Configure how OpenGL should draw stuff Draw stuff
27
27 Controlling OpenGL’s Drawing Set OpenGL’s rendering state –State controls how things are drawn shading – lighting texture maps – line styles (stipples) polygon patterns – transparency
28
28 The Power of Setting OpenGL State Appearance is controlled by setting OpenGL’s state.
29
29 Setting OpenGL State Three ways to set OpenGL state: 1.Set values to be used for processing vertices most common methods of setting state –glColor() / glIndex() –glNormal() –glTexCoord() state must be set before calling glVertex()
30
30 Setting OpenGL State (cont’d.) 2.Turning on a rendering mode glEnable() / glDisable() 3.Configuring the specifics of a particular rendering mode Each mode has unique commands for setting its values glMaterialfv()
31
31 OpenGL Color There are two color models in OpenGL –RGB Color (True Color) –Indexed Color (Color map) Colors are specified as floating-point numbers in the range [ 0.0, 1.0 ] 31
32
32 RGB Color Model 32 R, G, B components are stored for each pixel
33
33 How Many Colors? Color number = 33 For example: 4-bit color = 16 colors 8-bit color = 256 colors 24-bit color = 16.77 million colors
34
34 How Much Memory? Buffer size = width * height *color depth 34 For example: If width = 640, height = 480, color depth = 24 bits Buffer size = (640 * 480 * 2) bytes If width = 640, height = 480, color depth = 32 bits Buffer size = (640 * 480 * 4) bytes
35
35 Alpha Component Alpha value A value indicating the pixels opacity 0 usually represents totally transparent and the 1 represents completely opaque Alpha buffer Hold the alpha value for every pixel Alpha values are commonly represented in 8 bits, in which case transparent to opaque ranges from 0 to 255 35
36
36 RGB Color Commands glColor*( … ) specifies vertex colors 36 glClearColor(r, g, b, a) sets current color for cleaning color buffer
37
37 Example 37 void drawLine (GLfloat *color) { glColor3fv ( color ); glBegin(GL_LINE); glVertex2f ( 0.0, 0.0 ); glVertex2f ( 1.0, 0.0 ); glEnd(); }
38
38 Example 38 void drawLine (GLfloat *color) { glBegin(GL_LINE); glColor3f(1.0,0.0,0.0 ); glVertex2f ( 0.0, 0.0 ); glColor3f(0.0,0.0,1.0); glVertex2f ( 1.0, 0.0 ); glEnd(); }
39
39 Color Interpolation glShadeModel(GL_SMOOTH); Or glShadeModel(GL_FLAT); - the last vertex color Linear interpolation for a line Bilinear interpolation for a polygons 39
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.