OpenGL Introduction by hmb46803

VIEWS: 83 PAGES: 3

									                                                                                                           OpenGL


    OpenGL Introduction                                                                                        Portable
     Computer Graphics and Visualization                                                                         – Consistent visual display regardless of hardware,
                                                                                                                   OS and windowing system
                                                                                                                 – Callable from Ada, C, C++, Fortran, Python, Perl
                         Fall 2009                                                                                 and Java
                                                                                                                 – Runs on all major OS and windowing system.
                                                                                                               Well documented
               Pedher Johansson
               Department of Computer Science




                                                                                                       2                                  OpenGL Introduction
                                                                                                                                     Computer Graphics and Visualization
                                                                                                                                                                               Pedher Johansson
                                                                                                                                                                           Department of Computer Science




    OpenGL System Interaction                                                                              OpenGL Utility Toolkit


        OpenGL is completely window system independent.
                                                                                                               GLUT has been ported to all platforms that support
        But we need the window system for things like:                                                         OpenGL.
           – Opening and closing windows
                                                                                                               Provides basic window system interaction
           – Interaction
                                                                                                                 –   Open/Close Window
           – Handling events
                                                                                                                 –   Mouse/Keyboard callbacks
        We can work directly with the window system                                                              –   ¸    ˇ
                                                                                                                     SIdleT callback for animation
                            ˇ
        (X-windows, Win 32E) or use a higher level toolkit                                                       –   Pop-Up Menus
                    ˇ
        (GLUT, MotifE).




3                                    OpenGL Introduction
                                Computer Graphics and Visualization
                                                                          Pedher Johansson
                                                                      Department of Computer Science
                                                                                                       4                                  OpenGL Introduction
                                                                                                                                     Computer Graphics and Visualization
                                                                                                                                                                               Pedher Johansson
                                                                                                                                                                           Department of Computer Science




    OpenGL Libraries                                                                                       OpenGL Functions

                                      GTK+                                                                     Primitives
                                                                                     WGL                         – Points
                                       GLUI                                                                      – Line Segments
                                                                                      GLX                        – Polygons

                                      GLUT                                                                     Attributes
                                                                                      AGL                      Transformations
      Application
                                                                                                                 – Viewing
                                        GLU                                                                      – Modeling
                                                                                                               Control (GLUT)
                                                                                                               Input (GLUT)
                                                                           GL                                  Query

5                                    OpenGL Introduction
                                Computer Graphics and Visualization
                                                                          Pedher Johansson
                                                                      Department of Computer Science
                                                                                                       6                                  OpenGL Introduction
                                                                                                                                     Computer Graphics and Visualization
                                                                                                                                                                               Pedher Johansson
                                                                                                                                                                           Department of Computer Science
     OpenGL State                                                                                         Lack of Object Orientation


      OpenGL functions are of two types
          Primitive generating                                                                             OpenGL is not object oriented so that there are
             – Can cause output if primitive is visible                                                    multiple functions for a given logical function
             – How vertices are processed and appearance of
               primitive are controlled by the state                                                         glVertex3f
                                                                                                             glVertex2i
          State changing
                                                                                                             glVertex3dv
             – Transformation functions
             – Attribute functions




7                                  OpenGL Introduction
                              Computer Graphics and Visualization
                                                                        Pedher Johansson
                                                                    Department of Computer Science
                                                                                                     8                                 OpenGL Introduction
                                                                                                                                  Computer Graphics and Visualization
                                                                                                                                                                            Pedher Johansson
                                                                                                                                                                        Department of Computer Science




     OpenGL                                                                                               A Simple Program

                                 function name                                                             Generate a square on a solid background
                                                                      dimensions

                       glVertex3f(x,y,z)


                                                   x,y,z are floats
      belongs to GL library


               glVertex3fv(p)

                                                   p is a pointer to an array


9                                  OpenGL Introduction
                              Computer Graphics and Visualization
                                                                        Pedher Johansson
                                                                    Department of Computer Science
                                                                                                     10                                OpenGL Introduction
                                                                                                                                  Computer Graphics and Visualization
                                                                                                                                                                            Pedher Johansson
                                                                                                                                                                        Department of Computer Science




     simple.c                                                                                             Event Loop
        #include <GL/glut.h>
        void display(){
            glClear(GL_COLOR_BUFFER_BIT);
                                                                                                               Note that the program defines a display callback
            glBegin(GL_POLYGON);
                glVertex2f(-0.5, -0.5);                                                                        function named display
                glVertex2f(-0.5, 0.5);                                                                         Every glut program must have a display callback
                glVertex2f(0.5, 0.5);
                glVertex2f(0.5, -0.5);                                                                         The display callback is executed whenever OpenGL
            glEnd();                                                                                           decides the display must be refreshed, for example
            glFlush();                                                                                         when the window is opened
        }
                                                                                                               The main function ends with the program entering
        int main(int argc, char** argv){
            glutCreateWindow("simple");                                                                        an event loop
            glutDisplayFunc(display);
            glutMainLoop();
        }
11                                 OpenGL Introduction
                              Computer Graphics and Visualization
                                                                        Pedher Johansson
                                                                    Department of Computer Science
                                                                                                     12                                OpenGL Introduction
                                                                                                                                  Computer Graphics and Visualization
                                                                                                                                                                            Pedher Johansson
                                                                                                                                                                        Department of Computer Science
     GTK+ and Glade                                                                                      GLUT and the Given GUI

         Glade is a RAD (Rapid Application Development)
         tool to develope user interfaces for the GTK+ toolkit.                                              GUI is initialized by calling guiInit and
         The user interfaces are saved as XML                                                                initGuiWindow
           – Using the libglade library these can be loaded                                                  Callback functions from the GUI exists in
             dynamically.                                                                                    project1gui.c
           – Can be used in many languages including C, C++,                                                 GTK also uses a main loop, but
             Java, Perl, Python, C#, Pike, Ruby, Haskell,
                                                                                                                – GLUT main loop calls an idle callback function.
             Objective Caml and Scheme.
                                                                                                                – GTK main loop content can be called separately.
                                                                                                             Call guiMainIteration from the GLUT idle
       GladeXML *xml = glade_xml_new(                                                                        callback.
          "project1gui.glade", NULL, NULL);


13                                OpenGL Introduction
                             Computer Graphics and Visualization
                                                                       Pedher Johansson
                                                                   Department of Computer Science
                                                                                                    14                                OpenGL Introduction
                                                                                                                                 Computer Graphics and Visualization
                                                                                                                                                                           Pedher Johansson
                                                                                                                                                                       Department of Computer Science




     Given GUI HOWTO                                                                                     OpenGL Examples



        Compile and link project1gui.c
        Use the callback functions to
        implement the features of your                                                                   http://www.cs.umu.se/kurser/5DV009/HT09/projects/intro/
        program.
        GUI can also display simple text in
        its status bar (see
        project1gui.h).



15                                OpenGL Introduction
                             Computer Graphics and Visualization
                                                                       Pedher Johansson
                                                                   Department of Computer Science
                                                                                                    16                                OpenGL Introduction
                                                                                                                                 Computer Graphics and Visualization
                                                                                                                                                                           Pedher Johansson
                                                                                                                                                                       Department of Computer Science

								
To top