索引缓冲区对象和UV坐标不是很好

时间:2014-01-12 12:21:19

标签: vb.net opengl opentk

这将生成25个主要顶点。

    For x As Single = -1 To 1 Step 0.5F
        For y As Single = 1 To -1 Step -0.5F
            Dim pt1 As New Vector3(x, y, 0)
            tFloats.Add(pt1)
        Next
    Next

这是索引,它组成了由32个三角形组成的16个图块,我实际生成它们但这是第一行:

        Dim inasd() As Integer = {
        0, 2, 10,
        2, 10, 12,
        10, 12, 20,
        12, 20, 22,
        20, 22, 30,
        22, 30, 32
        }

现在我正在尝试将纹理应用到每个三角形,每个纹理1个纹理。 16种不同的纹理。

现在我的问题在于当我使用

GL.DrawRangeElements(PrimitiveType.Triangles, 0, indices.Length - 1, 6, DrawElementsType.UnsignedShort, New IntPtr(0))

而不是沿着UV数据并像(0,1,2)(3,4,5)(6,7,8)那样使用它而不是跟随索引数据,并抓取像(0, 2,10)(2,10,12)(10,12,20)所以为了反驳我这样做:

        Dim UV_Data(12) As Vector2
        UV_Data(0) = New Vector2(0.0F, 1.0F)
        UV_Data(2) = New Vector2(0.0F, 0.0F)
        UV_Data(10) = New Vector2(1.0F, 1.0F)
        UV_Data(12) = New Vector2(1.0F, 0.0F)

这在第一块瓷砖上效果很好。在第二个瓷砖上因为它(10,12,20)它使用UV_Data(10)作为三角形的左上角,这是纹理的右上角所以它不起作用。无论如何,我可以摆脱UV上的索引而不是顶点上的索引吗?因为它只会让我这么头疼,否则我该怎么办?

编辑:

顶点数据和UV数据存储在两个独立的缓冲区中,如下所示:

    GL.GenBuffers(1, FloatBuffer)
    GL.BindBuffer(BufferTarget.ArrayBuffer, FloatBuffer)
    GL.BufferData(BufferTarget.ArrayBuffer, New IntPtr(floats.Length * Vector3.SizeInBytes), floats, BufferUsageHint.StaticDraw)

    GL.GenBuffers(1, UVBuffer)
    GL.BindBuffer(BufferTarget.ArrayBuffer, UVBuffer)
    GL.BufferData(BufferTarget.ArrayBuffer, New IntPtr(UV_Data.Length * Vector2.SizeInBytes), UV_Data, BufferUsageHint.StaticDraw)

他们像这样进入显卡:

    GL.EnableVertexAttribArray(0)
    GL.BindBuffer(BufferTarget.ArrayBuffer, FloatBuffer)
    GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, False, 0, 0)

    GL.EnableVertexAttribArray(1)
    GL.BindBuffer(BufferTarget.ArrayBuffer, UVBuffer)
    GL.VertexAttribPointer(1, 2, VertexAttribPointerType.Float, False, 0, 0)

我也有索引缓冲区:

    GL.BindBuffer(BufferTarget.ElementArrayBuffer, IndicesBuffer)

然后使用上面的Gl.DrawRangeElements代码绘制。

我的顶点着色器:

#version 330 core

// Input vertex data, different for all executions of this shader.
layout(location = 0) in vec3 vertexPosition_modelspace;
layout(location = 1) in vec2 vertexUV;

// Output data ; will be interpolated for each fragment.
out vec2 UV;

// Values that stay constant for the whole mesh.

void main(){

// Output position of the vertex, in clip space : MVP * position
gl_Position.xyz = vertexPosition_modelspace;
gl_Position.w = 1;

// UV of the vertex. No special space for this one.
UV = vertexUV;
}

我的片段着色器:

#version 330 core

// Interpolated values from the vertex shaders
in vec2 UV;

// Ouput data
out vec3 color;

// Values that stay constant for the whole mesh.
uniform sampler2D myTextureSampler;

void main(){

// Output color = color of the texture at the specified UV
color = texture2D( myTextureSampler, UV ).rgb;
}

编辑2:是否可以在渲染时编辑UV数据,例如:

    Dim UV_Data(indices.Length) As Vector2
    For i As Integer = 0 To 1
        UV_Data(indices(i)) = New Vector2(0.0F, 1.0F)
        UV_Data(indices(i + 1)) = New Vector2(0.0F, 0.0F)
        UV_Data(indices(i + 2)) = New Vector2(1.0F, 1.0F)
        UV_Data(indices(i + 3)) = New Vector2(1.0F, 0.0F)
        'GL.BindBuffer(BufferTarget.ArrayBuffer, UVBuffer)
        GL.BufferData(BufferTarget.ArrayBuffer, New IntPtr(UV_Data.Length * Vector2.SizeInBytes), UV_Data, BufferUsageHint.DynamicDraw)
        GL.VertexAttribPointer(1, 2, VertexAttribPointerType.Float, False, 0, 0)

        'GL.BindBuffer(BufferTarget.ElementArrayBuffer, IndicesBuffer)
        GL.BindTexture(TextureTarget.Texture2D, mapTextures.Item(i))
        GL.DrawRangeElements(PrimitiveType.Triangles, 0, indices.Length - 1, 6, DrawElementsType.UnsignedShort, New IntPtr(i * 12))
    Next

1 个答案:

答案 0 :(得分:1)

索引适用于顶点数据和纹理数据和属性。没有办法解决这个问题。例如,在一个简单的4 quad数组中,它可以像下面这样工作。

var backgroundObj = [ -1,-1,-1, 1,-1,-1, 1,1,-1, -1,1,-1 ];

var backgroundObjTexCoords = [ 0.000000,0.000000,1.000000,0.000000,
1.000000,1.00000000,0.000000,1.000000 ];

var backgroundObjIndices = [ 0,1,2, 0,2,3 ];

EDIT1:

triangle strip image

如果采用与上图相反的流程,三角形将遵循012,213,234等的顺序。即使手动操作,也需要以这种方式生成顶点。