【Ray Tracing The Next Week 超详解】 光线追踪2-6 Cornell box
?
Chapter 6:Rectangles and Lights
?
今天,我們來學習長方形區域光照
?
?先看效果
?
?light
首先我們需要設計一個發光的材質
/// light.hpp// ----------------------------------------------------- // [author] lv // [begin ] 2019.1 // [brief ] the areaLight-class for the ray-tracing project // from the 《ray tracing the next week》 // -----------------------------------------------------#pragma oncenamespace rt {//the statement of areaLight classclass areaLight :public material{ public:areaLight() { }areaLight(texture* mat) :_emit(mat) { }virtual bool scatter(const ray& InRay, const hitInfo& info, rtvec& attenuation, ray& scattered)const { return false; }virtual rtvec emitted(rtvar u, rtvar v, const rtvec& p)const { return _emit->value(u, v, p); }private:texture* _emit;};} // rt namespace?
關于設計方面,我們需要把發光函數設為可繼承虛函數,基類也要添加,但是不是所有的材質都需要發光,所以,基類中的發光函數并不需要設置為純虛
/// material.hpp// ----------------------------------------------------- // [author] lv // [begin ] 2018.12 // [brief ] the material-class for the ray-tracing project // from the 《ray tracing in one week》 // -----------------------------------------------------#pragma oncenamespace rt {// the statement of material classclass material{ public:/*@brief: produce a scattered ray@param: InRay -> Incident lightinfo -> the information of intersect-point(hit-point)attenuation -> when scattered, how much the ray should be attenuated by tis reflectance Rscattered -> as we talk, it is a new sight; orit is the scattered ray with the intersect-point@retur: the function calculate a scattered ray or not*/virtual bool scatter(const ray& InRay, const hitInfo& info, rtvec& attenuation, ray& scattered)const = 0;/*@brief: 自發光@param: 紋理所需信息@retur: 紋理像素值*/virtual rtvec emitted(rtvar u, rtvar v, const rtvec& p)const { return rtvec(); }};}?
這樣的話,一般的材質繼承之后,發光為黑色即不發光,較為合理
我們既然添加了光照,那么計算插值函數時候也要將它加進去
?
到此,我們的發光材質就設置妥當了
?
?rectangle
我們定義的長方形均為平行于軸的
(引用書上一張圖)
假設長方形位于 z = k 平面,x和y邊界如上,交點為P(x,y,k)
我們如何確定光線參數t?
已知:
光線:p(t) = eye + t * direction
則,z方向的方程為:z(t) = eye.z + t * direction.z
那么,若滿足z = k,則
t = (k - eye.z) / direction.z
同理可得x和y的等式
?
如果,得到的x坐標或者y坐標不在邊界之內,那么就沒有相交,反之則光線和長方形相交
?
上面的代碼都比較簡單,那個 hit 呢,就是,根據已知的一個分量求出t,然后,把這個解帶入求出對應的其他兩個分量,如果其他兩個分量不在邊界內,那么返回false
反之,我們求取該點的紋理坐標,以及其他碰撞點信息記錄之
獲取包圍盒嘛,理論上面無厚薄,線無粗細,但是實際中面有厚薄,我們可以將厚度設置為0.0002,以此模擬理論厚度
同理寫出其他兩個平面類即可。
?
這個沒什么問題,我們就往下進行
我們來做Cornell box
?
相機參數設置:
?
得到的圖如下:
?
有幾個面是黑色的??也就是根本沒畫出來
我們細細看一下,發現,長方形的法向量是關鍵
比如畫出來的紅墻,對面與之平行的面的法線是朝左邊的,展現在我們視線中的是背面
?
所以,我們有時候需要反轉一下法向量
/// flip_normal.hpp// ----------------------------------------------------- // [author] lv // [begin ] 2019.1 // [brief ] the flip_normal-class for the ray-tracing project // from the 《ray tracing the next week》 // -----------------------------------------------------#pragma oncenamespace rt {class flip_normal: public intersect{ public:flip_normal(intersect * p) :_p(p) { }virtual bool hit(const ray& sight, rtvar t_min, rtvar t_max, hitInfo& info)const override{if (_p->hit(sight, t_min, t_max, info)){info._n = -info._n;return true;}return false;}virtual aabb getbox()const override{return _p->getbox();}private:intersect* _p;};} // rt namespace?
這樣就可以了,我們改一下場景
?
如下:
?
?此外,我們還需要注意的是,light對應的紋理中的數值越大光強越強
我們可以試一下
material * light = new areaLight(new constant_texture(rtvec(20, 20, 20)));如下:
?
可以看出來兩張圖對比之下,第二張亮多了
?
但是我們依舊看著很不舒服,好多黑點點,太難受了
我想啊想,為什么這么多黑點??可能是因為背景是黑色的,畢竟是漫反射,如果隨機反射失敗那就是黑色,所以隨機反射點可能產生好多黑色小點,你千萬別想著換成鏡面材質,那個更無語
所以啊,我想了下,把背景改為白色,那樣更好,畢竟色彩中摻雜一點白色,無傷大雅
如是,我改了下,效果可觀
此法只適用于Cornell box本身,具體場景下的畫面優化請見下一篇
?
?
感謝您的閱讀,生活愉快~
??
轉載于:https://www.cnblogs.com/lv-anchoret/p/10303112.html
總結
以上是生活随笔為你收集整理的【Ray Tracing The Next Week 超详解】 光线追踪2-6 Cornell box的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: App推荐 | Google Tasks
- 下一篇: 深入剖析 css