我有以下代码在lwjgl中初始化我的两个矩阵:
GL20.glUseProgram(shaderProgramID);
Matrix4f camera = new Matrix4f();
Matrix4f.translate(new Vector3f(0, 0, 0), camera, camera);
FloatBuffer buffer = BufferUtils.createFloatBuffer(16);
camera.store(buffer);
buffer.flip();
GL20.glUniformMatrix2(GL20.glGetUniformLocation(shaderProgramID, "camera"), false, buffer);
GL20.glUseProgram(0);
float near = 0.1f, far = 100.0f, top = 300, bottom = -300, left = -400, right = 400;
Matrix4f projection = new Matrix4f();
projection.setIdentity();
projection.m00 = 2*near/(right - left);
projection.m11 = 2*near/(top - bottom);
projection.m22 = -(far +near)/(far - near);
projection.m23 = -1;
projection.m32 = -2*far*near/(far - near);
projection.m20 = (right+left)/(right -left);
projection.m21 = (top + bottom)/(top-bottom);
projection.m33 = 0;
FloatBuffer buffer2 = BufferUtils.createFloatBuffer(16);
projection.store(buffer2);
buffer.flip();
GL20.glUniformMatrix2(GL20.glGetUniformLocation(shaderProgramID, "projection"), false, buffer);
GL20.glUseProgram(0);
顶点着色器执行以下操作:
gl_Position = projection * camera * vec4(vert, 1);
我不知道它有什么问题......任何人都有想法?
答案 0 :(得分:1)
我知道这已经很晚了,但是为了完整起见:
上面的代码在调用glUseProgram(0):时没有保留任何程序,因此稍后调用glUniform *会创建一个GL_INVALID_OPERATION,因为没有程序对象存在。这也意味着实际上从未为您的程序设置投影,因此在渲染时它是一个空矩阵,它与矩阵相乘,矢量仍然会产生空矢量。
此外,您可以考虑在加载着色器后保存一次均匀位置,并检查它是否确实可用。