【问题标题】:Rendering using VBOs/VAOs in OpenGL4在 OpenGL4 中使用 VBO/VAO 进行渲染
【发布时间】:2015-02-07 07:00:47
【问题描述】:

我正在尝试将一些老式 OpenGL 代码更新为现代 OpenGL4。

我有一个巨大的纹理和预先镶嵌的立方体模型要渲染。

我现在得到的是这个:

这是一个白色背景上的半任意黑色三角形区域。

我的 VAO/VBO 构建代码:

        // Normal buffer
        GL.GenBuffers(1, out VBONormals);
        GL.BindBuffer(BufferTarget.ArrayBuffer, VBONormals);
        GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(Normals.Length * Vector3.SizeInBytes),
        Normals, BufferUsageHint.StaticDraw);
        GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
        // TexCoord buffer
        GL.GenBuffers(1, out VBOTexCoords);
        GL.BindBuffer(BufferTarget.ArrayBuffer, VBOTexCoords);
        GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(TexCoords.Length * Vector3.SizeInBytes),
        TexCoords, BufferUsageHint.StaticDraw);
        GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
        // Vertex buffer
        GL.GenBuffers(1, out VBO);
        GL.BindBuffer(BufferTarget.ArrayBuffer, VBO);
        GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(Positions.Length * Vector3.SizeInBytes),
        Positions, BufferUsageHint.StaticDraw);
        GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
        // Index buffer
        GL.GenBuffers(1, out VBOIndices);
        GL.BindBuffer(BufferTarget.ElementArrayBuffer, VBOIndices);
        GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(Indices.Length * sizeof(uint)),
        Indices, BufferUsageHint.StaticDraw);
        GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
        GL.GenVertexArrays(1, out VAO);
        GL.BindVertexArray(VAO);
        GL.BindBuffer(BufferTarget.ArrayBuffer, VBO);
        GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 0, 0);
        GL.BindBuffer(BufferTarget.ArrayBuffer, VBONormals);
        GL.VertexAttribPointer(1, 3, VertexAttribPointerType.Float, false, 0, 0);
        GL.BindBuffer(BufferTarget.ArrayBuffer, VBOTexCoords);
        GL.VertexAttribPointer(2, 3, VertexAttribPointerType.Float, false, 0, 0);
        GL.EnableVertexAttribArray(0);
        GL.EnableVertexAttribArray(1);
        GL.EnableVertexAttribArray(2);
        GL.BindBuffer(BufferTarget.ElementArrayBuffer, VBOIndices);
        // Clean up
        GL.BindVertexArray(0);
        GL.DisableVertexAttribArray(0);
        GL.DisableVertexAttribArray(1);
        GL.DisableVertexAttribArray(2);
        GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
        GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);

和渲染代码:

        VBOTexture.Bind();
        GL.BindVertexArray(VAO);
        //GL.DrawRangeElements(PrimitiveType.Triangles, 0, Positions.Length, Indices.Length, DrawElementsType.UnsignedInt, IntPtr.Zero);
        GL.DrawArrays(PrimitiveType.Triangles, 0, Indices.Length);

顶点着色器:

#version 430 core

layout (location = 0) in vec3 position;
layout (location = 1) in vec3 normal;
layout (location = 2) in vec3 texcoord;

layout (location = 1) uniform mat4 projection;

layout (location = 0) out vec3 f_color;

void main()
{
    f_color = normal;
    gl_Position = projection * vec4(position, 1.0);
}

片段着色器:

#version 430 core

layout (binding = 0) uniform sampler2D tex;

layout (location = 0) in vec3 f_color;

out vec4 color;

void main()
{
    vec4 tcolor = texture2D(tex, gl_TexCoord[0].st);
    float dist = 0.0;
    if (gl_TexCoord[0].x < 0.5)
    {
        dist = gl_TexCoord[0].x;
    }
    else
    {
        dist = 1.0 - gl_TexCoord[0].x;
    }
    if (gl_TexCoord[0].y < 0.5)
    {
        dist *= gl_TexCoord[0].y;
    }
    else
    {
        dist *= 1.0 - gl_TexCoord[0].y;
    }
    dist *= 32;
    dist = min(1, dist);
    color = vec4(tcolor[0] * dist, tcolor[1] * dist, tcolor[2] * dist, tcolor[3]);
}

请注意,将绘图调用切换到已注释掉的调用会产生相同的结果。

另请注意:原始代码完全相同,减去 VAO 块,并使用以下代码进行渲染:

        GL.PushClientAttrib(ClientAttribMask.ClientVertexArrayBit);
        GL.BindBuffer(BufferTarget.ArrayBuffer, VBONormals);
        GL.NormalPointer(NormalPointerType.Float, Vector3.SizeInBytes, IntPtr.Zero);
        GL.EnableClientState(ArrayCap.NormalArray);
        GL.BindBuffer(BufferTarget.ArrayBuffer, VBOTexCoords);
        GL.TexCoordPointer(2, TexCoordPointerType.Float, Vector2.SizeInBytes, IntPtr.Zero);
        GL.EnableClientState(ArrayCap.TextureCoordArray);
        GL.BindBuffer(BufferTarget.ArrayBuffer, VBO);
        GL.VertexPointer(3, VertexPointerType.Float, Vector3.SizeInBytes, IntPtr.Zero);
        GL.EnableClientState(ArrayCap.VertexArray);
        GL.BindBuffer(BufferTarget.ElementArrayBuffer, VBOIndices);
        GL.DrawElements(PrimitiveType.Quads, Indices.Length, DrawElementsType.UnsignedInt, IntPtr.Zero);
        GL.PopClientAttrib();

(除了我也出于不相关的原因将 texcoords 从 Vector2 切换到 Vector3)

我哪里出错了?

【问题讨论】:

  • 离题,但通常你希望避免 conditionals 出现在着色器中,因为性能会受到影响
  • 那段代码只是一个临时的概念验证,一点儿也没有优化。
  • 好吧,glDrawArrays() 调用在概念上是完全错误的,尤其是使用Indices.Leght 作为计数。 glDrawRangeElements() 原则上应该可以工作。当两个调用产生相同的结果时,您的索引数组要么是前 n 个整数(包括零)的平凡情况,要么是某处的数据搞砸了。只是

标签: c# opengl opentk opengl-4


【解决方案1】:

您的旧代码使用drawElements,但您的新代码使用drawArrays。

    VBOTexture.Bind();
    GL.BindVertexArray(VAO);
    //GL.DrawRangeElements(PrimitiveType.Triangles, 0, Positions.Length, Indices.Length, DrawElementsType.UnsignedInt, IntPtr.Zero);
    GL.DrawElements(PrimitiveType.Quads, Indices.Length, DrawElementsType.UnsignedInt, IntPtr.Zero);

【讨论】:

  • PrimitiveType.Quads 是错字吗?看起来索引缓冲区存储三角形,核心 GL 中没有四边形。
  • @ColonelThirtyTwo 这是他在原始代码中使用的内容
  • ...请告诉我我没有不小心从三角形切换到四边形,反之亦然,因为这很好地解释了一切...
  • 是的。三角形 -> 四边形是差异的程度。我自己都很惊讶。谢谢你发现!现在我只需要找出为什么纹理是漆黑的......可能我的着色器没有通过 texcoords 或其他东西,我可以处理它。如果没有,我现在有这个很棒的网站来寻求更多帮助:D,你肯定回答了我的主要问题。接受...(另外,我可能应该在这里使用三角形,为什么我有四边形?这对我来说是第二个问题)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多