gpt4 book ai didi

java - OpenGL/着色器/矩阵 : Weird scaling of the shape after a rotation of the view matrix

转载 作者:行者123 更新时间:2023-11-30 08:36:17 25 4
gpt4 key购买 nike

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

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

enter image description here

enter image description here

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

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;

这是我计算 View 矩阵的方法:

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;
}

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

提前致谢!

最佳答案

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

尝试将其更改为:

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));

关于java - OpenGL/着色器/矩阵 : Weird scaling of the shape after a rotation of the view matrix,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37852969/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com