我正在检查日志,一切正常。着色器如下所示:
顶点:
#version 330 core
layout(location = 0) in struct InData {
vec3 position;
vec4 color;
} inData;
out struct OutData {
vec3 position;
vec4 color;
} outData;
void main()
{
outData.position = inData.position;
outData.color = inData.color;
}
分段:
#version 330 core
in struct InData {
vec2 position;
vec4 color;
} inData;
out vec4 color;
void main(){
color = inData.color;
}
我正在准备这样的着色器:
public Shader(string src, ShaderType type)
{
shaderId = GL.CreateShader(type);
GL.ShaderSource(shaderId, GetShader(src));
GL.CompileShader(shaderId);
EventSystem.Log.Message(GL.GetShaderInfoLog(shaderId));
EventSystem.Log.Message("GLERROR: " + GL.GetError());
packs = new List<ShaderPack>();
}
public void Attach(ShaderPack pack)
{
packs.Add(pack);
GL.AttachShader(pack.ProgramID, shaderId);
EventSystem.Log.Message(GL.GetProgramInfoLog(pack.ProgramID));
EventSystem.Log.Message("GLERROR: " + GL.GetError());
}
然后我编译着色器:
public void Compile()
{
if(program >= 0)
GL.DeleteProgram(program);
program = GL.CreateProgram();
foreach (var s in shaders.Values)
s.Attach(this);
EventSystem.Log.Message(GL.GetProgramInfoLog(program));
EventSystem.Log.Message("GLERROR: " + GL.GetError());
}
然后我尝试使用它:
mesh = new Mesh();
mesh.AddTriangle(
new Vector3(0, 0, 0), new Vector4(1, 0, 0, 1),
new Vector3(0, sizeY, 0), new Vector4(0, 1, 0, 1),
new Vector3(sizeX, sizeY, 0), new Vector4(0, 0, 1, 1));
mesh.RefreshBuffer();
shaderPack.Apply();
shaderPack.SetVertexAttribute<Mesh.MeshData1>("vertex", 0, mesh.meshData);
EventSystem.Log.Message("GLERROR: " + GL.GetError());
在 Apply GL.UseProgram 被调用并且 GetError 返回“无效操作”
更新:
好的,我更改了代码:
public void Compile()
{
if(program >= 0)
GL.DeleteProgram(program);
program = GL.CreateProgram();
foreach (var s in shaders.Values)
s.Attach(this);
// GL.LinkProgram(program);
//GL.ValidateProgram(program);
GL.ValidateProgram(program);
EventSystem.Log.Message("Validate: " + GL.GetProgramInfoLog(program) + " - " + GL.GetError());
}
public void Apply()
{
GL.UseProgram(program);
EventSystem.Log.Message("GLERROR (Apply): " + GL.GetError());
}
输出是
[23:25:55][日志]:验证:- NoError
[23:25:55][日志]:GLERROR(应用):InvalidOperation
编辑:好的,我更改了顶点着色器:
#version 330 core
layout(location = 0) in struct InData {
vec3 position;
vec4 color;
} inData;
void main()
{
gl_Position = vec4(inData.position, 1);
}
...
#version 330 core
//in struct InData {
// vec2 position;
// vec4 color;
//} inData;
out vec4 color;
void main(){
color = vec4(1,0,0,1);
}
它编译没有错误,但我有一个空白屏幕......
回滚前:
编辑:好的,我怀疑问题出在这里:
public void VertexAttribute<T>(int loc, ShaderPack p, T[] dataArray) where T : struct
{
int buf;
GL.GenBuffers(1, out buf);
GL.BindBuffer(BufferTarget.ArrayBuffer, buf);
GL.BufferData<T>(BufferTarget.ArrayBuffer, (IntPtr)(dataArray.Length * Marshal.SizeOf(typeof(T))), dataArray, BufferUsageHint.StaticDraw);
GL.EnableVertexAttribArray(0);
GL.VertexAttribPointer<T>(loc, 2, VertexAttribPointerType.Float, false, 0, ref dataArray[0]);
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
}
我使用以下类型的数组传递它:
public struct MeshData1
{
public Vector3 vertex;
public Vector4 color;
public MeshData1(Vector3 vertex, Vector4 color)
{
this.vertex = vertex;
this.color = color;
}
}
输入如下所示:
#version 330 core
layout(location = 0) in struct InData {
vec3 position;
vec4 color;
} inData;
void main()
{
gl_Position = vec4(inData.position, 1.0);
}
我究竟做错了什么?