我正在尝试在我的光线追踪器中为球体实现纹理。 我设法让一些东西运作,但我不确定它是否正确。 下面是获取纹理坐标的代码。 目前,纹理是随机生成的并在运行时生成。
virtual void GetTextureCoord(Vect hitPoint, int hres, int vres, int& x, int& y) {
float theta = acos(hitPoint.getVectY());
float phi = atan2(hitPoint.getVectX(), hitPoint.getVectZ());
if (phi < 0.0) {
phi += TWO_PI;
}
float u = phi * INV_TWO_PI;
float v = 1 - theta * INV_PI;
y = (int) ((hres - 1) * u);
x = (int) ((vres - 1) * v);
}
这就是现在的球形外观:
![enter image description here](https://istack.dev59.com/7043d.webp)
![enter image description here](https://istack.dev59.com/jD0R2.webp)
![enter image description here](https://istack.dev59.com/OboD4.webp)
u
和v
是纹理坐标,计算纹理坐标的函数通常只返回它们。将它们映射到整数纹素索引并不特定于球体,并且应该在其他地方完成。 - Wyzard