opengl对象在移动一定距离后会振动 [英] opengl object vibrate after moving a distance

查看:181
本文介绍了opengl对象在移动一定距离后会振动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在地形上移动的物体,并且有一个第三人称照相机跟随它,在我沿不同方向移动了一定距离后,即使它不动并且照相机绕着它旋转,它也会开始震动或振动.是对象的移动代码

I have an object which moves on a terrain and a third person camera follow it, after I move it for some distance in different directions it begin to shaking or vibrating even if it is not moving and the camera rotates around it, this is the moving code of the object

double& delta = engine.getDeltaTime();
GLfloat velocity = delta * movementSpeed;
glm::vec3 t(glm::vec3(0, 0, 1) * (velocity * 3.0f));
//translate the objet atri before rendering
matrix = glm::translate(matrix, t);
//get the forward vetor of the matrix
glm::vec3 f(matrix[2][0], matrix[2][1], matrix[2][2]);
f = glm::normalize(f);
f = f * (velocity * 3.0f);
f = -f;
camera.translate(f);

并且相机旋转为

void Camera::rotate(GLfloat xoffset, GLfloat yoffset, glm::vec3& c, double& delta, GLboolean constrainpitch) {
    xoffset *= (delta * this->rotSpeed);
    yoffset *= (delta * this->rotSpeed);
    pitch += yoffset;
    yaw += xoffset;
    if (constrainpitch) {
        if (pitch >= maxPitch) {
            pitch = maxPitch;
            yoffset = 0;
        }
        if (pitch <= minPitch) {
            pitch = minPitch;
            yoffset = 0;
        }
    }
    glm::quat Qx(glm::angleAxis(glm::radians(yoffset), glm::vec3(1.0f, 0.0f, 0.0f)));
    glm::quat Qy(glm::angleAxis(glm::radians(xoffset), glm::vec3(0.0f, 1.0f, 0.0f)));
    glm::mat4 rotX = glm::mat4_cast(Qx);
    glm::mat4 rotY = glm::mat4_cast(Qy);
    view = glm::translate(view, c);
    view = rotX * view;
    view = view * rotY;
    view = glm::translate(view, -c);
}

推荐答案

  1. float有时是不够的.

  1. float is sometimes not enough.

我在 CPU 端使用double精度矩阵来避免此类问题.但是由于您使用的是 Android ,因此可能无法实现.对于 GPU ,由于还没有64位插值器,请再次使用float.

I use double precision matrices on CPU side to avoid such problems. But as you are on Android it might not be possible. For GPU use floats again as there are no 64bit interpolators yet.

通常是大数字

如果您的世界很大,那么您要将大数字传递给方程式,以乘以任何误差,并且仅在最后阶段,相对于摄影机位置才进行填充,这意味着误差会倍增,但数字会被钳位,因此误差/数据比会得到大.

If your world is big then you are passing big numbers into the equations multiplying any errors and only at the final stage the stuff is translated relative to camera position meaning the errors stay multiplied but the numbers got clamped so error/data ratio got big.

要解决此问题,请在渲染之前将所有顶点转换为坐标系,其坐标系为相机处或相机附近.您可以忽略旋转而只是偏移位置.

To lower this problem before rendering convert all vertexes to coordinate system with origin at or near your camera. You can ignore rotations just offset the positions.

通过这种方式,您只会在离相机始终不可见的相机较远的地方得到更高的错误...有关更多信息,请参见:

This way you will got higher errors only far away from camera which is with perspective not visible anyway... For more info see:

使用累积变换矩阵代替欧拉角

有关更多信息,请参见了解4x4均匀变换矩阵以及该答案底部的所有链接.

for more info see Understanding 4x4 homogenous transform matrices and all the links at bottom of that answer.

这篇关于opengl对象在移动一定距离后会振动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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