OpenTK - After multiplying with ProjectionMatrix nothing is rendering anymore

可紊 提交于 2019-12-12 04:06:43

问题


From a tutorial that refused to explain it I got the following code. It was a lwjgl tutorial I copied where it worked perfectly fine. I'm not sure if the problem is created by having OpenGl differ from the java openGl, but since it's only math I would be surprised by that:

private void createProjectionMatrix(int width, int height)
{
    float aspectRatio = width / height;
    float yScale = (float)((1f / Math.Tan(Maths.degreeToRadian(FOV/2f))) * aspectRatio);
    float xScale = yScale / aspectRatio;
    float frustumLength = FAR_PLANE - NEAR_PLANE;

    projectionMatrix = new Matrix4();
    projectionMatrix.M11 = xScale;
    projectionMatrix.M22 = yScale;
    projectionMatrix.M33 = -((FAR_PLANE + NEAR_PLANE) / frustumLength);
    projectionMatrix.M34 = -1;
    projectionMatrix.M43 = -((2 * NEAR_PLANE * FAR_PLANE) / frustumLength);
    projectionMatrix.M44 = 0;   
}

This I multiplied with the transformation matrix (which worked). I get the matrix to the shader by doing uniformLocation_projectionMatrix = GL.GetUniformLocation(programID, "projectionMatrix") followed by GL.UniformMatrix4(uniformLocation_projectionMatrix, false, ref projectionMatrix) this way I got the transformationMatrix in there too, so this shouldn't be the problem.
In the shader I do gl_Position = projectionMatrix * transformationMatrix * vec4(in_position, 1.0) (again, without the projectionMatrix everything works fine), and before that uniform mat4 projectionMatrix; (as you can confirm I spelled it exactly the same).
I'm not sure what further part of the code you need, so feel free to ask, thank you.
Edit 1: I tried transposing it (by setting the false to true), the resut was the same

Edit 2:
The matrix created by the code above:
(1,428148; 0; 0; 0) ( 0; 2,53893; 0; 0) ( 0; 0; -1,002002; -1) ( 0; 0; -0,2002002; 0)

The matrix created by Matrix4.CreatePerspectiveFieldOfView():
(0,8033332; 0; 0; 0) ( 0; 1,428148; 0; 0) ( 0; 0; -1,002002; -1) ( 0; 0; -0,2002002; 0)
The matrix created by the layout from Draykoon D's answer:
(-0,00015625; 0; 0; 0) ( 0; 0,0002777778; 0; 0) ( 0; 0; -1,002002; -0,2002002) ( 0; 0; -1; 0)

I am now convinced that the problem lies elsewere - all three matricies don't work.

Edit 3 - More code: This is my vertexShader so far

#version 440 core

layout (location = 0) in vec3 in_position;
layout (location = 1) in vec2 in_textureCoordinates;

uniform mat4 translation;
uniform mat4 rotationX;
uniform mat4 rotationY;
uniform mat4 rotationZ;
uniform mat4 scale;

uniform mat4 projectionMatrix;

out vec2 textureCoordinates;

void main(void)
{
    mat4 transformationMatrix = translation * rotationX * rotationY * rotationZ * scale;
    gl_Position = projectionMatrix * transformationMatrix * vec4(in_position,  1.0);
    textureCoordinates = in_textureCoordinates;
}

And this is the render function:

public void render(Entity entity, ShaderProgram shader)
    {
        TexturedModel texturedModel = entity.Model;
        RawModel model = texturedModel.Model;
        GL.BindVertexArray(model.VaoID);
        GL.EnableVertexAttribArray(0);
        GL.EnableVertexAttribArray(1);
        shader.loadTransformationMatrix(
            entity.Position, entity.RotX, entity.RotY, entity.RotZ, entity.Scale);
        GL.ActiveTexture(TextureUnit.Texture0);
        GL.BindTexture(TextureTarget.Texture2D, texturedModel.Texture.ID);
        GL.DrawElements(BeginMode.Triangles, model.VertexCount, DrawElementsType.UnsignedInt, 0);
        GL.DisableVertexAttribArray(0);
        GL.DisableVertexAttribArray(1);
        GL.BindVertexArray(0);
    }    

and the constructor of the Renderer loads the projectionMatrix:

public Renderer(int width, int height, ShaderProgram shader)
    {
        createProjectionMatrix(width, height);
        shader.loadProjectionMatrix(projectionMatrix);
    }

回答1:


if you want to try an other method of building this projection matrix, you can try this one with th math explanation

should not be too hard,

r and l stand for right and left and should be something like -width/2, width2

b and t stand for bottom and right, -height/2, height/2

n and f stand for near and far plane try different values, just check your z belongs to interval, good luck. I am not sure but look on the scheme to see the sign of near and far plane.




回答2:


First and foremost, before doing anything else, check if you call GL.UseProgram(shaderProgram) directly after creating the program to avoid arrors like trying to load a projection matrix without having GL using the shader.
Thanks for everyone helping, at least I learned something



来源:https://stackoverflow.com/questions/45370564/opentk-after-multiplying-with-projectionmatrix-nothing-is-rendering-anymore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!