1 / 46

Unit-2 The OpenGL

Course code:10CS65 | Computer Graphics and Visualization. Unit-2 The OpenGL. Engineered for Tomorrow. Prepared by : Ms Sandhya, Ms Preeti & Ms Hymavathi Department : Computer Science and Engineering. Date : dd.mm.yyyy. UNIT - 2 THE OPENGL. The OpenGL API Primitives and attributes

mattieg
Download Presentation

Unit-2 The OpenGL

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Course code:10CS65 | Computer Graphics and Visualization Unit-2 The OpenGL Engineered for Tomorrow Prepared by : Ms Sandhya, Ms Preeti & Ms Hymavathi Department : Computer Science and Engineering Date : dd.mm.yyyy

  2. UNIT - 2 THE OPENGL • The OpenGL API • Primitives and attributes • Color • Viewing • Control functions • The Gasket program • Polygons and recursion • The three-dimensional gasket • Plotting implicit functions.

  3. Functionality can be grouped to: primitive functions – low level primitives (points, pixels etc.) primitives API – what should be displayedattribute functions are – how should be displayed –lines, points, polygons etc. Synthetic camera must describe how to create an image – viewing functions (camera properties – lens, viewing field, several cameras, stereoscopic views etc.) OpenGL API Graphics package as a BLACK BOX

  4. OpenGL API • geometric transformation – scaling, rotation, translation, sheering etc. and use of transformation matrix. This forms the main advantage as the geometric engine can be used for substantial speed up. • input functions – allow interaction with graphics primitives or objects; functions for communication with a keyboard, mouse, data tablets etc. • control functions – initialization, errors handling etc. in multiprocessor multi-window & network environment • Query functions – helps query information about the properties of the particular implementation.

  5. OpenGL Interface OpenGL function names begin with gl and are stored in library referred as GL Libraries: • GLU - graphics utility library – uses only GL primitives but contains code for common objects like spheres etc. – available in all OpenGL implementations • GLUT – GL Utility Toolkit / provides minimum functionality to be expected from any modern windowing system • others libraries are called from OpenGL libraries – but indirectly, hidden from the user.

  6. Strings like GL_FILL, GL_POINTS are defined in header ( .h) files – in most implementation #include <GL/glut.h> or #include <glut.h> includes glut.h, gl.h, glu.h OpenGL Interface Picture shows the organization of the libraries for the X-Windows, similarly for Microsoft Windows

  7. glBegin(type) glVertex*(...); ............. glVertex*(...); glEnd ( ); Primitives should be minimal & orthogonal (capability that cannot be obtained from others) more advanced to offer complex functions to a user – GLU line segments and polylinescan form the edges of objects OpenGL Primitives & Attributes

  8. Polygons have borders and interior Can be displayed rapidly and approximate curved surfaces Must be simple, convex and flat OpenGL - Polygons Simple and non-simple polygon Filled objects Methods of displaying polygons

  9. Convex polygon – all points on the line segment between any two points inside of the object, are inside of the polygon Flat polygon – polygon can lie on a plane in 3D in general Tests on planarity, simplicity, flatness are not performed OpenGL - Polygons Convexity in 2D Convexity in 3D

  10. several OpenGL types were defined GL_* polygon edge itself has no width if polygon edges are to be drawn and polygon filled – it must be drawn twice with different modes OpenGL – Polygons Types

  11. Graphical output in applications requires text output Fonts – Times, Helvetica, Computer Modern etc. Forms – stroke vs. raster text stroke – constructed as other graphics primitives – points etc. – the standard PostScript fonts defined by polynomial curves – easy to enlarge raster – fast; using bit-block-transfer (bitblt) operations OpenGL – Text

  12. Curved primitives (like circles, spheres, curves and surfaces etc.) are not directly supported – must be approximated by a mesh polygons – a tessellation – at the rendering stage or within the user program GLU can be used to approximate common curved surfaces Advanced OpenGL functions can be used to work with parametric curves and surfaces OpenGL – Curved Objects

  13. Distinction between what the type of a primitive is how the primitive is displayed A red solid line and green dashed line are the the same geometric type, but displayed differently due to attributes OpenGL – Attributes

  14. OpenGL – Color Color is one of the most interesting aspects of both human perception and computer graphics • Light is a part of electromagnetic spectrum 350 – 780 nm • Color C = rR + gG + bB r,g,b are tristimulus values • assumption of three-color theoryif two colors produce the same tristimulus values then they are visually indistinguishable

  15. Color space can be represented as RGB cube Gray levels are represented by the Black-White line segment OpenGL – Color

  16. Range of colors that can be produced on the given system is called color gamut Additive vs. subtractive color system RGB vs. CMY (K) color systems OpenGL – Color

  17. Additive RGB primary color system is used 8 bits/pixel for each channel R,G,B 224 possible colors 1024 x 1024 output resolution 3MB RAM for graphics purposes RGBA system (Alpha channel) – for transparency/opacity glColor3f(1.0, 0.0, 0.0) glClearColor(1.0, 1.0, 1.0, 1.0) – screen solid and white OpenGL – RGB Color

  18. Limit of the frame buffer to k bits per pixel 2k –1 colors can be displayed from a palette of 23m of colorstable of 2k x 3m is to be specified for k=m=8 – a common configuration – 256 colors out of 16M. 256 entries constitute the user’s color palette Color palette enables to change in the whole image OpenGL – RGB Color – Indexed Color

  19. In color-index mode – present color is selected byglIndexi(element) – selects particular color out of the table System can have just one common table or multiple tables for different windows – GLUT allows that glutSetColor(int color, GLfloat red, GLfloat blue, GLfloat green) - we will work directly with RGB colors (not indexed) in the next OpenGL – RGB Color – Indexed Color

  20. Setting of Color Attributes For the Sierpinski gasket program we need to set: • clear color glClearColor(1.0, 1.0, 1.0, 1.0); • rendering color for points – set to RED glColor3f(1.0, 0.0, 0.0); • set point size – let us use 2 pixels size glPointSize(2.0); /* size is in pixels not in [mm] */ These attributes will be valid for all the time if not changed explicitly.

  21. Viewing In the synthetic camera concept the object specification is completely independent from • the camera specification • lights properties and positions used • material properties as well We used some default settings for those expecting that objects are placed “in the right position”, sufficiently far from the camera etc. We will concentrate on • 2D viewing, • orthographic projection, • matrix mode application

  22. Objects outside are clipped out and are not displayed partially inside – only parts inside are displayed Objects before and after 2D clipping Clipping rectangle or viewing rectangle 2D Viewing

  23. Viewing rectangle is at z = 0 with corners (-1.0,-1.0) & (1.0, 1.0) Implicit viewing volume in OpenGL is 2 x 2 x 2 cube – center in the origin 2D Viewing – Orthographic View

  24. void glOrtho(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far) void gluOrtho2D(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top) /* near=-1, far=1*/ 2D Viewing – Orthographic View

  25. 2D Viewing - Matrix Mode • Pipeline of the graphics system utilizes matrices multiplications for geometric transformations. • Two the most important matrices: • model-view • projection Setting matrix mode ( it is the status definition of the system): glMatrixMode(GL_PROJECTION); /* change to PROJECTION status */ glLoadIdentity( ); /* sets the matrix to the IDENTITY matrix */ gluOrtho2D(0.0, 500.0, 0.0, 500.0); /* sets window to 500 x 500 image */ glMatrixMode(GL_MODELVIEW); /* return back to the MODEL-VIEW state – highly recommended */

  26. Control Functions • Minimal interaction between the program and the particular OS must be used (X-Windows - UNIX, Windows) • GLUT library provides minimal & simple interface between API and particular OS (full functionality for interaction between a program and OS is not a part of this course) • Programs using GLUT interface should run under multiple window system.

  27. Interaction with the Window System • Window or screen window – rectangular area of a display • Window system – refers to X-Windows or MS Windows • The origin – not always lower-left corner – OpenGL style(some have orientation: top to bottom, left to right as TV sets or position information returned from input devices like mouse) glutInit(int *argcp, char *argv) – enables to pass command-line arguments as in the standard C main function glutCreateWindow(char *title) – creates a window with the name specified in the string title with default values (size, position etc.)

  28. Interaction with the Window System BEFORE creating the window GLUT functions can be used to specify properties: glutDisplayMode(GLUT_RGB|GLUT_DEPTH|GLUT_DOUBLE); /* parameters or-ed and stored in the argument to glutInitDisplayMode */ glutWindowSize(480, 640); glutWindowPosition(0 , 0); specifies the windows 480 x 640 in the top-left corner of the display GLUT_RGB x GLUT_INDEX – type of color system to be used GLUT_DEPTH – a depth buffer for hidden-line removal GLUT_DOUBLE x GLUT_SINGLE – double x single buffering Implicit options: RGB color, no hidden-line removal, single buffer

  29. Aspect ratio – the ratio of the rectangle’s width and height If different in glOrtho and glutInitWindowSize – undesirable side effects Caused by the independence of object, viewing parameters and workstation window specifications Concept ofa VIEWPORT Aspect Ratio and Viewports

  30. void glViewport(GLint x, GLint y, GLsizei w, GLsizei h) The viewport is part of the state – when changed between rendering objects or redisplay – different window-viewport transformations used to make the scene. Aspect Ratio and Viewports

  31. Functions main, display & myinit Program = simple initialization code + sequence with OpenGL Problems: • generic to all graphics systems • interaction with underlying window system in immediate-mode graphics a primitive is rendered to the screen immediately. The application ends and the screen is cleaned before anything can be seen - solution sleep(enough_time)better solutionvoid glutMainLoop( void );execution causes the program to begin an event-processing loop – until program is not terminated through the “kill” key

  32. Functions main, display & myinit Graphics are sent to the scene through a function called the display callback that is specified through the GLUT function as void glutDisplayFunction (void (*func)(void)) func is the function name that will be called whenever the windowing system determines that the OpenGL windows needs to be redisplayed (created, moved, resized, made visible etc.) This function is called also when the window is created For non-interactive use the code can be specified in this function

  33. Functions main, display & myinit #include <GL/glut.h> /* compiler should include gl.h & glu.h */ void main (intargc, char **argv) /* the main function */ { glutInit(&argc,argv); glutDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize (500,500); glutInitWindowPosition(0,0); glutCreateWindow(“simple OpenGL example”); glutDisplayFunc(display); myinit( ); glutMainLoop( ); } /* should work for all non-interactive programs */

  34. The Gasket Program void myinit (void) { /* attributes */ glClearColor(1.0, 1.0, 1.0, 1.0); /* white background */ glColor3f(1.0, 0.0, 0.0); /* draw in red */ /* set up viewing */ glMatrixMode(GL_PROJECTION); gluLoadIdentity ( ); gluOrtho2D(0.0, 500.0,0.0, 500.0); glMatrixMode(GL_MODELVIEW); }

  35. Functions main, display & myinit void display(void) { typedefGLfloat point2 [2]; /* defines a point as 2D array */ point2 vertices[3]= { {0.0,0.0}, {250.500}, {500.0,0.0}}; /*triangle*/ inti, j, k; int rand( ); {* standard random number generator */ point2 p = {75.0, 50.0}; /* set to any initial point */ glClear(GL_COLOR_BUFFER_BIT); /* clear the window */ for (k=0; k<5000; k++) { j = rand ( )%3; /* pick a random vertex from 0, 1, 2 */ /* compute a point halfway between vertex and old point */ p[0] = (p[0]+vertices[ j ][0])/2; p[1] = (p[1]+vertices[ j ][1])/2; /* display the new point */ glBegin(GL_points); glVertex2fv(p); glEnd( ); } glFlush(); /* points are to be displayed as soon as possible */ }

  36. A similar approach – a triangle is split to triangles recursively original triangle given by the array point2 v[3]; void triangle (point2 a, point2 b, point2 c) { glBegin(GL_TRIANGLES); glVertex2fv(a); glVertex2fv(b); glVertex2fv(c); glEnd( ); } ------ mid-points computations ------------- for (j=0; j<2; j++) m[0][j]=(v[0][j]+ v[1][j])/2.0; for (j=0; j<2; j++) m[1][j]=(v[0][j]+ v[2][j])/2.0; for (j=0; j<2; j++) m[2][j]=(v[1][j]+ v[2][j])/2.0; Polygons and Recursion

  37. Sierpinski Gasket Program - triangles Modification for recursive triangles void divide_triangle(point2 a, point2 b, point2 c, int k) { point2 ab, ac, bc; int j; if ( k>0) { /* compute midpoints */ for (j=0; j<2; j++) ab[j]=(a[j]+ b[j])/2.0; for (j=0; j<2; j++) ac[j]=(a[j]+ c[j])/2.0; for (j=0; j<2; j++) bc[j]=(b[j]+ c[j])/2.0; /* subdivide all but inner triangle */ divide_triangle(a, ab, ac, k-1); divide_triangle(c, ac, bc, k-1); divide_triangle(b, bc, ab, k-1); } else triangle(a,b,c); /*draw a triangle at end of recursion */ }

  38. Sierpinski Gasket Program - triangles void display(void) { glClear(GL_COLOR_BUFFER_BIT); divide_triangle(v[0], v[1], v[2], n); /* n determines No of subdivision steps */ glFlush( ); } Note: there is no convenient way to pass variables to OpenGL functions and callback other than through global parameters

  39. Now we expand 2D to 3D case data type definition:typedef GLfloat point3[3]; int n; /* global variable */ tetrahedron specifications:point3 vertices[4] = { {0.0,0.0,0.0}, {250.0,500.0,100.0}, {500.0,250.0,250.0}, {250.0,100.0,250.0}};point3 p= {250.0,50.0,250.0}; function glPoints3fv is used to define points different colors used for points to distinguish them drawing polygons – visibility should be solved 3D Sierpinski Gasket Program

  40. 3D Sierpinski Gasket Program void triangle (point3 a, point3 b, point3 c) { point3 norm; cross(a, b, c, norm); /* computes the normal vector of the triangle */ glBegin(GL_POLYGON); glNormal3fv(a); glVertex3fv(a); glVertex3fv(b); glVertex3fv(c); glEnd( ); } /* normal computation - using cross product */

  41. 3D Sierpinski Gasket Program void divide_triangle(point3 a, point3 b, point3 c, int k); { point3 v1, v2, v3; int j; if ( k>0) { /* compute midpoints */ for (j=0; j<3; j++) v1[ j]=(a[ j]+ b[ j])/2.0; for (j=0; j<3; j++) v2[ j]=(a[ j]+ c[ j])/2.0; for (j=0; j<3; j++) v3[ j]=(b[ j]+ c[ j])/2.0; divide_triangle(a, v1, v2, k-1); divide_triangle(c, v2, v3, k-1); divide_triangle(b, v3, v1, k-1); } else triangle(a,b,c); /*draw a triangle at end of recursion */ }

  42. 3D Sierpinski Gasket Program void tetrahedron (int m); { /* apply triangle subdivision to faces of tetrahedron */ glColor3f(1.0, 0.0, 0.0); divide_triangle (v[0], v[1], v[2], m); glColor3f(0.0, 1.0, 0.0); divide_triangle (v[3], v[2], v[1], m); glColor3f(0.0, 0.0, 1.0); divide_triangle (v[0], v[3], v[1], m); glColor3f(0.0, 0.0, 0.0); divide_triangle (v[0], v[2], v[3], m); }

  43. 3D Sierpinski Gasket Program void display ( ); { glClear(GL_COLOR_BUFFER_BIT | GLDEPTH_BUFFER_BIT); glLoadIdentity (); tetrahedron(n); glFlush ( ); } void myReshape(int w, int h) { glViewport (0,0,w,h); glMatrixMode(GL_PROJECTION); glLoadIdentity (); if (w<=h) glOrtho(-2.0,2.0, (GLfloat)h/(GLfloat)w, (GLfloat)h/(GLfloat)w, -10.0, 10.0); else glOrtho(-2.0,2.0, (GLfloat)w/(GLfloat)h, (GLfloat)w/(GLfloat)h, -10.0, 10.0); glMatrixMode(GL_MODELVIEW); glutPostRedisplay ( ); }

  44. 3D Sierpinski Gasket Program void main ( intargc, char **argv); { n = atoi(argv[1]); /* parameter on the command line */ /* n is a global variable */ glutInit ( intargc, char **argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize (500,500); glutCreateWindow (“3D Gasket”); glutReshapeFunc(myReshape); glutDisplayFunc(display); glEnable(GL_DEPTH_TEST); glClearColor (1.0, 1.0, 1.0, 1.0); glutMainLoop( ); }

  45. Triangles are drawn in order of generation (given by the algorithm) not according to their geometric position Hidden surface removal is enabled by setting glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH); and enable that by (using z-buffer algorithm) glEnable(GL_DEPTH_TEST); 3D Sierpinski Gasket Program

  46. Conclusion • The structure of the OpenGL API and interaction with OS have been explained • you learnt how to write non-interactive complex OpenGL programs using non-trivial features, especially how to: • redraw the windows if any change • use window-viewport transformation • handle orthographic projection • use OpenGL in recursive environment • draw primitives that are filled and • solve the visibility of objects Some features will be explored in the following parts in detail

More Related