RayTracer球形映射

问题描述 投票:0回答:2

我正在使用“Ray Tracer From The Ground Up”一书作为教程,但我在相同的代码中没有相同的故障,我认为它是相同的(我已经检查了几次)。

我的问题是球形将纹理映射到Sphere.Spherical Mapping

代码(d_type :: Bfloat是double):

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
2个回答
2
投票

根据坐标空间的惯用手法,我在自己的光线跟踪器中有以下几乎相同的代码:

double u = 0.5 - atan2(n.z, n.x) * math::one_over_2pi;
double v = 0.5 - asin(n.y) * math::one_over_pi;

注意0.5的使用,在我的例子中,坐标都在0..1范围内运行。


0
投票

这一切都很奇怪......但是球体的光辉必须是1 ......并且一切正常;)enter image description here

问题出在localHitPoint上。它不是本地的全球所以,所以在this问题中,一切都被解释了。 SphereMapping的工作代码:

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
    {

        Vector3Bf normal=localHitPoint - m_sphere->getCenter();
        Vector3Bf::normalize(normal);
        d_type::Bfloat u=0.5+atan2(normal.z,normal.x)*INV_TWO_PI;
        d_type::Bfloat v = 0.5-asin(normal.y)*INV_PI;
        column =(int)(m_width*u);
        row =(int)(m_height)*v;
    }

其中m_sphere是具有此映射的材质(纹理)的球体。

© www.soinside.com 2019 - 2024. All rights reserved.