【发布时间】:2014-09-30 15:32:03
【问题描述】:
我希望能够通过按键盘上的一些键将一个较小的立方体转换成一个较大的立方体。
以下是我的尝试:
最初 x = 0,y = 0,z = 0,origin = 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();
}
【问题讨论】:
-
x、y和z的值在哪里初始化? -
所有
zero并且是全球性的 -
如果
size恰好是1,opposite = 1 - size -origin;将评估为0并且没有任何反应 -
我现在很简单,这样我的尺寸将永远低于
1