OpenGL xforms

Document Sample
OpenGL xforms Powered By Docstoc
					Understanding of OpenGL
   Transformations and Projections




            TA: Dong Hyun Jeong
    Instructor : Dr. Kalpathi Subramanian



                                            1
Purpose
• Short overview of OpenGL
• Understanding of basic knowledge about
  OpenGL
• Short discussion on how to use OpenGL
  including transformations and projections




                                              2
 What is OpenGL?
• Software Interface for 3D Graphic Hardware (Instruction sets)
• Interactive 2D and 3D graphics application programming
  interface (API)
• Support high visual quality and performance
   – CAD/CAM, entertainment, medical imaging, virtual reality, Etc.
• Developer-driven advantages
   – Industry standard, stable, reliable and portable, easy to use ……




                                                                        3
Supports
• Operating systems
  – Mac OS, OS/2, UNIX, Windows 95/98, NT, 2000,
    Linux, OPENStep, BeOS
• Windowing systems
  – Win32, MacOS, Presentation Manager, X-Window
    System
• Callable from
  – C, C++, Ada, Fortran, Python, Perl, Java




                                                   4
Flexibility and Extensions
• Has flexibility of defining a particular OpenGL
  implementation depending on systems
• OpenGL has extensions (API)
     – GLU, GLX, WGL (defined by vendors)

       UNIX APPLICATION                      WINDOWS APPLICATION


                              GLU                                        GLU



    Xlib        GLX       OpenGL             GDU          WGL       OpenGL

•     OpenGL applications use the window system’s window, input, and event
      mechanism
•     GLU supports quadrics (2D curve), NURBS, complex polygons, matrix utilities,   5

      and more
Several Advanced API
• Open Inventor
  – Supports cross-platform user interface and flexible
    scene graph


• IRIS Performer
  – Visual simulation and virtual sets (demanding high
    frame rate)



• OpenGL Optimizer
  – Real-time interaction, modification, and rendering of
    complex surface-based models (CAD/CAM, special
    effects)                                                6
Several Advanced API
• OpenGL Volumizer
  – High-level immediate mode volume rendering
    API (energy, medical and sciences)




• OpenGL Shader
  – Realistic visual effects,
    bump mapping,
    multiple textures, etc.
                                                 7
Viewport and Viewport Clipping
• Projecting 3D objects onto 2D screen (CG process
  pipeline)
                                     Transform
      Vectorize         Clip        To viewport
                                                      Draw
• Viewport
   – A two-dimensional (2D) rectangle into which a 3D scene is
     projected
• Vecterize
   – Representing 3D objects as line or plane pieces of objects or
     elements
• Clipping (Clip)
   – Remove the not interested regions from the vecterized elements


                                                                      8
Viewport and Viewport Clipping
• Clipping Volume
  – Physical coordinate or Global coordinate to represent
    the 3D objects. glOrtho(), gluPerspective()
• Viewport
  – Plane regions to map the physical coordinate into
    windows’ pixel coordinate. glViewport()
• Viewing transform
  – Changing user’s viewing angle to represent the 3d
    objects. glTranslated(), glRoated(), glMatrixMode(),
    gluLookAt()

                                                            9
Displaying Objects




                     10
Drawing a triangle on screen
1. Clear the screen
2. Create a triangle
3. Determine the location and direction of
   the triangle
4. Change the viewing angle
5. Define a projection method



                                             11
Clear the screen
• Remove the frame buffer
  – glClearColor(0.0,0.0,0.0,0.0);
  – glClear(GL_COLOR_BUFFER_BIT);




                                     12
Create a triangle
• A triangle as a
  geometry object
void DrawTriangle()
{
  glColor3f(1.0f,0.0f,0.0f);
  glBegin(GL_TRIANGLES);
  glVertex3f(0.0f,0.0f,0.0f);
  glVertex3f(1.0f,0.0f,0.0f);
  glVertex3f(0.0f,1.0f,0.0f);
  glEnd();
}




                                13
Positioning
• Determine the
                                           Y
  location and direction
  of the triangle                                  Rotate
  – glTranslatef(1.0f,1.0f,0.0f);
  – glRotatef(45.0f,0.0f,1.0f,0.0f);
  – DrawTriangle();
                                               Translate

                                                            X




                                       Z

                                                                14
Viewing angle
• Change the viewing                       Y

  angle

  – glMatrixMode(GL_PROJECTION);
  – glLoadIdentity();
  – glOrtho(-1.0f,1.0f,-1.0f,1.0f,-
    1.0f,10.0f);                               X
  – glMatrixMode(GL_MODELVIEW);
  – gluLookAt(1.5f,1.5f,1.0f,1.5f,1.5f,-
    0.5f, 0.0f,1.0f,0.0f);

                                     Z


                                                   15
                                        Y

Projection
• Determine the projection
  – Perspective Projection
     • glFrustum(-1.0f,1.0f,-                                 X
       1.0f,1.0f,1.0f,100.0f);


  – Orthographic Projection          Perspective Projection
     • glOrtho(-1.0f,1.0f,-      Z
       1.0f,1.0f,0.0f,100.0f);



                                     Orthographic Projection


                                                                  16
Geometry elements I
                                                                                      v4
                                                 v4        v0          v5
• Points                v0
                                            v3                  v1          v2
                                                                                 v3


      – GL_POINTS            v1        v2                                             v4
                                                           v0          v5
• Lines                                                     v1             v2
                                                                                 v3


      – GL_LINES, GL_LINE_STRIP,                                                      v4
        GL_LINE_LOOP                                       v0          v5        v3
                                                                v1
• Triangles                                                                 v2


      – GL_TRIANGLES, GL_TRIANGLES_STRIP,
        GL_TRIANGLE_FAN
                                                                 v5
                   v4                                                            v4
v0      v5              v0        v2                  v0
              v3                            v4                              v3
                                                                                       17
 v1      v2              v1        v3                  v1             v2
                                             v5
Geometry elements II
• Quadric
  – GL_QUADS, GL_QUAD_STRIP
         v3     v5                                        v7
   v0         v4             v6                   v3
                                            v0          v5          v6
    v1        v2     v7                      v1
                                                       v2      v4
• Polygon
  – GL_POLYGON

                        v1        v2
               v0                           v3
                   v5                  v4
                                                                         18
Basic restrictions
• All have to be plane objects


         Valid             Invalid



• No halls
• Edges do not have to be crossing



                                     19
Creating complex objects
• Complex objects using connected lines




• Complex objects using connected triangles




                                          20
Vertex transformation




                        21
Viewing
• Positioning a object
• Change the viewing
  angle
• Projection and
  transformation
• Viewport and
  coordinate
  determination


                         22
Modeling Transformation
• Place object and change the viewing angle

• glMatrixMode(GL_MODELVIEW)
• glLoadIdentity() – Identify matrix is called
   – glTranslatef(x,y,z)
   – glRotatef(degree,x,y,z)
   – glScalef(x,y,z)




                 Translate       Rotate          Scale   23
Viewing Transformation
• Changes the position and orientation of the viewpoint
• Generally it consists of translations and rotations.
   – gluLookAt()




                                                          24
Projection Transformations
• Defining a viewing volume
  – determines how an object is projected onto the
    screen

  – glMatrixMode(GL_PROJECTION);
  – glLoadIdentity();
  – glFrustum(left,right, bottom,top, near, far);
  – glOrtho(left,right, bottom,top, near, far);


                                                    25
Perspective Projection I




• glFrustum(left, right, bottom, top, near, far);
• (left, right, bottom, top) - near clipping plane
• near and far give the distances from the viewpoint to the
  near and far clipping planes.
• Easy to understand, but not intuitive to use
                                                              26
Perspective Projection II




• gluPerspective(fovy, aspect, near, far);
• Creates a matrix for a symmetric perspective-
  view frustum
                                                  27
Orthographic Projection




• the viewing volume is a rectangular
  parallelepiped
• glOrtho(left, right, bottom, top, near, far);
                                                  28
Viewport Transformation
• The viewport is the rectangular region of
  the window where the image is drawn
• Measured in window coordinates, which
  reflect the position of pixels on the screen
  relative to the lower left corner of the
  window.
  – glViewport(x, y, width, height);



                                                 29
Aspect Ratio
• Mapping the Viewing Volume to the
  Viewport




• Displayed image can be distorted
  depending on the ratio (w/h)
                                      30
Matrix Stacks
                                           Pushing and Popping the Matrix Stack




  Modelview and Projection Matrix Stacks


• A stack of matrices is useful for constructing hierarchical
  models, in which complicated objects are constructed
  from simpler ones.
   –   glMatrixMode()
   –   glLoadMatrix(), glMultMatrix()
   –   glPushMatrix(), glPopMatrix()
   –   glLoadIdentity()
                                                                          31
Matrix Stacks Example
                                                                          (0.0, 0.0, 0.0)



                                              glPushMatrix()
                            (0.0, 0.0, 0.0)


CreateTriangle         glTranslatef()                            CreateCircle
                                              glLoadIdentity()




         (0.0, 0.0, -5.0)




                                               (0.0, 0.0, 0.0)
                                                                                            32
CreateCircle
                                              CreateCircle
How to program?
• Need OpenGL Extensions
  – Win32 OS: WGL. Prefix starting with wgl. (brief explanation at
    MSDN)
  – X Windows: GLX
  – Apple Macintosh: AGL
  – OS/2: PGL
  – Each extensions supports making windows-based applications
    depending on operating systems.
  – But, it has restrictions of broadly using in other operating systems.




                                                                       33
What is GLUT?
• OpenGL Utility Toolkit
   – A window system independent toolkit for writing OpenGL
     programs.
   – Works across all PC and workstation OS platforms
   – http://www.opengl.org/resources/libraries/glut.html
   – Better than AUX (Auxiliary) library
   – Need to locate the header and library files when using Windows
     Operating System (not obligation, but useful when making
     OpenGL applications)
          – (*.dll) C:\WINDOWS\system32
      • Vs6 :
          – (*.h) C:\Program Files\Microsoft Visual Studio\VC98\Include\GL
          – (*.lib) C:\Program Files\Microsoft Visual Studio\VC98\Lib
      • .net
          – (*.h) C:\Program Files\Microsoft Visual Studio .NET
            2003\Vc7\PlatformSDK\Include\gl
          – (*.lib) C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\PlatformSDK\Lib


                                                                                             34
How to use GLUT?
•   Initialization functions
     –   void glutInit(int *argc, char **argv);
•   Initialize display mode
     –   glutInitDisplayMode(unsigned int mode);
           •   GLUT_RGB, GLUT_SINGLE, GLUT_DOUBLE
•   Set the initial size of the window
     –   glutInitWindowSize(int width, int height);
•   The initial position of the window
     –   glutInitWindowPosition(int x, int y);
•   Creates the window
     –   int glutCreateWindow(char *name);
•   Specifying the function that needs to be called (callback function)
     –   void glutDisplayFunc(void (*func)(void));
•   Redraw the current window
     –   void glutPostRedisplay(void);
•   A callback function called when window size is changed
     –   void glutReshapeFunc(void (*func)(int width, int height));

•   More info. at “An introduction to GLUT”, http://mindfuck.de-brauwer.be/articles/glut/


                                                                                            35
A simple example
#include<gl\glut.h>                               void reshape(int w, int h)
                                                  {
void DrawTriangle()                                  glViewport(0,0,w,h);
{                                                    glMatrixMode(GL_PROJECTION);
     glBegin(GL_TRIANGLES);                          glLoadIdentity();
     glColor3f(1.0f, 0.0f, 0.0f); // red             glOrtho(-1.0, 1.0, -1.0, 1.0, 0.0, 100.0);
     glVertex2f(0.0f, 0.0f);                         glMatrixMode(GL_MODELVIEW);
     glColor3f(0.0f, 1.0f, 0.0f); // green           glLoadIdentity();
     glVertex2f(1.0f, 0.0f);                         gluLookAt(1.5f, 1.5f, 1.0f, 1.5f, 1.5f, -0.5f,
     glColor3f(0.0f, 0.0f, 1.0f); // blue                       0.0f, 1.0f, 0.0f);
     glVertex2f(0.0f, 1.0f);                      }
     glEnd();                                     void init()
}                                                 { // Gouraud Shading
void display()                                        glShadeModel(GL_SMOOTH);
{                                                 }
     glClearColor(0.0, 0.0, 0.0, 0.0); // black
     glClear(GL_COLOR_BUFFER_BIT);                void main(int argc, char** argv)
     glPushMatrix(); // push modelview matrix     {
     glTranslatef(1.0f, 1.0f, 0.0f);                 glutInit(&argc, argv);
     glRotatef(45.0f, 0.5f, 1.0f, 0.5f);             glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
     DrawTriangle();                                 glutInitWindowPosition(100,100);
     glPopMatrix(); // pop modelview matrix          glutInitWindowSize(400,400);
     glFlush();                                      glutCreateWindow("OpenGL EXAMPLE");
}                                                    init();
                                                     glutReshapeFunc(reshape);
                                                     glutDisplayFunc(display);
                                                     glutMainLoop();
                                                  }                                               36
Transformation Example I
#include "glut.h"                                                   void Key1(int key, int x, int y)
                                                                    {
                                                                           switch(key)
static int year = 0, day = 0;                                              {
void display(void)                                                         case GLUT_KEY_LEFT:
{                                                                                      year = (year - 5) % 360;
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);                              break;
        glColor3f (1.0, 1.0, 1.0);                                         case GLUT_KEY_RIGHT:
        glPushMatrix();                                                                year = (year + 5) % 360;
        glColor3f(1.0, 0.0, 0.0);                                                      break;
        glutSolidSphere(1.0, 10, 10);/* draw sun */                        case GLUT_KEY_UP:
        glRotatef((GLfloat) year, 0.0, 1.0, 0.0);                                      day = (day + 10) % 360;
        glTranslatef (2.0, 0.0, 0.0);                                                  break;
        glRotatef((GLfloat) day, 0.0, 1.0, 0.0);                           case GLUT_KEY_DOWN:
        glColor3f(0.0, 0.0, 1.0);                                                      day = (day - 10) % 360;
        glutSolidSphere(0.2, 10, 10);/* draw smaller planet */                         break;
        glPopMatrix();                                                     }
        glFlush();                                                         glutPostRedisplay();
}                                                                   }
void myinit(void)                                                   void main(int argc, char** argv)
{                                                                   {
        glShadeModel(GL_SMOOTH);                                       glutInit(&argc, argv);
        glEnable(GL_DEPTH_TEST);                                       glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
}                                                                      glutInitWindowPosition(100,100);
void myReshape(GLsizei w, GLsizei h)                                   glutInitWindowSize(400,400);
{                                                                      glutCreateWindow("OpenGL EXAMPLE");
        glViewport(0, 0, w, h);                                        myinit();
        glMatrixMode(GL_PROJECTION);                                   glutReshapeFunc(myReshape);
        glLoadIdentity();                                              glutDisplayFunc(display);
        gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);      glutSpecialFunc(Key1);
        glMatrixMode(GL_MODELVIEW);                                    glutMainLoop();
        glLoadIdentity();                                           }
        glTranslatef (0.0, 0.0, -5.0);
}


                                                                                                                      37
Display List
• OpenGL display lists are           buildCircle()
  designed to optimize               {
  performance                             GLint i;
                                          GLfloat cosine, sine;
                                          glNewList(MY_CIRCLE_LIST,
drawCircle()                                  GL_COMPILE);
{                                         glBegin(GL_POLYGON);
     GLint i;                             for(i=0;i<100;i++){
     GLfloat cosine, sine;                      cosine=cos(i*2*PI/100.0);
                                                sine=sin(i*2*PI/100.0);
     glBegin(GL_POLYGON);
                                                glVertex2f(cosine,sine);
     for(i=0;i<100;i++){                  }
         cosine=cos(i*2*PI/100.0);        glEnd();
         sine=sin(i*2*PI/100.0);          glEndList();
         glVertex2f(cosine,sine);    }
     }
    glEnd();                         glCallList(MY_CIRCLE_LIST);
}

                                                                            38
Transformation Example II
#include "glut.h"                           void myReshape(GLsizei w, GLsizei h)
                                            {
GLuint listName = 1;                              glViewport(0, 0, w, h);
void display(void)                                glMatrixMode(GL_PROJECTION);
{                                                 glLoadIdentity();
      glClear(GL_COLOR_BUFFER_BIT);               if (w <= h)
      GLuint i;                                               gluOrtho2D (0.0, 2.0, -0.5 * (GLfloat) h/(GLfloat)
      glColor3f(0.0, 1.0, 0.0);                   w,1.5 * (GLfloat) h/(GLfloat) w);
      for (i = 0; i < 10; i++)                    else
                   glCallList (listName);                     gluOrtho2D (0.0, 2.0 * (GLfloat) w/(GLfloat) h, -0.5,
      glFlush ();                                 1.5);
}                                                 glMatrixMode(GL_MODELVIEW);
void myinit(void)                                 glLoadIdentity();
{                                           }
      glShadeModel(GL_SMOOTH);              void main(int argc, char** argv)
      glNewList (listName, GL_COMPILE);     {
      glColor3f(1.0, 0.0, 0.0);                glutInit(&argc, argv);
      glBegin (GL_TRIANGLES);                  glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
      glVertex2f (0.0, 0.0);                   glutInitWindowPosition(100,100);
      glVertex2f (1.0, 0.0);                   glutInitWindowSize(400,400);
      glVertex2f (0.0, 1.0);                   glutCreateWindow("OpenGL EXAMPLE 2");
      glEnd ();                                myinit();
      glTranslatef (1.2, 0.0, 0.0);            glutReshapeFunc(myReshape);
      glEndList ();                            glutDisplayFunc(display);
}                                              glutMainLoop();
                                            }




                                                                                                                      39
Shading model
• Flat shading
  – glShadeModel(GL_FLAT)


• Smooth (Gouraud) shading
  – glShadeModel(GL_SMOOTH)




                              40
Light source
• Ambient (GL_AMBIENT)
    – The light from that source that's been scattered so much by the
      environment
• Diffuse (GL_DIFFUSE)
    – Comes from one direction
• Specular (GL_SPECULAR)
    – Comes from a particular direction

GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);

                                                                        41
Material Properties
• Define the material properties of the
  objects in the scene:
  – GL_AMBIENT, GL_DIFFUSE,
    GL_AMBIENT_AND_DIFFUSE,
    GL_SPECULAR, Etc.
     • GLfloat mat_amb_diff[] = { 0.1, 0.5, 0.8, 1.0 };
     • glMaterialfv(GL_FRONT_AND_BACK,
       GL_AMBIENT_AND_DIFFUSE, mat_amb_diff);



                                                          42
Light Example I
#include "glut.h"                                                                        void main(int argc, char** argv)
                                                                                         {
void display(void)                                                                          glutInit(&argc, argv);
{                                                                                           glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);                                 glutInitWindowPosition(100,100);
        glutSolidSphere(1.0, 50, 50);                                                       glutInitWindowSize(500,500);
        glFlush();                                                                          glutCreateWindow("OpenGL EXAMPLE 3");
}                                                                                           myinit();
void myinit(void)                                                                           glutReshapeFunc(myReshape);
{                                                                                           glutDisplayFunc(display);
        GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };                                    glutMainLoop();
        GLfloat mat_shininess[] = { 50.0 };                                              }
        GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
        glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
        glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
        glLightfv(GL_LIGHT0, GL_POSITION, light_position);
        glEnable(GL_LIGHTING);
        glEnable(GL_LIGHT0);
        glEnable(GL_DEPTH_TEST);
}
void myReshape(GLsizei w, GLsizei h)
{
        glViewport(0, 0, w, h);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        if (w <= h)
                      glOrtho (-1.5, 1.5, -1.5*(GLfloat)h/(GLfloat)w,
        1.5*(GLfloat)h/(GLfloat)w, -10.0, 10.0);
        else
                      glOrtho (-1.5*(GLfloat)w/(GLfloat)h,1.5*(GLfloat)w/(GLfloat)h, -
        1.5, 1.5, -10.0, 10.0);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
}




                                                                                                                                           43
Light Example II
#include "glut.h"                                                    void Key1(int key, int x, int y)
                                                                     {
static int spin = 0;                                                         switch(key)
void display(void)                                                           {
{                                                                            case GLUT_KEY_LEFT:
         GLfloat position[] = { 0.0, 0.0, 1.5, 1.0 };                                     spin = (spin + 30) % 360;
         glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);                             break;
         glPushMatrix ();                                                    }
         glTranslatef (0.0, 0.0, -5.0);                                      glutPostRedisplay();
         glPushMatrix ();                                            }
         glRotated ((GLdouble) spin, 1.0, 0.0, 0.0);                 void main(int argc, char** argv)
         glRotated (0.0, 1.0, 0.0, 0.0);                             {
         glLightfv (GL_LIGHT0, GL_POSITION, position);                  glutInit(&argc, argv);
         glTranslated (0.0, 0.0, 1.5);                                  glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
         glDisable (GL_LIGHTING);                                       glutInitWindowPosition(100,100);
         glColor3f (0.0, 1.0, 1.0);                                     glutInitWindowSize(500,500);
         glutWireCube(0.1);                                             glutCreateWindow("OpenGL EXAMPLE 4");
         glEnable (GL_LIGHTING);                                        myinit();
         glPopMatrix ();                                                glutReshapeFunc(myReshape);
         glutSolidSphere(0.9, 50, 50);                                  glutDisplayFunc(display);
         glPopMatrix ();                                                      glutSpecialFunc(Key1);
         glFlush ();                                                    glutMainLoop();
}                                                                    }
void myinit(void)
{
         glEnable(GL_LIGHTING);
         glEnable(GL_LIGHT0);
         glEnable(GL_DEPTH_TEST);
}
void myReshape(GLsizei w, GLsizei h)
{
         glViewport(0, 0, w, h);
         glMatrixMode(GL_PROJECTION);
         glLoadIdentity();
         gluPerspective(40.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
         glMatrixMode(GL_MODELVIEW);}




                                                                                                                       44
Light Example III
#include "glut.h"                                                                        void Mouse(int button, int state, int x, int y)
                                                                                         {
GLfloat diffuseMaterial[4] = { 0.5, 0.5, 0.5, 1.0 };                                            if (state == GLUT_DOWN)
void display(void)                                                                              {
{                                                                                                             switch (button)
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);                                                   {
        glutSolidSphere(1.0, 50, 50);                                                                         case GLUT_LEFT_BUTTON :
        glFlush();                                                                                                                    diffuseMaterial[0] += 0.1;
}                                                                                                                                     if (diffuseMaterial[0] > 1.0)
void myinit(void)                                                                                                                                            diffuseMaterial[0] = 0.0;
{                                                                                                                                     glColor4fv(diffuseMaterial);
        GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };                                                                              break;
        GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };                                                    case GLUT_RIGHT_BUTTON :
        glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuseMaterial);                                                                          diffuseMaterial[1] += 0.1;
        glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);                                                                            if (diffuseMaterial[1] > 1.0)
        glMaterialf(GL_FRONT, GL_SHININESS, 25.0);                                                                                                           diffuseMaterial[1] = 0.0;
        glLightfv(GL_LIGHT0, GL_POSITION, light_position);                                                                            glColor4fv(diffuseMaterial);
        glEnable(GL_LIGHTING);                                                                                                        break;
        glEnable(GL_LIGHT0);                                                                                  case GLUT_MIDDLE_BUTTON :
        glEnable(GL_DEPTH_TEST);                                                                                                      diffuseMaterial[2] += 0.1;
        glColorMaterial(GL_FRONT, GL_DIFFUSE);                                                                                        if (diffuseMaterial[2] > 1.0)
        glEnable(GL_COLOR_MATERIAL);                                                                                                                         diffuseMaterial[2] = 0.0;
}                                                                                                                                     glColor4fv(diffuseMaterial);
void myReshape(GLsizei w, GLsizei h)                                                                                                  break;
{                                                                                                             }
        glViewport(0, 0, w, h);                                                                 }
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();                                                                        glutPostRedisplay();
        if (w <= h)                                                                      }
                      glOrtho (-1.5, 1.5, -                                              void main(int argc, char** argv)
        1.5*(GLfloat)h/(GLfloat)w,1.5*(GLfloat)h/(GLfloat)w, -10.0, 10.0);               {
        else                                                                                glutInit(&argc, argv);
                      glOrtho (-1.5*(GLfloat)w/(GLfloat)h,1.5*(GLfloat)w/(GLfloat)h, -      glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
        1.5, 1.5, -10.0, 10.0);                                                             glutInitWindowPosition(100,100);
        glMatrixMode(GL_MODELVIEW);                                                         glutInitWindowSize(500,500);
        glLoadIdentity();                                                                   glutCreateWindow("OpenGL EXAMPLE 5");
}                                                                                           myinit();
                                                                                            glutReshapeFunc(myReshape);
                                                                                            glutDisplayFunc(display);
                                                                                            glutMouseFunc(Mouse);
                                                                                            glutMainLoop();                                                                         45
                                                                                         }
Complicated Examples




                       46
Complex Model I
          • Does it look complicated?
            – Only use simple shape models
              such as sphere, rectangle
              boxes, octagon, etc.
          • Configurations
            – Torso, Hip, Shoulder,
              RocketPod, UpperArm,
              ForeAm, UpperLeg, Foot,
              VulcanGun, Building models
          • Smooth animation
            – Only transformation functions
                                              47
Complex Model II
                     • Environment
                       – Loading the tunnel model
                       – Add texture images
                       – For navigation pre-calculated
                         camera route used
                     • Text
                       – glfont library used




 Environmental map
                                                     48
Complex Model III
              • Loading models
                – Use user-defined file
                  format
              • Additional Features
                – Collision detection
                – Fog effects (OpenGL
                  support)




                                          49
Complex Model IV
                  • Loading models
                      – Importing Wavefront OBJ
                        file to display
                      – *.obj file includes vertex,
                        texture coordinate, and face
                        numbers

     Rendered using 3DS MAX
     without texture



                         Rendered using 3DS MAX
                         with texture
                                                   50
Useful Website for OpenGL



• OpenGL Official site: http://www.opengl.org
• SGI website: http://www.sgi.com
• OpenGL documentations:
  – http://www.opengl.org/documentation/
• Tutorials
  – http://flipcode.com/tutorials/
• GLUT
  – http://www.opengl.org/resources/libraries/glut.html

                                                          51

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:155
posted:6/26/2012
language:English
pages:51