160 likes | 316 Views
OpenGL (Graphics Library). Software Interface to graphics software Allows to create interactive programs that produce color images of moving 3D objects. OpenGL. ● OpenGL – Hardware independent, only specifies graphics output low level, no GUI operations (events, windows, menu’s)
E N D
OpenGL (Graphics Library) • Software Interface to graphics software • Allows to create interactive programs that produce color images of moving 3D objects.
OpenGL ●OpenGL– Hardware independent, only specifies graphics output low level, no GUI operations (events, windows, menu’s) ●OpenGL Utility (GLU) – Higher level graphics primitives (spheres, curves, curved surfaces) ●OpenGL Utility Toolkit (GLUT) – Primitive window setup & event handling. enables truly window system independent applications
OpenGL Basic Syntax (C Language Binding) ●Functions: glXXXX – glBegin, glEnd, glPolygonMode ●Constants: GL_XXXX – GL_2D, GL_RGB, GL_CCW ●Types: GLxxx – GLbyte, GLshort, GLint, GLfloat, GLdouble, GLboolean
OpenGL Header Files #include <windows.h> #include <GL/gl.h> #include <GL/glu.h> However, If using GLUT to handle window- managing operations #include <GL/glut.h>
Display-Window Management Using GLUT • Step1 : GLUT Initialization • glut Init (&argc, argv); • Step2 : Display window is created on the screen with the function • glutCreateWindow ( “ An Example OpenGL Program”); • Other functions such as : • glutDisplayFunc ( lineSegment); //displaying any object • glutInitWindowPosition (50, 100); // set the position of window
Display-Window Management Using GLUT.. • glutInitWindowSize (400, 300); //set the size of the window • glutInitWindowSize (400, 300); //set the size of the window • glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); • //options for buffering and color modes • Final Step : All display windows are activated and displayed • glutMainLoop ( ); • This function completes the window- processing operations.
OpenGL Program Example #include <GL/glut.h> void init (void) { glClearColor (1.0 , 1.0, 1.0, 0.0); //Set Display-window color to white glMatrixMode (GL_PROJECTION); // Set projection parameters gluOrtho2D (0.0, 200.0, 0.0, 150.0); }
Continued.. void lineSegment (void) { glClear (GL_COLOR_BUFFER_BIT); // Clear display window glColor3f (1.0, 0.0, 0.0); //Set line segment color to red glBegin (GL_LINES); glVertex2i (180, 15); //Specify line- segment geometry glVertex2i (10, 145); glEnd (); glFlush (); // Process all OpenGL routines as quickly as possible }
Continued.. void main (int argc, char** argv) { glutInit (&argc, argv); // Initialize GLUT glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); // Set display mode glutInitWindowPosition (50, 100); //Set top-left display-window position glutInitWindowSize (400,300); //Set display-window width and height
Continued.. glutCreateWindow ("An Example OpenGL Program");// Create display window init (); // Execute initialization procedure glutDisplayFunc (lineSegment); // Send graphics to display window glutMainLoop (); }
OpenGL Geometric Output Primitives ●Types: GL_POINTS, GL_LINES, GL_LINE_LOOP, GL_LINE_STRIP, GL_POLYGON, GL_QUADS, GL_QUAD_STRIP, GL_TRIANGLE, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN ●Basic Syntax: glBegin (PrimitiveConstant); glVertex3f (x1,y1,z1) glVertex3f (x2,y2,z2) …. glEnd(); glVertex(2|3|4)(s|i|d|f)[v]
Points & Lines GL_LINE_STRIP GL_LINES v1 GL_POINTS v0 v1 v2 v3 v0 v5 v2 v3 v4 v1 GL_LINE_LOOP v0 v2 v3 v4 glBegin (PrimitiveConstant); glVertex3f (x1,y1,z1) glVertex3f (x2,y2,z2) …. glEnd();
Polygons GL_QUADS GL_QUAD_STRIP v0 v3 GL_POLYGON v7 v4 v6 v1 v0 v3 v2 v4 v7 v5 v6 v3 v1 v2 v2 v0 v4 v5 v1 glBegin (PrimitiveConstant); glVertex3f (x1,y1,z1) glVertex3f (x2,y2,z2) …. glEnd();
Polygons (2) GL_TRIANGLES GL_TRIANGLE_STRIP GL_TRIANGLE_FAN v2 v4 v5 v2 v0 v5 v4 v4 v0 v3 v3 v5 v3 v0 v1 v1 v2 v1 N+2 vertices N Triangles N vertices N/3 Triangles N+2 vertices N Triangles glBegin (PrimitiveConstant); glVertex3f (x1,y1,z1) glVertex3f (x2,y2,z2) …. glEnd();
References • Computer Graphics with OpenGL :H & B • RedBook : www.opengl.org • Tutorials : www.nehe.gamedev.net • ITCS 4120 class notes