我正在尝试将纹理应用于我的.md2模型。我使用了Gouraud阴影对其进行着色(带有底部/顶部平坦三角形的标准算法),并且必须对纹理坐标U和V使用类似的代码。但是我不太了解如何对它们进行插值。从我尝试过的内容来看,我的代码似乎只沿边缘插值,而不是沿边缘插值。我错过了什么? 谢谢。
在这里,颜色由u形成为红色,v为绿色和255为蓝色(仅适用于底部的扁平三角形):
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;
}
}
答案 0 :(得分:0)
问题在这里:
//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;
快速健全性测试:对于最初的y
,我们有x1U = Vertex1.u
和x2U = Vertex1.u
。对于最后的y
,我们有x1U = Vertex2.u
,x2U = Vertex3.u
对吗?
我认为正在发生的事情是每个三角形都包含您纹理的一半,我认为我们可以在图像中看到它。
编辑: 我在python中实现了相同的算法,并得到了预期的结果:
import numpy as np
p = np.array([[0,0],
[-5,10],
[10,10]],dtype=np.float)
uv = np.array([[0.1,0.6],
[0.2,0.5],
[0.3,0.4]])
slope = (p[[1,2],0] - p[0,0])/(p[[1,2],1] - p[0,1])
slope_uv = (uv[[1,2],:] - uv[0,:])/(p[[1,2],1] - p[0,1]).reshape(-1,1)
x1 = p[0,0]
x2 = p[0,0] + 0.5
uv1 = uv[0,:]
uv2 = np.copy(uv1)
result = []
for scanY in range(int(p[0,1]),int(p[1,1])):
for x in range(int(np.ceil(x1)),int(x2)):
t = (x-x1)/(x2-x1)
u = (1-t) * uv1 + t * uv2
result.append((x,scanY,u[0],u[1]))
x1 += slope[0]
x2 += slope[1]
uv1 += slope_uv[0,:]
uv2 += slope_uv[1,:]
#%%
R = np.array(result,dtype=float)
from matplotlib import pyplot as plt
plt.figure()
plt.subplot(2,1,1)
plt.scatter(R[:,0],R[:,1],c = R[:,2])
plt.title('u')
plt.colorbar()
plt.subplot(2,1,2)
plt.scatter(R[:,0],R[:,1],c = R[:,3])
plt.title('v')
plt.colorbar()
具有以下结果:
我想您的问题是顶点的uv
值不正确。
此外,我可以在您的代码中看到以下行:
COLORREF colour = _model.GetTexture().GetTextureValue((int)u, (int)v);
这没有什么意义,因为u,v
通常在0到1之间。我认为可能是您需要在调用GetTextureValue之前将其除以纹理的大小,或者乘以该纹理。 >