代码
GL.GetUniformLocation();
为glsl代码中的统一"transform"
返回-1
#version 420
layout(location = 0) in vec3 position;
uniform mat4 transform;
void main()
{
gl_Position = transform * vec4(position, 1.0);
}
我能找到的关于这个的唯一其他帖子谈到了优化器编译它,但我不知道它是如何做到的,因为它在着色器的最终输出中是必不可少的。我正在使用Windows和普通的编译器。当我运行GL.GetError()
时,我收到InvalidOperation错误。
完整的代码是
C#代码:
public void AddUniform(string Uniform)
{
int tries = 6;
Start:
int UniLocation = GL.GetUniformLocation(Program, Uniform);
#if DEBUG
tries--;
if(UniLocation == -1)
{
Console.WriteLine(this.ToString() + " Failed DEBUG AddUniform check");
if (tries > 0)
{
goto Start;
}
}
#endif
Uniforms.Add(Uniform, UniLocation);
}
制服是Dictionary<string, int>
着色器代码:
#version 420
layout(location = 0) in vec3 position;
uniform mat4 transform = mat4(1.0);
void main()
{
gl_Position = transform * vec4(position, 1.0);
}
实施:
NewWindow.Init(() =>
{
mesh = new MobiusEngine.NewSystems.Mesh();
shader = new MobiusEngine.NewSystems.Shader();
mesh.AddVerticies(new MobiusEngine.NewSystems.Vertex(new OpenTK.Vector3d(-1, -1, 0)), new MobiusEngine.NewSystems.Vertex(new OpenTK.Vector3d(0, 1, 0)), new MobiusEngine.NewSystems.Vertex(new OpenTK.Vector3d(1, -1, 0)));
shader.AddVertexShader(MobiusEngine.NewSystems.ShaderFunctions.LoadShader("BasicVertex.vert"));
shader.AddFragmentShader(MobiusEngine.NewSystems.ShaderFunctions.LoadShader("BasicFrag.frag"));
shader.AddUniform("transform");
shader.ComplieShader();
});
double temp = 0d;
NewWindow.Render(() =>
{
shader.Bind();
shader.SetUniformM("transform", Matrix4.CreateTranslation((float)(Math.Abs(Math.Sin(temp))), 0, 0));
temp += Time.DeltaTime.TotalSeconds;
mesh.Draw();
});
答案 0 :(得分:1)
你很明显在编译着色器之前尝试查询位置,程序在这里链接:
shader.AddUniform("transform"); shader.ComplieShader();
在链接期间分配统一位置。