tutorial

Document Sample
tutorial Powered By Docstoc
					              OpenGL Tutorial
       CISC 640/440 Computer Graphics

               TA: Qi Li/Mani Thomas
       qili@cis.udel.edu/manivt@cis.udel.edu


02/17/05          CISC640/440 OpenGL Tutorial   1
                   Outline
•   Submission policy
•   OpenGL: Basics and setup
•   Draw geometry primitives
•   Transformations in OpenGL
•   Sample codes
•   Office hour


02/17/05        CISC640/440 OpenGL Tutorial   2
           Submission Policy
• How to submit
• What to submit
• On late submission




02/17/05      CISC640/440 OpenGL Tutorial   3
                       How to Submit
• Tar/zip file
     – Unix:
           > tar -cf lastname_projectNum.tar projectDir
           > gzip lastname_projectNum.tar
     – Windows:
           • get a winzip application (if you don't have)
           • go the directory storing your assignments
           • right click your mouse to zip the directory
• Submit the tar/zip file through the course web
  (More details will be announced later)
02/17/05                    CISC640/440 OpenGL Tutorial     4
                What to Submit
• Must contain
     – readme
     – Makefile
     – Source codes
     – Output figures (if any)
• Must NOT contain
     – obj intermediate files
     – obj data files

02/17/05            CISC640/440 OpenGL Tutorial   5
           What to Submit: Readme
  % My name
  % My email: myemail@udel.edu
  % Project Num
  %

  % Part 1: description of this project
   This project is to apply xxx algorithm to plot xxx, …

  % Part 2: what I did and what I didn't do
  I completed all/most/some functionalities required in this project.
  The system is robust and the rendering is fairly efficient, …

  I didn't do …. The reason is ….

  % Part 3: What files contained

  % Part 4: How to compile and how to run
  The project is developed in windows system and tested in stimpy (strauss) unix system, …




02/17/05                            CISC640/440 OpenGL Tutorial                              6
           What to Submit: Makefile for
                     EECIS
## Makefile for EECIS
## uncomment the following line for the C compiler(cc)
#CC = cc
CC = CC
TOP = /usr/local/mesa
# list the name of the executable you wish to produce
exec = hello
## list all your object files separated by spaces
objects = hello.o
## list all your header files separated by spaces
## includes = lines5.h lines5_2.h
      ############################################################
LINKDIR = -L$(TOP)/lib -L/usr/openwin/lib
INCLUDEDIR = $(TOP)/include
LLDLIBS = -lGL -lglut -lMesaGLU -lMesaGL -lm -lX11 -lXext -lXmu \ -lXt -lXi
      ############################################################
$(exec) : $(objects)
     $(CC) -o $@ $(LINKDIR) $(objects) $(LLDLIBS)
$(objects) : $(includes) $(CC) -c -I$(INCLUDEDIR) $<
.PHONY : clean
clean: -rm -f $(objects) $(exec)


02/17/05                             CISC640/440 OpenGL Tutorial              7
           What to Submit: Makefile for
                   Composer
## Makefile for Composer/Strauss
## uncomment the following line for the C compiler(cc)
#CC = cc
CC = CC
TOP = /home/base/usrb/chandrak/640/OpenGL/Mesa-2.6
# list the name of the executable you wish to produce
exec = hello
## list all your object files separated by spaces
objects = hello.o
## list all your header files separated by spaces
## includes = lines5.h lines5_2.h
      ############################################################
LINKDIR = -L$(TOP)/lib -L/usr/openwin/lib
INCLUDEDIR = $(TOP)/include
LLDLIBS = -lGL -lglut -lMesaGLU -lMesaGL -lm -lX11 -lXext -lXmu \ -lXt -lXi
      ############################################################
$(exec) : $(objects)
        $(CC) -o $@ $(LINKDIR) $(objects) $(LLDLIBS)
$(objects) : $(includes) $(CC) -c -I$(INCLUDEDIR) $<
.PHONY : clean
clean: -rm -f $(objects) $(exec)

02/17/05                         CISC640/440 OpenGL Tutorial                  8
           Late Submission
• N * 10 percent of the points you got
  deducted if there are N (<=5) late days
  (not counting weekends). No acceptance
  for the submission more than 5-day late
• Each student has three free (i.e. without
  any penalty) late days for entire semester.
  You should notify the TA the use of free
  late days ahead

02/17/05        CISC640/440 OpenGL Tutorial     9
                   Outline
•   Submission policy
•   OpenGL: Basics and setup
•   Draw geometry primitives
•   Transformations in OpenGL
•   Sample codes
•   Office hour


02/17/05        CISC640/440 OpenGL Tutorial   10
            OpenGL: What is It?
  • GL (Graphics Library): Library of 2-D, 3-D
    drawing primitives and operations
       – API for 3-D hardware acceleration
  • GLU (GL Utilities): Miscellaneous functions
    dealing with camera set-up and higher-level
    shape descriptions
  • GLUT (GL Utility Toolkit): Window-system
    independent toolkit with numerous utility
    functions, mostly dealing with user interface

02/17/05              CISC640/440 OpenGL Tutorial   11
               OpenGL: Setup in Unix
•     Unix (strongly recommended)
     1.    add the following command in your .cshrc
           •   EECIS
               setenv LD_LIBRARY_PATH /usr/local/mesa/lib:/usr/openwin/lib
           •   Composor
               setenv LD_LIBRARY_PATH
               /home/usra/usrb/chandrak/640/OpenGL/Mesa-2.6/lib
               :/usr/openwin/lib
     2. Download Makefile (for hello.c)
           •   Makefile_eecis
           •   Makefile_composor
     3. remote display: (If your local machine is not stimpy or strauss,
        you need to do the following)
           •   localmachine > ssh stimpy.cis.udel.edu
     4. Compile: stimpy> make
     5. Run:    stimpy > hello

02/17/05                     CISC640/440 OpenGL Tutorial                     12
      OpenGL: Setup in Windows
• Windows/VC++ 6.0
     – Get glut:
       http://www.xmission.com/~nate/glut.html
     – An excellent source for the setup of openGL
       on Windows/VC++ 6.0 is available at
           • http://www.lighthouse3d.com/opengl/glut/index.php




02/17/05                CISC640/440 OpenGL Tutorial        13
      OpenGL: Setup in Windows
•     Windows (step 1: create new project)




02/17/05            CISC640/440 OpenGL Tutorial   14
      OpenGL: Setup in Windows
•     Windows (step 2: create new project)




02/17/05            CISC640/440 OpenGL Tutorial   15
      OpenGL: Setup in Windows
•     Windows (step 3: insert file)




02/17/05             CISC640/440 OpenGL Tutorial   16
      OpenGL: Setup in Windows
•     Windows (step 4: project setting)




02/17/05            CISC640/440 OpenGL Tutorial   17
      OpenGL: Setup in Windows
•     Windows (step 5: project setting)




02/17/05            CISC640/440 OpenGL Tutorial   18
                   Outline
•   Submission policy
•   OpenGL: Basics and setup
•   Draw geometry primitives
•   Transformations in OpenGL
•   Sample codes
•   Office hour


02/17/05        CISC640/440 OpenGL Tutorial   19
    OpenGL Geometric Primitives

            GL_LINES
                                                               GL_POLYGON
                       GL_LINE_STRIP       GL_LINE_LOOP
GL_POINTS



                GL_TRIANGLES

                                                  GL_QUADS
                                                             GL_QUAD_STRIP
GL_TRIANGLE_STRIP         GL_TRIANGLE_FAN



 02/17/05                CISC640/440 OpenGL Tutorial                    20
  Specifying Geometric Primitives
• Primitives are specified using
       glBegin(primType);
       ...
       glEnd();

  – primType determines how vertices are combined
        GLfloat red, green, blue;
        GLfloat x, y;

        glBegin(primType);
        for (i = 0; i < nVerts; i++) {
          glColor3f(red, green, blue);
          glVertex2f(x, y);
          ... // change coord. values
        }                                      21
        glEnd();
            OpenGL Vertex/Color
             Command Formats
                 glVertex3fv( v )
                 glColor3fv( v )



Number of               Data Type                 Vector
components         b    -   byte                omit “v” for
                   ub   -   unsigned byte
2 - (x,y)                                      scalar form–
                   s    -   short
3 - (x,y,z),
                   us   -   unsigned short         e.g.,
    (r,g,b)                                   glVertex2f(x, y)
                   i    -   int
4 - (x,y,z,w),                               glColor3f(r, g, b)
                   ui   -   unsigned int
    (r,g,b,a)
                   f    -   float
                                                             22
                   d    -   double
                   Outline
•   Policy
•   OpenGL: Basics and setup
•   Draw geometry primitives
•   Transformations in OpenGL
•   Sample codes
•   Office hour


02/17/05        CISC640/440 OpenGL Tutorial   23
           OpenGL 3-D coordinates
• Right-handed system
• From point of view of camera
  looking out into scene:
    +X right, -X left
    +Y up, -Y down
    +Z behind camera, -Z in front
• Positive rotations are counterclockwise
  around axis of rotation

02/17/05         CISC640/440 OpenGL Tutorial   24
           Transformations in OpenGl
• Modeling transformation
• Viewing transformation
• Projection transformation




02/17/05          CISC640/440 OpenGL Tutorial   25
             Modeling Transformation
• Refer to the transformation of models (i.e., the
  scenes, or objects)
• Generally,
     – glMultMatrixf(M_i)
• Some simple transformations
     – Translation: glTranslate(x,y,z)
     – Scale: glScale(sx,sy,sz)
     – Rotation: glRotate(theta, x,y,z)
           • x,y,z are components of vector defining axis of rotation
           • Angle in degrees; direction is counterclockwise

02/17/05                    CISC640/440 OpenGL Tutorial                 26
             Viewing Transformation
• Refer to the transformation on the camera
• Using glTranslate*() and glRotate*()
• Using gluLookAt()
     – gluLookAt (eyeX, eyeY, eyeZ, centerX,
       centerY, centerZ, upX, upY, upZ)
        • eye = (eyeX, eyeY, eyeZ)T: Desired camera
            position
           • center = (centerX, centerY, centerZ)T:
            Where camera is looking
           • up = (upX, upY, upZ)T: Camera’s “up” vector
02/17/05               CISC640/440 OpenGL Tutorial         27
           Viewing Transformation




                from Woo et al

02/17/05         CISC640/440 OpenGL Tutorial   28
           Projection Transformation
• Refer to the transformation from scene to
  image
• Orthographic projection
     – glOrtho (left, right, bottom, top, near, far)




                                              from Hill
02/17/05             CISC640/440 OpenGL Tutorial          29
           Projection Transformation
• Refer to the transformation from scene to
  image
• Orthographic projection
     – glOrtho (left, right, bottom, top, near, far)
• Perspective projection
     – glFrustum (left, right, bottom, top, near, far)



02/17/05             CISC640/440 OpenGL Tutorial         30
           Projection Transformation
• Refer to the transformation from scene to image
• Orthographic projection
     – glOrtho (left, right, bottom, top, near, far)
• Perspective projection
     – glFrustum (left, right, bottom, top, near, far)




02/17/05                CISC640/440 OpenGL Tutorial      31
   Notes on openGl transformations
• Before applying modeling or viewing
  transformations, need to set
           glMatrixMode(GL_MODELVIEW)
• Before applying projection transformations, need
  to set
           glMatrixMode(GL_Projection)
• Replacement by either following commands
           glLoadIdentity();
           glLoadMatrix(M);
• Multiple transformations (either in modeling or
  viewing) are applied in reverse order
02/17/05          CISC640/440 OpenGL Tutorial       32
   Notes on openGl transformations
• Before applying modeling or viewing
  transformations, need to set
           glMatrixMode(GL_MODELVIEW)
• Before applying projection transformations, need
  to set
           glMatrixMode(GL_Projection)
• Replacement by either following commands
           glLoadIdentity();
           glLoadMatrix(M);
• Multiple transformations (either in modeling or
  viewing) are applied in reverse order
02/17/05          CISC640/440 OpenGL Tutorial       33
                   Outline
•   Policy
•   OpenGL: Basics and setup
•   Draw 2-D primitives
•   Transformation in OpenGL
•   Sample codes
•   Office hour


02/17/05        CISC640/440 OpenGL Tutorial   34
                 Sample codes
•   void main(int, char**)
•   void init (void);
•   void reshape (int, int);
•   void display (void);
•   void menu (void);
•   void dda_line (int, int, int, int);
•   void midpoint_line (int, int, int, int);
•   void gl_line (int, int, int, int);
•   void mouse (int, int, int, int);
•   void keyboard (unsigned char, int, int);

02/17/05            CISC640/440 OpenGL Tutorial   35
             Sample code: main function
   int main (int argc, char **argv) {
     glutInit (&argc, argv);
     glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
     glutInitWindowSize (400, 400);
     glutInitWindowPosition (400, 350);
     glutCreateWindow ("Scan Conversion Algorithms");

       init ();

       glutDisplayFunc (display);
       glutReshapeFunc (reshape);
       glutMouseFunc (mouse);
       glutKeyboardFunc (keyboard);

       menu ();
       glutMainLoop ();
       return (0); /* ANSI C requires main to return int. */
   }
02/17/05                       CISC640/440 OpenGL Tutorial     36
                     Sample code: init
   void
   init (void)
   {
     /* select clearing color       */
     /* this color is white --- full red, green, and blue components */
     /* the last component is alpha(transparency) --- use zero */
     glClearColor (1.0, 1.0, 1.0, 0.0);
   }




02/17/05                     CISC640/440 OpenGL Tutorial                  37
                 Sample code: reshape
   void
   reshape (int w, int h)
   {
     /* This routine is called when the initial GL window is created
        and when the window is resized. It is followed in execution
        by the 'display' routine. */

       /* initialize viewing values */
       glViewport (0, 0, (GLint) w - 1, (GLint) h - 1);
       glMatrixMode (GL_PROJECTION);
       glLoadIdentity ();
       glOrtho (0.0, (GLdouble) w, 0.0, (GLdouble) h, -1.0, 1.0);
   }




02/17/05                       CISC640/440 OpenGL Tutorial             38
                  Sample code: display
   void
   display (void)
   {
     /* clear all pixels */
     glClear (GL_COLOR_BUFFER_BIT);

       /* don't wait!
        * start processing buffered OpenGL routines
        */
       glFlush ();
   }




02/17/05                      CISC640/440 OpenGL Tutorial   39
                  Sample code: gl_line
   void
   gl_line (int x0, int y0, int x1, int y1)
   {
     glColor3ub (0, 255, 0); /* green */
     glBegin (GL_LINES);
      glVertex2i (x0, y0);
      glVertex2i (x1, y1);
     glEnd ();
   }




02/17/05                       CISC640/440 OpenGL Tutorial   40
                 Sample code: mouse
   void
   mouse (int button, int state, int x, int y)
   {
     switch (button)
      {
      case GLUT_LEFT_BUTTON:
        if (state == GLUT_DOWN)
          printf ("x = %d, y = %d\n", x, y);
        break;
      case GLUT_RIGHT_BUTTON:
        if (state == GLUT_DOWN)
          exit (0);
        break;
      default:
        break;
      }
   }
02/17/05                       CISC640/440 OpenGL Tutorial   41
                   Outline
•   Policy
•   OpenGL: Basics and setup
•   Draw 2-D primitives
•   Transformation in OpenGL
•   Sample codes
•   Office hour


02/17/05        CISC640/440 OpenGL Tutorial   42
           Office hour of CISC640
• Monday 3:00-5:00pm Pearson 115B
• Email: qili@cis.udel.edu
• Webpage: http://www.cis.udel.edu/~qili/




02/17/05         CISC640/440 OpenGL Tutorial   43
           Office hour of CISC440
• Wed 5:00-7:00pm Pearson 115B
• Email: manivt@cis.udel.edu
• Webpage:
  http://www.cis.udel.edu/~manivt/




02/17/05         CISC640/440 OpenGL Tutorial   44

				
DOCUMENT INFO