Demion
Demion

Reputation: 867

Texture repeating

I want to repeat small 2x2 pixels texture on a bigger quad, for instance, 50x50 pixels.

Set vertices -

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;

Draw -

DrawPrimitive(D3DPT_TRIANGLELIST, 0, 6);

Problem is "glitch" in the edge between the triangles, probably because of wrong vertices coordinates and also "glitch" on quad borders.

Original texture - https://i.sstatic.net/X8l0Z.png

Result - https://i.sstatic.net/Tub9W.png

Upvotes: 2

Views: 479

Answers (2)

Darthman
Darthman

Reputation: 457

If you draw 2d, you must add 0.5px to U and V coordianates when texturing. This will give you exact pixel/texel precision. Otherwise you will lose 0.5 pixels every time and texture will look blurry.

Upvotes: 1

Viktor Latypov
Viktor Latypov

Reputation: 14467

Before the call to DrawPrimitive you should setup the texture wrapping as in this 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);

To eliminate the glitch at the diagonal you may use the single Quad instead of two triangles.

The problem at the edges is considered here. You have to add small offset to each texture coordinate. "Small" means a normalized half of the pixel. If your texture resolution is 512x512, then add (0.5/512.0) to each of the u/v coords.

Upvotes: 1

Related Questions