android - opengl对象在移动一段距离后振动

标签 android c++ opengl-es

我有一个在地形上移动的物体,第三人称摄像机跟随它,在我将它朝不同方向移动一段距离后,它开始摇晃或振动,即使它没有移动并且摄像机围绕它旋转,这是物体的移动代码

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 有时是不够的。

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

  2. 大数字通常是问题所在

    如果你的世界很大,那么你会将大数字传递到方程中,乘以任何误差,并且只有在最后阶段,这些东西才会相对于相机位置进行平移,这意味着误差会保持成倍增加,但数字会被限制,因此误差/数据比率会得到大。

    为了在渲染之前降低这个问题,将所有顶点转换为坐标系,原点位于或靠近您的相机。您可以忽略旋转只是偏移位置。

    这样你只会在远离相机的地方得到更高的错误,无论如何透视都不可见......有关更多信息,请参阅:

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

    有关详细信息,请参阅 Understanding 4x4 homogenous transform matrices以及该答案底部的所有链接。

关于android - opengl对象在移动一段距离后振动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46566270/

相关文章:

android - 统一3D : What is the Android Bundle Version and Version Code and how do they relate?

java - 为什么应用程序崩溃?将 Arraylist 分配给 Array 时出现 NullPointerException

c++ - 有什么方法可以更快地处理 "predictable branches"吗?

c++ - 为什么这个 typedef 允许我在此模板中使用基类成员函数指针?

ios - swift/OpenGL ES 2.0 - 从 YUV420 缓冲区创建纹理

ios - 场景套件: Use multiple SCNView instances side by side

java - Android studio 中不允许嵌套事务

C++ 重载运算符>>(输入)不改变原来的

android - 如何在 EGL 或 GLSurfaceView 中设置 OpenGL 版本?

Android 分享 Intent 选择器