freeglut - 使用鼠标平滑的相机旋转 [英] freeglut - a smooth camera rotation using mouse
问题描述
当我使用鼠标旋转我的 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屋!