我正在使用vecmath库来帮助矩阵数学,在将OpenGL程序转换为更好地利用
glsl
时使用。
写完问题后,我有三个小问题:
- 模型矩阵从哪里获取它的值?
- 我需要在
display()
中重置所有模型、视图和投影矩阵吗?即每一帧 - 在绘制每个对象后,我需要重置模式、视图和投影矩阵吗?
这是我在GLEventListener
的reshape
函数中计算我的projection
和view
矩阵的方法..
Matrix4f mProjectionMatrix = createPerspectiveProjection(
60.0f, width / height, 0.1f, 100.0f);
Matrix4f mViewMatrix = new Matrix4f();
mViewMatrix.setIdentity();
辅助函数...
private Matrix4f createPerspectiveProjection(float fov, float aspect, float zNear, float zFar){
Matrix4f mat = new Matrix4f();
float yScale = (float) (1 / (Math.tan(Math.toRadians(fov / 2))));
float xScale = yScale / aspect;
float frustrumLength = zFar - zNear;
mat.m00 = xScale;
mat.m11 = yScale;
mat.m22 = -((zFar + zNear) / frustrumLength);
mat.m23 = -1;
mat.m32 = -((2 * zFar * zNear) / frustrumLength);
mat.m33 = 0;
return mat;
}
在GLEventListener
的display()
方法中,我对下一步操作不确定。
从概念上讲,我脑海中有以下步骤,请问这些是我需要执行的正确步骤吗?..
public void display(GLAutoDrawable gLDrawable){
// reset view matrix, needed?
// Calculate the model view projection matrix .. where does model matrix get it's values from?
// translate to Square1's position?
// rotate to Square1's angle?
// draw Square1 .. passing MVP matrix
// translate to Square2's position?
// rotate to Square2's angle?
// draw Square2 .. passing MVP matrix
}
这是我的顶点着色器...
uniform mat4 uMVPMatrix;
attribute vec4 vPosition;
void main() {
gl_Position = uMVPMatrix * vPosition;
}