2015-11-16 58 views
0

我使用「Ray Tracer從頭開始」的書作爲教程,但我沒有在相同的代碼中具有相同的resaults,我認爲它是相同的(我已經檢查過幾次)。RayTracer球形映射

我的問題是球形映射到球體的紋理。 Spherical Mapping

代碼(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);  
} 

在球命中本地點計算如下:

info.m_localHitPoint=(ray.getOrigin()+(ray.getDirection()*t)); 

其中是關閉交點

回答

2

根據您的座標空間的指向性,我有以下almos在我自己的射線追蹤牛逼等效代碼:

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

相同....它只改變紋理的旋轉,但白色錶帶是相同的。 – Menos

+1

因此,無論你的紋理只填充其圖像的一半,或者你在某處的高度值出來的因子爲2 – Alnitak

+0

當我將文件的替換值更改爲紅色時,所有球體都變成紅色......所以它是somwthing與紋理錯誤。 – Menos

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是具有此映射的材質(紋理)的球體。

+0

啊,所以你確實使用了我的方程,然後(儘管可能仍然存在「慣性」問題,也許是在'u'的計算中使用'+'而不是'-'來解釋)。我沒有發現原始問題的原因是,這種射線在「物體」空間而不是「世界」空間的實現中更爲常見。 – Alnitak