光线追踪问题,如何将屏幕坐标映射到世界坐标?

时间:2011-01-09 17:25:03

标签: coordinates raytracing

当我遇到这段代码时,我正在http://www.devmaster.net/articles/raytracing_series/part1.php研究Ray Tracing:

void Engine::InitRender()
{
// set first line to draw to
m_CurrLine = 20;
// set pixel buffer address of first pixel
m_PPos = 20 * m_Width;
// screen plane in world space coordinates
m_WX1 = -4, m_WX2 = 4, m_WY1 = m_SY = 3, m_WY2 = -3;
// calculate deltas for interpolation
m_DX = (m_WX2 - m_WX1) / m_Width;
m_DY = (m_WY2 - m_WY1) / m_Height;
m_SY += 20 * m_DY;
// allocate space to store pointers to primitives for previous line
m_LastRow = new Primitive*[m_Width];
memset( m_LastRow, 0, m_Width * 4 );
}

我对作者地图屏幕如何坐标到世界坐标感到困惑...
任何人都可以告诉我作者如何得出这些线? 或者告诉我如何将屏幕坐标映射到世界坐标?

// screen plane in world space coordinates
m_WX1 = -4, m_WX2 = 4, m_WY1 = m_SY = 3, m_WY2 = -3;

提前谢谢!

1 个答案:

答案 0 :(得分:1)

编辑:以下是来自raytracer.cpp的相关代码:

// render scene
vector3 o( 0, 0, -5 );

// initialize timer
int msecs = GetTickCount();

// reset last found primitive pointer
Primitive* lastprim = 0;

// render remaining lines
for(int y = m_CurrLine; y < (m_Height - 20); y++)
{
    m_SX = m_WX1;

    // render pixels for current line
    for ( int x = 0; x < m_Width; x++ )
    {
        // fire primary ray
        Color acc( 0, 0, 0 );
        vector3 dir = vector3( m_SX, m_SY, 0 ) - o;
        NORMALIZE( dir );
        Ray r( o, dir );
        float dist;
        Primitive* prim = Raytrace( r, acc, 1, 1.0f, dist );
        int red = (int)(acc.r * 256);
        int green = (int)(acc.g * 256);
        int blue = (int)(acc.b * 256);
        if (red > 255) red = 255;
        if (green > 255) green = 255;
        if (blue > 255) blue = 255;
        m_Dest[m_PPos++] = (red << 16) + (green << 8) + blue;
        m_SX += m_DX;
    }

    m_SY += m_DY;

    // see if we've been working to long already
    if ((GetTickCount() - msecs) > 100) 
    {
        // return control to windows so the screen gets updated
        m_CurrLine = y + 1;
        return false;
    }
}

return true;

因此,相机位于(0,0,-5),投影世界的屏幕位于左上角(-4,3,0)和右下角(4,-3,0)