-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
2117 lines (1038 loc) · 121 KB
/
index.html
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html class="theme-next pisces use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.9.0">
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">
<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
<link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">
<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">
<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">
<link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">
<link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">
<link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">
<link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">
<meta name="keywords" content="Hexo, NexT">
<meta name="description" content="念念不忘,必有回响!">
<meta property="og:type" content="website">
<meta property="og:title" content="Mirocos's blog">
<meta property="og:url" content="http://yoursite.com/index.html">
<meta property="og:site_name" content="Mirocos's blog">
<meta property="og:description" content="念念不忘,必有回响!">
<meta property="og:locale" content="zh-Hans">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Mirocos's blog">
<meta name="twitter:description" content="念念不忘,必有回响!">
<script type="text/javascript" id="hexo.configurations">
var NexT = window.NexT || {};
var CONFIG = {
root: '/',
scheme: 'Pisces',
version: '5.1.4',
sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
fancybox: true,
tabs: true,
motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
duoshuo: {
userId: '0',
author: '博主'
},
algolia: {
applicationID: '',
apiKey: '',
indexName: '',
hits: {"per_page":10},
labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
}
};
</script>
<link rel="canonical" href="http://yoursite.com/">
<title>Mirocos's blog</title>
<script type="text/javascript">
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "https://hm.baidu.com/hm.js?7def3e39fcff7e066c7a83b56b268c0f";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
</script>
</head>
<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">
<div class="container sidebar-position-left
page-home">
<div class="headband"></div>
<header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
<div class="header-inner"><div class="site-brand-wrapper">
<div class="site-meta ">
<div class="custom-logo-site-title">
<a href="/" class="brand" rel="start">
<span class="logo-line-before"><i></i></span>
<span class="site-title">Mirocos's blog</span>
<span class="logo-line-after"><i></i></span>
</a>
</div>
<p class="site-subtitle">自闭肥宅,Dota赛高!</p>
</div>
<div class="site-nav-toggle">
<button>
<span class="btn-bar"></span>
<span class="btn-bar"></span>
<span class="btn-bar"></span>
</button>
</div>
</div>
<nav class="site-nav">
<ul id="menu" class="menu">
<li class="menu-item menu-item-home">
<a href="/" rel="section">
<i class="menu-item-icon fa fa-fw fa-home"></i> <br>
首页
</a>
</li>
<li class="menu-item menu-item-tags">
<a href="/tags/" rel="section">
<i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
标签
</a>
</li>
<li class="menu-item menu-item-categories">
<a href="/categories/" rel="section">
<i class="menu-item-icon fa fa-fw fa-th"></i> <br>
分类
</a>
</li>
<li class="menu-item menu-item-archives">
<a href="/archives/" rel="section">
<i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
归档
</a>
</li>
</ul>
</nav>
</div>
</header>
<main id="main" class="main">
<div class="main-inner">
<div class="content-wrap">
<div id="content" class="content">
<section id="posts" class="posts-expand">
<article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
<div class="post-block">
<link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/10/14/Mit-6-837-Assignment1-Ray-Casting/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="Mirocos">
<meta itemprop="description" content>
<meta itemprop="image" content="/images/avatar.gif">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Mirocos's blog">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2019/10/14/Mit-6-837-Assignment1-Ray-Casting/" itemprop="url">Mit 6.837 Assignment1 Ray Casting</a></h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建于" itemprop="dateCreated datePublished" datetime="2019-10-14T16:15:54+08:00">
2019-10-14
</time>
</span>
<span class="post-category">
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-folder-o"></i>
</span>
<span class="post-meta-item-text">分类于</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/Mit-6-837-Computer-Graphics-Learning-Note/" itemprop="url" rel="index">
<span itemprop="name">Mit 6.837 Computer Graphics Learning Note</span>
</a>
</span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<p>[TOC]</p>
<h1 id="Assignment1-Ray-Casting"><a href="#Assignment1-Ray-Casting" class="headerlink" title="Assignment1 Ray Casting"></a>Assignment1 Ray Casting</h1><h2 id="Task-Introduction"><a href="#Task-Introduction" class="headerlink" title="Task Introduction"></a>Task Introduction</h2><blockquote>
<p><a href="https://groups.csail.mit.edu/graphics/classes/6.837/F04/assignments/assignment1/" target="_blank" rel="noopener">https://groups.csail.mit.edu/graphics/classes/6.837/F04/assignments/assignment1/</a></p>
</blockquote>
<ul>
<li><p>主要实现几个基类和派生类</p>
</li>
<li><p>Object3D (pure virtual)</p>
<ul>
<li>Sphere</li>
<li>Group</li>
</ul>
</li>
<li><p>Camera(pure virtual)</p>
<ul>
<li>OrthgraphicCamera</li>
</ul>
</li>
<li><font color="red">注意mit作业中给的scene_parser中的函数parseMaterials需要手动更改才可使用</font>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">while</span> (num_materials > count) {</span><br><span class="line"> getToken(token);</span><br><span class="line"> <span class="keyword">if</span> (!<span class="built_in">strcmp</span>(token, <span class="string">"PhongMaterial"</span>)) { <span class="comment">//将原Material改为PhongMaterial</span></span><br><span class="line"> materials[count] = parseMaterial();</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> <span class="built_in">printf</span> (<span class="string">"Unknown token in parseMaterial: '%s'\n"</span>, token);</span><br><span class="line"> <span class="built_in">exit</span>(<span class="number">0</span>);</span><br><span class="line"> }</span><br><span class="line"> count++;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
</li>
</ul>
<h2 id="Achievement"><a href="#Achievement" class="headerlink" title="Achievement"></a>Achievement</h2><ul>
<li><p>几个主要类中函数的实现</p>
</li>
<li><p>Sphere</p>
<ul>
<li>方法参考我的之前一篇博客</li>
</ul>
</li>
</ul>
<pre><code>[Ray Casting I Note]:https://mirocosphp.github.io/2019/10/09/Ray-Casting-I-Note/
</code></pre> <figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">bool</span> Sphere::intersect(<span class="keyword">const</span> Ray &r, Hit &h, <span class="keyword">float</span> tmin)</span><br><span class="line">{</span><br><span class="line"> <span class="comment">// Ray(t) = origin + t * direction</span></span><br><span class="line"> <span class="comment">// Sphere P(t): P*P - r*r = 0</span></span><br><span class="line"> <span class="keyword">bool</span> res = <span class="literal">false</span>;</span><br><span class="line"> Vec3f origin = r.getOrigin();</span><br><span class="line"> Vec3f direction = r.getDirection();</span><br><span class="line"> </span><br><span class="line"><span class="comment">// float a = direction.Dot3(direction);</span></span><br><span class="line"> </span><br><span class="line"> <span class="comment">// noticed that this resolution needs that the sphere is centered at the Origin of coordinate.</span></span><br><span class="line"> </span><br><span class="line"> <span class="keyword">float</span> a = <span class="number">1.0</span>; <span class="comment">// a should be 1.0 since direction is normailized</span></span><br><span class="line"> <span class="keyword">float</span> b = <span class="number">2</span> * direction.Dot3(origin - (*center));</span><br><span class="line"> <span class="keyword">float</span> c = (origin - *center).Dot3(origin-(*center)) - radius * radius;</span><br><span class="line"> <span class="keyword">float</span> delta = b * b - <span class="number">4</span> * a * c;</span><br><span class="line"> <span class="keyword">float</span> t = INT_MAX * <span class="number">1.0f</span>;</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">if</span>( delta > <span class="number">0</span>) <span class="comment">// No intersection Or just One intersection does not make sense;</span></span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">float</span> discriminant = <span class="built_in">sqrt</span>(delta);</span><br><span class="line"> <span class="keyword">float</span> <span class="keyword">pos_t</span> = (-b + discriminant) * <span class="number">0.5f</span>;</span><br><span class="line"> <span class="keyword">float</span> <span class="keyword">neg_t</span> = (-b - discriminant) * <span class="number">0.5f</span>;</span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line"> <span class="comment">// the closest intersection is always the neg_t</span></span><br><span class="line"> <span class="keyword">if</span>(<span class="keyword">neg_t</span> >= tmin)</span><br><span class="line"> {</span><br><span class="line"> t = <span class="keyword">neg_t</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">if</span>(<span class="keyword">pos_t</span> >= tmin)</span><br><span class="line"> {</span><br><span class="line"> t = <span class="keyword">pos_t</span>;</span><br><span class="line"> }</span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line"> <span class="comment">// t shoule be smaller than t_current</span></span><br><span class="line"> <span class="keyword">if</span>(t < h.getT())</span><br><span class="line"> {</span><br><span class="line"> h.<span class="built_in">set</span>(t, material, r);</span><br><span class="line"> }</span><br><span class="line"> res = <span class="literal">true</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> {</span><br><span class="line"> res = <span class="literal">false</span>;</span><br><span class="line"> }</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">return</span> res;</span><br><span class="line"> </span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<ul>
<li><p>Group</p>
</li>
<li><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">bool Group::intersect(const Ray &r, Hit &h, float tmin)</span><br><span class="line">{</span><br><span class="line"> bool res = false;</span><br><span class="line"> for(int i = 0; i < obj_num; i++)</span><br><span class="line"> {</span><br><span class="line"> if(obj_array[i] != NULL)</span><br><span class="line"> {</span><br><span class="line"> if(obj_array[i]->intersect(r, h, tmin) == true)</span><br><span class="line"> {</span><br><span class="line"> res = true;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> //printf("current t is %.2f\n",h.getT());</span><br><span class="line"> }</span><br><span class="line"> return res;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
</li>
<li><p>OrthographicCamera</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">OrthographicCamera::OrthographicCamera(<span class="keyword">const</span> Vec3f &center, <span class="keyword">const</span> Vec3f &direction, <span class="keyword">const</span> Vec3f &up,<span class="keyword">const</span> <span class="keyword">float</span> &size)</span><br><span class="line">{</span><br><span class="line"> <span class="keyword">this</span>->center = <span class="keyword">new</span> Vec3f(center.x(),center.y(),center.z());</span><br><span class="line"> <span class="keyword">this</span>->direction = <span class="keyword">new</span> Vec3f(direction.x(),direction.y(),direction.z());</span><br><span class="line"> <span class="keyword">this</span>->up = <span class="keyword">new</span> Vec3f(up.x(),up.y(),up.z());</span><br><span class="line"> <span class="keyword">this</span>->size = size;</span><br><span class="line"> Vec3f::Cross3(*<span class="keyword">this</span>->horizontal, *<span class="keyword">this</span>->direction, *<span class="keyword">this</span>->up);</span><br><span class="line"> Vec3f::Cross3(*<span class="keyword">this</span>->up, *<span class="keyword">this</span>->direction, *horizontal);</span><br><span class="line"></span><br><span class="line"> <span class="keyword">this</span>->up->Normalize();</span><br><span class="line"> <span class="keyword">this</span>->horizontal->Normalize();</span><br><span class="line"> <span class="keyword">this</span>->direction->Normalize();</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">Ray OrthographicCamera::generateRay(Vec2f point)</span><br><span class="line">{</span><br><span class="line"> Vec3f origin = *center + *horizontal * point.x() * size * <span class="number">0.5f</span> + *up * point.y() * size * <span class="number">0.5f</span>;</span><br><span class="line"> <span class="keyword">return</span> Ray(origin, *direction);</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
</li>
</ul>
<h3 id="整体代码"><a href="#整体代码" class="headerlink" title="整体代码"></a>整体代码</h3><p><strong>Object3D.h</strong></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">ifndef</span> Object3D_h</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Object3D_h</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"ray.h"</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"hit.h"</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"material.h"</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"vectors.h"</span></span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Object3D</span></span></span><br><span class="line"><span class="class">{</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line"> </span><br><span class="line"> <span class="function"><span class="keyword">virtual</span> <span class="keyword">bool</span> <span class="title">intersect</span><span class="params">(<span class="keyword">const</span> Ray &r, Hit &h, <span class="keyword">float</span> tmin)</span> </span>= <span class="number">0</span>;</span><br><span class="line"> </span><br><span class="line"><span class="keyword">protected</span>:</span><br><span class="line"> Material* material;</span><br><span class="line"> </span><br><span class="line">};</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span> <span class="comment">/* Object3D_h */</span></span></span><br></pre></td></tr></table></figure>
<p><strong>Sphere.h</strong></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">ifndef</span> _Sphere_h</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> _Sphere_h</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"Object3D.h"</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"hit.h"</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"ray.h"</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"vectors.h"</span></span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Sphere</span>:</span> <span class="keyword">public</span> Object3D</span><br><span class="line">{</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line"> ~Sphere()</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">delete</span> center;</span><br><span class="line"> }</span><br><span class="line"> <span class="function"><span class="keyword">bool</span> <span class="title">intersect</span><span class="params">(<span class="keyword">const</span> Ray &r, Hit &h, <span class="keyword">float</span> tmin)</span></span>;</span><br><span class="line"> Sphere(<span class="keyword">const</span> Vec3f &center,<span class="keyword">float</span> radius, Material* material);</span><br><span class="line"> </span><br><span class="line"><span class="keyword">protected</span>:</span><br><span class="line"> Vec3f* center;</span><br><span class="line"> <span class="keyword">float</span> radius;</span><br><span class="line">};</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span> <span class="comment">/* _Sphere_h */</span></span></span><br></pre></td></tr></table></figure>
<p><strong>Sphere.cpp</strong></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><stdio.h></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"Sphere.h"</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"vectors.h"</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"material.h"</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"Object3D.h"</span></span></span><br><span class="line"></span><br><span class="line">Sphere::Sphere(<span class="keyword">const</span> Vec3f &center, <span class="keyword">float</span> radius, Material* material)</span><br><span class="line">{</span><br><span class="line"> <span class="keyword">this</span>->center = <span class="keyword">new</span> Vec3f(center.x(),center.y(),center.z());</span><br><span class="line"> <span class="keyword">this</span>->radius = radius;</span><br><span class="line"> <span class="keyword">this</span>->material = material;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">bool</span> Sphere::intersect(<span class="keyword">const</span> Ray &r, Hit &h, <span class="keyword">float</span> tmin)</span><br><span class="line">{</span><br><span class="line"> <span class="comment">// Ray(t) = origin + t * direction</span></span><br><span class="line"> <span class="comment">// Sphere P(t): P*P - r*r = 0</span></span><br><span class="line"> <span class="keyword">bool</span> res = <span class="literal">false</span>;</span><br><span class="line"> Vec3f origin = r.getOrigin();</span><br><span class="line"> Vec3f direction = r.getDirection();</span><br><span class="line"> </span><br><span class="line"><span class="comment">// float a = direction.Dot3(direction);</span></span><br><span class="line"> </span><br><span class="line"> <span class="comment">// noticed that this resolution needs that the sphere is centered at the Origin of coordinate.</span></span><br><span class="line"> </span><br><span class="line"> <span class="keyword">float</span> a = <span class="number">1.0</span>; <span class="comment">// a should be 1.0 since direction is normailized</span></span><br><span class="line"> <span class="keyword">float</span> b = <span class="number">2</span> * direction.Dot3(origin - (*center));</span><br><span class="line"> <span class="keyword">float</span> c = (origin - *center).Dot3(origin-(*center)) - radius * radius;</span><br><span class="line"> <span class="keyword">float</span> delta = b * b - <span class="number">4</span> * a * c;</span><br><span class="line"> <span class="keyword">float</span> t = INT_MAX * <span class="number">1.0f</span>;</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">if</span>( delta > <span class="number">0</span>) <span class="comment">// No intersection Or just One intersection does not make sense;</span></span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">float</span> discriminant = <span class="built_in">sqrt</span>(delta);</span><br><span class="line"> <span class="keyword">float</span> <span class="keyword">pos_t</span> = (-b + discriminant) * <span class="number">0.5f</span>;</span><br><span class="line"> <span class="keyword">float</span> <span class="keyword">neg_t</span> = (-b - discriminant) * <span class="number">0.5f</span>;</span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line"> <span class="comment">// the closest intersection is always the neg_t </span></span><br><span class="line"> <span class="keyword">if</span>(<span class="keyword">neg_t</span> >= tmin)</span><br><span class="line"> {</span><br><span class="line"> t = <span class="keyword">neg_t</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">if</span>(<span class="keyword">pos_t</span> >= tmin)</span><br><span class="line"> {</span><br><span class="line"> t = <span class="keyword">pos_t</span>;</span><br><span class="line"> }</span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line"> <span class="comment">// t shoule be smaller than t_current</span></span><br><span class="line"> <span class="keyword">if</span>(t < h.getT())</span><br><span class="line"> {</span><br><span class="line"> h.<span class="built_in">set</span>(t, material, r);</span><br><span class="line"> }</span><br><span class="line"> res = <span class="literal">true</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> {</span><br><span class="line"> res = <span class="literal">false</span>;</span><br><span class="line"> }</span><br><span class="line"> </span><br><span class="line"> <span class="keyword">return</span> res;</span><br><span class="line"> </span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p><strong>Group.h</strong></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">ifndef</span> Group_h</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Group_h</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"Object3D.h"</span></span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Group</span>:</span><span class="keyword">public</span> Object3D</span><br><span class="line">{</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line"> Group(<span class="keyword">const</span> <span class="keyword">int</span> &obj_num)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">this</span>->obj_num = obj_num;</span><br><span class="line"> currentIndex = <span class="number">0</span>;</span><br><span class="line"> obj_array = <span class="keyword">new</span> Object3D*[obj_num];</span><br><span class="line"> }</span><br><span class="line"> ~Group()</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i < obj_num; i++)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(obj_array[i] != <span class="literal">NULL</span>)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">delete</span> obj_array[i];</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">delete</span>[] obj_array;</span><br><span class="line"> }</span><br><span class="line"> <span class="function"><span class="keyword">int</span> <span class="title">getNextIndex</span><span class="params">()</span></span>;</span><br><span class="line"> <span class="function"><span class="keyword">void</span> <span class="title">addObject</span><span class="params">(<span class="keyword">int</span> index, Object3D *obj)</span></span>;</span><br><span class="line"> <span class="function"><span class="keyword">bool</span> <span class="title">intersect</span><span class="params">(<span class="keyword">const</span> Ray &r, Hit &h,<span class="keyword">float</span> tmin)</span></span>;</span><br><span class="line"> </span><br><span class="line"> </span><br><span class="line"><span class="keyword">protected</span>:</span><br><span class="line"> Object3D** obj_array;</span><br><span class="line"> <span class="keyword">int</span> obj_num;</span><br><span class="line"> <span class="keyword">int</span> currentIndex;</span><br><span class="line">};</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span> <span class="comment">/* Group_h */</span></span></span><br></pre></td></tr></table></figure>
<p><strong>Group.cpp</strong></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"Group.h"</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"hit.h"</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">void</span> Group::addObject(<span class="keyword">int</span> index, Object3D *obj)</span><br><span class="line">{</span><br><span class="line"> <span class="keyword">if</span>(index < <span class="number">0</span> || index >= obj_num)</span><br><span class="line"> {</span><br><span class="line"> <span class="built_in">printf</span>(<span class="string">"index is invalid..."</span>);</span><br><span class="line"> <span class="keyword">return</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">else</span> <span class="keyword">if</span>(index >= <span class="number">0</span> && index < obj_num)</span><br><span class="line"> {</span><br><span class="line"> obj_array[index] = obj;</span><br><span class="line"> <span class="comment">// currentIndex++;</span></span><br><span class="line"> }</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">bool</span> Group::intersect(<span class="keyword">const</span> Ray &r, Hit &h, <span class="keyword">float</span> tmin)</span><br><span class="line">{</span><br><span class="line"> <span class="keyword">bool</span> res = <span class="literal">false</span>;</span><br><span class="line"> <span class="keyword">for</span>(<span class="keyword">int</span> i = <span class="number">0</span>; i < obj_num; i++)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(obj_array[i] != <span class="literal">NULL</span>)</span><br><span class="line"> {</span><br><span class="line"> <span class="keyword">if</span>(obj_array[i]->intersect(r, h, tmin) == <span class="literal">true</span>)</span><br><span class="line"> {</span><br><span class="line"> res = <span class="literal">true</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">//printf("current t is %.2f\n",h.getT());</span></span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> res;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="comment">// Not useful</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">int</span> Group::getNextIndex()</span><br><span class="line">{</span><br><span class="line"> <span class="keyword">return</span> currentIndex;</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<p><strong>Camera.h</strong></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">ifndef</span> Camera_h</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> Camera_h</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"vectors.h"</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"ray.h"</span></span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Camera</span></span></span><br><span class="line"><span class="class">{</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line"> <span class="function"><span class="keyword">virtual</span> Ray <span class="title">generateRay</span><span class="params">(Vec2f point)</span> </span>= <span class="number">0</span>;</span><br><span class="line"> <span class="function"><span class="keyword">virtual</span> <span class="keyword">float</span> <span class="title">getTMin</span><span class="params">()</span> <span class="keyword">const</span> </span>= <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> </span><br><span class="line">};</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span> <span class="comment">/* Camera_h */</span></span></span><br></pre></td></tr></table></figure>
<p><strong>OrthographicCamera.h</strong></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">ifndef</span> OrthographicCamera_h</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> OrthographicCamera_h</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"ray.h"</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"vectors.h"</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"Camera.h"</span></span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">OrthographicCamera</span>:</span> <span class="keyword">public</span> Camera</span><br><span class="line">{</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line"> OrthographicCamera(<span class="keyword">const</span> Vec3f &center, <span class="keyword">const</span> Vec3f &direction, <span class="keyword">const</span> Vec3f &up,<span class="keyword">const</span> <span class="keyword">float</span> &size);</span><br><span class="line"> ~OrthographicCamera();</span><br><span class="line"> </span><br><span class="line"> <span class="function">Ray <span class="title">generateRay</span><span class="params">(Vec2f point)</span></span>;</span><br><span class="line"> <span class="function"><span class="keyword">float</span> <span class="title">getTMin</span><span class="params">()</span> <span class="keyword">const</span></span>;</span><br><span class="line"> </span><br><span class="line"><span class="keyword">protected</span>:</span><br><span class="line"> Vec3f* center;</span><br><span class="line"> Vec3f* direction;</span><br><span class="line"> Vec3f* up;</span><br><span class="line"> Vec3f* horizontal;</span><br><span class="line"> <span class="keyword">float</span> size;</span><br><span class="line"> </span><br><span class="line">};</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span> <span class="comment">/* OrthographicCamera_h */</span></span></span><br></pre></td></tr></table></figure>
<p><strong>OrthographicCamera.cpp</strong></p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string"><stdio.h></span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"OrthographicCamera.h"</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"ray.h"</span></span></span><br><span class="line"></span><br><span class="line">OrthographicCamera::OrthographicCamera(<span class="keyword">const</span> Vec3f &center, <span class="keyword">const</span> Vec3f &direction, <span class="keyword">const</span> Vec3f &up,<span class="keyword">const</span> <span class="keyword">float</span> &size)</span><br><span class="line">{</span><br><span class="line"> <span class="keyword">this</span>->center = <span class="keyword">new</span> Vec3f(center.x(),center.y(),center.z());</span><br><span class="line"> <span class="keyword">this</span>->direction = <span class="keyword">new</span> Vec3f(direction.x(),direction.y(),direction.z());</span><br><span class="line"> <span class="keyword">this</span>->up = <span class="keyword">new</span> Vec3f(up.x(),up.y(),up.z());</span><br><span class="line"> <span class="keyword">this</span>->size = size;</span><br><span class="line"> Vec3f::Cross3(*<span class="keyword">this</span>->horizontal, *<span class="keyword">this</span>->direction, *<span class="keyword">this</span>->up);</span><br><span class="line"> Vec3f::Cross3(*<span class="keyword">this</span>->up, *<span class="keyword">this</span>->direction, *horizontal);</span><br><span class="line"></span><br><span class="line"> <span class="keyword">this</span>->up->Normalize();</span><br><span class="line"> <span class="keyword">this</span>->horizontal->Normalize();</span><br><span class="line"> <span class="keyword">this</span>->direction->Normalize();</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">OrthographicCamera::~OrthographicCamera()</span><br><span class="line">{</span><br><span class="line"> <span class="keyword">delete</span> center;</span><br><span class="line"> <span class="keyword">delete</span> direction;</span><br><span class="line"> <span class="keyword">delete</span> up;</span><br><span class="line"> <span class="keyword">delete</span> horizontal;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="keyword">float</span> OrthographicCamera::getTMin() <span class="keyword">const</span></span><br><span class="line">{</span><br><span class="line"> <span class="keyword">return</span> <span class="number">-1.0f</span> * INT_MAX;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line">Ray OrthographicCamera::generateRay(Vec2f point)</span><br><span class="line">{</span><br><span class="line"> Vec3f origin = *center + *horizontal * point.x() * size * <span class="number">0.5f</span> + *up * point.y() * size * <span class="number">0.5f</span>;</span><br><span class="line"> <span class="keyword">return</span> Ray(origin, *direction);</span><br><span class="line">}</span><br></pre></td></tr></table></figure>
<h2 id="Result"><a href="#Result" class="headerlink" title="Result"></a>Result</h2><ul>
<li>raytracer -input scene1_01.txt -size 200 200 -output output1_01.tga -depth 9 10 depth1_01.tga</li>
</ul>
<div class="group-picture"><div class="group-picture-container"><div class="group-picture-row"><div class="group-picture-column" style="width: 50%;"><img src="/2019/10/14/Mit-6-837-Assignment1-Ray-Casting/output1_01.png" title="output1_01"></div><div class="group-picture-column" style="width: 50%;"><img src="/2019/10/14/Mit-6-837-Assignment1-Ray-Casting/depth1_01.png" title="depth1_01"></div></div><div class="group-picture-row"></div></div></div>
<ul>
<li>raytracer -input scene1_02.txt -size 200 200 -output output1_02.tga -depth 8 12 depth1_02.tga</li>
</ul>
<div class="group-picture"><div class="group-picture-container"><div class="group-picture-row"><div class="group-picture-column" style="width: 50%;"><img src="/2019/10/14/Mit-6-837-Assignment1-Ray-Casting/output1_02.png" title="output1_01"></div><div class="group-picture-column" style="width: 50%;"><img src="/2019/10/14/Mit-6-837-Assignment1-Ray-Casting/depth1_02.png" title="depth1_02"></div></div><div class="group-picture-row"></div></div></div>
<ul>
<li>raytracer -input scene1_03.txt -size 200 200 -output output1_03.tga -depth 8 12 depth1_03.tga</li>
</ul>
<div class="group-picture"><div class="group-picture-container"><div class="group-picture-row"><div class="group-picture-column" style="width: 50%;"><img src="/2019/10/14/Mit-6-837-Assignment1-Ray-Casting/output1_03.png" title="output1_03"></div><div class="group-picture-column" style="width: 50%;"><img src="/2019/10/14/Mit-6-837-Assignment1-Ray-Casting/depth1_03.png" title="depth1_03"></div></div><div class="group-picture-row"></div></div></div>
<ul>
<li>raytracer -input scene1_04.txt -size 200 200 -output output1_04.tga -depth 12 17 depth1_04.tga</li>
</ul>
<div class="group-picture"><div class="group-picture-container"><div class="group-picture-row"><div class="group-picture-column" style="width: 50%;"><img src="/2019/10/14/Mit-6-837-Assignment1-Ray-Casting/output1_04.png" title="output1_04"></div><div class="group-picture-column" style="width: 50%;"><img src="/2019/10/14/Mit-6-837-Assignment1-Ray-Casting/depth1_04.png" title="depth1_04"></div></div><div class="group-picture-row"></div></div></div>
<ul>
<li>raytracer -input scene1_05.txt -size 200 200 -output output1_05.tga -depth 14.5 19.5 depth1_05.tga</li>
</ul>
<div class="group-picture"><div class="group-picture-container"><div class="group-picture-row"><div class="group-picture-column" style="width: 50%;"><img src="/2019/10/14/Mit-6-837-Assignment1-Ray-Casting/output1_05.png" title="output1_05"></div><div class="group-picture-column" style="width: 50%;"><img src="/2019/10/14/Mit-6-837-Assignment1-Ray-Casting/depth1_05.png" title="depth1_05"></div></div><div class="group-picture-row"></div></div></div>
<ul>
<li>raytracer -input scene1_06.txt -size 200 200 -output output1_06.tga -depth 3 7 depth1_06.tga</li>
</ul>
<div class="group-picture"><div class="group-picture-container"><div class="group-picture-row"><div class="group-picture-column" style="width: 50%;"><img src="/2019/10/14/Mit-6-837-Assignment1-Ray-Casting/output1_06.png" title="output1_06"></div><div class="group-picture-column" style="width: 50%;"><img src="/2019/10/14/Mit-6-837-Assignment1-Ray-Casting/depth1_06.png" title="depth1_06"></div></div><div class="group-picture-row"></div></div></div>
<ul>
<li>raytracer -input scene1_07.txt -size 200 200 -output output1_07.tga -depth -2 2 depth1_07.tga</li>
</ul>
<div class="group-picture"><div class="group-picture-container"><div class="group-picture-row"><div class="group-picture-column" style="width: 50%;"><img src="/2019/10/14/Mit-6-837-Assignment1-Ray-Casting/output1_07.png" title="output1_07"></div><div class="group-picture-column" style="width: 50%;"><img src="/2019/10/14/Mit-6-837-Assignment1-Ray-Casting/depth1_07.png" title="depth1_07"></div></div><div class="group-picture-row"></div></div></div>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</div>
</article>
<article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
<div class="post-block">
<link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/10/11/Ray-Casting-II-Note/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="Mirocos">
<meta itemprop="description" content>
<meta itemprop="image" content="/images/avatar.gif">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Mirocos's blog">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2019/10/11/Ray-Casting-II-Note/" itemprop="url">Ray Casting II Note</a></h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建于" itemprop="dateCreated datePublished" datetime="2019-10-11T16:19:10+08:00">
2019-10-11
</time>
</span>
<span class="post-category">
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-folder-o"></i>
</span>
<span class="post-meta-item-text">分类于</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/Mit-6-837-Computer-Graphics-Learning-Note/" itemprop="url" rel="index">
<span itemprop="name">Mit 6.837 Computer Graphics Learning Note</span>
</a>
</span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<p>[TOC]</p>
<h1 id="Ray-Casting-II-Note"><a href="#Ray-Casting-II-Note" class="headerlink" title="Ray Casting II Note"></a>Ray Casting II Note</h1><h2 id="Ray-Box-Intersection-射线-方体求交"><a href="#Ray-Box-Intersection-射线-方体求交" class="headerlink" title="Ray-Box Intersection(射线- 方体求交)"></a>Ray-Box Intersection(射线- 方体求交)</h2><hr>
<font size="5" color="red">前提</font>
<ul>
<li><p>轴对齐</p>
</li>
<li><p>Box:从(X1, Y1, Z1) - - > (X2, Y2, Z2)</p>
</li>
<li><p>Ray: P(t) = $R_o$ + t$R_d$ </p>
</li>
</ul>
<h3 id="朴素求交方法"><a href="#朴素求交方法" class="headerlink" title="朴素求交方法"></a>朴素求交方法</h3><ul>
<li><p>6个平面方程,计算所有交点</p>
</li>
<li><p>最近的交点在Box上</p>
<ul>
<li>注意交点是在每个平面的正确一侧:Ax + By + Cz + D < 0</li>
</ul>
</li>
</ul>
<h3 id="如何减少计算量"><a href="#如何减少计算量" class="headerlink" title="如何减少计算量"></a>如何减少计算量</h3><ul>
<li><p>互相平行的一对平面有相同的法向量(Normal)</p>
</li>
<li><p>一次计算一维</p>
<ul>
<li><p>测试是否平行</p>
</li>
<li><p>如果$R_{dx} = 0$ && $R_{ox} < X_1$ or $R_{ox} > X_2$, 则无交点</p>
</li>
</ul>
</li>
</ul>
<img src="/2019/10/11/Ray-Casting-II-Note/testpara.png" title="test if parallel">
<ul>
<li><p>对每一个dimension求交(<font color="blue">这里求出的t1和t2应该是原射线方程t的倍数</font>)</p>
<ul>
<li><p>$t_1 = (X_1 - R_{ox})/R_{dx}$</p>
</li>
<li><p>$t_2 = (X_2 - R_{ox})/R_{dx}$</p>
</li>
</ul>
</li>
</ul>
<img src="/2019/10/11/Ray-Casting-II-Note/onedemension_example.png" title="One Dimension Example">
<ul>
<li><p>保留$t_{near}$ & $t_{far}$</p>
<ul>
<li>If $t_1 > t{near}, t_{near} = t1$</li>
<li>If $t_2 > t_{far}, t_{far} = t2$</li>
</ul>
</li>
</ul>
<img src="/2019/10/11/Ray-Casting-II-Note/closest_farest.png" title="Maintain closest and farthest">
<ul>
<li><p>射线Ray和Box有交集吗?</p>
<ul>
<li>If $t_{near} > t_{far}$, 则没有交集(missed)</li>
</ul>
</li>
</ul>
<img src="/2019/10/11/Ray-Casting-II-Note/missed.png" title="Box is missed">
<ul>
<li>If $t_{far}<t_{min}$, 则Box在射线后方(behind)</li>
</ul>
<img src="/2019/10/11/Ray-Casting-II-Note/behind.png" title="One Box is behind">
<ul>
<li><p>获得正确的交点</p>
<ul>
<li><p>If $t_{near} > t_{min}$,则最近的交点在$t_{near}$</p>
</li>
<li><p>否则最近的交点在$t_{far}$</p>
</li>
</ul>
</li>
</ul>
<img src="/2019/10/11/Ray-Casting-II-Note/correctIntersection.png" title="Return the Correct Intersection">
<h3 id="射线盒体求交概括"><a href="#射线盒体求交概括" class="headerlink" title="射线盒体求交概括"></a>射线盒体求交概括</h3><img src="/2019/10/11/Ray-Casting-II-Note/Ray_Box_Summary.png" title="Ray- Box Intersection Summary">
<hr>
<h2 id="射线多边形求交"><a href="#射线多边形求交" class="headerlink" title="射线多边形求交"></a>射线多边形求交</h2><font color="red">实际上可以归结为和平面求交</font>
<h3 id="检测交点是否在多边形内"><a href="#检测交点是否在多边形内" class="headerlink" title="检测交点是否在多边形内"></a>检测交点是否在多边形内</h3><ul>
<li><p>在2D平面内</p>
<ul>
<li>从交点向任意一个方向(最好是和轴平行)发出射线</li>
<li>获得交点的个数<ul>
<li>奇数个数,交点在多边形内部</li>
<li>偶数个数,交点在多边形外部</li>
</ul>
</li>
<li>如何解决五角星的问题<ul>
<li>对五角星的边进行编号,在内部的为+,外部为- </li>
<li>如果射线交点两侧是+和- ,则计数</li>
</ul>
</li>
</ul>
<img src="/2019/10/11/Ray-Casting-II-Note/star.png" title="Winding Number">
<ul>
<li><strong>另一种角度的方法</strong><ul>
<li> 对交点和多变形构成的夹角求</li>
<li>If $Sum = k*{\pm360\degree,k为正整数}$,则点在多边形内部</li>
<li>If $Sum = 0\degree$, 则点在多边形外部</li>
</ul>
</li>
</ul>
</li>
</ul>
<img src="/2019/10/11/Ray-Casting-II-Note/Alternative.png" title="Alternative Definition">
<ul>
<li><p><strong>如何将图形投影到2D平面上</strong></p>
<ul>
<li>沿着平面法向量</li>
<li>这太耗时了</li>
</ul>
</li>
</ul>
<img src="/2019/10/11/Ray-Casting-II-Note/alongnormal.png" title="Return Along Normal">
<ul>
<li>沿着坐标轴<ul>
<li>只需要舍弃一维信息即可,往往是深度或者高度</li>
</ul>
</li>
</ul>
<img src="/2019/10/11/Ray-Casting-II-Note/alongaxis.png" title="Return Along Axis">
<hr>
<h2 id="射线-三角形求交"><a href="#射线-三角形求交" class="headerlink" title="射线- 三角形求交"></a>射线- 三角形求交</h2><ul>
<li>有两种方法<ul>
<li>使用射线- 多边形求交</li>
<li><strong>更好的,使用重心坐标</strong></li>
</ul>
</li>
</ul>
<h3 id="平面重心坐标定义"><a href="#平面重心坐标定义" class="headerlink" title="平面重心坐标定义"></a>平面重心坐标定义</h3><ul>
<li><p>$P(\alpha,\beta,\gamma) = \alpha a+ \beta b + \gamma c, 且 \alpha + \beta+\gamma = 1$</p>
</li>
<li><p>$\alpha,\beta,\gamma\in(0,1)$</p>
</li>
<li><p>如何计算这三个参数</p>
<ul>
<li>$\alpha = A_a/A \space\space \beta = A_b/A \space\space \gamma = A_c/A$</li>
</ul>
<img src="/2019/10/11/Ray-Casting-II-Note/compute3para.png" title="Compute $\alpha,\beta,\gamma$">
<ul>
<li>在这其中的一些有用的知识<ul>
<li>P是a和Q的重心</li>
<li>$A_a$是线段aQ之间的比例系数</li>
<li>所有在过P点且和直线bc平行的直线上的点有这一样的$\alpha$</li>
</ul>
</li>
</ul>
</li>
</ul>
<img src="/2019/10/11/Ray-Casting-II-Note/usefulinfo.png">
<ul>
<li>方程化简<ul>
<li>$因为 \alpha+\beta+\gamma=1,则\alpha = 1- \beta- \gamma$</li>
<li>$P(\alpha,\beta,\gamma) = \alpha a+ \beta b + \gamma c, 且 \alpha + \beta+\gamma = 1$</li>
<li>$P(\beta,\gamma) = (1- \beta- \gamma)a+\beta b + \gamma= a + \beta(b- a)+\gamma(c- a)$</li>
</ul>
</li>
</ul>
<h3 id="与重心三角形求交"><a href="#与重心三角形求交" class="headerlink" title="与重心三角形求交"></a>与重心三角形求交</h3><ul>
<li><p>$P(t) = P(\beta,\gamma)$</p>
</li>
<li><p>$R_o+t*R_d = a+\beta(b- a)+\gamma(c- a)$</p>
<ul>
<li>$R_{ox}+tR_{dx} = a_x+\beta(b_x- a_x)+\gamma(c_x- a_x)$</li>
<li>$R_{oy}+tR_{dy} = a_y+\beta(b_y- a_y)+\gamma(c_z- a_z)$</li>
</ul>
</li>
<li>$R_{oz}+tR_{dz} = a_z+\beta(b_z- a_z)+\gamma(c_z- a_z)$</li>
</ul>
<script type="math/tex; mode=display">
\Rightarrow
\begin{bmatrix}
a_x- b_x & a_x- c_x & R_{dx} \\
a_y- b_y & a_y- c_y & R_{dy} \\
a_z- b_z & a_z- c_z & R_{dz}
\end{bmatrix}
\begin{bmatrix}
\beta \\
\gamma\\
t
\end{bmatrix}=
\begin{bmatrix}
a_x- R_{ox}\\
a_y- R_{oy}\\
a_z- R_{oz}
\end{bmatrix}</script><hr>
<script type="math/tex; mode=display">
A =
\begin{bmatrix}
a_x- b_x & a_x- c_x & R_{dx} \\
a_y- b_y & a_y- c_y & R_{dy} \\
a_z- b_z & a_z- c_z & R_{dz}
\end{bmatrix}</script><script type="math/tex; mode=display">
\beta = \frac{
\begin{bmatrix}
a_x- R_{ox} & a_x- c_x & R_{dx} \\
a_y- R_{oy} & a_y- c_y & R_{dy} \\
a_z- R_{oz} & a_z- c_z & R_{dz}
\end{bmatrix}
}{|A|}</script><script type="math/tex; mode=display">
\gamma = \frac{
\begin{bmatrix}
a_x- b_x & a_x- R_{ox} & R_{dx} \\
a_y- b_y & a_y- R_{oy} & R_{dy} \\
a_z- b_z & a_z- R_{oz} & R_{dz}
\end{bmatrix}
}{|A|}</script><script type="math/tex; mode=display">
t = \frac{
\begin{bmatrix}
a_x- b_x & a_x- c_x & R_{ox} \\
a_y- b_y & a_y- c_y & R_{oy} \\
a_z- b_z & a_z- c_z & R_{oz}
\end{bmatrix}
}{|A|}</script><hr>
<ul>
<li>If $\beta + \gamma < 1$, $\beta>0$, $\gamma>0$ ,$t > t_{min}$, 则有交点</li>
</ul>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</div>
</article>
<article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
<div class="post-block">
<link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/10/09/Ray-Casting-I-Note/">
<span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="name" content="Mirocos">
<meta itemprop="description" content>
<meta itemprop="image" content="/images/avatar.gif">
</span>
<span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
<meta itemprop="name" content="Mirocos's blog">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a class="post-title-link" href="/2019/10/09/Ray-Casting-I-Note/" itemprop="url">Ray Casting I Note</a></h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建于" itemprop="dateCreated datePublished" datetime="2019-10-09T21:53:17+08:00">
2019-10-09
</time>
</span>
<span class="post-category">
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-folder-o"></i>
</span>
<span class="post-meta-item-text">分类于</span>
<span itemprop="about" itemscope itemtype="http://schema.org/Thing">
<a href="/categories/Mit-6-837-Computer-Graphics-Learning-Note/" itemprop="url" rel="index">
<span itemprop="name">Mit 6.837 Computer Graphics Learning Note</span>
</a>
</span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<p>[TOC]</p>
<h1 id="Ray-Casting-I-NOTE"><a href="#Ray-Casting-I-NOTE" class="headerlink" title="Ray Casting I NOTE"></a>Ray Casting I NOTE</h1><h2 id="Ray-Casting-Basics"><a href="#Ray-Casting-Basics" class="headerlink" title="Ray Casting Basics"></a>Ray Casting Basics</h2><h3 id="基本算法"><a href="#基本算法" class="headerlink" title="基本算法"></a>基本算法</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">For every pixel // 对于每一个像素块</span><br><span class="line"> Construct a ray from the eye // 从观察位置(眼睛)发出一条射线</span><br><span class="line"> For every object in the scene // 对在场景中的每一个对象(物体)</span><br><span class="line"> Find intersection with the ray // 求射线与对象的交集(点的集合)</span><br><span class="line"> Keep if closest // 如果是最近的,就保留它(这里可能存在歧义)</span><br></pre></td></tr></table></figure>
<img src="/2019/10/09/Ray-Casting-I-Note/overview.png" title="Ray Casting OverView">
<h3 id="什么是Shading?"><a href="#什么是Shading?" class="headerlink" title="什么是Shading?"></a>什么是Shading?</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">For every pixel</span><br><span class="line"> Construct a ray from the eye</span><br><span class="line"> For every object in the scene</span><br><span class="line"> Find intersection with the ray</span><br><span class="line"> Keep if closest</span><br><span class="line"> Shade depending on the light and normal vector // 当每个pixel上有图像时,根据光线和接触点法线进行计算和渲染。</span><br></pre></td></tr></table></figure>
<img src="/2019/10/09/Ray-Casting-I-Note/Shading.png" title="Shading">
<h4 id="影响Shading的几个因素"><a href="#影响Shading的几个因素" class="headerlink" title="影响Shading的几个因素"></a>影响Shading的几个因素</h4><ul>
<li><p>===Surface/Scene Characteristics ===</p>
<ul>
<li>surface normal (表面法线N)</li>
<li>Direction to light(光线方向L) </li>
<li>Viewpoint(视点)</li>
</ul>
</li>
</ul>