【问题标题】:Why does the rendered object appear distorted?为什么渲染的对象会出现扭曲?
【发布时间】:2018-01-01 19:54:31
【问题描述】:

我得到一个扭曲的红色三角形当我试图显示一个具有 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))
        });

【问题讨论】:

  • 请提供minimal reproducible example,以便我们 a) 放心,您不是在做 3D openGL,b) 我们可以重现问题
  • 是的,我希望这对你来说足够了
  • 您知道会发生什么吗?第一个点与第一个顶点位置匹配,但第二个顶点似乎与第一个顶点颜色匹配,即最右边的一个 (1f,0f,0f,1f)。你的布局一定是有问题的。
  • 哦,真的,所以现在当有人真正阅读这篇文章时,有人知道什么是不正确的吗?
  • 我刚刚注意到三角形顶点位置看起来好像是由您的第一个顶点位置、您的第一个颜色向量和您的第二个顶点位置组成的。

标签: c# opengl vertex vertex-attributes


【解决方案1】:

步幅不正确。步幅应该是下一个顶点属性要跳过的字节数。

在您的情况下,要到达下一个位置,您必须跳过 4 个位置的浮点数和 4 个颜色的浮点数,总共 32 个字节(颜色也是如此)。

如果您在单个缓冲区中有多个属性,则步幅应该是属性大小的总和。

使用 16 字节而不是 32 字节的跨度会产生三角形:(-0.5, -0.5, 0)-(1, 0, 0)-(0.5, -0.5, 0)。正如@Zebrafish 在 cmets 中指出的,这些是第一个顶点的位置、第一个顶点的颜色和第二个顶点的位置。

它不是从第一个顶点的位置前进到第二个顶点的位置(相距 32 个字节),而是前进到第一个顶点的颜色,然后到第二个顶点的位置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-02
    • 2016-06-20
    • 1970-01-01
    • 2022-11-28
    • 2022-10-13
    • 1970-01-01
    相关资源
    最近更新 更多