Docstoc

Objects

Document Sample
Objects Powered By Docstoc
					 Computer Graphics
- Graphics Programming -



     Hanyang University

        Jong-Il Park
Essential Functions in API
 Objects
 Viewer
 Light sources
 Material properties




                        Division of Electrical and Computer Engineering, Hanyang University
Objects
 Defined by
    Sets of vertices
    Relationship between a list of vertices and the object

 Relationship
    Simple relationship
       Line segments, rectangles, polygons, …

    Complex relationship
       Circle defined by 3 points or center and one point

 Primitive
    Points, line segments, polygons, text …




                            Division of Electrical and Computer Engineering, Hanyang University
Eg. Triangle in OpenGL



 glBegin(GL_POLYGON);
   glVertex3f(0.0, 0.0, 0.0);
   glVertex3f(0.0, 1.0, 0.0);
   glVertex3f(0.0, 0.0, 1.0);
 glEnd();




                     Division of Electrical and Computer Engineering, Hanyang University
 Viewer = Virtual Camera

 Necessary specifications
    Position
    Orientation
    Focal length
    Film plane




                         Division of Electrical and Computer Engineering, Hanyang University
 Eg. Viewer in OpenGL

  Independence
      Viewer  Objects
    cf. Classical viewing techniques in architecture



gluLookAt(cop_x, cop_y, cop_z, at_x, at_y, at_z, …);
      // position and orientation
gluPerspective(field_of_view, aspect_ratio …);
      // lens, field of view..



                         Division of Electrical and Computer Engineering, Hanyang University
Perspective vs. Orthographic


      Perspective projection




      Orthographic projection
                               Division of Electrical and Computer Engineering, Hanyang University
Viewport




      glViewport(x,y,w,h)

                  Division of Electrical and Computer Engineering, Hanyang University
 Modeling-Rendering Paradigm




                                          M+A+R
 Modeling software                                     3D Studio
 Animation software                                    SOFTIMAGE
                                                        MAYA …
 Rendering software                      M+R
                                                        Rhinoceros
                                                        solidThinking …
                                          A+R
                                                        ArcRender …
                       Division of Electrical and Computer Engineering, Hanyang University
 Graphics Architecture
 Early graphics systems




 Display processors




                           Division of Electrical and Computer Engineering, Hanyang University
Pipeline Architectures


 Principle




 Throughput
    The rate at which the data flows through the system

 Latency
    The time for a datum to pass through the system




                          Division of Electrical and Computer Engineering, Hanyang University
Geometric Pipeline


 Geometric processing
   Transformation
      Concatenating

   Clipping
   Projection

 Rasterization = scan-conversion




                         Division of Electrical and Computer Engineering, Hanyang University
OpenGL API




 Graphics functions
   Primitive functions
   Attribute functions
   Viewing functions
   Transformation functions
   Input functions
   Control functions




                         Division of Electrical and Computer Engineering, Hanyang University
OpenGL Pipeline




             Division of Electrical and Computer Engineering, Hanyang University
OpenGL Data Types

 For portability
 Eg. glVertex2f()           f: suffix standing for GLfloat

     Suffix     Typical C/C++               OpenGL type name
       b          signed char                         GLbyte
       s             short                           GLshort
       i          int or long                   GLint, GLsizei
       f             float                           GLfloat
       d            double                          GLdouble
      ub        unsigned char                        GLubyte
      us        unsigned short                      GLushort
       ui     unsigned int or long             GLuint, GLenum


                                 Division of Electrical and Computer Engineering, Hanyang University
Primitives and Attributes
 Points and line-segment types




              glBegin(type);
                       glVertex*(…);
                       …
                       glVertex*(…);
              glEnd();
                          Division of Electrical and Computer Engineering, Hanyang University
• Polygon types




                  Division of Electrical and Computer Engineering, Hanyang University
Application Structure
 Configure and open window
 Initialize OpenGL state
 Register input callback functions
   render
   resize
   input: keyboard, mouse, etc.

 Enter event processing loop




                         Division of Electrical and Computer Engineering, Hanyang University
“Hello World” Example

                                                      #include <stdio.h>
#include <stdio.h>
#include <GL/glut.h>
void display(void)
{
        glClear( GL_COLOR_BUFFER_BIT);
        glColor3f(0.0, 1.0, 0.0);
        glBegin(GL_POLYGON);
                                                      #include <GL/glut.h>
          glVertex3f(2.0, 4.0, 0.0);
          glVertex3f(8.0, 4.0, 0.0);
          glVertex3f(8.0, 6.0, 0.0);
          glVertex3f(2.0, 6.0, 0.0);
        glEnd();
        glFlush();
                                                      int main(int argc, char **argv)
}
int main(int argc, char **argv)
{                                                     {
                                                          printf("hello world\n");
       printf("hello world\n");
       glutInit(&argc, argv);
       glutInitDisplayMode ( GLUT_SINGLE | GLUT_RGB
       | GLUT_DEPTH);
                                                                                                 Initialize GLUT and
       glutInitWindowPosition(100,100);                                                       processes command line
       glutInitWindowSize(300,300);
       glutCreateWindow ("square");                                                                      options
       glClearColor(0.0, 0.0, 0.0, 0.0);
       glMatrixMode(GL_PROJECTION);                       glutInit(&argc, argv);
       glLoadIdentity();
       glOrtho(0.0, 10.0, 0.0, 10.0, -1.0, 1.0);
       glutDisplayFunc(display);
       glutMainLoop();

}
       return 0;
                                                          glutInitDisplayMode (GLUT_SINGLE |
                                                                       GLUT_RGB | GLUT_DEPTH);
                                                          Division of Electrical and Computer Engineering, Hanyang University
glutInitDisplayMode();


  RGB      red, green, and blue, typically 8 bits per pixel                      GLUT_RGB

           alpha or accumulation buffer; used for compositing
   A                                                                             GLUT_RGBA
           images

                                                                                 GLUT_DEPT
   Z       depth value, used for Z-buffer visibility tests
                                                                                 H

 double                                                                          GLUT_DOUB
           extra copy of all buffers, used for smooth animation
 buffer                                                                          LE

 stencil                                                                         GLUT_STEN
           several extra bits, useful in compositing images
 buffer                                                                          CIL



                                    Division of Electrical and Computer Engineering, Hanyang University
“Hello World” Example (2)

#include <stdio.h>
#include <GL/glut.h>
                                                  • Set window position, size, and name
void display(void)
{                                                   And create a window
        glClear( GL_COLOR_BUFFER_BIT);
        glColor3f(0.0, 1.0, 0.0);
        glBegin(GL_POLYGON);
          glVertex3f(2.0, 4.0, 0.0);
                                                     glutInitWindowPosition(100,100);
          glVertex3f(8.0, 4.0, 0.0);
          glVertex3f(8.0, 6.0, 0.0);
          glVertex3f(2.0, 6.0, 0.0);
                                                     glutInitWindowSize(300,300);
        glEnd();
        glFlush();                                   glutCreateWindow ("square");
}
int main(int argc, char **argv)

                                                  • Clear color (white) and
{
       printf("hello world\n");
       glutInit(&argc, argv);

                                                    Set the viewer
       glutInitDisplayMode ( GLUT_SINGLE | GLUT_RGB
       | GLUT_DEPTH);
       glutInitWindowPosition(100,100);
       glutInitWindowSize(300,300);
       glutCreateWindow ("square");
       glClearColor(0.0, 0.0, 0.0, 0.0);
       glMatrixMode(GL_PROJECTION);
                                                      glClearColor(0.0, 0.0, 0.0, 0.0);
       glLoadIdentity();
       glOrtho(0.0, 10.0, 0.0, 10.0, -1.0, 1.0);      glMatrixMode(GL_PROJECTION);
       glutDisplayFunc(display);
       glutMainLoop();                                glLoadIdentity();
                                                      glOrtho(0.0, 10.0, 0.0, 10.0, -1.0, 1.0);
       return 0;
}




                                                                                   * GL_MODELVIEW
                                                        Division of Electrical and Computer Engineering, Hanyang University
“Hello World” Example (3)
                                                      • Display call-back function
#include <stdio.h>                                       glClear( GL_COLOR_BUFFER_BIT); // screen clear
#include <GL/glut.h>
void display(void)
                                                         glColor3f(0.0, 1.0, 0.0); // set the color to G
{                                                        glBegin(GL_POLYGON); // draw
        glClear( GL_COLOR_BUFFER_BIT);
        glColor3f(0.0, 1.0, 0.0);
                                                           glVertex3f(2.0, 4.0, 0.0);
        glBegin(GL_POLYGON);
          glVertex3f(2.0, 4.0, 0.0);
                                                           glVertex3f(8.0, 4.0, 0.0);
          glVertex3f(8.0, 4.0, 0.0);                       glVertex3f(8.0, 6.0, 0.0);
                                                           glVertex3f(2.0, 6.0, 0.0);
          glVertex3f(8.0, 6.0, 0.0);
          glVertex3f(2.0, 6.0, 0.0);
        glEnd();
        glFlush();                                       glEnd();
}                                                        glFlush();      // send all output to display
int main(int argc, char **argv)
{
       printf("hello world\n");
       glutInit(&argc, argv);
       glutInitDisplayMode ( GLUT_SINGLE | GLUT_RGB
       | GLUT_DEPTH);
       glutInitWindowPosition(100,100);
       glutInitWindowSize(300,300);                   • glutDisplayFunc() registers the call-back
                                                        function.
       glutCreateWindow ("square");
       glClearColor(0.0, 0.0, 0.0, 0.0);
       glMatrixMode(GL_PROJECTION);
       glLoadIdentity();
       glOrtho(0.0, 10.0, 0.0, 10.0, -1.0, 1.0);
                                                      • glutMainLoop() hands execution control
       glutDisplayFunc(display);
       glutMainLoop();                                  over to the glut library
       return 0;
}
                                                       glutDisplayFunc(display);
                                                       glutMainLoop();
                                                          Division of Electrical and Computer Engineering, Hanyang University
Run and you get




              Division of Electrical and Computer Engineering, Hanyang University
 Summary
 Essential Components

 Main program  main() function
 Display callback function
 Optionally, interaction function
   Mouse callback function
   Keyboard callback function
   Reshape callback function




                            Division of Electrical and Computer Engineering, Hanyang University
Interaction
 glutMouseFunc(myMouse)
   registers myMouse() with the event that occurs when the
     mouse button is pressed or released
 glutMotionFunc(myMovedMouse)
   registers myMovedMouse() with the event that occurs
     when the mouse is moved while one of the buttons is
     pressed
 glutKeyboardFunc(myKeyboard)
   Registers myKeyboard() with the event that occurs when
     a keyboard key is pressed




                          Division of Electrical and Computer Engineering, Hanyang University
  Eg. Placing Dots with Mouse

  Leftbutton  draw a dot                        void drawDot(GLint x, GLint y)
  Rightbutton  terminate                        {
                                                          glBegin(GL_POINTS);
                                                           glVertex2i(x,y);
                                                          glEnd();
                                                 }

void myMouse(int button, int state, int x, int y)
{
        if(button == GLUT_LEFT_BUTTON &&
            state == GLUT_DOWN)
                   drawDot(x,screenHeight –y);
        else if(button == GLUT_RIGHT_BUTTON &&
            state == GLUT_DOWN)
                   exit(-1);
}
                                   Division of Electrical and Computer Engineering, Hanyang University
Pen-Plotter Model



moveto(0,0);
lineto(1,0);
lineto(1,1);
lineto(0,1);
lineto(0,0);



               •Used in early graphics systems
               •LOGO, GKS, PostScript…

                  Division of Electrical and Computer Engineering, Hanyang University
Homework #3
                                                                [Due: 12 Oct.]


 2D Graphics Programming [OpenGL]
    Open a window with 480 lines x 640 pixels
    Build your own paint program capable of drawing dots,
     lines, line strips, line loop, …
    Interaction: Use the mouse and the keyboard
    Hint: Follow the methods in Chap.3 of the textbook


 Read Chap.4




                          Division of Electrical and Computer Engineering, Hanyang University

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:0
posted:6/11/2013
language:English
pages:28
wang nianwu wang nianwu http://
About wangnianwu