这会生成 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) 那样使用它,而是跟随索引数据,并抓取 UV 线,例如 (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 上的索引,但不能摆脱顶点上的索引?因为这让我很头疼,不然我能做什么呢?
EDIT:
顶点数据和 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