我正在尝试将纹理应用于.md2模型。我使用了Gouraud阴影对其进行着色(带有底部/顶部平坦三角形的标准算法),并且必须对纹理坐标U和V使用类似的代码。但是我不太了解如何对它们进行插值。从我尝试过的内容来看,我的代码似乎只沿边缘插值,而不是沿边缘插值。我错过了什么?谢谢。
void Rasteriser::TfillBottomFlatTriangle(Vertex vertex1, Vertex vertex2, Vertex vertex3, COLORREF c1, COLORREF c2, COLORREF c3, HDC hdc)
{
float slope1 = (vertex2.GetX() - vertex1.GetX()) / (vertex2.GetY() - vertex1.GetY());
float slope2 = (vertex3.GetX() - vertex1.GetX()) / (vertex3.GetY() - vertex1.GetY());
//U and V
float slope1U = (vertex2.GetU() - vertex1.GetU()) / (vertex2.GetY() - vertex1.GetY());
float slope2U = (vertex3.GetU() - vertex1.GetU()) / (vertex3.GetY() - vertex1.GetY());
float slope1V = (vertex2.GetV() - vertex1.GetV()) / (vertex2.GetY() - vertex1.GetY());
float slope2V = (vertex3.GetV() - vertex1.GetV()) / (vertex3.GetY() - vertex1.GetY());
float x1 = vertex1.GetX();
float x2 = vertex1.GetX() + 0.5f;
//U and V
float x1U = vertex1.GetU();
float x2U = vertex1.GetU() + 0.5f;
float x1V = vertex1.GetV();
float x2V = vertex1.GetV() + 0.5f;
if (slope2 < slope1)
{
float slopeTmp = slope1;
slope1 = slope2;
slope2 = slopeTmp;
float slopeTmpU = slope1U;
slope1U = slope2U;
slope2U = slopeTmpU;
float slopeTmpV = slope1V;
slope1V = slope2V;
slope2V = slopeTmpV;
}
for (float scanlineY = vertex1.GetY(); scanlineY <= vertex2.GetY(); scanlineY++)
{
/* loop over each pixel of horizontal line */
for (float xPos = ceil(x1); xPos < x2; xPos++)
{
float t = (xPos - x1) / (x2 - x1);
float u = (1 - t) * x1U + t * x2U;
float v = (1 - t) * x1V + t * x2V;
COLORREF colour = _model.GetTexture().GetTextureValue((int)u, (int)v);
SetPixel(hdc, (int)xPos, (int)scanlineY, colour);
}
// get new x-coordinate of endpoints of horizontal line
x1 += slope1;
x2 += slope2;
x1U += slope1U;
x2U += slope2U;
x1V += slope1V;
x2V += slope2V;
}
}
问题在这里:
//U and V
float x1U = vertex1.GetU();
float x2U = vertex1.GetU() + 0.5f;
float x1V = vertex1.GetV();
float x2V = vertex1.GetV() + 0.5f;
0.5
对于uv坐标来说很多,所以这告诉我这是错误的。
我认为正确的方法是:
//U and V
float x1U = vertex1.GetU();
float x2U = x1U;
float x1V = vertex1.GetV();
float x2V = x1V;
我认为正在发生的事情是每个三角形都包含您纹理的一半,我认为我们可以在图像中看到它。