本帖最后由 hopedark 于 2009-10-17 11:04:32 编辑

解决方案 »

  1.   

    在菜单的函数menu里面你只是调用键盘的处理,而没有设置a的值,默认a=0,当然不会出现菜单了;
    在void menu(int item) 里面加上
    switch(item) 
    {
    case 1:
    a=1;
    break;
    case 2:
    a=2;
    break;
    }
      

  2.   

    楼上的朋友,我把程序改成这样了:
    void keyboard(unsigned char key, int x, int y)
    {
    switch(key)
    {
    case 'q' | 'Q' :
    exit(0);
    break;
    case VK_ESCAPE :
    exit(0);
    break;

    case 'z':
    case 'Z':
    viewer[0] -= 0.1;
    break;
    case 'c':
    case 'C':
    viewer[0] += 0.1;
    break;
    case 's':
    case 'S':
    viewer[1] -= 0.1;
    break;
    case 'x':
    case 'X':
    viewer[1] += 0.1;
    break;
    case 'a':
    case 'A':
    viewer[2] -= 0.1;
    break;
    case 'd':
    case 'D':
    viewer[2] += 0.1;
    break;
    case 1:
    a=1;//这里改了,原来用的指针
    break;
    case 2:
    a=2;//这里改了,原来用的指针
    break; 
    default:
    break;
    }
    glutPostRedisplay();
    }
    这样不行,然后又把这里找你说的这样改了,也是不行
    void menu(int item)
    {
    switch(item)
    {
    case 1:
    a=1;
    break;
    case 2:
    a=2;
    break;
    }

    keyboard((unsigned char)item,0,0);

    }
      

  3.   

    void menu(int item)里
    用keyboard((unsigned char)item,0,0); 做键盘处理就不用了吧,难道你选中菜单用键盘控制?
    还有glutMotionFunc是在按下鼠标后移送鼠标才用的,你这我看用glutMouseFunc就可以了还有上面的代码有点问题,应该是这样,呵呵
    void menu(int item) 

    switch(item) 

    case 0: 
    a=1; 
    break; 
    case 1: 
    a=2; 
    break; 
    } 以上是我的建议,如果还不行,我也没办法了
      

  4.   

    在  ChangeSize(int w, int h)  函数 中
    if (a=1) 
        { 
          if(w <=h)  
              ......
        } 
      
      
        else if (a=2) 
        { 
          if(w <=h) 
               ...... 
        } 
      
    if 语句 中 你是 要赋值 还是 判断?
      

  5.   

    LS说的是一方面,但是不是主要问题所在,LZ光设置了同赢方式,但是没有进行模型视图等矩阵变换,下面是我调好的代码,请LZ好好看一下:#include <windows.h> 
    #include <gl/gl.h> 
    #include <gl/glu.h> 
    #include <gl/glut.h> 
    #include <winUser.h> 
    #include <math.h> static float  viewer[3]; int *pointer,a=1; 
    int g_w,g_h;
    static bool mouseLeftDown; 
    static float point[2][2]; 
    static float theta; 
    void RenderScene(void) 

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

        glMatrixMode(GL_MODELVIEW); 
        glLoadIdentity(); 

        gluLookAt(viewer[0], viewer[1], viewer[2], 0, 0, 0, 0, 1, 0); 
        glRotatef(theta,0,1,0); 
        GLfloat vertices[8][3] = {{-1,-1,1},{-1,1,1},{1,1,1},{1,-1,1},{-1,-1,-1},{-1,1,-1},{1,1,-1},{1,-1,-1}}; 
        GLfloat colors[8][3] = {{0,0,1},{0,1,1},{1,1,1},{1,0,1},{0,0,0},{0,1,0},{1,1,0},{1,0,0}}; 

        glBegin(GL_QUADS); 
    glColor3fv(colors[0]); glVertex3fv(vertices[0]); 
    glColor3fv(colors[3]); glVertex3fv(vertices[3]); 
    glColor3fv(colors[2]); glVertex3fv(vertices[2]); 
    glColor3fv(colors[1]); glVertex3fv(vertices[1]); 

    glColor3fv(colors[2]); glVertex3fv(vertices[2]); 
    glColor3fv(colors[3]); glVertex3fv(vertices[3]); 
    glColor3fv(colors[7]); glVertex3fv(vertices[7]); 
    glColor3fv(colors[6]); glVertex3fv(vertices[6]); 

    glColor3fv(colors[3]); glVertex3fv(vertices[3]); 
    glColor3fv(colors[0]); glVertex3fv(vertices[0]); 
    glColor3fv(colors[4]); glVertex3fv(vertices[4]); 
    glColor3fv(colors[7]); glVertex3fv(vertices[7]); 

    glColor3fv(colors[1]); glVertex3fv(vertices[1]); 
    glColor3fv(colors[2]); glVertex3fv(vertices[2]); 
    glColor3fv(colors[6]); glVertex3fv(vertices[6]); 
    glColor3fv(colors[5]); glVertex3fv(vertices[5]); 

    glColor3fv(colors[4]); glVertex3fv(vertices[4]); 
    glColor3fv(colors[5]); glVertex3fv(vertices[5]); 
    glColor3fv(colors[6]); glVertex3fv(vertices[6]); 
    glColor3fv(colors[7]); glVertex3fv(vertices[7]); 

    glColor3fv(colors[5]); glVertex3fv(vertices[5]); 
    glColor3fv(colors[4]); glVertex3fv(vertices[4]); 
    glColor3fv(colors[0]); glVertex3fv(vertices[0]); 
    glColor3fv(colors[1]); glVertex3fv(vertices[1]); 
    glEnd(); 
        glutSwapBuffers(); 

    void mouseMotion(int x, int y) 

    if(mouseLeftDown) 
        { 
            double viewport[4]; 
            glGetDoublev(GL_VIEWPORT,viewport); 
            point[1][0] = x / (float)viewport[2] * 500; 
            point[1][1] = (viewport[3] - y ) / (float)viewport[3]*500; 
        } 

        glutPostRedisplay(); 
    } void timer(int value) 

        theta += 2.0; 
        if(theta >= 360.0) 
            theta -= 360.0; 
        glutPostRedisplay(); 
        glutTimerFunc(1000/30,timer,1); 
    } void init(void) 


        viewer[0] = 2.5; 
        viewer[1] = 2.5; 
        viewer[2] = 2.5; 

    void keyboard(unsigned char key, int x, int y) 

        switch(key) 
        { 
        case 'q' | 'Q' : 
            exit(0); 
            break; 
        case VK_ESCAPE : 
            exit(0); 
            break; 

        case 'z': 
        case 'Z': 
            viewer[0] -= 0.1; 
            break; 
        case 'c': 
        case 'C': 
            viewer[0] += 0.1; 
            break; 
        case 's': 
        case 'S': 
            viewer[1] -= 0.1; 
            break; 
        case 'x': 
        case 'X': 
            viewer[1] += 0.1; 
            break; 
        case 'a': 
        case 'A': 
            viewer[2] -= 0.1; 
            break; 
        case 'd': 
        case 'D': 
            viewer[2] += 0.1; 
            break; 
        default: 
            break; 
        } 
        glutPostRedisplay(); 

    void SetupRC(void) 

        glClearColor(1.0f,1.0f,1.0f,1.0f ); 
        glEnable(GL_DEPTH_TEST); 
    } void ChangeSize(int w, int h) 

        if(h==0) 
            h=1; 
        glViewport(0,0,w,h); 

        glMatrixMode(GL_PROJECTION); 
        glLoadIdentity();  g_w=w;g_h=h; if(w <=h)  
    glOrtho(-2.0 , 2.0, -2.0*(float)h/(float)w, 2.0*(float)h/(float)w, 1.0, 10.0 ); 
    else 
    glOrtho(-2.0*(float)w/(float)h, 2.0*(float)w/(float)h, -2.0, 2.0, 1.0, 10.0 ); 
    } void menu(int item) 

        switch(item) 

    case 1: 
    glViewport (0, 0, (GLsizei)(g_w), (GLsizei)(g_w)); // 设置窗口可见区为窗口大小
    glMatrixMode (GL_PROJECTION); // 设置投影矩阵
    glLoadIdentity (); // 重置它

    if(g_w <=g_h) 
    glOrtho(-2.0 , 2.0, -2.0*(float)g_h/(float)g_w, 2.0*(float)g_h/(float)g_w, 1.0, 10.0 ); 
    else 
    glOrtho(-2.0*(float)g_w/(float)g_h, 2.0*(float)g_w/(float)g_h, -2.0, 2.0, 1.0, 10.0 ); 
    glMatrixMode (GL_MODELVIEW); // 设置为模型变换矩阵
    glLoadIdentity ();
    break; 
    case 2: 
    glViewport (0, 0, (GLsizei)(g_w), (GLsizei)(g_w));
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();

    if(g_w <=g_h) 
    glFrustum(-2.0 , 2.0, -2.0*(float)g_h/(float)g_w, 2.0*(float)g_h/(float)g_w, 1.0, 10.0 ); 
    else 
    glFrustum(-2.0*(float)g_w/(float)g_h, 2.0*(float)g_w/(float)g_h, -2.0, 2.0, 1.0, 10.0 ); 
    glMatrixMode (GL_MODELVIEW); // 设置为模型变换矩阵
    glLoadIdentity ();
    break; 
    }
    glutPostRedisplay(); 
    } void main(int argc, char* argv[]) 
    {
    pointer = &a;     glutInit(&argc, argv); 
        glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); 
        glutInitWindowSize(600,600); 
        glutCreateWindow("Simple");  init(); 
        SetupRC();     glutDisplayFunc(RenderScene); 

        glutMotionFunc(mouseMotion); 
        glutKeyboardFunc(keyboard); 

        glutCreateMenu(menu); 
        glutAddMenuEntry("Ortho",1); 
        glutAddMenuEntry("Frustum",2); 
        glutAttachMenu(GLUT_RIGHT_BUTTON);     glutReshapeFunc(ChangeSize); 

        glutMainLoop(); 
    }
      

  6.   

    主要是 menu函数,ChangeSize函数
    以及两个变量的定义g_w,g_h,a赋了初值=1;
      

  7.   

    啊,我menu函数出了那么大的问题,看了我学的东西还有很多需要加固啊。
    但是楼上的朋友,我还是有一点疑问,void keyboard(unsigned char key, int x, int y) 函数是只接受键盘的指令吗?如果是只接受键盘的指令的话。那么将他改成下面这样。用鼠标点击菜单时候依然能退出。
    void keyboard(unsigned char key, int x, int y)
    {
    switch(key)
    {
    case 'q' | 'Q' :
    exit(0);
    break;
    case VK_ESCAPE :
    exit(0);
    break;case 'z':
    case 'Z':
    viewer[0] -= 0.1;
    break;
    case 'c':
    case 'C':
    viewer[0] += 0.1;
    break;
    case 's':
    case 'S':
    viewer[1] -= 0.1;
    break;
    case 'x':
    case 'X':
    viewer[1] += 0.1;
    break;
    case 'a':
    case 'A':
    viewer[2] -= 0.1;
    break;
    case 'd':
    case 'D':
    viewer[2] += 0.1;
    break;
    case 1:
    exit(0);//这里改了
    break;
    case 2:
    exit(0);//这里改了
    break;
    default:
    break;
    }
    glutPostRedisplay();