2013-06-03 80 views
2

我想在較大的四邊形上重複小2×2像素紋理,例如50x50像素。紋理重複

組頂點 -

float X = 100, Y = 100, Width = 50, Height = 50; 
float TextureLeft = 0, TextureTop = 0, TextureRight = 25, TextureBottom = 25; 

Vertices[0].x = X; 
Vertices[0].y = Y + Height; 
Vertices[0].z = 0; 
Vertices[0].rhw = 1; 
Vertices[0].tu = TextureLeft; 
Vertices[0].tv = TextureBottom; 

Vertices[1].x = X; 
Vertices[1].y = Y; 
Vertices[1].z = 0; 
Vertices[1].rhw = 1; 
Vertices[1].tu = TextureLeft; 
Vertices[1].tv = TextureTop; 

Vertices[2].x = X + Width; 
Vertices[2].y = Y; 
Vertices[2].z = 0; 
Vertices[2].rhw = 1; 
Vertices[2].tu = TextureRight; 
Vertices[2].tv = TextureTop; 

Vertices[3].x = X; 
Vertices[3].y = Y + Height; 
Vertices[3].z = 0; 
Vertices[3].rhw = 1; 
Vertices[3].tu = TextureLeft; 
Vertices[3].tv = TextureBottom; 

Vertices[4].x = X + Width; 
Vertices[4].y = Y; 
Vertices[4].z = 0; 
Vertices[4].rhw = 1; 
Vertices[4].tu = TextureRight; 
Vertices[4].tv = TextureTop; 

Vertices[5].x = X + Width; 
Vertices[5].y = Y + Height; 
Vertices[5].z = 0; 
Vertices[5].rhw = 1; 
Vertices[5].tu = TextureRight; 
Vertices[5].tv = TextureBottom; 

抽獎 -

DrawPrimitive(D3DPT_TRIANGLELIST, 0, 6); 

問題是在三角形的邊緣「毛刺」,因爲錯誤的頂點大概座標,並在四邊框「毛刺」。

原有的質感 - http://i.imgur.com/tNqYePs.png

結果 - http://i.imgur.com/sgUZvqE.png

+0

您尚未指定您正在使用的光柵化器狀態。也許你錯過了紋理夾/重複設置。在DrawPrimitive設置紋理之前你還要調用什麼? –

+0

你能告訴我應該設置哪個光柵器/鉗位/重複設置?我用單紋理四繪製空場景。 – Demion

+0

「重複」設置在DX中稱爲「換行」。如果您對這些值有疑問,我已將答案添加到MSDN的鏈接中。 –

回答

1

該呼叫之前的DrawPrimitive,您應該設置紋理包裹在本article

// For the textures other than the first one use "D3DVERTEXTEXTURESAMPLER0+index" 
YourDevice->SetSamplerState(D3DVERTEXTEXTURESAMPLER0, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP); 
YourDevice->SetSamplerState(D3DVERTEXTEXTURESAMPLER0, D3DSAMP_ADDRESSV, D3DTADDRESS_WRAP); 

要消除對角線上的毛刺,您可以使用單個Quad而不是兩個三角形。

在邊緣的問題被認爲是here。您必須爲每個紋理座標添加小偏移量。 「小」是指像素的歸一化的一半。如果您的紋理分辨率爲512x512,則將每個u/v座標添加(0.5/512.0)。

+0

謝謝你的回答,但是這完全沒有改變。在邊界和三角形之間的邊緣仍然存在「毛刺」。 – Demion

+0

我在這個問題上加了一點點,希望它有幫助。首先嚐試添加偏移量。 –

1

如果繪製2d,則紋理化時必須將0.5px添加到U和V座標。這會給你準確的像素/紋理精度。否則,每次都會丟失0.5像素,紋理看起來會模糊。