我是 OpenGL 的新手,并使用 SharpGL 来研究它,它是简单的 C# 绑定到 OpenGL 函数。我正在尝试使用最简单的顶点和片段着色器。当片段着色器自己生成输出时,一切都很好,但是当它尝试使用来自顶点着色器的数据时,只有黑屏。着色器已成功编译并链接到程序。OpenGL 没有返回任何错误。经过大量文章、示例和教程后,我仍然不知道为什么会发生这种情况。这是顶点着色器。
#version 330
layout(location = 0) in vec3 position;
layout(location = 1) in vec3 color;
out vec3 rgb;
void main()
{
rgb = vec3(1.0, 0.5, 0.5);
gl_Position = vec4(position, 1.0);
}
片段着色器:
#version 330
in vec3 rgb;
out vec3 outColor;
void main()
{
//outColor = rgb; // black display
outColor = vec3(1.0, 0.0, 0.4); // works fine
}
这是初始化代码:
gl.GenVertexArrays(1, arrayBuffer);
gl.BindVertexArray(arrayBuffer[0]);
gl.GenBuffers(1, pointsBuffer);
gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, pointsBuffer[0]);
gl.BufferData(OpenGL.GL_ARRAY_BUFFER, triangle, OpenGL.GL_STATIC_DRAW);
gl.GenBuffers(1, colorsBuffer);
gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, colorsBuffer[0]);
gl.BufferData(OpenGL.GL_ARRAY_BUFFER, colors, OpenGL.GL_STATIC_DRAW);
最后,这是每次迭代中发生的事情:
gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);
gl.UseProgram(program.Id);
gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, pointsBuffer[0]);
gl.EnableVertexAttribArray(0);
gl.VertexAttribPointer(0, 3, OpenGL.GL_FLOAT, false, 0, IntPtr.Zero);
gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, colorsBuffer[0]);
gl.EnableVertexAttribArray(1);
gl.VertexAttribPointer(1, 3, OpenGL.GL_FLOAT, false, 0, IntPtr.Zero);
gl.DrawArrays(OpenGL.GL_TRIANGLES, 0, 12);
gl.DisableVertexAttribArray(0);
gl.DisableVertexAttribArray(1);
更新 1。这是传递给缓冲区的数据。
private float[] triangle = {
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.0f, 0.5f, 0.0f,
};
private float[] colors = {
1.0f, 0.0f, 0.0f,
0.5f, 1.0f, 0.0f,
0.0f, 0.5f, 0.0f
};
private uint[] arrayBuffer = new uint[1];
private uint[] pointsBuffer = new uint[1];
private uint[] colorsBuffer = new uint[1];
UPD 2这是着色器编译代码
private uint CompileProgram(OpenGL gl, List<ShaderMetadata> shaders)
{
var program = gl.CreateProgram();
shaders.ForEach(shader => gl.AttachShader(program, CompileShader(gl, shader.Path, shader.Mode)));
gl.LinkProgram(program);
var status = ProgramErrorInfo(gl, program);
if (!String.IsNullOrEmpty(status))
{
throw new ArgumentException(status);
}
return program;
}
private uint CompileShader(OpenGL gl, string path, uint kind)
{
using (var fs = new FileStream(path, FileMode.Open))
{
using (var reader = new StreamReader(fs))
{
var program = reader.ReadToEnd();
var id = gl.CreateShader(kind);
gl.ShaderSource(id, program);
gl.CompileShader(id);
var status = ShaderErrorInfo(gl, id);
if (!String.IsNullOrEmpty(status))
{
throw new ArgumentException(status);
}
return id;
}
}
}
private string ShaderErrorInfo(OpenGL gl, uint shaderId)
{
StringBuilder builder = new StringBuilder(2048);
gl.GetShaderInfoLog(shaderId, 2048, IntPtr.Zero, builder);
return builder.ToString();
}
private string ProgramErrorInfo(OpenGL gl, uint programId)
{
StringBuilder builder = new StringBuilder(2048);
gl.GetProgramInfoLog(programId, 2048, IntPtr.Zero, builder);
return builder.ToString();
}
UPD 3 以下是编译器的调用方式
var programId = CompileProgram(gl, new List<ShaderMetadata>
{
new ShaderMetadata(
@"vertex.shader",
OpenGL.GL_VERTEX_SHADER),
new ShaderMetadata(
@"fragment.shader",
OpenGL.GL_FRAGMENT_SHADER)
});