GAMES101作业5,光线与三角形求交

在这里插入图片描述

任何一个光线 r ray 在t时刻的位置为他起始位置 o origin加上时间乘以他的方向 d direction
在这里插入图片描述
光线在球上可以解出来
在这里插入图片描述
在这里插入图片描述

第一步

 for (int j = 0; j < scene.height; ++j)
    {
        for (int i = 0; i < scene.width; ++i)
        {
            // generate primary ray direction
            //刚开始给了我们一个屏幕大小为1280*960的空间,眼睛位置在屏幕的正中间
            //我们将屏幕空间归一化,左下角定义为-1,-1 右上角为1,1
            float x = (2*((float)i+0.5)/scene.width-1) * imageAspectRatio * scale;
            float y = (1.0-2*((float)j+0.5)/scene.height) * scale;

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

第二步
MT算法:用重心坐标可以表示平面内任意一个点
在这里插入图片描述

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 = v1 - v0;
    Vector3f e2 = v2 - v0;
    Vector3f s0 = orig - v0;
    Vector3f s1 = crossProduct(dir, e2);
    Vector3f s2 = crossProduct(s0, e1);

    Vector3f s = Vector3f(dotProduct(s2, e2), dotProduct(s1, s0), dotProduct(s2, dir)) / dotProduct(s1, e1);
    //tnear, u, v对应t b1 b2
    tnear = s.x;
    u = s.y;
    v = s.z;
	//保证参数有意义
    if (tnear >=0 && u >= 0 && v >=0 && 1-u-v>=0)
    {
        return true;
    }

    return false;
}

运行结果 输出文件
在这里插入图片描述

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值