【问题标题】:OpenGL / Shader / Matrix : Weird scaling of the shape after a rotation of the view matrixOpenGL / Shader / Matrix:旋转视图矩阵后形状的奇怪缩放
【发布时间】:2016-06-16 07:49:10
【问题描述】:

我尝试使用带有 Shader 的 OpenGL 显示三角形,但在移动相机时出现问题。我的三角形的方向看起来不错,但是 x 和 y 缩放看起来很有趣......

以下两个图像说明了我的问题:图像的左侧是我使用着色器的输出,右侧是使用 JOGL 函数(例如“glLookAt”/“glFrustum”)的同一场景的表示)。两个三角形应该是一样的,但是我旋转相机的时候就不是这样了。

这是我的顶点着色器的“主要”:

void main(void) {

varying_Color = attribute_Color;
gl_Position = projectionMatrix * viewMatrix * vec4(attribute_Position,1.0);

}

这是我计算投影矩阵的方法:

    final int height = getSurfaceHeight();
    final double aspect = getSurfaceWidth() / height;
    final double maxDim = getMaxEnvDim();
    final double zNear = maxDim / 1000;
    final double zFar = maxDim*10;
    final double frustum_length = zFar - zNear;
    double fW, fH;
    final double fovY = 45;
    if (aspect > 1.0) {
        fH = FastMath.tan(fovY / 360 * Math.PI) * zNear;
        fW = fH * aspect;
    } else {
        fW = FastMath.tan(fovY / 360 * Math.PI) * zNear;
        fH = fW / aspect;
    }

    projectionMatrix = new Matrix4f();
    projectionMatrix.m00 = (float) (zNear / fW);
    projectionMatrix.m11 = (float) (zNear / fH);
    projectionMatrix.m22 = (float) -((zFar + zNear) / frustum_length);
    projectionMatrix.m23 = -1;
    projectionMatrix.m32 = (float) -((2 * zNear * zFar) / frustum_length);
    projectionMatrix.m33 = 0;

这是我计算视图矩阵的方法:

public static double[] CrossProduct(final double[] vect1, final double[] vect2) {
    final double[] result = new double[3];
    result[0] = vect1[1] * vect2[2] - vect1[2] * vect2[1];
    result[1] = vect1[2] * vect2[0] - vect1[0] * vect2[2];
    result[2] = vect1[0] * vect2[1] - vect1[1] * vect2[0];
    return result;
}

public static double ScalarProduct(final double[] vect1, final double[] vect2) {
    return vect1[0]*vect2[0]+vect1[1]*vect2[1]+vect1[2]*vect2[2];
}

public static double[] Normalize(final double[] vect) {
    double[] result = new double[vect.length];
    double sum = 0;
    for (int i = 0; i < vect.length ; i++) {
        sum += Math.abs(vect[i]);
    }
    for (int i = 0; i < vect.length ; i++) {
        result[i] = vect[i] / sum;
    }
    return result;
}

public static Matrix4f createViewMatrix(ICamera camera) {
    // see http://in2gpu.com/2015/05/17/view-matrix/
    Matrix4f viewMatrix = new Matrix4f();

    double[] fVect = new double[3]; // forward vector : direction vector of the camera.
    double[] sVect = new double[3]; // orthogonal vector : "right" or "sideways" vector.
    double[] vVect = new double[3]; // cross product between f and s.
    double[] pVect = new double[3]; // camera position.

    double sum = Math.abs(camera.getTarget().x - camera.getPosition().x)
            + Math.abs(camera.getTarget().y - camera.getPosition().y)
            + Math.abs(camera.getTarget().z - camera.getPosition().z);
    fVect[0] = -(camera.getTarget().x - camera.getPosition().x) / sum;
    fVect[1] = (camera.getTarget().y - camera.getPosition().y) / sum;
    fVect[2] = -(camera.getTarget().z - camera.getPosition().z) / sum;

    double[] crossProduct = CrossProduct(fVect,new double[]{camera.getOrientation().x,
            -camera.getOrientation().y,camera.getOrientation().z});
    sVect = Normalize(crossProduct);

    vVect = CrossProduct(sVect,fVect);

    pVect = new double[]{camera.getPosition().x,
            -camera.getPosition().y,camera.getPosition().z};

    viewMatrix.m00 = (float) sVect[0];
    viewMatrix.m01 = (float) sVect[1];
    viewMatrix.m02 = (float) sVect[2];
    viewMatrix.m03 = (float) -ScalarProduct(sVect,pVect);
    viewMatrix.m10 = (float) vVect[0];
    viewMatrix.m11 = (float) vVect[1];
    viewMatrix.m12 = (float) vVect[2];
    viewMatrix.m13 = (float) -ScalarProduct(vVect,pVect);
    viewMatrix.m20 = (float) fVect[0];
    viewMatrix.m21 = (float) fVect[1];
    viewMatrix.m22 = (float) fVect[2];
    viewMatrix.m23 = (float) -ScalarProduct(fVect,pVect);
    viewMatrix.m30 = (float) 0;
    viewMatrix.m31 = (float) 0;
    viewMatrix.m32 = (float) 0;
    viewMatrix.m33 = (float) 1;
    viewMatrix.transpose();
    return viewMatrix;
}

请注意,当我使用简单的变换矩阵而不是视图矩阵并且对三角形应用旋转时,它看起来是正确的。这导致我发现问题出在我的视图矩阵中,但我找不到我的错误...... 另请注意,我有一些奇怪的“-y”来获取视图矩阵,因为我的 openGL 世界是一个间接坐标系。

提前致谢!

【问题讨论】:

    标签: java opengl matrix camera shader


    【解决方案1】:

    你的Normalize 函数很奇怪。您不是在计算向量的欧几里得长度,而是计算其统一范数。

    尝试将其更改为:

    double[] result = new double[vect.length];
    double sum = 0;
    for (int i = 0; i < vect.length ; i++) {
        sum += Math.pow(vect[i], 2);
    }
    for (int i = 0; i < vect.length ; i++) {
        result[i] = vect[i] / Math.sqrt(sum);
    }
    return result;
    

    你的createViewMatrix函数在这一行也有同样的问题:

    double sum = Math.abs(camera.getTarget().x - camera.getPosition().x) +
    Math.abs(camera.getTarget().y - camera.getPosition().y) + 
    Math.abs(camera.getTarget().z - camera.getPosition().z);
    

    改成:

    double sum = Math.sqrt(Math.pow(camera.getTarget().x - camera.getPosition().x, 2) +
    Math.pow(camera.getTarget().y - camera.getPosition().y, 2) + 
    Math.pow(camera.getTarget().z - camera.getPosition().z, 2));
    

    【讨论】:

    • 哇!确实,我这里有错误,谢谢!但即使进行了这种修改,我仍然遇到与截图描述的相同的问题......
    • @Julien Mazars 你在createViewMatrix做同样的事情:double sum = Math.abs(camera.getTarget().x - camera.getPosition().x) + Math.abs(camera.getTarget().y - camera.getPosition().y) + Math.abs(camera.getTarget().z - camera.getPosition().z);
    • 哦,是的,你是对的!我真是瞎了^^。非常感谢,现在完美运行!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多