2013-03-27 97 views
0

我在修改時遇到了困難。我想添加2個固體(在這裏稱爲手指)在一起,以便當按下'o'或'c'鍵時它們可以打開和關閉。我無法得到正確的座標,所以他們可以放在靠近綠色的物體。所以它會看起來像一個用兩根手指的手。用3d物體正確設置座標的座標

這裏是所需的輸出http://www.tinyuploads.com/gallery/view/zO6rYg

#include <cstdlib> 
#include <cmath> 
#include <vector> 
#include <iostream> 
#ifdef __APPLE__ 
#include <GLUT/glut.h> 
//#include <gl.h> 
//#include <glut.h> 
#else 
# include <GL/glut.h> 
#endif 
using namespace std; 
#define ESC 27 
static GLfloat red[]={1,0,0}, green[]={0,1,0}, blue[]={0,0,1}; 
static int shoulder=0, elbow=0, finger1 = 0, finger2 = 0; 
static int xrot=0, yrot=0, zrot=0; 
static GLfloat xshift=-1.0, yshift=0.0, zoom=-3.0; 

/* some basic GL initialization */ 
void init() 
{ 
    glClearColor(1.0,1.0,1.0,0.0); 
    glShadeModel(GL_FLAT); 
    glEnable(GL_DEPTH_TEST); 
    glEnable(GL_CULL_FACE); 
    glCullFace(GL_BACK); 
} 

/* The display callback */ 
/* Put the "fingers" code somewhere in this function*/ 
void draw_stuff() 
{ 
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); 
    glLoadIdentity(); 
    glTranslatef(0.0,0.0,-5.0); 
    glPushMatrix(); 
    glTranslatef(xshift,yshift,zoom); 
    glRotatef((GLfloat)xrot,1.0,0.0,0.0); 
    glRotatef((GLfloat)yrot,0.0,1.0,0.0); 
    glRotatef((GLfloat)zrot,0.0,0.0,1.0); 

    glColor3fv(red); 
    glPushMatrix(); 
    glTranslatef(-1.0,0.0,0.0); 
    glRotatef((GLfloat)shoulder,0.0,0.0,1.0); 
    glTranslatef(1.0,0.0,0.0); 
    glPushMatrix(); 
    glScalef(2.0,0.4,1.0); 
    glutSolidCube(1.0); 
    glPopMatrix(); 

    glColor3fv(green); 
    glTranslatef(1.0,0.0,0.0); 
    glRotatef((GLfloat)elbow,0.0,0.0,1.0); 
    glTranslatef(1.0,0.0,0.0); 
    glPushMatrix(); 
    glScalef(2.0,0.4,1.0); 
    glutSolidCube(1.0); 
    glPopMatrix(); 

    glColor3fv(blue); 
    glTranslatef(1.0,0.2,0.0); 
    glRotatef((GLfloat)finger1,0.0,0.0,1.0); 
    glRotatef((GLfloat)finger2,0.0,0.0,1.0); 
    glTranslatef(1.0,0.0,0.0); 
    glPushMatrix(); 
    glScalef(2.0,0.4,1.0); 
    glutSolidCube(0.25); 
    glPopMatrix(); 

    glColor3fv(blue); 
    glTranslatef(-1.0,0.0,1.0); 
    glRotatef((GLfloat)finger1,0.0,0.0,1.0); 
    glRotatef((GLfloat)finger2,0.0,0.0,1.0); 
    glTranslatef(1.0,0.0,0.0); 
    glPushMatrix(); 
    glScalef(2.0,0.4,1.0); 
    glutSolidCube(0.25); 
    glPopMatrix(); 

    glPopMatrix(); 
    glPopMatrix(); 
    glutSwapBuffers(); 
} 

/* the window-reshape callback */ 
void reshape(int w, int h) 
{ 
    GLfloat myFov = 65.0, myNear = 1.0, myFar = 30.0; 
    glViewport(0,0,(GLsizei)w,(GLsizei)h); 
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    gluPerspective(myFov,(GLfloat)w/(GLfloat)h, myNear, myFar); 
    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 
} 

/* The basic keyboard controls, callback for glutKeyboardFunc() */ 
void kb(unsigned char key, int x, int y) 
{ 
    switch(key) 
    { 
     case 's': 
      shoulder=(shoulder+5)%360; 
      glutPostRedisplay(); 
      break; 
     case 'S': 
      shoulder=(shoulder-5)%360; 
      glutPostRedisplay(); 
      break; 
     case 'e': 
      elbow=(elbow+5)%360; 
      glutPostRedisplay(); 
      break; 
     case 'E': 
      elbow=(elbow-5)%360; 
      glutPostRedisplay(); 
      break; 

     case 'C' |'c': 
      /* close the "hand" */ 
      finger1=(finger1+5)%360; 
      finger2=(finger2+5)%360; 
      glutPostRedisplay(); 
      break; 
     case 'O' | 'o': 
      /* open the "hand" */ 
      finger1=(finger1-5)%360; 
      finger2=(finger2-5)%360; 
      glutPostRedisplay(); 
      break; 
     case 'x': 
      xshift+=.25; 
      glutPostRedisplay(); 
      break; 
     case 'X': 
      xshift-=.25; 
      glutPostRedisplay(); 
      break; 
     case 'y': 
      yshift+=.25; 
      glutPostRedisplay(); 
      break; 
     case 'Y': 
      yshift-=.25; 
      glutPostRedisplay(); 
      break; 
     case 'r': 
     case 'R': 
      xrot=yrot=zrot=shoulder=elbow=0; 
      xshift=-1.0; 
      yshift=0.0; 
      zoom=-3.0; 
      glutPostRedisplay(); 
      break; 
     case 'z': 
      zrot=(zrot-1)%360; 
      glutPostRedisplay(); 
      break; 
     case 'Z': 
      zrot=(zrot+1)%360; 
      glutPostRedisplay(); 
      break; 
     case ESC: 
      exit(0); 
      break; 
     default: 
      break; 
    } 
} 

/* The "special" keyboard controls, callback for glutSpecialFunc() */ 
void skb(int key, int x, int y) 
{ 
    switch(key) 
    { 
     case GLUT_KEY_UP: 
      zoom+=1; 
      glutPostRedisplay(); 
      break; 
     case GLUT_KEY_DOWN: 
      zoom-=1; 
      glutPostRedisplay(); 
      break; 
     case GLUT_KEY_RIGHT: 
      yrot=(yrot-1)%360; 
      glutPostRedisplay(); 
      break; 
     case GLUT_KEY_LEFT: 
      yrot=(yrot+1)%360; 
      glutPostRedisplay(); 
      break; 
     case GLUT_KEY_PAGE_UP: 
      xrot=(xrot-1)%360; 
      glutPostRedisplay(); 
      break; 
     case GLUT_KEY_PAGE_DOWN: 
      xrot=(xrot+1)%360; 
      glutPostRedisplay(); 
      break; 
     default: 
      break; 
    } 
} 


void printInteraction(void) 
{ 


    cout << "s/S: Positive/negative shoulder rotation" << endl; 
    cout << "e/E: Positive/negative elbow rotation" << endl 
    << "O/C: Open/Close the hand -- YOU GET TO IMPLEMENT THIS" << endl 
    << "x/X: Positive/negative X-axis shift of the model" << endl 
    << "y/Y: Positive/negative Y-axis shift of the model" << endl 
    << "UP/DOWN ARROWS: (zoom) Z-axis shift of the model" << endl 
    << "LEFT/RIGHT ARROWS: Y-axis rotations" << endl 
    << "PAGE UP/DOWN: X-axis rotations" << endl 
    << "ESC: exit" << endl; 
} 

int main(int argc, char **argv) 
{ 
    int winWidth = 800, winHeight = 600, winPosX = 100, winPosY = 100; // OpenGL window 
    printInteraction(); 
    glutInit(&argc,argv); 
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH); 
    glutInitWindowSize(600,400); 
    glutInitWindowPosition(0,0); 
    glutCreateWindow("window"); 
    init(); 
    glutDisplayFunc(draw_stuff); 
    glutReshapeFunc(reshape); 
    glutKeyboardFunc(kb); 
    glutSpecialFunc(skb); 
    glutMainLoop(); 
    return 0; 
} 

回答

2

鏈接你glPush/PopMatrix()調用序列顯示在你的模型缺乏層次結構。

應該是這樣的(我已經縮進僞代碼來說明這是怎麼回事):

glPushMatrix(); 
    // do stuff to position shoulder 
    // draw shoulder 
    glPushMatrix(); 
    // do stuff to position elbow 
    // draw elbow 
    glPushMatrix(); 
     // do stuff to position finger1 
     // draw finger1 
    glPopMatrix(); 
    glPushMatrix(); 
     // do stuff to position finger2 
     // draw finger2 
    glPopMatrix(); 
    glPopMatrix(); // elbow 
glPopMatrix(); // shoulder 

希望有所幫助。

+0

是啊我的glPush/PopMatrix()很時髦 – SeaCode 2013-03-29 13:37:11