我正在尝试给我的.md2模型应用纹理。我使用了Gouraud着色来着色它(底部/顶部平面三角形的标准算法),我必须使用类似的代码来处理纹理坐标U和V。但我不太明白如何插值它们。从我尝试过的结果来看,我的代码似乎只在边缘之间插值,而不是在它们之间插值。我错过了什么吗?
谢谢。
这里颜色由u作为红色,v作为绿色和255作为蓝色形成(只针对底部平面三角形):
![enter image description here](https://istack.dev59.com/jJYaP.webp)
![enter image description here](https://istack.dev59.com/Wei0V.webp)
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 = x1U;
float x1V = vertex1.GetV();
float x2V = x1V;
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;
}
}
uv
作为颜色输出,而不是采样。生成的图像将给您一个生成的想法。 - 3Dave