【发布时间】:2019-04-12 04:50:00
【问题描述】:
我正在使用“Ray Tracer From The Ground Up”一书作为教程,但我在相同的代码中没有相同的结果,我认为它是相同的(我已经检查了几次)。
我的问题是球形 Mapping texture to Sphere.
代码(d_type::Bfloat 是双精度):
void getTexelCoord(const Vector3Bf localHitPoint, const d_type::Bint m_width, const d_type::Bint m_height, d_type::Bint& row, d_type::Bint& column) const
{
d_type::Bfloat theta=acosf(localHitPoint.y);
d_type::Bfloat phi= atan2f(localHitPoint.x,localHitPoint.z);
if(phi<0.0)
{
phi+=TWO_PI;
}
d_type::Bfloat u =phi*INV_TWO_PI;
d_type::Bfloat v=1-theta*INV_PI;
column = (d_type::Bint)((m_width-1)*u);
row = (d_type::Bint)((m_height-1)*v);
}
virtual Colour getColour(const Info&info )const
{
d_type::Bint row,column;
if(m_mapping)
{
m_mapping->getTexelCoord(info.m_localHitPoint,hres, vres, row, column);
}
else
{
row=(int)(info.uv.x*(hres-1));
column=(int)(info.uv.y*(vres-1));
}
return m_image->getColour(row,column);
}
Colour getColour(const int row, const int column) const {
int index = column + hres * (vres - row - 1);
int pixels_size = hres*vres;
if (index < pixels_size)
return (m_pixels[index]);
else
return (Colour::Red);
}
Sphere 中的局部生命值是这样计算的:
info.m_localHitPoint=(ray.getOrigin()+(ray.getDirection()*t));
其中 t 是闭合交点
【问题讨论】:
标签: c++ raytracing