2014-01-12 51 views
0

這會生成25個主要頂點。索引緩衝區對象和UV座標不起作用

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)這樣的UV線,以便對抗我做到這一點:

 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數據頃:

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 
+0

願力量與你同在。 –

+0

您在哪些緩衝區中存儲了UV數據和頂點數據,如何將它們流式傳輸至圖形卡以及如何向着色器程序描述緩衝區? – Vengarioth

+0

我編輯了這些問題的答案的問題。 –

回答

1

的索引適用於頂點數據和紋理數據和屬性。這是沒有辦法的。例如,在一個簡單的4 quad數組中,它可以像下面那樣工作。

變種backgroundObj = [ -1,-1,-1,1 ,-1,-1,1,1- ,-1, -1,1,-1 ];

變種backgroundObjTexCoords = [ 0.000000,0.000000,1.000000,0.000000,
1.000000,1.000000,0.000000,1。000000 ];

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

EDIT1:

triangle strip image

如果採取相反的流動,如上圖中,三角形也會跟着,你需要生成頂點這樣即使訂單012,213,234等。手動完成。

+0

這只是使1個四?如果我想使用頂點1和頂點2在旁邊做另一個四邊形?它會爲此檢索錯誤的texCoords? –

+0

除非您手動執行某些操作(並且這種分割方式不正確),否則工具會使相鄰邊緣繼續到下一個四邊形。例如,在你的情況下,排序會像[0,3,2,0,2,1,(next quad),1,2,4等。 – prabindh

+0

雖然我不使用工具,但我...只是自己生成四邊形。 –

相關問題