Ray Tracing in One Week Note 1

Write a Path Tracer

  • C++ fast, portable, most renders use
    inheritance and operator overloading is useful

Chapter 1 Output an image

  • PPM example: an example of a color RGB image stored in PPM
    need PPM viewer to run the code.

      P3 // Magic Number means type is pixmap and colors are coding with ASCII
      3 2 // 3 columns, 2 rows
      255 // max colors
      ------聲明部分-------
      ---------------------
      255 0 0
      0 255 0
      0 0 255
      255 255 0 
      255 255 255 
      0 0 0
    

Chapter 2 vec3 Class

4D vector
  • 3D + a coord for geometry
  • RGB + Alpha transparency channel

[vec3 → color, locations, directions, offsets]
define a vec3 class by self, and overload the operator( +, -, +=, -=, *=, /=, etc )

[Key word "inline": 指定內聯(lián)函數(shù),修飾函數(shù)岭皂,用于函數(shù)定義。
目的是消除函數(shù)調用時的時間開銷,常應用于頻繁執(zhí)行的函數(shù)。]

Chapter 3 Rays, camera, background

Ray Class & A computation of color

Ray(射線) Function: P(t) = A + t*B
P : 3D postion
A : ray origin(起點,原點)
B : ray direction
t : real number(常數(shù)), float
( t > 0, half-line, ray; t < 0, go any where of 3D line.)

Core of Ray tracer

  • send rays through pixels
  • compute what color is seen in direction of those rays

Calculate form

  • which ray goes from the eyes to pixel
  • compute what that ray intersects(橫斷她肯,貫穿)
  • compute a color for that intersections point

Color(Ray) function - linear blends/ linear interpolation/ lerp(線性插值)
blended_value = ( 1 - t ) * start_value + t * end_value ( 0.0 < t < 1.0 )

Chapter 4 Adding a sphere(范圍、領域鹰贵、球)

原點為球心晴氨,基于半徑R的球體方程:x2 + y2 + z2 = R2
for any ( x, y, z ), if x2 + y2 + z2 = R2, then ( x, y, z ) is on the sphere and otherwise it is not.

球心原點為( cx, cy, cz ) 時,球體方程為 (x-cx)2 + (y-cy)2 + (z-cz)2 = R2
球心到球上任意一點的向量為(P - C)
其中C = ( cx, cy, cz )碉输, P = ( x, y, z )
可得到 dot((P - C), (P - C)) =(x-cx)2 + (y-cy)2 + (z-cz)2籽前, dot((P - C), (P - C)) = R2
any point P that satisfies the equation is on the sphere

Whether the ray P(t) = A + t*B hits the sphere anywhere?
If hit,
dot((P(t) - C), (P(t) - C)) = R2dot(A + t*B - C), (A + t*B - C)) = R2
所以,t2*dot(B, B) + 2t*dot(B, A-C) + dot(A-C, A-C) - R2 = 0(t和R均為常數(shù))
該方程可以看做是關于t的二次方程, 有兩個有效解枝哄、一個有效解肄梨、無解三種情況,分別可以看做是射線和球體有兩個交點挠锥、一個交點和不相交峭范。

但,若在該方程無解時瘪贱,將圓心的z坐標取在反方向纱控,將會獲得位于攝像機背后的圖像。

Chapter 5 Surface normals and multiple objects

Surface Normals 法線

  • points out
  • unit length

For a sphere, the normal is in the direction of hitpoint minus the center.


  • Assume N(visualizing normal) is a unit length vector-so each component is between -1 to 1.
    Map each component to the interval from 0 to 1.
    Map x/y/z to r/g/b.
    Assume the closest hit point ( smallest t )
    Then, make an abstract class for anything might hit by ray, and make both a sphere and a list of spheres just something you can hit.
    called it Hitable( an object of object oriented )

The Hitable abstract class:

    #ifndef HITABLEH
    #define HITABLEH

    #include "ray.h"

    struct hit_record {
        float t;
        vec3 p;
        vec3 normal;
    };
    
    class hitable {
        public:
            virtual bool hit( const ray& r, float t_min, float t_max, hit_record& rec) const = 0;
    };
    #endif

The sphere:

    #ifndef SPHEREH
    #define SPHEREH

    #include "hitable.h"

    class sphere: public hitable {
        public:
            sphere( ) { }
            sphere( vec3 cen, float r ) : center ( cen ), radius( r ) { };
            virtual bool hit ( const ray& r, float tmin, float tmax, hit_record& rec ) const;
            vec3 center;
            float radius;
    };

        bool sphere :: hit ( const ray& r, float t_min, float t_max, hit_record& rec ) const {
            vec3 oc =r.origin() - center;
            float a = dot( r.direction( ), r.direction( ) );
            float b = dot( oc, r.direction( ) );
            float c = dot( oc,oc ) - radius*radius;
            float discriminant = b*b - a*c;
            if ( discriminant > 0 ) {
                float temp = ( -b - sqrt( b*b - a*c ) ) / a;
                if ( temp < t_max && temp > t_min ) {
                    rec.t = temp;
                    rec.p = r.point_at_parameter( rec.t );
                    rec.normal = ( rec.p - center ) / radius;
                    return true;
                }
            }
        return false;
}

    #endif

The list of objects:

    #ifndef HITABLELISTH
    #define HITABLELISTH

    #include "hitable.h"

    class hitable_list : public hitable {
        public:
            hitable_list( ) { };
            hitable_list( hitable **l, int n) { list = l; list_size = n; }
            virtual bool hit( const ray& r, float tmin, float tmax, hit_record& rec ) const;
            hitable **list;
            int list_size;
    };

    bool hitable_list :: hit( const ray& r, float t_min, float t_max, hit_record& rec) const {
        hit_record temp_rec;
        bool hit_anything = false;
        double closest_so_far = t_max;
        for ( int i =0; i < list_size; i++ ) {
            if ( list[i] -> hit ( r, t_min, closest_so_far, temp_rec)) {
                hit_anything = true;
                closest_so_far = temp_rec.t;
                rec = temp_rec;
            }
        }
        return hit_anything;
    }
    #endif

The main:

    #include <iostream>
    #include "sphere.h"
    #include "hitable_list.h"
    #include "float.h"

    vec3 color( const ray& r, hitable *world) {
        hit_record rec;
        if( world -> hit (r, 0, 0, MAXFLOAT, rec ) ) {
            return 0.5*vec3( rec.normal.x( ) + 1, rec.normal.y( ) + 1, rec.normal.z( ) + 1 );
        }
        else {
            vec3 unit_direction = unit_vector ( r.direction( ) );
            float t = 0.5 * (unit_direction.y( ) + 1.0);
            return ( 1.0 - t ) * vec3 ( 1.0, 1.0, 1,0 ) + t * vec3 ( 0.5, 0.7, 1.0 );
        }
    }

    int main ( ) {
        int nx = 200;
        int ny = 100;
        std :: cout << "P3\n" << nx << " " << ny << "\n255\n";
        vec3 lower_left_corner( -2.0, -1.0, -1.0 );
        vec3 horizontal ( 4.0, 0.0, 0.0 );
        vec3 vertical ( 0.0, 2.0, 0.0 );
        vec3 origin ( 0.0, 0.0, 0.0 );
        hitable *list[2];
        list[0] = new sphere( vec3( 0, 0, -1 ), 0.5 );
        list[1] = new sphere( vec3( 0, -100, 5 ), 100);
        hitable *world = new hitable_list( list, 2 );
        for( int j = ny - 1; j >= 0; j-- ) {
            for( int i = 0; i <( nx; i++ ) {
                float u = float(i) / float(nx);
                float v = float(i) / float(ny);
                ray r( origin, lower_left_corner + u*horizontal + v*vertical );
                vec3 p = r.point_at_parameter( 2, 0 );
                vec3 col = color( r, world );
                int ir = int( 255.99 * col[0] );
                int ig = int( 255.99 * col[1] );
                int ib = int( 255.99 * col[2] );

                std :: cout << ir << " " << ig << " " << ib << "\n";
            }
        }
    }
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末菜秦,一起剝皮案震驚了整個濱河市甜害,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌球昨,老刑警劉巖尔店,帶你破解...
    沈念sama閱讀 221,695評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異主慰,居然都是意外死亡嚣州,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,569評論 3 399
  • 文/潘曉璐 我一進店門共螺,熙熙樓的掌柜王于貴愁眉苦臉地迎上來该肴,“玉大人,你說我怎么就攤上這事藐不≡群澹” “怎么了?”我有些...
    開封第一講書人閱讀 168,130評論 0 360
  • 文/不壞的土叔 我叫張陵雏蛮,是天一觀的道長涎嚼。 經(jīng)常有香客問我,道長挑秉,這世上最難降的妖魔是什么法梯? 我笑而不...
    開封第一講書人閱讀 59,648評論 1 297
  • 正文 為了忘掉前任,我火速辦了婚禮犀概,結果婚禮上立哑,老公的妹妹穿的比我還像新娘。我一直安慰自己阱冶,他們只是感情好刁憋,可當我...
    茶點故事閱讀 68,655評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著木蹬,像睡著了一般至耻。 火紅的嫁衣襯著肌膚如雪若皱。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,268評論 1 309
  • 那天尘颓,我揣著相機與錄音走触,去河邊找鬼。 笑死疤苹,一個胖子當著我的面吹牛互广,可吹牛的內容都是我干的。 我是一名探鬼主播卧土,決...
    沈念sama閱讀 40,835評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼惫皱,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了尤莺?” 一聲冷哼從身側響起旅敷,我...
    開封第一講書人閱讀 39,740評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎颤霎,沒想到半個月后媳谁,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,286評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡友酱,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 38,375評論 3 340
  • 正文 我和宋清朗相戀三年晴音,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片缔杉。...
    茶點故事閱讀 40,505評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡锤躁,死狀恐怖,靈堂內的尸體忽然破棺而出壮吩,到底是詐尸還是另有隱情进苍,我是刑警寧澤加缘,帶...
    沈念sama閱讀 36,185評論 5 350
  • 正文 年R本政府宣布鸭叙,位于F島的核電站,受9級特大地震影響拣宏,放射性物質發(fā)生泄漏沈贝。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,873評論 3 333
  • 文/蒙蒙 一勋乾、第九天 我趴在偏房一處隱蔽的房頂上張望宋下。 院中可真熱鬧,春花似錦辑莫、人聲如沸学歧。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,357評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽枝笨。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間横浑,已是汗流浹背剔桨。 一陣腳步聲響...
    開封第一講書人閱讀 33,466評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留徙融,地道東北人洒缀。 一個月前我還...
    沈念sama閱讀 48,921評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像欺冀,于是被迫代替她去往敵國和親树绩。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,515評論 2 359

推薦閱讀更多精彩內容