7.module ii opengl
TRANSCRIPT
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
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
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
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
Hardware implementation of Hardware implementation of Open GLOpen GL
Application program
OSservices
I/Oservices
GDI OpenGL
Display/Windowing system Hardware Driver
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
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
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
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();
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)
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
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)
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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>
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 );