7.module ii opengl

28
Introduction to OpenGL Introduction to OpenGL What is OpenGL? What is OpenGL? Computer-graphics API (application Computer-graphics API (application programming interface) programming interface) Efficient, streaming interface Efficient, streaming interface Hardware independent Hardware independent Operating system independent Operating system independent

Upload: pradiptart

Post on 23-Nov-2014

121 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: 7.Module II OpenGL

Introduction to OpenGLIntroduction to OpenGL

►What is OpenGL?What is OpenGL? Computer-graphics API (application Computer-graphics API (application

programming interface)programming interface)►Efficient, streaming interfaceEfficient, streaming interface►Hardware independentHardware independent►Operating system independentOperating system independent

Page 2: 7.Module II OpenGL

Uses of OpenGLUses of OpenGL► CADCAD► LightingLighting► ShadingShading► Texture mappingTexture mapping► TransparencyTransparency► AnimationAnimation► Wire frame modelWire frame model► AntialiasingAntialiasing► ShadowingShadowing► Can work access a networkCan work access a network► Content creationContent creation► EntertainmentEntertainment► Game developmentGame development► ManufacturingManufacturing► MedicalMedical

Page 3: 7.Module II OpenGL

How OpenGL works?How OpenGL works?

► OpenGL is a procedural rather than a OpenGL is a procedural rather than a descriptive graphics APIdescriptive graphics API

► Include more than 200 (250) commands and Include more than 200 (250) commands and functions by using which Open GL primitives( or functions by using which Open GL primitives( or graphic primitives) like points, lines, triangles graphic primitives) like points, lines, triangles and polygons in 2D and 3D can be drawnand polygons in 2D and 3D can be drawn

► It keep track of many state variables, such as It keep track of many state variables, such as the current size of a point, the current color of the current size of a point, the current color of drawing, the current background etcdrawing, the current background etc

► The value of the state variable remains active The value of the state variable remains active until a new value is givenuntil a new value is given

Page 4: 7.Module II OpenGL

Software( or generic ) Software( or generic ) implementation of Open GLimplementation of Open GL

Application program

OSservices

I/Oservices

GDI OpenGL

Display/Windowing system Software Rasterizer

Page 5: 7.Module II OpenGL

Hardware implementation of Hardware implementation of Open GLOpen GL

Application program

OSservices

I/Oservices

GDI OpenGL

Display/Windowing system Hardware Driver

Page 6: 7.Module II OpenGL

PipelinePipeline

► It is used to describe a process that It is used to describe a process that can take two or more distinct stages or can take two or more distinct stages or stepssteps

►The OpenGL pipeline is the following The OpenGL pipeline is the following fig:fig:

Open Gl command

buffer

Transform and lighting

Rasterization Frame buffer

Page 7: 7.Module II OpenGL

OpenGL Command FormatsOpenGL Command Formats

glVertex3fv( v )

Number ofcomponents

2 - (x,y) 3 - (x,y,z)4 - (x,y,z,w)

Data Typeb - byteub - unsigned bytes - shortus - unsigned shorti - intui - unsigned intf - floatd - doublel -long

Vector

omit “v” forscalar form

glVertex2f( x, y )

PrefixInitial capital letters

Page 8: 7.Module II OpenGL

OpenGL Data typesOpenGL Data types( ISO C implementations of open ( ISO C implementations of open

GL) GL) ►GLbyte-8 bit integer-signed char-bGLbyte-8 bit integer-signed char-b►GLshort-16 bit integer-short-sGLshort-16 bit integer-short-s►GLint-32 bit integer-long-lGLint-32 bit integer-long-l►GLfloat-32 bit floating point-float-fGLfloat-32 bit floating point-float-f►GLdouble-64 bit floating point-double-GLdouble-64 bit floating point-double-

dd►GLubyte,GLboolean-8 bit unsigned GLubyte,GLboolean-8 bit unsigned

integer-unsigned char- ubinteger-unsigned char- ub

Page 9: 7.Module II OpenGL

Open GL functionOpen GL function

►Syntax:Syntax:<Library prefix> <Root command> <optional argument count><<Library prefix> <Root command> <optional argument count><optional optional

argument typeargument type>;>;

Examples:Examples:

glColor3f(0.0,0.0,0.0); displays black colorglColor3f(0.0,0.0,0.0); displays black color

glVertex2i(1,2); plot a point (1,2)glVertex2i(1,2); plot a point (1,2)

glBegin(GL_POINTS);glBegin(GL_POINTS);

glEnd();glEnd();

Page 10: 7.Module II OpenGL

ColorColor

► Color Color values Displayed Command usedvalues Displayed Command used► 000 Black glColor3f(0.0,0.0,0.0)000 Black glColor3f(0.0,0.0,0.0)► 001 Blue glColor3f(0.0,0.0,1.0)001 Blue glColor3f(0.0,0.0,1.0)► 010 Green glColor3f(0.0,1.0,0.0)010 Green glColor3f(0.0,1.0,0.0)► 011 Cyan glColor3f(0.0,1.0,1.0)011 Cyan glColor3f(0.0,1.0,1.0)► 100 Red glColor3f(1.0,0.0,0.0)100 Red glColor3f(1.0,0.0,0.0)► 101 Magenta glColor3f(1.0,0.0,1.0)101 Magenta glColor3f(1.0,0.0,1.0)► 110 Yellow glColor3f(1.0,1.0,0.0) 110 Yellow glColor3f(1.0,1.0,0.0) ► 111 White glColor3f(1.0,1.0,1.0)111 White glColor3f(1.0,1.0,1.0)

Page 11: 7.Module II OpenGL

OpenGL Geometric PrimitivesOpenGL Geometric Primitives

►All geometric primitives are specified All geometric primitives are specified by verticesby vertices

GL_QUAD_STRIPGL_QUAD_STRIP

GL_POLYGONGL_POLYGON

GL_TRIANGLE_STRIPGL_TRIANGLE_STRIP

GL_TRIANGLE_FANGL_TRIANGLE_FAN

GL_POINTSGL_POINTSGL_LINESGL_LINES

GL_LINE_LOOPGL_LINE_LOOPGL_LINE_STRIPGL_LINE_STRIP

GL_TRIANGLESGL_TRIANGLES

GL_QUADSGL_QUADS

Page 12: 7.Module II OpenGL

Cont.Cont.

►OpenGL primitives are described by OpenGL primitives are described by

glBegin() and glEnd() pairsglBegin() and glEnd() pairs

->gl Begin() marks the beginning of a vertex ->gl Begin() marks the beginning of a vertex data list that describes a geometric primitivedata list that describes a geometric primitive

->gl end() marks the end of a vertex data list->gl end() marks the end of a vertex data list

->The argument of the glBegin() may be the ->The argument of the glBegin() may be the following values( given in the next slide)following values( given in the next slide)

Page 13: 7.Module II OpenGL

Cont.Cont.

► Value MeaningValue Meaning► GL_POINTS Individual pointsGL_POINTS Individual points► GL_LINES Pair of vertices interpreted as GL_LINES Pair of vertices interpreted as ► individual line segments individual line segments ► GL_LINE-STRIP Series of connected lineGL_LINE-STRIP Series of connected line► segmentssegments► GL_LINE_LOOP same as above, with a segmentGL_LINE_LOOP same as above, with a segment► added between between last andadded between between last and► first vertices first vertices ► GL_TRIANGLES Triples of vertices interpreted asGL_TRIANGLES Triples of vertices interpreted as► triangle triangle

Page 14: 7.Module II OpenGL

Cont.Cont.

► Value MeaningValue Meaning► GL_TRIANGLE_STRIP Linked strip of triangleGL_TRIANGLE_STRIP Linked strip of triangle► GL_TRIANGLE_FAN Linked fan of trianglesGL_TRIANGLE_FAN Linked fan of triangles► GL_QUADS Quadruple of vertices GL_QUADS Quadruple of vertices ► interpreted as 4 sided polygonsinterpreted as 4 sided polygons► GL_QUAD_STRIP Linked strip of GL_QUAD_STRIP Linked strip of ► quadrilateralsquadrilaterals► GL_POLYGON Boundary of a simple , GL_POLYGON Boundary of a simple , ► convex polygonconvex polygon

Page 15: 7.Module II OpenGL

OpenGL Drawing FunctionsOpenGL Drawing Functions

glBegin (glBegin (GL_POINTSGL_POINTS););

glVertex2iv (p1);glVertex2iv (p1);

glVertex2iv (p2);glVertex2iv (p2);

glVertex2iv (p3);glVertex2iv (p3);

glVertex2iv (p4);glVertex2iv (p4);

glVertex2iv (p5);glVertex2iv (p5);

glVertex2iv (p6);glVertex2iv (p6);

glEnd();glEnd();

P1

P2 P3

P4

P5P6

Page 16: 7.Module II OpenGL

OpenGL Drawing FunctionsOpenGL Drawing Functions

glBegin (glBegin (GL_LINESGL_LINES););

glVertex2iv (p1);glVertex2iv (p1);

glVertex2iv (p2);glVertex2iv (p2);

glVertex2iv (p3);glVertex2iv (p3);

glVertex2iv (p4);glVertex2iv (p4);

glVertex2iv (p5);glVertex2iv (p5);

glVertex2iv (p6);glVertex2iv (p6);

glEnd();glEnd();

P1

P2 P3

P4

P5P6

Page 17: 7.Module II OpenGL

OpenGL Drawing FunctionsOpenGL Drawing Functions

glBegin (glBegin (GL_LINE_STRIPGL_LINE_STRIP););

glVertex2iv (p1);glVertex2iv (p1);

glVertex2iv (p2);glVertex2iv (p2);

glVertex2iv (p3);glVertex2iv (p3);

glVertex2iv (p4);glVertex2iv (p4);

glVertex2iv (p5);glVertex2iv (p5);

glVertex2iv (p6);glVertex2iv (p6);

glEnd();glEnd();

P1

P2 P3

P4

P5P6

Page 18: 7.Module II OpenGL

OpenGL Drawing FunctionsOpenGL Drawing Functions

glBegin (glBegin (GL_LINE_LOOPGL_LINE_LOOP););

glVertex2iv (p1);glVertex2iv (p1);

glVertex2iv (p2);glVertex2iv (p2);

glVertex2iv (p3);glVertex2iv (p3);

glVertex2iv (p4);glVertex2iv (p4);

glVertex2iv (p5);glVertex2iv (p5);

glVertex2iv (p6);glVertex2iv (p6);

glEnd();glEnd();

P1

P2 P3

P4

P5P6

Page 19: 7.Module II OpenGL

OpenGL Drawing FunctionsOpenGL Drawing Functions

glBegin (glBegin (GL_POLYGONGL_POLYGON););

glVertex2iv (p1);glVertex2iv (p1);

glVertex2iv (p2);glVertex2iv (p2);

glVertex2iv (p3);glVertex2iv (p3);

glVertex2iv (p4);glVertex2iv (p4);

glVertex2iv (p5);glVertex2iv (p5);

glVertex2iv (p6);glVertex2iv (p6);

glEnd();glEnd();

P1

P2 P3

P4

P5P6

Page 20: 7.Module II OpenGL

OpenGL Drawing FunctionsOpenGL Drawing Functions

glBegin (glBegin (GL_TRIANGLESGL_TRIANGLES););

glVertex2iv (p1);glVertex2iv (p1);

glVertex2iv (p2);glVertex2iv (p2);

glVertex2iv (p6);glVertex2iv (p6);

glVertex2iv (p3);glVertex2iv (p3);

glVertex2iv (p4);glVertex2iv (p4);

glVertex2iv (p5);glVertex2iv (p5);

glEnd();glEnd();

P1

P2 P3

P4

P5P6

Page 21: 7.Module II OpenGL

OpenGL Drawing FunctionsOpenGL Drawing Functions

glBegin (glBegin (GL_TRIANGLES_STRIPGL_TRIANGLES_STRIP););

glVertex2iv (p1);glVertex2iv (p1);

glVertex2iv (p2);glVertex2iv (p2);

glVertex2iv (p6);glVertex2iv (p6);

glVertex2iv (p3);glVertex2iv (p3);

glVertex2iv (p5);glVertex2iv (p5);

glVertex2iv (p4);glVertex2iv (p4);

glEnd();glEnd();

P1

P2 P3

P4

P5P6

If N is odd, order: n, n+1, n+2If N is even, order: n+1, n, n+2

Page 22: 7.Module II OpenGL

OpenGL Drawing FunctionsOpenGL Drawing Functions

glBegin (glBegin (GL_TRIANGLES_FANGL_TRIANGLES_FAN););

glVertex2iv (p1);glVertex2iv (p1);

glVertex2iv (p2);glVertex2iv (p2);

glVertex2iv (p3);glVertex2iv (p3);

glVertex2iv (p4);glVertex2iv (p4);

glVertex2iv (p5);glVertex2iv (p5);

glVertex2iv (p6);glVertex2iv (p6);

glEnd();glEnd();

P1

P2 P3

P4

P5P6

Page 23: 7.Module II OpenGL

OpenGL Drawing FunctionsOpenGL Drawing Functions

glBegin (glBegin (GL_QUADSGL_QUADS););glVertex2iv (p1);glVertex2iv (p1);glVertex2iv (p2);glVertex2iv (p2);glVertex2iv (p3);glVertex2iv (p3);glVertex2iv (p4);glVertex2iv (p4);glVertex2iv (p5);glVertex2iv (p5);glVertex2iv (p6);glVertex2iv (p6);glVertex2iv (p7);glVertex2iv (p7);glVertex2iv (p8);glVertex2iv (p8);

glEnd();glEnd();

P1

P2P3

P4 P5

P6P7

P8

Page 24: 7.Module II OpenGL

OpenGL Drawing FunctionsOpenGL Drawing Functions

glBegin (glBegin (GL_QUADS_STRIPGL_QUADS_STRIP););glVertex2iv (p1);glVertex2iv (p1);glVertex2iv (p2);glVertex2iv (p2);glVertex2iv (p4);glVertex2iv (p4);glVertex2iv (p3);glVertex2iv (p3);glVertex2iv (p5);glVertex2iv (p5);glVertex2iv (p6);glVertex2iv (p6);glVertex2iv (p8);glVertex2iv (p8);glVertex2iv (p7);glVertex2iv (p7);

glEnd();glEnd();

P1

P2P3

P4 P5

P6P7

P8

Order: 2n-1, 2n, 2n+2, 2n+1

Page 25: 7.Module II OpenGL

OpenGL-Related LibrariesOpenGL-Related Libraries

► GLU (OpenGL Utility Library)GLU (OpenGL Utility Library) part of OpenGLpart of OpenGL NURBS, tessellators, quadric shapes, etc…NURBS, tessellators, quadric shapes, etc…

► GLUT (OpenGL Utility Toolkit)GLUT (OpenGL Utility Toolkit) portable windowing APIportable windowing API not officially part of OpenGLnot officially part of OpenGL

Page 26: 7.Module II OpenGL

GLUT: OpenGLUT: OpenGLGL UUtility tility TToolkitoolkit

►Application StructureApplication Structure Configure and open windowConfigure and open window Initialize OpenGL stateInitialize OpenGL state Register input callback functionsRegister input callback functions

►renderrender►resizeresize►input: keyboard, mouse, etc.input: keyboard, mouse, etc.

Enter event processing loopEnter event processing loop

Page 27: 7.Module II OpenGL

Header files available in a gl Header files available in a gl subdirectorysubdirectory

►#include<gl/gl.h>#include<gl/gl.h>►#include<gl/glu.h>#include<gl/glu.h>►#include<gl/glut.h>#include<gl/glut.h>►#include<windows.h>#include<windows.h>

Page 28: 7.Module II OpenGL

GLUT Callback FunctionsGLUT Callback Functions

►Routine to call when something Routine to call when something happenshappens window resized, user input, window needs window resized, user input, window needs

drawing, etc…drawing, etc…►““Register” callbacks with GLUTRegister” callbacks with GLUT

glutDisplayFunc( glutDisplayFunc( displaydisplay ); );glutIdleFunc( glutIdleFunc( idleidle ); );glutKeyboardFunc( glutKeyboardFunc( keyboardkeyboard ); );glutMouseFunc( mouse );glutMouseFunc( mouse );