2

我想在更大的四边形上重复小的 2x2 像素纹理,例如 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

4

2 回答 2

1

在调用 DrawPrimitive 之前,您应该设置纹理环绕,如本文所述

// 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 而不是两个三角形。

这里考虑边缘的问题。您必须为每个纹理坐标添加小的偏移量。“小”是指归一化的一半像素。如果您的纹理分辨率为 512x512,则将 (0.5/512.0) 添加到每个 u/v 坐标。

于 2013-06-03T09:50:53.130 回答
1

如果绘制 2d,则必须在纹理时向 U 和 V 坐标添加 0.5px。这将为您提供精确的像素/纹素精度。否则每次都会丢失 0.5 个像素,并且纹理看起来会模糊。

于 2013-06-03T13:07:57.220 回答