freeglut-使用鼠标进行平滑的相机旋转 [英] freeglut - a smooth camera rotation using mouse

查看:282
本文介绍了freeglut-使用鼠标进行平滑的相机旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用鼠标旋转fps相机时,动画不流畅.当我使用键盘时,一切正常.对于键盘,我使用bool类型的数组来缓冲键.使用鼠标时,如何使动画流畅?

When I rotate my fps camera using mouse the animation is not smooth. When I use a keyboard everything works nice. For keyboard I use an array of type bool to buffer keys. What Can I do to make the animation smooth when using mouse ?

void MousePassiveMotion(int x, int y)
{
     int centerX = glutGet(GLUT_WINDOW_WIDTH) / 2;
     int centerY = glutGet(GLUT_WINDOW_HEIGHT) / 2;

     int deltaX =  x - centerX;
     int deltaY =  y - centerY;

     if(deltaX != 0 || deltaY != 0) 
     {
          heading = deltaX * 0.2f;
          pitch = deltaY  * 0.2f;
          glutWarpPointer(centerX, centerY);
     }
}

推荐答案

有时,当鼠标的轮询率和屏幕刷新率不成比例时,基于鼠标位置更新显示可能会导致抖动.

Sometimes when the mouse polling rate and the screen refresh rate are not of a good ratio, updating the display based on the mouse position can result in a jerky effect.

您已启用垂直同步,对吗?而且如果您将其旋转,鼠标移动会更顺畅,但会以撕裂为代价?

You have vertical sync on, correct? And if you turn it of the mouse movement is smoother at the expense of tearing?

一种选择是使用平滑功能,该功能将鼠标位置使用的值滞后"于实际鼠标位置的后面一点点

One option is to use a smoothing function, that "lags" the values you use for the mouse position just a tiny bit behind the real mouse position

它的要旨是这样的:

float use_x,use_y;      // position to use for displaying
float springiness = 50; // tweak to taste.

void smooth_mouse(float time_d,float realx,float realy) {
    double d = 1-exp(log(0.5)*springiness*time_d);

    use_x += (realx-use_x)*d;
    use_y += (realy-use_y)*d;
}

这是指数衰减函数.您可以在每一帧都调用它,以建立用于鼠标位置的内容.诀窍是为springiness获取正确的值.要学究,springiness是实际鼠标位置和使用的位置之间的距离减半的次数.为了使鼠标移动平滑,springiness的一个好的值可能是50-100.

This is an exponential decay function. You call it every frame to establish what to use for a mouse position. The trick is getting the right value for springiness. To be pedantic, springiness is the number of times the distance between the real mouse position and the used position will halve. For smoothing mouse movement, a good value for springiness is probably 50-100.

time_d是自上次鼠标轮询以来的间隔.如果可以的话,传递实时增量(以秒为单位),但是只要传递1.0/fps即可.

time_d is the interval since the last mouse poll. Pass it the real time delta (in fractional seconds) if you can, but you can get away with just passing it 1.0/fps.

如果您具有支持WebGL的浏览器,则可以看到实时版本此处-查找在viewer.js中称为GLDraggable的类.

If you have a WebGL-capable browser you can see a live version here - look for a class called GLDraggable in viewer.js.

这篇关于freeglut-使用鼠标进行平滑的相机旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆