计算机图形学(八)Modeling and Hierarchy

计算机图形学(八)Modeling and Hierarchy

1. Example I: Car Model

  • DAG Model
    • Directed acyclic graph 有向无环图
  • Modeling with Tree
    • Nodes: what to draw, pointers to children
    • Edges: may have information on incremental changes to transformation matrices (can also store in nodes)
  • 层级(hierarchical)

2. Example II: Robot Arm

  • Articulated Model 铰链模型

  • Possible Node Structure

    08-01 Possible Node Structure

  • OpenGL code for robot

    mat4 ctm;
    robot_arm()
    {
    ctm = RotateY(theta);
    base();
    ctm *= Translate(0.0, h1, 0.0) * RotateZ(phi);
      lower_arm();
    ctm *= Translate(0.0, h2, 0.0) * RotateZ(psi);
      upper_arm();
    }

3. Example III: Humanoid Figure

  • Tree with Matrices

    08-02 Tree with Matrices

  • Display and Traversal

    • Stack-based Traversal code
    figure() {
    mvstack.push( model_view );//save present model-view matrix 
    torso();
    //update model-view matrix for head
    model_view = model_view*Translate(...)*Rotate(...); 
    head();
    
    model_view = mvstack.pop();//recover original model-view matrix 
    mvstack.push( model_view );//save it again
    //update model-view matrix for left upper arm
    model_view= model_view*Translate(...)*Rotate(...); 
    left_upper_arm();
    
    model_view = mvstack.pop();//recover original model-view matrix again ......
    }
    • General Tree Data Structure

      • left-child right sibling structure

        • Used linked lists
        • Each node in data structure is two pointers
        • Left: next node
        • Right: linked list of children

        08-03 tree structure

      • Tree node structure

        • Pointer to sibling / child
        • Pointer to a function that draws the object represented by the node
        • Homogeneous coordinate matrix to multiply on the right of the current model-view matrix

4. OpenGL and Objects

  • OpenGL lacks an object orientation

5. Scene Graph

  • Scene graph

08-04 scene graph

08-05 Scene Graph Example

  • Group Nodes
    • Necessary to isolate state chages
      • Equivalent to Push/Pop
    • Note that as with the figure model
      • We can write a universal traversal algorithm
      • The order of traversal can matter
  • Events and the Scene Graph
    • It is not difficult to let any part of the scene graph vary
    • Events could be used to identify the part of the scene graph and how it is to change
      • Change geometry
      • Change transformations
      • Change appearance

6. Events in OpenGL

  • Event Processing in OpenGL

08-06 Event Processing

  • code example

    int main( int argc, char **argv){
        glutInit( &argc, argv); 
        glutInitWindowSize( 400, 400); 
        glutInitWindowPosition( 200, 100);
        glutCreateWindow( “Sample”);
    
        //specify callback functions
        glutDisplayFunc( display);
        glutIdleFunc( animation); 
        glutKeyboardFunc( kboard);
        glutSpecialFunc(special_key); 
        glutMouseFunc( mousef);
        glutMotionFunc( mousePos); 
        glutCreateMenu( menuf ); 
        glutTimerFunc(300, timer, -1); glutReshapeFunc(reshape);
    
        init(); 
        glutMainLoop();
        return 0;
    }
    
    timer() {
      ... ; 
      glutPostRedisplay(); 
      // call display() right now
    }
    
    void mousef(int button, int state, int x, int y){
      if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
        drag = true; 
      }
      if (button == GLUT_LEFT_BUTTON && state == GLUT_UP) { 
        xx = x/100.-2.; yy = 2.-y/100.;
        drag = false;
        glutPostRedisplay();
      }
    }
    
    void mousePos( int x, int y) { 
      if ( drag ) {
        xx = x/100.-2.; yy = 2.-y/100.;
        glutPostRedisplay(); 
      }
    }
    
    glutCreateMenu( menuf ); //callback menuf( int i) is called if triggered
    glutAddMenuEntry("Level 1: Beginner", 1);
    glutAddMenuEntry("Level 2: amateur", 2);
    glutAddMenuEntry("Level 3: Professional", 3);
    glutAttachMenu(GLUT_RIGHT_BUTTON);
    
    void  menuf( int i) { 
      if (i == 1) {
        ispeed = 0.002; rw = .125;
        }
        else if (i == 2) {
            ... 
      }
        else if (i == 3) { 
        ...
        }
        ...
    }
    
    //Create the main menu
    glutCreateMenu( mainf ); 
    glutAddMenuEntry("Restart", 1);
    //Link the sub-menu to the main menu 
    glutAddSubMenu("Set Level", levelMenu); 
    glutAddMenuEntry("Quit", 3); 
    glutAttachMenu( GLUT_RIGHT_BUTTON);
    //Create a sub-menu
    int levelMenu = glutCreateMenu( levelf); 
    glutAddMenuEntry("Learner", 1); 
    glutAddMenuEntry("Average", 2); 
    glutAddMenuEntry("Racing God", 3);
    
    //define Callback function for the main menu
    void mainf( int i) {
      if (i == 1) { restart(); . . . }
      else if (i == 3) { . . . }
      //If i == 2, levelf() will be called automatically
    }
    
    //define Callback for //the sub-menu 
    void levelf( int i) {
      if ( i == 1) . . .
      ... 
    }
  • MUI

    • MUI is a particular user interaction toolkit
    • It’s very simple, but very easy to use

  TOC