C# GetUniformLocation始终-1/Projectionmatrix错误
我想开始开发一个opengl基本游戏。经过一些编码,我遇到了一个错误。 我想添加一个投影矩阵,创建后,渲染图像变为白色,甚至变换矩阵也无法正常工作。 我用C# GetUniformLocation始终-1/Projectionmatrix错误,c#,opengl,opentk,C#,Opengl,Opentk,我想开始开发一个opengl基本游戏。经过一些编码,我遇到了一个错误。 我想添加一个投影矩阵,创建后,渲染图像变为白色,甚至变换矩阵也无法正常工作。 我用GL.GetUniformLocation(变量名)测试了位置ID,它们都是-1 我添加投影矩阵的方式: projectionMatrix = Matrix4.CreatePerspectiveFieldOfView((float)(Math.PI / 180) * FOV, aspectRatio, NEAR_PLANE, FAR_PLANE
GL.GetUniformLocation(变量名)
测试了位置ID,它们都是-1
我添加投影矩阵的方式:
projectionMatrix = Matrix4.CreatePerspectiveFieldOfView((float)(Math.PI / 180) * FOV, aspectRatio, NEAR_PLANE, FAR_PLANE);
Matrix4 matrix = Matrix4.CreateRotationX(MathHelper.DegreesToRadians(rx)) * Matrix4.CreateRotationY(MathHelper.DegreesToRadians(ry)) * Matrix4.CreateRotationZ(MathHelper.DegreesToRadians(rz));
Matrix4 translationsMatrix = Matrix4.CreateTranslation(translation);
Matrix4 scaleMatrix = Matrix4.CreateScale(scale);
matrix *= scaleMatrix;
matrix *= translationsMatrix;
我添加转换矩阵的方式:
projectionMatrix = Matrix4.CreatePerspectiveFieldOfView((float)(Math.PI / 180) * FOV, aspectRatio, NEAR_PLANE, FAR_PLANE);
Matrix4 matrix = Matrix4.CreateRotationX(MathHelper.DegreesToRadians(rx)) * Matrix4.CreateRotationY(MathHelper.DegreesToRadians(ry)) * Matrix4.CreateRotationZ(MathHelper.DegreesToRadians(rz));
Matrix4 translationsMatrix = Matrix4.CreateTranslation(translation);
Matrix4 scaleMatrix = Matrix4.CreateScale(scale);
matrix *= scaleMatrix;
matrix *= translationsMatrix;
这个(转换矩阵)本身工作得很好
这是我的渲染器类:
TexturedModel texturedModel = entity.Model;
RawModel model = texturedModel.Model;
GL.BindVertexArray(model.VaoID);
GL.EnableVertexAttribArray(0);
GL.EnableVertexAttribArray(1);
Matrix4 transformationMatrix = Maths.CreateTransformationMatrix(entity.Position, entity.RotationX, entity.RotationY, entity.RotationZ, entity.Scale);
shader.LoadTransformationMatrix(transformationMatrix);
GL.ActiveTexture(TextureUnit.Texture0);
GL.BindTexture(TextureTarget.Texture2D, texturedModel.Texture.ID);
GL.DrawElements(PrimitiveType.Triangles, model.VertexCount, DrawElementsType.UnsignedInt, 0);
GL.DisableVertexAttribArray(0);
GL.DisableVertexAttribArray(1);
GL.BindVertexArray(0);
为什么它会这样,我如何“修复”它
编辑
这是我的着色器代码。(我以前忘了)
顶点着色器:
#version 400 core
in vec3 position;
in vec2 textureCoords;
out vec2 pass_textureCoords;
uniform mat4 transformationMatrix;
unfiorm mat4 projectionMatrix;
void main(void){
gl_Position = projectionMatrix * transformationMatrix * vec4(position, 1.0);
pass_textureCoords = textureCoords;
}
抽象着色器类:
abstract class ShaderProgram
{
private int programID;
private int vertexShaderID;
private int fragmentShaderID;
public ShaderProgram(string vertexShaderFile, string fragmentShaderFile)
{
vertexShaderID = LoadShader(vertexShaderFile, ShaderType.VertexShader);
fragmentShaderID = LoadShader(fragmentShaderFile, ShaderType.FragmentShader);
programID = GL.CreateProgram();
GL.AttachShader(programID, vertexShaderID);
GL.AttachShader(programID, fragmentShaderID);
BindAttributes();
GL.LinkProgram(programID);
GL.ValidateProgram(programID);
GetAllUniformLocations();
}
protected abstract void GetAllUniformLocations();
protected int GetUniformLocation(string uniformName)
{
return GL.GetUniformLocation(programID, uniformName);
}
public void Start()
{
GL.UseProgram(programID);
}
public void Stop()
{
GL.UseProgram(0);
}
public void CleanUp()
{
Stop();
GL.DetachShader(programID, vertexShaderID);
GL.DetachShader(programID, fragmentShaderID);
GL.DeleteShader(vertexShaderID);
GL.DeleteShader(fragmentShaderID);
GL.DeleteProgram(programID);
}
protected abstract void BindAttributes();
protected void BindAttribute(int attribute, string variableName)
{
GL.BindAttribLocation(programID, attribute, variableName);
}
protected void LoadFloat(int location, float value)
{
GL.Uniform1(location, value);
}
protected void LoadVector(int location, Vector3 vector)
{
GL.Uniform3(location, vector);
}
protected void LoadBoolean(int location, bool value)
{
GL.Uniform1(location, value ? 1 : 0);
}
protected void LoadMatrix(int location, Matrix4 matrix)
{
GL.UniformMatrix4(location, false, ref matrix);
}
private static int LoadShader(String file, ShaderType type)
{
string shaderSource = File.ReadAllText(file);
int shaderID = GL.CreateShader(type);
GL.ShaderSource(shaderID, shaderSource);
GL.CompileShader(shaderID);
return shaderID;
}
}
静态着色器:
class StaticShader : ShaderProgram
{
private static string vertexFile = "f/shaders/vertexShader.txt";
private static string fragmentFile = "f/shaders/fragmentShader.txt";
private int location_transformationMatrix;
private int location_projectionMatrix;
public StaticShader()
: base(vertexFile, fragmentFile)
{
}
protected override void BindAttributes()
{
base.BindAttribute(0, "position");
base.BindAttribute(1, "textureCoords");
}
protected override void GetAllUniformLocations()
{
location_transformationMatrix = base.GetUniformLocation("transformationMatrix");
location_projectionMatrix = base.GetUniformLocation("projectionMatrix");
}
public void LoadTransformationMatrix(Matrix4 matrix)
{
base.LoadMatrix(location_transformationMatrix, matrix);
}
public void LoadProjectionMatrix(Matrix4 projection)
{
base.LoadMatrix(location_projectionMatrix, projection);
}
}
着色器代码在哪里?我已在中编辑了它。您应该明确添加检测编译和链接错误的检查。找到了它…我拼错了单词“uniform”,下次我将多次检查我的代码。谢谢