ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

games101,作业5(whitted-style光线追踪)

2021-12-20 23:30:17  阅读:254  来源: 互联网

标签:style scale Vector3f float scene whitted 像素 games101 dir


任务

你需要修改的函数是:

  • Renderer.cpp 中的 Render():这里你需要为每个像素生成一条对应的光线,然后调用函数 castRay() 来得到颜色,最后将颜色存储在帧缓冲区的相应像素中。
  • Triangle.hpp 中的 rayTriangleIntersect(): v0, v1, v2 是三角形的三个顶点,orig 是光线的起点,dir 是光线单位化的方向向量。tnear, u, v 是你需要使用我们课上推导的 Moller-Trumbore 算法来更新的参数。

说明

第一个函数需要我们通过视口视角和像素窗口大小,向每一个像素投射一条光线(生成一个通过特定像素的光线)。

第二个函数用来实现课上讲过的计算。理论部分在另一篇blog中有提到。

在这次的框架中不再使用opencv,和Eigen库。
因此:

  1. 对于矩阵、向量的计算在自定义的Vector库中(需要的矩阵运算在库中查找)。
  2. 不使用opencv使得此次代码中只能将图片保存为ppm格式(一种最简单的RGB存储格式,这种格式会使文件很大)。

第一个函数

void Renderer::Render(const Scene& scene)中提供了参数

  • float scale = std::tan(deg2rad(scene.fov * 0.5f));为一半视角的tan值。
  • float imageAspectRatio = scene.width / (float)scene.height;为视口宽高比。

对于每一个像素:(具体解析在代码中注释给出)

	for (int j = 0; j < scene.height; ++j){
        for (int i = 0; i < scene.width; ++i){
            // generate primary ray direction
            float x = 0.0f;
            float y = 0.0f;
            //首先我们这里需要一个光线的方向,这个方向为(x,y,-1);
            //即已经确定视距为1,因为视角为scene.fov已经确定,所以tanΘ已知(为scale),得到y的范围为[-scale,scale]
            //同理确定了x的范围为[-scale*imageAspect,scale*imageAspect].

            //而在循环中xi的值为[0,width],yi的值为[0,height]。
            //要将范围[0,height]变为[-tanΘ,tanΘ]需要:
            //y = (static_cast<float>(j) / scene.height * 2 - 1) * scale;
            //同理 
            //x = (static_cast<float>(i) / scene.width * 2 - 1) * scale * imageAspectRatio;
            

            //对于每一个像素,像素点由像素左上方点表示,为使光线通过像素中心,有:
            y = ((static_cast<float>(j) + 0.5) / scene.height * 2 - 1) * scale;
            x = ((static_cast<float>(i) + 0.5) / scene.width * 2 - 1) * scale * imageAspectRatio;
            //因为屏幕空间y轴由上自下依次递增,而世界空间y轴由下自上,故需要翻转y轴方向。

            y = -y;

            Vector3f dir = Vector3f(x, y, -1); // Don't forget to normalize this direction!
            dir = normalize(dir);

            framebuffer[m++] = castRay(eye_pos, dir, scene, 0);
        }
        UpdateProgress(j / (float)scene.height);
    }

第二个函数

这个函数很简单,只要实现讲解的内容就好。

传入三角形的三个顶点V0,V1,V2。
传入光线点光源orig
传入光线传播方向dir

返回bool表示是否相交。
返回tnear(引用传值)记录光源传播到该点的时间。
返回u,v(引用传值)记录该点在三角形中的重心坐标。

bool rayTriangleIntersect(const Vector3f& v0, const Vector3f& v1, const Vector3f& v2, const Vector3f& orig,
                          const Vector3f& dir, float& tnear, float& u, float& v)
{
    // TODO: Implement this function that tests whether the triangle
    // that's specified bt v0, v1 and v2 intersects with the ray (whose
    // origin is *orig* and direction is *dir*)
    // Also don't forget to update tnear, u and v.
    Vector3f E_1 = v1 - v0;
    Vector3f E_2 = v2 - v0;
    Vector3f S = orig - v0;
    Vector3f S_1 = crossProduct(dir, E_2);
    Vector3f S_2 = crossProduct(S, E_1);

    float factor = dotProduct(S_1, E_1);
    float t = dotProduct(S_2, E_2)/ factor;
    float b1 = dotProduct(S_1, S) / factor;
    float b2 = dotProduct(S_2, dir) / factor; 
    
    if (t > 0 && b1 > 0 && b2 > 0 && (b1 + b2) < 1) {
        tnear = t;//记录该像素与三角形相交所需的时间
        u = b1;
        v = b2;//记录相交点重心坐标
        return true;
    }

    return false;
}

最终效果

在这里插入图片描述

标签:style,scale,Vector3f,float,scene,whitted,像素,games101,dir
来源: https://blog.csdn.net/weixin_44518102/article/details/122052236

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有