Presentation is loading. Please wait.

Presentation is loading. Please wait.

CA 302 Computer Graphics and Visual Programming Lecture 2: Introduction to OpenGL Aydın Öztürk

Similar presentations


Presentation on theme: "CA 302 Computer Graphics and Visual Programming Lecture 2: Introduction to OpenGL Aydın Öztürk"— Presentation transcript:

1 CA 302 Computer Graphics and Visual Programming Lecture 2: Introduction to OpenGL Aydın Öztürk aydin.ozturk@ege.edu.tr http://www.ube.ege.edu.tr/~ozturk

2 Introduction to OpenGL OpenGL: Open Graphics Library OpenGL provides a library of functions for various graphical operations including Graphics pirimitives Attiributes Geometric transformations Viewing transformations OpenGL is hardware independent

3 Basic OpenGL Syntax In OpenGL, function names are prefixed with gl. glBegin glClear glCopyPixels glPolygonMode Symbolic constants assigened as an argument of a function begin as GL followed by its name GL_2D GL_RGB GL_POLYGON Example: glBegin(GL_POLYGON);

4 Basic OpenGL Syntax(cont.) In OpenGL functions expect specific data types. The OpenGL uses special built-in data-type names GLbyte GLshort GLint GLfloat GLdouble GLboolean

5 Basic OpenGL Syntax(cont.) SuffixData TypeTypical Corresponding C- Language Type OpenGL Type Definition b8-bit integersigned charGLbyte s16-bit integershortGLshort i32-bit integerlongGLint, GLsizei f32-bit floating-pointfloatGLfloat, GLclampf d64-bit floating-pointdoubleGLdouble, GLclampd ub8-bit unsigned integerunsigned charGLubyte, GLboolean us16-bit unsigned integerunsigned shortGLushort ui32-bit unsigned integerunsigned longGLuint, GLenum, GLbitfield

6 Basic OpenGL Syntax(cont.) The two commands are equivalent, glVertex2i(1, 3); glVertex2f(1.0, 3.0); Except that the first specifies the vertex's coordinates as 32-bit integers and the second specifies them as single-precision floating-point numbers.

7 Basic OpenGL Syntax(cont.) Some OpenGL commands can take a final letter v, which indicates that the command takes a pointer to a vector (or array) of values rather than a series of individual arguments. glColor3f(1.0, 0.0, 0.0); float color_array[] = {1.0, 0.0, 0.0}; glColor3fv(color_array);

8 The OpenGL Utility Library In addition to the OpenGL core library, there are a number of associated libraries for handling special operations: The OpenGL Utility Library (GLU) contains several routines that use lower-level OpenGL commands to perform such tasks as setting up matrices for specific viewing orientations and projections, performing polygon tessellation, and rendering surfaces. GLU (The OpenGL Utility) library: Provides routines for  Setting up vieving and projection matrices  Describing complex objects with line and polygon approximations  Displaying B_splines and processing surface-rendering operations.

9 Related Libraries Every OpenGL implementation includes the GLU library. GLU function names start with the prefix glu: gluBeginCurve gluCylinder gluPerspective gluNurbsSurface

10 Related Libraries(cont.) Since OpenGL is hardware independent, we need to set up a display window on our video screen by using OpenGL. This is a rectangular area of the screen in which our picture will be displayed. There are several window-system libraries that support OpenGL functions for a variety of machines. For Microsoft Windows systems, the WGL routines provide a Windows-to-OpenGL interface. These routines are prefixed with the letters wgl.

11 Related Libraries(cont.) The OpenGL Utility Toolkit (GLUT) provides a library of functions for interacting with any screen-windowing system. The GLUT library functions are prefixed with glut and this library contains methods for rendering curves and surfaces. glutCreateWindow glutDisplayFunc glutFullScreen glutSetColor

12 Related Libraries(cont.) Since the OpenGL Utility Toolkit(GLUT) is an interface to other device-specific windows systems, we can use GLUT so that our programs will be device independent.

13 Header Files Files:.h,.lib,.dll The entire folder gl is placed in the Include directory of Visual C++ The individual lib files are placed in the lib directory of Visual C++ The individual dll files are placed in C:\Windows\System32

14 Header Files In all graphics programs we need to include the header file for the OpenGL core library. For most applications we also need header file for GLU and for window system. #include

15 Header Files(cont.) If we use GLUT we do not need to include gl.h and glu.h Thus, we replace the header files for OpenGL and GLU with #include

16 Header Files(cont.) In addition, we need to include header files that are required by the C++. For example, #include With the new ISO/ANSI standard for C++, these header files are called cstdio, stdlib and math.

17 Display-Window Management Using GLUT Since we use GLUT for window operations, we need to initialize it. We perform the GLUT initialization with the statement glutinit (&argc, argv);

18 Display-Window Management Using GLUT To create a display window on the screen we write the statement glutCreateWindow ("An OpenGL program");

19 Display-Window Management Using GLUT(cont) The next step is to specify what the display window is to contain. We create a picture using OpenGL functions and pass it to glutDisplayFunc which assigns the picture to the display window by the statement glutDisplayFunc(lineSegment);

20 Display-Window Management Using GLUT(cont) We need one more GLUT function to complete the window-processing operations. glutMainLoop ( ); After execution of above statement, all display windows that have been created are now activated.

21 Display-Window Management Using GLUT(cont) Although the display window that we created will be in some default location and size, we can control it by the following statements which locates the top left corner of the resulting window at the point with coordinates(50,100) and with size 400×300 glutInitWindowPosition (50, 100); glutInitWindowSize (400, 300);

22 Display-Window Management Using GLUT(cont) We can also set a number of other options for the display window. For example the following command specifies that a single refresh buffer is to be used and that the RGB color mode is to be used. glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);

23 A Complete OpenGL Program We need a few additional task to obtain a complete program First we choose a background color (e.g. white): glClearColor (1.0, 1.0, 1.0, 0.0); The first argument : Red The secondargument: Green The third argument : Blue The fourth argument: Alpha value (0: Completely transparent 1: Opac)

24 A Complete OpenGL Program (cont.) Although glClearColor command assigns a color to the display window, it does not put the display window on the screen. The following command gets the assigned window color displayed glClear (GL_COLOR_BUFFER_BIT);

25 A Complete OpenGL Program (cont.) In addition to setting the background color for the display window, we can choose colors for the objects we want to display. For example to specify three color components(RGB) using floating-point (f) values: glColor3f (1.0, 0.0, 0.0);

26 A Complete OpenGL Program (cont.) To display a straight-line (for example) we need to tell OpenGL how we want to project our picture onto the display window. glMatrixMode (GL_PROJECTION); gluOrtho2D (0.0, 200.0, 0.0, 150.0); The secod commands specify that the orthographic projection map the contents of a 2D rectangular area of world coordinates to the screen with coordinate ranges (x,y) → [(0.0, 200.0), (0.0, 150.0)]

27 A Complete OpenGL Program (cont.) Finally, we need to call the appropriate OpenGL routines to create the requred picture(e.g. the line segment). The folowing code defines a 2D line segment with integer, Cartesian endpoint coordinates (180,15) and (10,145). glBegin(GL_LINES); glVertex2i(180, 15); glVertex2i(10, 145); glEnd ( );

28 A Complete OpenGL Program (cont.) The following OpenGL program is organized into three procedures: init : Place all initializations and one-time parameter settings. lineSegment : Geometric description of the picture we want to display. This will be referenced by glutDisplayFunc. main : Contains the GLUT functions for setting up the display window and getting our line segment onto the screen.

29 An Example OpenGL Program #include 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); } 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 }

30 An Example OpenGL Program(cont.) 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. glutCreateWindow ("An OpenGL program");//Create display window. init ( );//Execute initialization procedure glutDisplayFunc(lineSegment);//Send graphics to display window. glutMainLoop ( );//Display everything and wait. }


Download ppt "CA 302 Computer Graphics and Visual Programming Lecture 2: Introduction to OpenGL Aydın Öztürk"

Similar presentations


Ads by Google