I have the following code to initialize my two matrices in 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);

The vertex shader does the following:

gl_Position = projection * camera * vec4(vert, 1);

I dont know what could be wrong with it... anyone an idea?

有帮助吗?

解决方案

I know this is late but for completenesse's sake:

The code above leaves no program bound when glUseProgram(0): is called, thus the later call of glUniform* creates a GL_INVALID_OPERATION as there is no program oject present. This also means that projection is never actually set for your program, so upon rendering it is an empty matrix which multiplied with a matrix and a vector still results in the null vector.

Also you might consider saving the uniform locations once after loading the shader and check if it actually is available.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top