我的问题是我的透视投影与它应该做的相反。不是将我的对象“缩小”到远处,而是将其扩展:
白色部分是最近的三角形。我想你明白了。
我正在创建自己的透视投影,并将其发送到我的顶点着色器。我正在使用以下 opengl 状态:
gl.glEnable(GL2ES2.GL_DEPTH_TEST);
gl.glDepthFunc(GL2ES2.GL_LEQUAL);
gl.glEnable(GL2ES2.GL_CULL_FACE);
gl.glCullFace(GL2ES2.GL_BACK);
gl.glFrontFace(GL2ES2.GL_CW);
我正在为透视矩阵部分使用以下代码:
eye = {0,0,5};
viewMatrix = Calc.translate(identity_matrix, -eye[0], -eye[1], -eye[2]);
perspectiveMatrix = Calc.buildPerspProjMat(90, 1, 0.5f, 10);
model_view_projection = Calc.multiply(perspectiveMatrix, viewMatrix);
其中 Calc.java 有这些功能:
public static float[] translate(float[] m,float x,float y,float z){
float[] t = { 1.0f, 0.0f, 0.0f, x,
0.0f, 1.0f, 0.0f, y,
0.0f, 0.0f, 1.0f, z,
0, 0, 0, 1.0f };
return multiply(t, m);
}
public static float[] buildPerspProjMat(float fov, float aspect, float znear, float zfar) {
float[] m = new float[16];
float ymax = (float) (znear * Math.tan(Math.toRadians(fov/2)));
float ymin = -ymax;
float xmax = ymax * aspect;
float xmin = ymin * aspect;
float width = xmax - xmin;
float height = ymax - ymin;
float depth = zfar - znear;
float q = -(zfar + znear) / depth;
float qn = -2 * (zfar * znear) / depth;
float w = 2 * znear / width;
w = w / aspect;
float h = 2 * znear / height;
m[0] = w;
m[1] = 0;
m[2] = 0;
m[3] = 0;
m[4] = 0;
m[5] = h;
m[6] = 0;
m[7] = 0;
m[8] = 0;
m[9] = 0;
m[10] = q;
m[11] = qn;
m[12] = 0;
m[13] = 0;
m[14] = -1;
m[15] = 0;
return m;
}