如何插入 UV 坐标?
How do I interpolate the UV co-ordinates?
我正在尝试将纹理应用于我的 .md2 模型。我使用 Gouraud 阴影为其着色(使用 bottom/top 平面三角形的标准算法)并且我必须对纹理坐标 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;
}
}
问题在这里:
//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 之前除以纹理的大小或乘以它或其他东西。
我正在尝试将纹理应用于我的 .md2 模型。我使用 Gouraud 阴影为其着色(使用 bottom/top 平面三角形的标准算法)并且我必须对纹理坐标 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;
}
}
问题在这里:
//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 之前除以纹理的大小或乘以它或其他东西。