C++ 在opengl中转换另一个图形中的图形

C++ 在opengl中转换另一个图形中的图形,c++,c,opengl,glut,C++,C,Opengl,Glut,我希望能够通过按键盘上的一些键,将较小的立方体转换为较大的立方体 以下是我为完成此任务所做的尝试: 初始值y x=0,y=0,z=0,原点=0,并且在范围内是全局的 void key_board(unsigned char key, int xx, int yy){//call back func for the glutKeyboardFunc switch (key){ case 'x': if(origin >= 0 &&

我希望能够通过按键盘上的一些键,将较小的立方体转换为较大的立方体

以下是我为完成此任务所做的尝试:

初始值y x=0,y=0,z=0,原点=0,并且在范围内是全局的

void key_board(unsigned char key, int xx, int yy){//call back func for the glutKeyboardFunc
      switch (key){
        case 'x':
          if(origin >= 0 && opposite >= 0 ){
            opposite = 1 - size -origin;
            x +=opposite; 
            break;
          }
        case 'y':
          if(origin >= 0 && opposite >= 0 ){
            opposite = 1 - size -origin;
            y +=opposite; 
            break;
          }
        case 'z':
          if(origin >= 0 && opposite >= 0 ){
            opposite = 1 - size -origin;
            z +=opposite; 
            break;
          }
      }
}

void solid_cube(double size){//this is the cube i would like to translate within the larger one,only perpendicular translation to the wall of bigger box are allowed

    glPushMatrix();
    glLineWidth(1.7f);
    glShadeModel(GL_SMOOTH);
    glColor3f(1.0f,0.0f,1.0f);
    glTranslatef(x, y, z);
    glutSolidCube(size);

  }

void display(){//display call back func

    glClear(GL_COLOR_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glShadeModel(GL_SMOOTH);
    gluPerspective(30.0, 4.0/3.0, 0.1f, 10.0);
    glFrustum(3.0, 5.0, 3.0, 3.0, 5.0, 10.0);
    gluLookAt(2.0,0,2.0,0.0,0.0,0.0,1.0,1.0,1.0);
    glLineWidth(1.7f);
    glColor3f(0.0f,0.0f,1.0f);
    glutWireCube(1.0); 
    solid_cube(0.3);//smaller cube to be moved around
    glutSwapBuffers();


}

此代码中存在许多问题:

  • 调用
    glPushMatrix()
    时没有相应的
    glPopMatrix()
    。顾名思义,这些调用管理一个矩阵堆栈。每个推送操作必须与pop操作匹配,否则堆栈将很快溢出。要解决此问题,请在
    solid\u cube()函数的末尾添加缺少的调用。
    函数:

    ...
    glutSolidCube(size);
    glPopMatrix();
    
  • 调用
    glupperspective()
    glFrustum()
    。这两个电话的目的相同
    glFrustum()
    支持设置常规视景台
    gluPerspective()
    是一个简化的方便界面,可连接到
    glFrustum()
    ,仅支持对称视锥。这两个调用都将新指定的投影矩阵与当前矩阵相乘。如果两者都有,你会得到一个投影的投影,这是。。。不是你想要的东西。您只需删除此代码中的
    glFrustum()
    调用即可

  • 投影矩阵通常应在
    GL\u投影
    matrix模式下设置。因此,转换设置调用的顺序应为:

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(30.0, 4.0/3.0, 0.1f, 10.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(2.0,0,2.0,0.0,0.0,0.0,1.0,1.0,1.0);
    
    这只需要一次,因此您也可以将其移动到设置代码中,而不是在每次重新显示时重复


  • gluLookAt()
    的参数看起来也有些不寻常。它们不是非法的,但您可能仍然需要再次检查,以验证它是否真的是您想要的。

    x、
    y
    z
    的值在哪里,初始化为什么?所有
    和是全局的
    大小
    恰好是
    1
    相反=1-大小-原点
    将计算为
    0
    并且没有发生任何事情。我现在简单地说,我的尺码将始终低于
    1