28
What is OpenGL What is OpenGL The OpenGL graphics system is a software interface to graphics hardware. (The GL stands for Graphics Library.) It allows you to create interactive programs that produce color images of moving three-dimensional objects. With OpenGL, you can control computer-graphics technology to produce realistic pictures or ones that depart from reality in imaginative ways. OpenGL is a software interface to graphics hardware. This interface consists of about 150 distinct commands that you use to specify the objects and operations needed to produce interactive three-dimensional applications.

What is OpenGL The OpenGL graphics system is a software interface to graphics hardware. (The GL stands for Graphics Library.) It allows you to create interactive

  • View
    234

  • Download
    0

Embed Size (px)

Citation preview

Page 1: What is OpenGL The OpenGL graphics system is a software interface to graphics hardware. (The GL stands for Graphics Library.) It allows you to create interactive

What is OpenGLWhat is OpenGL

• The OpenGL graphics system is a software interface to graphics hardware. (The GL stands for Graphics Library.) It allows you to create interactive programs that produce color images of moving three-dimensional objects. With OpenGL, you can control computer-graphics technology to produce realistic pictures or ones that depart from reality in imaginative ways.

• OpenGL is a software interface to graphics hardware. This interface consists of about 150 distinct commands that you use to specify the objects and operations needed to produce interactive three-dimensional applications.

Page 2: What is OpenGL The OpenGL graphics system is a software interface to graphics hardware. (The GL stands for Graphics Library.) It allows you to create interactive

Basic OpenGL commandsBasic OpenGL commands

• glClearColor(0.0, 0.0, 0.0, 0.0); & glClear(GL_COLOR_BUFFER_BIT);

• glColor3f(1.0, 0.0, 0.0);• void glRect{sifd}(TYPEx1, TYPEy1, TYPEx2, TYPEy

2); & void glRect{sifd}v(TYPE*v1, TYPE*v2);

• glVertex2s(2, 3); glVertex4f(2.3, 1.0, -2.2, 2.0);• glVertex3d(0.0, 0.0, 3.1415926535898);• GLdouble dvect[3] = {5.0, 9.0, 1992.0}; glVertex3dv(dve

ct);

Page 3: What is OpenGL The OpenGL graphics system is a software interface to graphics hardware. (The GL stands for Graphics Library.) It allows you to create interactive

What Is OpenGL?What Is OpenGL?• A software interface (API) to the display card.A software interface (API) to the display card.• Client-Server abstractionClient-Server abstraction

– The client is your program, it sends commands to the server.The client is your program, it sends commands to the server.– The server is the graphics card – get the commands, The server is the graphics card – get the commands,

interpret them and produces pixels on screen.interpret them and produces pixels on screen.• Pipeline oriented abstraction of the rendering Pipeline oriented abstraction of the rendering

process.process.• Extremely portable – supported by most OS and Extremely portable – supported by most OS and

display cards.display cards.• Uses algorithms developed by Silicon Graphics, Inc Uses algorithms developed by Silicon Graphics, Inc

(SGI).(SGI).– T&LT&L– Texture mappingTexture mapping– Alpha blendingAlpha blending

Page 4: What is OpenGL The OpenGL graphics system is a software interface to graphics hardware. (The GL stands for Graphics Library.) It allows you to create interactive

Open GL – history and feature listOpen GL – history and feature list• Version 1.0 – 1992Version 1.0 – 1992

– Basic geometry primitives, flat and Gouroud shading, ParametrBasic geometry primitives, flat and Gouroud shading, Parametric textures, alpha and stencil buffers and moreic textures, alpha and stencil buffers and more

• Version 1.1 -1996Version 1.1 -1996– Vertex Arrays, Texture enhancementsVertex Arrays, Texture enhancements

• Version 1.2 – 1998Version 1.2 – 1998– Extension mechanism, 3D textures, RGBA pixel formatExtension mechanism, 3D textures, RGBA pixel format

• Version 1.3 – 2001Version 1.3 – 2001– Compressed textures, Cube-maps, multitextures, multisamplesCompressed textures, Cube-maps, multitextures, multisamples

• Version 1.4 – 2002Version 1.4 – 2002– Automatic MipMap, Fog coordinates,Automatic MipMap, Fog coordinates,

• Current version 1.5 – 2003Current version 1.5 – 2003– Vertex Buffers, Occlusion queries, Full set of Boolean operatorsVertex Buffers, Occlusion queries, Full set of Boolean operators

• Version 2.0 – final spec announced in SIGGRAPH 2004Version 2.0 – final spec announced in SIGGRAPH 2004– Card Programmability is the most notable additionCard Programmability is the most notable addition

Page 5: What is OpenGL The OpenGL graphics system is a software interface to graphics hardware. (The GL stands for Graphics Library.) It allows you to create interactive

Basic OpenGL commandsBasic OpenGL commands

• How to draw the geometric objects?• glBegin(GL_POLYGON);

glVertex2f(0.0, 0.0);glVertex2f(0.0, 3.0);glVertex2f(4.0, 3.0);glVertex2f(6.0, 1.5);glVertex2f(4.0, 0.0);

glEnd();

Page 6: What is OpenGL The OpenGL graphics system is a software interface to graphics hardware. (The GL stands for Graphics Library.) It allows you to create interactive

GL PrimitivesGL Primitives• OpenGL support ten primitivesOpenGL support ten primitives

– GL_POINTS Individual pointsGL_POINTS Individual points– GL_LINES Vertices pairs, individual line segmentsGL_LINES Vertices pairs, individual line segments– GL_LINE_STRIPGL_LINE_STRIP– GL_LINE_LOOPGL_LINE_LOOP– GL_TRIANGLES Vertices triplets interpreted as trianglesGL_TRIANGLES Vertices triplets interpreted as triangles– GL_TRIANGLE_STRIPGL_TRIANGLE_STRIP– GL_TRIANGLE_FANGL_TRIANGLE_FAN– GL_QUADS Vertices quadruples, 4 sided polygonsGL_QUADS Vertices quadruples, 4 sided polygons– GL_QUAD_STRIPGL_QUAD_STRIP– GL_POLYGON Boundary of a simple, convex polygonGL_POLYGON Boundary of a simple, convex polygon

Page 7: What is OpenGL The OpenGL graphics system is a software interface to graphics hardware. (The GL stands for Graphics Library.) It allows you to create interactive

GL Primitives (cont.)GL Primitives (cont.)

Page 8: What is OpenGL The OpenGL graphics system is a software interface to graphics hardware. (The GL stands for Graphics Library.) It allows you to create interactive

• A Primitive is started byA Primitive is started by– glBegin(glBegin(GLenum GLenum mode);mode);

• And ended usingAnd ended using– glEnd();glEnd();

• Between them you can specify the primitive coordinates, cBetween them you can specify the primitive coordinates, color, normal etc.olor, normal etc.

• For example:For example:glBegin(GL_LINES);glBegin(GL_LINES);

glVertex3f(0.0, 0.0, 0.0);glVertex3f(0.0, 0.0, 0.0);glVertex3f(100.0,100.0, 0.0);glVertex3f(100.0,100.0, 0.0);glVertex3f(100.0, 0.0, 0.0);glVertex3f(100.0, 0.0, 0.0);glVertex3f(0.0, 100.0, 0.0);glVertex3f(0.0, 100.0, 0.0);

glEnd();glEnd();

Page 9: What is OpenGL The OpenGL graphics system is a software interface to graphics hardware. (The GL stands for Graphics Library.) It allows you to create interactive

Geometric PrimitivesGeometric Primitives• The following geometric primitives In 3D may be usedThe following geometric primitives In 3D may be used

– PointsPoints– LinesLines– PolygonsPolygons– Triangle strips/fansTriangle strips/fans– quadsquads

• Also supportsAlso supports– LightingLighting– ShadingShading– Texture mappingTexture mapping– AnimationAnimation– Other special effectsOther special effects

• Does not include any functions forDoes not include any functions for– Windows managementWindows management– User interactionUser interaction– File I/OFile I/O

Page 10: What is OpenGL The OpenGL graphics system is a software interface to graphics hardware. (The GL stands for Graphics Library.) It allows you to create interactive

Basic OpenGL commandsBasic OpenGL commands

Page 11: What is OpenGL The OpenGL graphics system is a software interface to graphics hardware. (The GL stands for Graphics Library.) It allows you to create interactive

Basic OpenGL commandsBasic OpenGL commands

• How to draw a circle?• GLint circle_points = 100;

glBegin(GL_LINE_LOOP);for (i = 0; i < circle_points; i++) {

angle = 2*PI*i/circle_points;glVertex2f(cos(angle), sin(angle));

}glEnd();

Page 12: What is OpenGL The OpenGL graphics system is a software interface to graphics hardware. (The GL stands for Graphics Library.) It allows you to create interactive

• Specifying Vertices:Specifying Vertices:– void void glVertexglVertex{234}{sifd}[v](TYPE coords);{234}{sifd}[v](TYPE coords); (for {x, y, z, w}) and short, int, float or double(for {x, y, z, w}) and short, int, float or double– Example:Example:

glVertex2s(2, 3);glVertex2s(2, 3);glVertex3d(0.0, 0.0, 3.1415927);glVertex3d(0.0, 0.0, 3.1415927);glVertex4f(2.3, 1.0, -2.2, 2.0);glVertex4f(2.3, 1.0, -2.2, 2.0);GLdouble dvect[3] = {5.0, 9.0, 1992.0};GLdouble dvect[3] = {5.0, 9.0, 1992.0};glVertex3dv(dvect);glVertex3dv(dvect);

• NormalsNormals– Void glNormal3{bdfis}[v]{params}Void glNormal3{bdfis}[v]{params}

• ColorsColors– Void glColor{34}[u]{bdfis}[v]Void glColor{34}[u]{bdfis}[v]

Page 13: What is OpenGL The OpenGL graphics system is a software interface to graphics hardware. (The GL stands for Graphics Library.) It allows you to create interactive

OpenGL OptionsOpenGL Options• The The glEnable() glEnable() and and glDisable() glDisable() functions enable ofunctions enable o

r disable OpenGL capabilities.r disable OpenGL capabilities.– GL_DEPTH_TEST for z-buffer, hidden surface eliminatiGL_DEPTH_TEST for z-buffer, hidden surface eliminati

onon– GL_CULL_FACE for enabling back face cullingGL_CULL_FACE for enabling back face culling– GL_LIGHTING for lightning enablingGL_LIGHTING for lightning enabling– GL_LIGHTGL_LIGHTii– GL_DITHER for color ditheringGL_DITHER for color dithering– GL_BLENDGL_BLEND– GL_COLOR_MATERIALGL_COLOR_MATERIALEtc…Etc…

Page 14: What is OpenGL The OpenGL graphics system is a software interface to graphics hardware. (The GL stands for Graphics Library.) It allows you to create interactive

Basic OpenGL commandsBasic OpenGL commands• void glEnable(GLenum cap);• void glDisable(GLenum cap);• void glGetBooleanv(GLenum pname, GLboolean *para

ms);• void glGetIntegerv(GLenum pname, GLint *params);• void glGetFloatv(GLenum pname, GLfloat *params);• void glGetDoublev(GLenum pname, GLdouble *param

s);• void glPointSize(GLfloat size);• void glLineWidth(GLfloat width);• void glNormal3{bsidf}(TYPEnx, TYPEny, TYPEnz);

Page 15: What is OpenGL The OpenGL graphics system is a software interface to graphics hardware. (The GL stands for Graphics Library.) It allows you to create interactive

Double BufferingDouble Buffering

• Most multimedia applications do not wish to haMost multimedia applications do not wish to have the flickering effect resulting from window uve the flickering effect resulting from window updates.pdates.– This is solved by having two buffers for a device contThis is solved by having two buffers for a device cont

ext.ext.– We always update the back buffer.We always update the back buffer.– Once it’s ready, we switch the two buffers, starting tOnce it’s ready, we switch the two buffers, starting t

he update again.he update again.• Windows has support for double buffering using Windows has support for double buffering using

the GDI functionthe GDI function– BOOL SwapBuffers( HDC hdc);BOOL SwapBuffers( HDC hdc);

Page 16: What is OpenGL The OpenGL graphics system is a software interface to graphics hardware. (The GL stands for Graphics Library.) It allows you to create interactive

Stages of Vertex TransformationsStages of Vertex Transformations

Page 17: What is OpenGL The OpenGL graphics system is a software interface to graphics hardware. (The GL stands for Graphics Library.) It allows you to create interactive

The Modelview MatrixThe Modelview Matrix

• A 4x4 matrix M representing the transformA 4x4 matrix M representing the transformed coordinate system.ed coordinate system.

• Setting the matrix mode to modelviewSetting the matrix mode to modelview– glMatrixMode(GL_MODELVIEW);glMatrixMode(GL_MODELVIEW);

• Each vertex V you provide is a single vectoEach vertex V you provide is a single vector matrix.r matrix.

• Before the rendering process, Each V is mBefore the rendering process, Each V is multiplied by the Modelview matrix.ultiplied by the Modelview matrix.– V’=MV’=M

Page 18: What is OpenGL The OpenGL graphics system is a software interface to graphics hardware. (The GL stands for Graphics Library.) It allows you to create interactive

The Projection MatrixThe Projection Matrix• A 4x4 matrix M representing the projection matrix.A 4x4 matrix M representing the projection matrix.• Setting the matrix mode to projectionSetting the matrix mode to projection

– glMatrixMode(GL_PROJECTION);glMatrixMode(GL_PROJECTION);– Defines the clipping volume – object out of the clippinDefines the clipping volume – object out of the clippin

g volume will be culled.g volume will be culled.• Initializing functionsInitializing functions

– void glOrtho( GLdouble left, GLdouble right, GLdouble void glOrtho( GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far );bottom, GLdouble top, GLdouble near, GLdouble far );

– void glFrustum( GLdouble left, GLdouble right, GLdouvoid glFrustum( GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble znear, GLdouble ble bottom, GLdouble top, GLdouble znear, GLdouble zfar );zfar );

– void gluPerspective( GLdouble fovy, GLdouble aspect, void gluPerspective( GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar );GLdouble zNear, GLdouble zFar );

Page 19: What is OpenGL The OpenGL graphics system is a software interface to graphics hardware. (The GL stands for Graphics Library.) It allows you to create interactive

TransformationsTransformations• RotationRotation

– glRotated(GLdouble angle, GLdouble x, GLdouble y, GLdouble z)glRotated(GLdouble angle, GLdouble x, GLdouble y, GLdouble z)• ScalingScaling

– glScaled( GLdouble glScaled( GLdouble xx, GLdouble , GLdouble yy, GLdouble , GLdouble z z ););• TranslationTranslation

– void glTranslated( GLdouble x, GLdouble y, GLdouble z );void glTranslated( GLdouble x, GLdouble y, GLdouble z );• The active GL matrix M is updated with the transformatioThe active GL matrix M is updated with the transformatio

n T as follows:n T as follows:– M = M x TM = M x T

• Each transformation effects only the vertices defined Each transformation effects only the vertices defined afteafter r the transformation call.the transformation call.

• In general, you make the active matrix the Modelview maIn general, you make the active matrix the Modelview matrix.trix.

Page 20: What is OpenGL The OpenGL graphics system is a software interface to graphics hardware. (The GL stands for Graphics Library.) It allows you to create interactive

Other Matrix OperationsOther Matrix Operations

• Selecting the matrix mode is done usingSelecting the matrix mode is done using– void glMatrixMode( GLenum mode );void glMatrixMode( GLenum mode );– mode can be one ofmode can be one of

•GL_MODELVIEWGL_MODELVIEW•GL_PROJECTIONGL_PROJECTION•GL_TEXTUREGL_TEXTURE

Page 21: What is OpenGL The OpenGL graphics system is a software interface to graphics hardware. (The GL stands for Graphics Library.) It allows you to create interactive

Matrix StackMatrix Stack

• The active matrix is save on a stack .The active matrix is save on a stack .– The top of the stack is the activematrixThe top of the stack is the activematrix– glPushMatrix();glPushMatrix();– glPopMatrix();glPopMatrix();

drawCar() {drawCar() {draw_car_body();draw_car_body();glPushMatrix();glPushMatrix();

glTranslatef(40,0,30);glTranslatef(40,0,30);draw_wheel();draw_wheel();

glPopMatrix();glPopMatrix();}}

Page 22: What is OpenGL The OpenGL graphics system is a software interface to graphics hardware. (The GL stands for Graphics Library.) It allows you to create interactive

• To initialize an identity matrix useTo initialize an identity matrix use– LoadIdentity()LoadIdentity()

• To load a given matrix useTo load a given matrix use– void glLoadMatrixd( const GLdouble *m );void glLoadMatrixd( const GLdouble *m );– m is a pointer to a 4x4 matrix stored in column-major om is a pointer to a 4x4 matrix stored in column-major o

rder as 16 consecutive valuesrder as 16 consecutive values• In order to directly multiply the active matrix with In order to directly multiply the active matrix with

a given matrix, usea given matrix, use– void glMultMatrixd( const GLdouble *m );void glMultMatrixd( const GLdouble *m );– m is a pointer to a 4x4 matrix stored in column-major om is a pointer to a 4x4 matrix stored in column-major o

rder as 16 consecutive valuesrder as 16 consecutive values

Page 23: What is OpenGL The OpenGL graphics system is a software interface to graphics hardware. (The GL stands for Graphics Library.) It allows you to create interactive

NormalsNormals

• Specifying the normalSpecifying the normal– void glNormal3d( GLdouble nx, GLdouble ny,void glNormal3d( GLdouble nx, GLdouble ny,

GLdouble nz ); GLdouble nz );• When using lighting effects, normals muWhen using lighting effects, normals mu

st be defined for each polygon or for eacst be defined for each polygon or for each vertex. OpenGL does not calculate a nh vertex. OpenGL does not calculate a normal by itself.ormal by itself.

Page 24: What is OpenGL The OpenGL graphics system is a software interface to graphics hardware. (The GL stands for Graphics Library.) It allows you to create interactive

Shading ModelsShading Models

• Choose the shading modelChoose the shading model– void glShadeModel( GLenum mode );void glShadeModel( GLenum mode );– mode may bemode may be

•GL_FLATGL_FLAT•GL_SMOOTH (the same as the Gouraud shading GL_SMOOTH (the same as the Gouraud shading

model)model)

• OpenGL does not support the Phong shaOpenGL does not support the Phong shading model.ding model.

Page 25: What is OpenGL The OpenGL graphics system is a software interface to graphics hardware. (The GL stands for Graphics Library.) It allows you to create interactive

Material PropertiesMaterial Properties

• An object has a certain reflective propertiesAn object has a certain reflective properties• When using lighting, each model part has its owWhen using lighting, each model part has its ow

n material properties, which also define its colon material properties, which also define its color.r.– glMaterialfv(GLenum face, GLenum pname, const GLglMaterialfv(GLenum face, GLenum pname, const GL

float *params )float *params )– Face Face must be one of must be one of GL_FRONT, GL_BACK, or GL_FRGL_FRONT, GL_BACK, or GL_FR

ONT_AND_BACK.ONT_AND_BACK.– pname pname can be can be GL_AMBIENT, GL_DIFFUSE, GL_SPECUGL_AMBIENT, GL_DIFFUSE, GL_SPECU

LAR, GL_SHININESS etc.LAR, GL_SHININESS etc.– params params change accordingly.change accordingly.

Page 26: What is OpenGL The OpenGL graphics system is a software interface to graphics hardware. (The GL stands for Graphics Library.) It allows you to create interactive

ColorsColors• In case you want to specify colors per face or per In case you want to specify colors per face or per

vertex, you mustvertex, you must– Enable color tracking.Enable color tracking.

• glEnable(GL_COLOR_MATRERIAL);glEnable(GL_COLOR_MATRERIAL);– Set material properties to follow glColor values.Set material properties to follow glColor values.

• glColorMaterial(GLenum face, GLenum mode);glColorMaterial(GLenum face, GLenum mode);– Specify the color before defining it’s corresponding vSpecify the color before defining it’s corresponding v

ertex.ertex.• glColor3d(GLdouble red, GLdouble green, GLdouble blue);glColor3d(GLdouble red, GLdouble green, GLdouble blue);• glColor3ub(GLubyte red, GLubyte green, GLubyte blue);glColor3ub(GLubyte red, GLubyte green, GLubyte blue);

Page 27: What is OpenGL The OpenGL graphics system is a software interface to graphics hardware. (The GL stands for Graphics Library.) It allows you to create interactive

Adding Light to a SceneAdding Light to a Scene• Enabling lightingEnabling lighting

– glEnable(GL_LIGHT);glEnable(GL_LIGHT);• OpenGL supports up to OpenGL supports up to GL_MAX_LIGHTS GL_MAX_LIGHTS lights simultalights simulta

neouslyneously– Query with glGetIntegerv(Query with glGetIntegerv(GL_MAX_LIGHTSGL_MAX_LIGHTS))

• Set the light propertiesSet the light properties– void glLightfv( GLenum light, GLenum pname, const GLfloat *pavoid glLightfv( GLenum light, GLenum pname, const GLfloat *pa

rams );rams );– pname pname can be can be GL_AMBIENT, GL_DIFFUSE, GL_SPECULAR, GL_PGL_AMBIENT, GL_DIFFUSE, GL_SPECULAR, GL_P

OSITION OSITION etc.etc.– params change accordingly.params change accordingly.

• Enable the specific lightEnable the specific light– glEnable(GL_LIGHTi);glEnable(GL_LIGHTi);– i i is between 0 and is between 0 and GL_MAX_LIGHTS-1GL_MAX_LIGHTS-1

Page 28: What is OpenGL The OpenGL graphics system is a software interface to graphics hardware. (The GL stands for Graphics Library.) It allows you to create interactive

Basic OpenGL commandsBasic OpenGL commands

• Where to get the information?• http://www.sgi.com/Technology/openGL• http://fly.cc.fer.hr/~unreal/theredbook/• http://www.opengl.org/resources/code/basics/redbo

ok/• www.gamedev.net/download/redbook.pdf • http://www.geocities.com/SiliconValley/Vista/8177/tuhttp://www.geocities.com/SiliconValley/Vista/8177/tu

torial/nehe.htmtorial/nehe.htm