我尝试使用带有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世界是一个间接坐标系。
提前致谢!
答案 0 :(得分:2)
您的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));