为什么渲染的对象看起来扭曲了?
Why does the rendered object appear distorted?
当我试图显示一个有 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))
});
步幅不正确。步幅应该是为下一个顶点属性跳过的字节数。
在您的情况下,要到达下一个位置,您必须跳过 4 个位置浮点数和 4 个颜色浮点数,总共 32 个字节(颜色也类似)。
如果单个缓冲区中有多个属性,则步幅应为属性大小的总和。
使用 16 字节而不是 32 字节的步幅会得到三角形:(-0.5, -0.5, 0)-(1, 0, 0)-(0.5, -0.5, 0)。正如@Zebrafish 在评论中指出的那样,这些是第一个顶点的位置、第一个顶点的颜色和第二个顶点的位置。
它不是从第一个顶点的位置前进到第二个顶点的位置(相隔 32 个字节),而是前进到第一个顶点的颜色,然后再到第二个顶点的位置。
当我试图显示一个有 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))
});
步幅不正确。步幅应该是为下一个顶点属性跳过的字节数。
在您的情况下,要到达下一个位置,您必须跳过 4 个位置浮点数和 4 个颜色浮点数,总共 32 个字节(颜色也类似)。
如果单个缓冲区中有多个属性,则步幅应为属性大小的总和。
使用 16 字节而不是 32 字节的步幅会得到三角形:(-0.5, -0.5, 0)-(1, 0, 0)-(0.5, -0.5, 0)。正如@Zebrafish 在评论中指出的那样,这些是第一个顶点的位置、第一个顶点的颜色和第二个顶点的位置。
它不是从第一个顶点的位置前进到第二个顶点的位置(相隔 32 个字节),而是前进到第一个顶点的颜色,然后再到第二个顶点的位置。