【发布时间】:2020-12-02 04:42:11
【问题描述】:
当我运行我的程序并开始移动鼠标时,什么都没有发生。我工作得很好,但就像提到的那样,鼠标移动有问题。
我相信我的问题是它总是重置为中心但我不确定。
#define FPS 60
#define TO_RADIANS 3.14/180.0
float pitch = 0.0, yaw= 0.0;
float cX=0.0,cZ=0.0;
const int width = 16*50;
const int height = 9*50;
struct Motion
{
bool Forward,Backward,Left,Right;
}motion = {false,false,false,false};
void init()
{
glutSetCursor(GLUT_CURSOR_NONE);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glutWarpPointer(width/2,height/2);
}
void camera()
{
if(motion.Forward)
{
cX += cos((yaw+90)*TO_RADIANS)/5.0;
cZ -= sin((yaw+90)*TO_RADIANS)/5.0;
}
if(motion.Backward)
{
cX += cos((yaw+90+180)*TO_RADIANS)/5.0;
cZ -= sin((yaw+90+180)*TO_RADIANS)/5.0;
}
if(motion.Left)
{
cX += cos((yaw+90+90)*TO_RADIANS)/5.0;
cZ -= sin((yaw+90+90)*TO_RADIANS)/5.0;
}
if(motion.Right)
{
cX += cos((yaw+90-90)*TO_RADIANS)/5.0;
cZ -= sin((yaw+90-90)*TO_RADIANS)/5.0;
}
/*limit the values of pitch
between -60 and 70
*/
if(pitch>=70)
pitch = 70;
if(pitch<=-60)
pitch=-60;
glRotatef(-pitch,1.0,0.0,0.0); // Along X axis
glRotatef(-yaw,0.0,1.0,0.0); //Along Y axis
glTranslatef(-cX,0.0,-cZ);
}
void timer()
{
glutPostRedisplay();
glutWarpPointer(width/2,height/2);
glutTimerFunc(1000/FPS,timer,0);
}
void keyboard(unsigned char key,int x,int y)
{
switch(key)
{
case 'W':
case 'w':
motion.Forward = true;
break;
case 'A':
case 'a':
motion.Left = true;
break;
case 'S':
case 's':
motion.Backward = true;
break;
case 'D':
case 'd':
motion.Right = true;
break;
}
}
void keyboard_up(unsigned char key,int x,int y)
{
switch(key)
{
case 'W':
case 'w':
motion.Forward = false;
break;
case 'A':
case 'a':
motion.Left = false;
break;
case 'S':
case 's':
motion.Backward = false;
break;
case 'D':
case 'd':
motion.Right = false;
break;
}
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(width, height);
glutCreateWindow("Jameson Marzak FPS");
glutFullScreen();
sky[0] = LoadTexBMP("sky0.bmp");
sky[1] = LoadTexBMP("sky1.bmp");
sky[2] = LoadTexBMP("grass.bmp");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
//glutPassiveMotionFunc(passive_motion);
glutTimerFunc(0,timer,0); //more info about this is given below at definition of timer()
glutKeyboardFunc(keyboard);
glutKeyboardUpFunc(keyboard_up);
glutMainLoop();
return 0;
}
【问题讨论】:
-
我没有看到任何处理鼠标移动事件的代码,那么它应该如何移动
-
提示:如果您有
M_PI,请使用它,而不仅仅是3.14。如果您没有它,请使用比与 Pi 相去甚远的近似值更好的近似值。 -
这是 2D 还是 3D ?