• 【GAMES101】作业 5: 光线与三角形相交


    作业 5: 光线与三角形相交

    作业要求

    在这部分的课程中,我们将专注于使用光线追踪来渲染图像。在光线追踪中最重要的操作之一就是找到光线与物体的交点。一旦找到光线与物体的交点,就可以执行着色并返回像素颜色。在这次作业中,我们需要实现两个部分:光线的生成和光线与三角的相交。本次代码框架的工作流程为:

    1. main 函数开始。我们定义场景的参数,添加物体(球体或三角形)到场景中,并设置其材质,然后将光源添加到场景中。
    2. 调用 Render(scene) 函数。在遍历所有像素的循环里,生成对应的光线并将返回的颜色保存在帧缓冲区(framebuffer)中。在渲染过程结束后,帧缓冲区中的信息将被保存为图像。
    3. 在生成像素对应的光线后,我们调用 CastRay 函数,该函数调用 trace 来查询光线与场景中最近的对象的交点。
    4. 然后,我们在此交点执行着色。我们设置了三种不同的着色情况,并且已经为你提供了代码。

    你需要修改的函数是:

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

    Render()

    为了得到每个像素的光线,我们需要得到每个像素的世界坐标,如何从像素坐标 i,j 到世界坐标 x,y 就是我们需要完成的。

    分析代码框架可以发现,我们已经将眼睛的位置防止在坐标原点

    Vector3f eye_pos(0);
    
    • 1

    并且从下面代码也可以看出,屏幕与 x O y xOy xOy 平面的距离为 -1,即屏幕位于 z = − 1 z=-1 z=1 的平面上

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

    对于每一宽 w i d t h width width h e i g h t height height 的帧,我们需要将上面的像素坐标 ( i , j ) (i,j) (i,j) 映射到世界坐标 ( x , y ) (x,y) (x,y)

    首先,先从像素坐标得到像素中心的坐标 ( i + 0.5 , j + 0.5 ) (i+0.5, j+0.5) (i+0.5,j+0.5),再将坐标进行归一化 ( i + 0.5 w i d t h , j + 0.5 h e i g h t ) (\frac{i+0.5}{width}, \frac{j+0.5}{height}) (widthi+0.5,heightj+0.5);因为 frame 的 y y y 坐标与世界坐标式相反的,所以将 y y y 坐标取反 ( i + 0.5 w i d t h , − j + 0.5 h e i g h t ) (\frac{i+0.5}{width}, -\frac{j+0.5}{height}) (widthi+0.5,heightj+0.5);我们知道屏幕的中心是在坐标 ( 0 , 0 , − 1 ) (0, 0, -1) (0,0,1) 上,所以需要将坐标平移使得其上下左右对称 ( i + 0.5 w i d t h − 0.5 , − j + 0.5 h e i g h t + 0.5 ) (\frac{i+0.5}{width}-0.5, -\frac{j+0.5}{height}+0.5) (widthi+0.50.5,heightj+0.5+0.5)

    因为我们知道
    s c a l e = tan ⁡ ( f o v / 2 ) = h e i g h t / 2 ,   i m a g e A s p e c t R a t i o = w i d t h / h e i g h t scale = \tan(fov/2)=height/2,\ imageAspectRatio=width/height scale=tan(fov/2)=height/2, imageAspectRatio=width/height
    所以
    h e i g h t = 2 s c a l e ,   w i d t h = 2 s c a l e ⋅ i m a g e A s p e c t R a t i o height=2scale,\ width=2scale\cdot imageAspectRatio height=2scale, width=2scaleimageAspectRatio
    在这里插入图片描述

    最后将归一化后的坐标乘以画面的大小,最终得到世界坐标为
    ( 2 s c a l e ⋅ i m a g e A s p e c t R a t i o ⋅ ( i + 0.5 w i d t h − 0.5 ) , 2 s c a l e ( − j + 0.5 h e i g h t + 0.5 ) ) (2 scale\cdot imageAspectRatio\cdot (\frac{i+0.5}{width}-0.5), 2scale(-\frac{j+0.5}{height}+0.5)) (2scaleimageAspectRatio(widthi+0.50.5),2scale(heightj+0.5+0.5))
    所以

    float x = 2 * scale * imageAspectRatio * ((i + 0.5) / scene.width - 0.5);
    float y = 2 * scale * (-(j + 0.5) / scene.height + 0.5);
    
    • 1
    • 2

    再设置方向向量,并初始化

    Vector3f dir = normalize(Vector3f(x, y, -1)); 
    
    • 1

    完整代码:

    void Renderer::Render(const Scene& scene)
    {
        std::vector<Vector3f> framebuffer(scene.width * scene.height);
    
        float scale = std::tan(deg2rad(scene.fov * 0.5f));
        float imageAspectRatio = scene.width / (float)scene.height;
    
        // Use this variable as the eye position to start your rays.
        Vector3f eye_pos(0);
        int m = 0;
        for (int j = 0; j < scene.height; ++j)
        {
            for (int i = 0; i < scene.width; ++i)
            {
                // generate primary ray direction
                    float x = 2 * scale * imageAspectRatio * ((i + 0.5) / scene.width - 0.5);
                    float y = 2 * scale * (-(j + 0.5) / scene.height + 0.5);
                // TODO: Find the x and y positions of the current pixel to get the direction
                // vector that passes through it.
                // Also, don't forget to multiply both of them with the variable *scale*, and
                // x (horizontal) variable with the *imageAspectRatio*            
    
                Vector3f dir = normalize(Vector3f(x, y, -1)); // Don't forget to normalize this direction!
                framebuffer[m++] = castRay(eye_pos, dir, scene, 0);
            }
            UpdateProgress(j / (float)scene.height);
        }
    
        // save framebuffer to file
        FILE* fp = fopen("binary.ppm", "wb");
        (void)fprintf(fp, "P6\n%d %d\n255\n", scene.width, scene.height);
        for (auto i = 0; i < scene.height * scene.width; ++i) {
            static unsigned char color[3];
            color[0] = (char)(255 * clamp(0, 1, framebuffer[i].x));
            color[1] = (char)(255 * clamp(0, 1, framebuffer[i].y));
            color[2] = (char)(255 * clamp(0, 1, framebuffer[i].z));
            fwrite(color, 1, 3, fp);
        }
        fclose(fp);    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    rayTriangleIntersect()

    Möller Trumbore Algorithm 是一种更快更便捷更直观的求解交点方式
    O → + t D → = ( 1 − b 1 − b 2 ) P → 0 + b 1 P → 1 + b 2 P → 2 \overrightarrow{\mathbf{O}}+t \overrightarrow{\mathbf{D}}=\left(1-b_{1}-b_{2}\right) \overrightarrow{\mathbf{P}}_{0}+b_{1} \overrightarrow{\mathbf{P}}_{1}+b_{2} \overrightarrow{\mathbf{P}}_{2} O +tD =(1b1b2)P 0+b1P 1+b2P 2

    [ t b 1 b 2 ] = 1 S → 1 ∙ E → 1 [ S → 2 ⋅ E → 2 S → 1 ⋅ S → S → 2 ∙ D → ] \left[

    tb1b2" role="presentation" style="position: relative;">tb1b2
    \right]=\frac{1}{\overrightarrow{\mathbf{S}}_{1} \bullet \overrightarrow{\mathbf{E}}_{1}}\left[
    S2E2S1SS2D" role="presentation" style="position: relative;">S2E2S1SS2D
    \right] tb1b2 =S 1E 11 S 2E 2S 1S S 2D

    其中
    E → 1 = P → 1 − P → 0 E → 2 = P → 2 − P → 0 S → = O → − P → 0 S → 1 = D → × E → 2 S → 2 = S → × E → 1

    E1=P1P0E2=P2P0S=OP0S1=D×E2S2=S×E1" role="presentation" style="position: relative;">E1=P1P0E2=P2P0S=OP0S1=D×E2S2=S×E1
    E 1=P 1P 0E 2=P 2P 0S =O P 0S 1=D ×E 2S 2=S ×E 1
    解出来后,需要进行验证: t > 0 , b 1 ⩾ 0 , b 2 ⩾ 0 , b 3 ⩾ 0 t>0,b_1\geqslant 0,b_2\geqslant 0,b_3\geqslant 0 t>0,b10,b20,b30

    按照上面式子实现即可

    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 E1, E2, S, S1, S2, res;
        E1 = v1 - v0;
        E2 = v2 - v0;
        S = orig - v0;
        S1 = crossProduct(dir, E2);
        S2 = crossProduct(S, E1);
    
        res = 1.0f / dotProduct(S1, E1) * Vector3f(dotProduct(S2, E2), dotProduct(S1, S), dotProduct(S2, dir));
    
        tnear = res.x;
        u = res.y;
        v = res.z;
    
        if (tnear > 0 && u >= 0 && v >= 0 && 1 - u - v >= 0) return true;
        return false;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    效果

  • 相关阅读:
    Google Earth Engine(GEE)——一个简单的影像波段时序折现图
    【布局优化】基于帝国企鹅算法求解潮流计算的电力系统总线优化问题附matlab代码
    C语言 自定义函数|函数
    【商鼎云更新】你想要的相册备份功能,已经安排上了​
    零基础备考软考高项要多久?
    linux下Qt的pro文件
    朴素迪氏最短单源路径的原理及C++实现
    图像的高频和低频细节
    win10添加回环网卡步骤
    C++ 函数模板
  • 原文地址:https://blog.csdn.net/m0_57714486/article/details/126291456