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

发布于 2024-10-11 10:12:57 字数 937 浏览 3 评论 0原文

我正在 http://www.devmaster.net/articles/raytracing_series/part1 上研究光线追踪.php 当我遇到这段代码时:

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;

先感谢您!

I was studying Ray Tracing on http://www.devmaster.net/articles/raytracing_series/part1.php when I came across this piece of code:

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 );
}

I'm quite confused on how the author map screen coordinates to world coordinates...
Can anyone please tell me how the author derived these lines?
Or tell me how one would map screen coordinates to world coordinates?

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

Thank you in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜无邪 2024-10-18 10:12:57

编辑:这是来自 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)

EDIT: Here is relevant code from 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;

Therefore the camera is at (0,0,-5) and the screen onto which the world is being projected has top-left corner (-4,3,0) and bottom-right corner (4,-3,0).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文