尝试显示具有 3 个顶点的红色三角形时出现扭曲的红色三角形(在 (-0.5,-0.5) (0.5,-0.5) (0,0.5))我正在使用此代码传递给着色器
public void LoadData<T>(BufferConfig<T> config) where T : struct, IVertex
{
Bind();
GL.BufferData(config.Target, config.VertexCount * config.Layout.SizeOfVertex, config.Vertices, config.Usage);
int offset = 0;
for (int i = 0; i < config.Layout.Attribs.Length; i++)
{
GL.EnableVertexAttribArray(i);
GL.VertexAttribPointer(
i,
config.Layout.Attribs[i].ElementCount,
config.Layout.Attribs[i].ElementType,
config.Layout.Attribs[i].IsNormalized,
config.Layout.Attribs[i].Stride,
offset
);
offset += config.Layout.Attribs[i].Stride;
}
Unbind();
}
顶点由 2 个分别代表位置和颜色的 Vector4 组成,我尝试过调试它并且值看起来很好,因为有 2 个属性,位置和颜色,循环运行 2 次,
第一次迭代:index = 0,count = 4,type = float,normalized = false,stride = 16,pointer = 0
第二次迭代:index = 1,count = 4,type = float,normalized = false,stride = 16,pointer = 16
为什么这在图像中看起来像?
编辑:
顶点着色器
#version 450 core
layout(location = 0) in vec4 position;
layout(location = 1) in vec4 color;
out vec4 vs_color;
void main(void)
{
gl_Position = position;
vs_color = color;
}
片段着色器
#version 450 core
in vec4 vs_color;
out vec4 fragColor;
void main(void)
{
fragColor = vs_color;
}
顶点
r1.AddVertices(new CVertex[] {
new CVertex(new Vector4(-0.5f,-0.5f,0f,1f), new Vector4(1f,0f,0f,1f)),
new CVertex(new Vector4(0.5f,-0.5f,0f,1f), new Vector4(0f,1f,0f,1f)),
new CVertex(new Vector4(0f,0.5f,0f,1f), new Vector4(0f,0f,1f,0f))
});