OpenTK-与ProjectionMatrix相乘后,不再呈现任何内容 [英] OpenTK - After multiplying with ProjectionMatrix nothing is rendering anymore
问题描述
从一个拒绝解释的教程中,我得到了以下代码.这是我复制的lwjgl教程,在该教程中效果很好.我不确定问题是否是由于OpenGl与Java openGl不同而引起的,但是由于它只是数学运算,我对此感到惊讶:
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;
}
我将此乘以转换矩阵(有效).我通过先执行uniformLocation_projectionMatrix = GL.GetUniformLocation(programID, "projectionMatrix")
然后执行GL.UniformMatrix4(uniformLocation_projectionMatrix, false, ref projectionMatrix)
的方式将矩阵添加到着色器,这样我也可以在其中获取TransformationMatrix,因此这不应该是问题.
在着色器中,我执行gl_Position = projectionMatrix * transformationMatrix * vec4(in_position, 1.0)
(同样,没有projectionMatrix,一切正常,),然后执行uniform mat4 projectionMatrix;
(您可以确认我的拼写完全相同).
我不确定您需要代码的哪一部分,请随时提出,谢谢.
我尝试将其转置(通过将false设置为true),结果相同
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
上面的代码创建的矩阵:
(1,428148; 0; 0; 0)
( 0; 2,53893; 0; 0)
( 0; 0; -1,002002; -1)
( 0; 0; -0,2002002; 0)
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)
由Matrix4.CreatePerspectiveFieldOfView()创建的矩阵:
(0,8033332; 0; 0; 0)
( 0; 1,428148; 0; 0)
( 0; 0; -1,002002; -1)
( 0; 0; -0,2002002; 0)
Draykoon D的答案由布局创建的矩阵:
(-0,00015625; 0; 0; 0)
( 0; 0,0002777778; 0; 0)
( 0; 0; -1,002002; -0,2002002)
( 0; 0; -1; 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.
编辑3-更多代码: 到目前为止,这是我的vertexShader
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;
}
这是渲染功能:
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);
}
和Renderer的构造函数将加载projectionMatrix:
and the constructor of the Renderer loads the projectionMatrix:
public Renderer(int width, int height, ShaderProgram shader)
{
createProjectionMatrix(width, height);
shader.loadProjectionMatrix(projectionMatrix);
}
推荐答案
首先,在执行其他操作之前,请检查是否在创建后直接调用GL.UseProgram(shaderProgram)
该程序可避免错误,例如尝试使用着色器在不使用GL的情况下尝试加载投影矩阵.
感谢大家的帮助,至少我学到了一些东西
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
这篇关于OpenTK-与ProjectionMatrix相乘后,不再呈现任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!