1 / 30

CA 302 Computer Graphics and Visual Programming

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. Introduction to OpenGL. OpenGL: Open Graphics Library

lassie
Download Presentation

CA 302 Computer Graphics and Visual Programming

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. CA 302 Computer Graphics and VisualProgramming Lecture 2: Introduction to OpenGL Aydın Öztürk aydin.ozturk@ege.edu.tr http://www.ube.ege.edu.tr/~ozturk

  2. IntroductiontoOpenGL 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. BasicOpenGLSyntax 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. BasicOpenGLSyntax(cont.) In OpenGL functions expect specific data types. The OpenGL uses special built-in data-type names GLbyte GLshort GLint GLfloat GLdouble GLboolean

  5. BasicOpenGLSyntax(cont.)

  6. BasicOpenGLSyntax(cont.) The two commandsare 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. BasicOpenGLSyntax(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. TheOpenGLUtilityLibrary 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. RelatedLibraries Every OpenGL implementation includes the GLU library. GLU function names start with the prefix glu: gluBeginCurve gluCylinder gluPerspective gluNurbsSurface

  10. RelatedLibraries(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. RelatedLibraries(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. RelatedLibraries(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. HeaderFiles 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. HeaderFiles Inallgraphicsprogramsweneedtoincludetheheader file fortheOpenGLcorelibrary. Formostapplicationswealsoneedheader file forGLU and forwindowsystem. #include <windows.h> #include <GL/gl.h> #include <GL/glu.h>

  15. HeaderFiles(cont.) If we use GLUT we do not need to include gl.handglu.h Thus, we replace the header files for OpenGL and GLU with #include <GL/glut.h>

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

  17. Display-WindowManagementUsing 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-WindowManagementUsing GLUT To create a display window on the screen we write the statement glutCreateWindow ("An OpenGL program");

  19. Display-WindowManagementUsing 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 toglutDisplayFuncwhich assigns the picture to the display window by the statement glutDisplayFunc(lineSegment);

  20. Display-WindowManagementUsing 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-WindowManagementUsing 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-WindowManagementUsing 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 CompleteOpenGL 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 CompleteOpenGL 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 CompleteOpenGL 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 CompleteOpenGL 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 CompleteOpenGL Program (cont.) Finally, weneedtocalltheappropriateOpenGLroutinestocreatetherequredpicture(e.g. thelinesegment). Thefolowingcodedefines a 2D linesegmentwithinteger, Cartesianendpointcoordinates (180,15) and (10,145). glBegin(GL_LINES); glVertex2i(180, 15); glVertex2i(10, 145); glEnd ( );

  28. A CompleteOpenGL Program (cont.) ThefollowingOpenGLprogram is organizedintothreeprocedures: init : Placeallinitializations and one-time parametersettings. lineSegment : Geometricdescription of thepicturewewanttodisplay. Thiswill be referencedbyglutDisplayFunc. main: ContainstheGLUTfunctionsforsettingupthedisplaywindow and gettingourlinesegmentontothescreen.

  29. An ExampleOpenGL Program #include <GL/glut.h> voidinit (void) { glClearColor (1.0, 1.0, 1.0, 0.0); //Set display-window color to white. glMatrixMode (GL_PROJECTION); //Set projectionparameters. gluOrtho2D (0.0, 200.0, 0.0, 150.0); } voidlineSegment (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 ExampleOpenGL 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. }

More Related