Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to OpenGL Muhammad Aamir Khan Lecturer, DCS, UOP.

Similar presentations


Presentation on theme: "Introduction to OpenGL Muhammad Aamir Khan Lecturer, DCS, UOP."— Presentation transcript:

1 Introduction to OpenGL Muhammad Aamir Khan Lecturer, DCS, UOP

2 27 May Spring '09CG by MAK2  Computer graphics is mastered most quickly by doing it. Write and test programs that produce a variety of pictures.  Start with the simple tasks Try some variations … see what happens … and move towards drawing more complex scenes.

3 27 May Spring '09CG by MAK3  You need an environment consisting of 1. Hardware to display pictures (usually a CRT display generally called a screen) and 2. A library of software tools that your program can use.  Initialization of the hardware 1. Establishes the display mode (to graphics) 2. Setting up a coordinate system on the display

4 27 May Spring '09CG by MAK4 (100,50) (150,75) (0,250) x increases to the right y inclrease donwards 1. The entire screen is used for drawing

5 27 May Spring '09CG by MAK5 5 2. A window-based system is used. Different rectangular windows of different sizes at various locations are supported. Initialization involves creating and opening a new window. A coordinate system is attached to each window. X inclreases to the right and y increases downwards X-Windows on Linux, Solaris, Ultrix etc Windows Application Programming Interface (API) on Windows XP Quick Drawn on Apple Macintosh system X-Windows on Linux, Solaris, Ultrix etc Windows Application Programming Interface (API) on Windows XP Quick Drawn on Apple Macintosh system

6 27 May Spring '09CG by MAK6 3. A window-based system is used. Different rectangular windows of different sizes at various locations are supported. Initialization involves creating and opening a new window. A coordinate system is attached to each window. X increases to the right and y increases upwards Any window based system using OpenGL

7 27 May Spring '09CG by MAK7  Device independent graphics programming means a uniform approach is made so that the same program could be compiled and run on a variety of environments with the guarantee to produce the same results  OpenGL offers such a tool.

8 27 May Spring '09CG by MAK8  OpenGL provides an API (application programming interface), i.e. a collection of routines that a programmer can call, shielding the programmer from the hardware and software details of the system.  OpenGL is most powerful when drawing images of complex three-dimensional (3D) scenes.  It also works well for two-dimensional (2D) drawings … which we will be looking into for the time being.

9 27 May Spring '09CG by MAK9  Most window-based programs are event-driven: a program responds to various events such as click of a mouse, pressing of a key on the keyboard or the resizing of the window.  The systems maintains an event-queue which receives messages stating that certain event has occurred and deals with them on first come first served basis.  The programs are generally written in terms of call-back functions: the function that is executed when a particular event takes place.

10 27 May Spring '09CG by MAK10 Setting Up the Environment There are three steps involved 1. Copy the glut32.lib to the folder C:\Program Files\Microsoft Visual Studio\VC98\Lib 2. Copy glut.h to C:\Program Files\Microsoft Visual Studio\VC98\include\GL 3. Copy glut32.dll to C:\Windows\System 4. Include the lib in the VC, Project-> Setting->link

11 27 May Spring '09CG by MAK11 COMMENTS void main() { initialize things create a screen window glutDisplayFunc(myDisplay); glutReshapeFunc(myReshape); glutMouseFunc(myMouse); glutKeyboardFunc(myKeyboard); some other code here glutMainLoop() } All other call-back functions are defined here. // An example main function. // Text in italics is pseudo code. // register the redraw function //register the reshape function // register the mouse action function // register the keyboard action function // enter the un-ending main loop A SKELETON OF AN EVENT-DRIVEN PROGRAM USING OPENGL

12 27 May Spring '09CG by MAK12  glutDisplayFunc(myDisplay): Here myDisplay is the function which is executed whenever the graphics window is redrawn.  glutReshapeFunc(myReshape): The function myReshape is called whenever the window is reshaped (its size is changed)  glutMouseFunc(myMouse): The function myMouse is executed in response to a mouse event.  glutKeyboardFunc(myKeyboard): This command registers the function myKeyboard with the event of pressing or releasing a key on the keyboard.  glutMainLoop(): This functions makes the program to enter into an endless loop waiting for the event to occur.

13 27 May Spring '09CG by MAK13 // appropriate #include go here. void main(int argc, char** argv) { glutInit(&argc, argv); // initialize the toolkit glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // set the display mode glutInitWindowSize(640, 480); // set the window size glutInitWindowPosition(100, 150); // set the window position on screen // open the screen window glutCreateWindow(“My first graphics program in opengl”); // register the callback functions glutDisplayFunc(myDisplay); glutReshapeFunc(myReshape); glutMouseFunc(myMouse); glutKeyboardFunc(myKeyboard); myInit();// some additional initialization as required glutMainLoop(); }

14 27 May Spring '09CG by MAK14  In the next few slides, I present a complete OpenGL program to draw some dots on screen.  This will be followed by an explanation of some of the functions that have been used in this program.

15 #include void myInit()// *************** myInit ************** { glClearColor(1.0, 1.0, 1.0, 0.0); // set white background color glColor3f(0.0, 0.0, 0.0); // set the drawing color glPointSize(4.0); // a dot will be 4 by 4 pixels glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0, 640.0, 0.0, 480.0); } void myDisplay(void)// *************** myDisplay **************** { glClear(GL_COLOR_BUFFER_BIT); // clear the screen glBegin(GL_POINTS); // draw three points glVertex2i(100, 50); glVertex2i(100, 130); glVertex2i(150, 130); glEnd(); glFlush(); // send all output to display }

16 27 May Spring '09CG by MAK16 void main(int argc, char** argv) { glutInit(&argc, argv); // initialize the toolkit glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // set the display mode glutInitWindowSize(640, 480); // set the window size glutInitWindowPosition(100, 150); // set the window position on screen // open the screen window glutCreateWindow(“Dots in opengl”); glutDisplayFunc(myDisplay); // register the redraw functions myInit();// some additional initialization as required glutMainLoop(); }

17 27 May Spring '09CG by MAK17 The initial coordinate system for drawing 479 Dots in OpenGL 639

18 27 May Spring '09CG by MAK18  Output primitives line points, lines, polylines and polygons are defined in terms of one or more vertices.  Such objects are drawn by passing a list of vertices. This list is defined within a pair of OpenGL function calls: glBegin() and glEnd(). glBegin(GL_POINTS); // draw three points glVertex2i(100, 50); glVertex2i(100, 130); glVertex2i(150, 130); glEnd();  Here GL_POINTS is a constant built-into OpenGL. Other constants are GL_LINES, GL_POLYGON etc.

19 27 May Spring '09CG by MAK19 SuffixData TypeTypical C or C++ type OpenGL type name b8-bit integersigned charGLbyte s16-bit integershortGLshort i32-bit integerint or longGLint or GLsizei f32-bit floating pointfloatGLfloat or GLclampf d64-bit floating pointdoubleGldouble or Glclampd ub8-bit unsigned numberunsigned charGLubyte or GLboolean us16-bit unsigned number unsigned shortGLushort ui32-bit unsigned number unsigned int or unsigned long GLuint, GLenum, GLbitfield

20 27 May Spring '09CG by MAK20  As an example, a function using suffix i “expects” a 32-bit integer, but your system might translate int as a 16-bit integer void drawDot(int x, int y) { glBegin(GL_POINTS); // draws a dot at (x, y) glVertex2i(150, 130); glEnd(); }  A better option will be to use: void drawDot(GLint x, GLint y) { glBegin(GL_POINTS); // draws a dot at (x, y) glVertex2i(150, 130); glEnd(); }

21 27 May Spring '09CG by MAK21  OpenGL keeps track of many “state variables”, such as The current size of a point, The current color of a drawing The current background color  The value of a state variable remains active until a new value is given.  The size of a point can be set with glPointSize(), which takes one floating point argument. If the argument is 3.0, the point is usually drawn as a square with 3 pixels on a side.

22 27 May Spring '09CG by MAK22  The color of a drawn can be specified using glColor3f( red, green, blue); where the values of red, green and blue vary between 0.0 and 1.0.  The background color is set with glClearColor( red, green, blue, alpha); where alpha specifies a degree of transparency.  To clear the entire window to the background color, use glClear( GL_COLOR_BUFFER_BIT);

23 27 May Spring '09CG by MAK23  We will discuss it in detail when we discuss windows, viewports and clipping.  The myInit() function is a good place to set up the coordinate system.  OpenGL performs routinely a large number of transformations. This is done using matrices. gluOrtho2D() function sets the transformation we need for a screen window of size 640 pixels by 480 pixels. gluOrtho2D() function sets the transformation we need for a screen window of size 640 pixels by 480 pixels.

24 27 May Spring '09CG by MAK24  Use GL_LINES as the argument to glBegin() and pass it the two endpoints as vertices. glBegin(GL_LINES ); glVertex2i(40, 100); glVertex2i(202, 96); glEnd();  If more than two vertices are specified between glBegin() and glEnd(), they are taken in pairs, and a separate line is drawn between each pair.

25 That’s all for today


Download ppt "Introduction to OpenGL Muhammad Aamir Khan Lecturer, DCS, UOP."

Similar presentations


Ads by Google