且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

freeglut - 使用鼠标平滑的相机旋转

更新时间:2023-02-02 15:38:20

有时当鼠标轮询率和屏幕刷新率的比例不是很好时,根据鼠标位置更新显示会导致抖动效果.

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.