怎么用ray tracing画第一张图

怎么用ray tracing画第一张图

ray 类:(位于ray.h中。ray.cpp为空)


 
 
  1. #ifndef RAY_H
  2. #define RAY_H
  3. #include "vec3.h"
  4. class ray
  5. {
  6. public:
  7. ray() {}
  8. ray( const vec3& a, const vec3& b) { A = a; B = b; }
  9. vec3 orgin() const { return A; }
  10. vec3 direction() const { return B; }
  11. vec3 point_at_parameter(float t) const { return A + t*B; }
  12. //已知t时,可以获得光线上该点的坐标(向量)
  13. vec3 A;
  14. vec3 B;
  15. /*
  16. virtual ~ray();
  17. protected:
  18. private:
  19. */
  20. };
  21. #endif // RAY_H


ray类中其实主要是定义两个向量:起点向量(坐标)A和方向向量B。

从ray的方程R(t)=A+t*B可以得知A,B两个向量即可决定一条光线。


 
 
  1. #include <iostream>
  2. #include <fstream>
  3. #include "ray.h"
  4. using namespace std;
  5. vec3 color(const ray& r)
  6. {
  7. vec3 unit_direction = unit_vector(r.direction());
  8. float t = 0.5*(unit_direction.y() + 1.0);
  9. return ( 1.0-t)*vec3( 1.0, 1.0, 1.0) + t*vec3( 0.5, 0.7, 1.0);
  10. }
  11. int main()
  12. {
  13. int nx = 200;
  14. int ny = 100;
  15. ofstream outfile( ".\\results\\RaysBackgroundY.txt", ios_base::out);
  16. outfile << "P3\n" << nx << " " << ny << "\n255\n";
  17. std:: cout << "P3\n" << nx << " " << ny << "\n255\n";
  18. vec3 lower_left_corner(-2.0, -1.0, -1.0);
  19. vec3 horizontal(4.0, 0.0, 0.0);
  20. vec3 vertical(0.0, 2.0, 0.0);
  21. vec3 origin(0.0, 0.0, 0.0);
  22. for ( int j = ny -1; j >= 0; j--)
  23. {
  24. for ( int i = 0; i < nx; i++)
  25. {
  26. float u = float(i) / float(nx);
  27. float v = float(j) / float(ny);
  28. ray r(origin, lower_left_corner + u*horizontal + v*vertical);
  29. vec3 col = color(r);
  30. int ir = int ( 255.99*col[ 0]);
  31. int ig = int ( 255.99*col[ 1]);
  32. int ib = int ( 255.99*col[ 2]);
  33. outfile << ir << " " << ig << " " << ib << "\n";
  34. std:: cout << ir << " " << ig << " " << ib << "\n";
  35. }
  36. }
  37. }


原理分析:

光线起点(也就是eye或者camera)固定的情况下,光线的方向向量的变动范围既形成光线束。光线束即是eye或者camera看到画面。

也就意味着:方向向量的变动范围决定着所能看到画面的范围。

另外,光线中每个光子的频率(颜色)决定这画面的内容。

 

所以,如果我们要通过光线追踪来画图的话,只需要做两件事情:

第一步,确定光线的方向向量的活动范围函数,从而确定画面的范围、大小(一条光线对应这画面上的一个像素点)。

第二步,对每一条光线(像素点)设置颜色,(高质量图的每个像素点上可能对应多个采样)从而确定画面上的内容。

 

如下图,光线的起点为(0,0,0),要求在黑框内作图(即光线和黑框平面的交点落在黑框内)


 
 
  1. vec3 lower_left_corner(-2.0, -1.0, -1.0);
  2. vec3 horizontal(4.0, 0.0, 0.0);
  3. vec3 vertical(0.0, 2.0, 0.0);

所以,交点坐标可以表示为向量:lower_left_corner + u*horizontal + v*vertical

光线的方向向量 = 交点的向量 - 起点向量,由于起点为原点,所以方向向量=交点向量。

每个交点的u,v的值即为该像素点在整个画面中的位置。


 
 
  1. int nx = 200;
  2. int ny = 100;
  3. for ( int j = ny -1; j >= 0; j--)
  4. {
  5. for ( int i = 0; i < nx; i++)
  6. {
  7. float u = float(i) / float(nx);
  8. float v = float(j) / float(ny);
  9. ray r(origin, lower_left_corner + u*horizontal + v*vertical);
  10. /*由画面中每个像素点在画面中的相对位置每个像素点对应的光线的方向向量从而确定画面的范围/大小。(完成第一步)*/
  11. vec3 col = color(r);
  12. //根据光线对每一个像素点上色。(完成第二步)
  13. }
  14. }
  15. vec3 color(const ray& r)
  16. {
  17. vec3 unit_direction = unit_vector(r.direction());
  18. //对方向向量进行标准化。
  19. float t = 0.5*(unit_direction.y() + 1.0);
  20. //标准化之后的y值在[-1,1]中y+1在[0,2]中0.5*(y+1)在[0,1]中
  21. return ( 1.0-t)*vec3( 1.0, 1.0, 1.0) + t*vec3( 0.5, 0.7, 1.0);
  22. //t=0时,color=vec3(1,1,1),乘以255后对应的RGB为(255,255,255)
  23. //t=1时,color=vec3(0.5,0.7,1),乘以255后对应的RGB为(127.5,178.5,255)
  24. //如上两个颜色分别对应着“白色”和“浅蓝色”。
  25. /*画面颜色=(1-t)*“白色”+ t*“浅蓝色”,即画面颜色为“白色”和“浅蓝色”(沿着Y方向)的线性插值的结果。如果要换成X或者Z方向,将上面的.y()改成.x()或者.z()即可。
  26. 若要换其他颜色,设置对应的RGB值即可。RGB值和颜色的对应可参考word中“字体颜色设置”“ 其他颜色”“自定义”*/
  27. }

通过“XnView”将结果的.txt转换成.jpg后的图片:

 

白色、浅蓝色沿着Y轴线性插值:



白色、浅蓝色沿着X轴线性插值:



白色、浅蓝色沿着Z轴线性插值:



白色、粉红色沿着Y轴线性插值:



  •                     <li class="tool-item tool-active is-like "><a href="javascript:;"><svg class="icon" aria-hidden="true">
                            <use xlink:href="#csdnc-thumbsup"></use>
                        </svg><span class="name">点赞</span>
                        <span class="count">5</span>
                        </a></li>
                        <li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{&quot;mod&quot;:&quot;popu_824&quot;}"><svg class="icon" aria-hidden="true">
                            <use xlink:href="#icon-csdnc-Collection-G"></use>
                        </svg><span class="name">收藏</span></a></li>
                        <li class="tool-item tool-active is-share"><a href="javascript:;" data-report-click="{&quot;mod&quot;:&quot;1582594662_002&quot;}"><svg class="icon" aria-hidden="true">
                            <use xlink:href="#icon-csdnc-fenxiang"></use>
                        </svg>分享</a></li>
                        <!--打赏开始-->
                                                <!--打赏结束-->
                                                <li class="tool-item tool-more">
                            <a>
                            <svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg>
                            </a>
                            <ul class="more-box">
                                <li class="item"><a class="article-report">文章举报</a></li>
                            </ul>
                        </li>
                                            </ul>
                </div>
                            </div>
            <div class="person-messagebox">
                <div class="left-message"><a href="https://blog.csdn.net/libing_zeng">
                    <img src="https://profile.csdnimg.cn/8/0/8/3_libing_zeng" class="avatar_pic" username="libing_zeng">
                                            <img src="https://g.csdnimg.cn/static/user-reg-year/2x/9.png" class="user-years">
                                    </a></div>
                <div class="middle-message">
                                        <div class="title"><span class="tit"><a href="https://blog.csdn.net/libing_zeng" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}" target="_blank">图形跟班</a></span>
                                            </div>
                    <div class="text"><span>发布了321 篇原创文章</span> · <span>获赞 1205</span> · <span>访问量 88万+</span></div>
                </div>
                                <div class="right-message">
                                            <a href="https://bbs.csdn.net/topics/395532018" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-messageboard">他的留言板
                        </a>
                                                            <a class="btn btn-sm  bt-button personal-watch" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}">关注</a>
                                    </div>
                            </div>
                    </div>
    </article>
    
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值