File tree Expand file tree Collapse file tree 5 files changed +115
-23
lines changed Expand file tree Collapse file tree 5 files changed +115
-23
lines changed Original file line number Diff line number Diff line change 752
752
function returns a random integer in the range 0 and RANDMAX. Hence we can get a real random number
753
753
as desired with the following code snippet:
754
754
755
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
755
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
756
756
#ifndef RANDOMH
757
757
#define RANDOMH
758
758
762
762
return rand() / (RAND_MAX + 1.0);
763
763
}
764
764
#endif
765
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
765
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
766
766
767
767
C++ did not traditionally have a standard random number generator, but newer versions of C++ have
768
768
addressed this issue with the `< random> ` header (if imperfectly according to some experts).
769
769
If you want to use this, you can obtain a random number with the conditions we need as follows:
770
770
771
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
771
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
772
772
#ifndef RANDOMH
773
773
#define RANDOMH
774
774
783
783
return rand_generator();
784
784
}
785
785
#endif
786
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
786
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
787
787
788
788
For a given pixel we have several samples within that pixel and send rays through each of the
789
789
samples. The colors of these rays are then averaged:
902
902
}
903
903
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
904
904
905
+ Then update the `color()` function to use the new random direction generator:
906
+
905
907
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
906
908
vec3 color(const ray& r, hitable *world, int depth) {
907
909
hit_record rec;
Original file line number Diff line number Diff line change 737
737
public:
738
738
virtual vec3 value(float u, float v, const vec3& p) const = O;
739
739
};
740
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
741
740
742
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
743
741
class constant_texture : public texture {
744
742
public:
745
743
constant_texture() ( }
1070
1068
1071
1069
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ C++
1072
1070
static vec3* perlin_generate() {
1073
- vec3 * p = new vec3[256];
1074
- for ( int i = 0; i < 256; ++i )
1075
- p[i] = unit_vector(vec3(
1076
- -1 + 2*random_double(),
1077
- -1 + 2*random_double(),
1078
- -1 + 2*random_double()
1079
- ));
1071
+ vec3 *p = new vec3[256];
1072
+ for (int i = 0; i < 256; ++i) {
1073
+ double x_random = 2*random_double() - 1;
1074
+ double y_random = 2*random_double() - 1;
1075
+ double z_random = 2*random_double() - 1;
1076
+ p[i] = unit_vector(vec3(x_random, y_random, z_random));
1077
+ }
1080
1078
return p;
1081
1079
}
1082
1080
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Original file line number Diff line number Diff line change 3
3
4
4
5
5
6
- **Ray tracing : The Rest of Your Life**
6
+ **Ray Tracing : The Rest of Your Life**
7
7
Peter Shirley
8
8
Version 1.123
9
9
< br > Copyright 2018. Peter Shirley. All rights reserved.
Original file line number Diff line number Diff line change @@ -64,9 +64,13 @@ class perlin {
64
64
};
65
65
66
66
static vec3* perlin_generate () {
67
- vec3 * p = new vec3[256 ];
68
- for ( int i = 0 ; i < 256 ; ++i )
69
- p[i] = unit_vector (vec3 (-1 + 2 *random_double (), -1 + 2 *random_double (), -1 + 2 *random_double ()));
67
+ vec3 *p = new vec3[256 ];
68
+ for (int i = 0 ; i < 256 ; ++i) {
69
+ double x_random = 2 *random_double () - 1 ;
70
+ double y_random = 2 *random_double () - 1 ;
71
+ double z_random = 2 *random_double () - 1 ;
72
+ p[i] = unit_vector (vec3 (x_random, y_random, z_random));
73
+ }
70
74
return p;
71
75
}
72
76
Original file line number Diff line number Diff line change 1
- body {
2
- }
3
-
4
1
/* Under-Development Warning Box */
5
2
6
3
div .warning {
7
- background : # fee ;
8
- margin : 1em 4em 4 em 4 em ;
4
+ background : # fdd ;
5
+ margin : 1em 4em ;
9
6
border : solid 4px red;
10
- padding : 0em 4ex 1 em 4 ex ;
7
+ padding : 0em 4ex ;
11
8
}
12
9
13
10
div .warning a {
@@ -28,6 +25,97 @@ div.warning p.title {
28
25
color : red;
29
26
}
30
27
28
+
29
+ /* General Body Styles */
30
+
31
+ body {
32
+ font-family : sans-serif;
33
+ }
34
+
35
+ .md a {
36
+ font-family : sans-serif;
37
+ }
38
+
39
+ .md div .title {
40
+ font-size : 220% ;
41
+ letter-spacing : -0.06em ;
42
+ }
43
+
44
+ .md .subtitle {
45
+ font-size : 100% ;
46
+ font-style : italic;
47
+ }
48
+
49
+ .md .longTOC , .md .mediumTOC , .md .shortTOC {
50
+ font-family : sans-serif;
51
+ }
52
+
53
+ .md .longTOC {
54
+ width : 72% ;
55
+ margin : 2em auto 0 auto;
56
+ padding : 0 4ex 1em 4ex ;
57
+ border : solid 4px # e0e0d0 ;
58
+ background : # e4e4d8 ;
59
+ }
60
+
61
+ .md .tocHeader {
62
+ font-size : 165% ;
63
+ margin-bottom : -1em ;
64
+ border-bottom : solid 4px # 777 ;
65
+ }
66
+
67
+ .md h1 {
68
+ font-size : 165% ;
69
+ letter-spacing : -0.05ex ;
70
+ margin-top : 2em ;
71
+ padding-top : 0.25em ;
72
+ border-bottom : solid 4px # 777 ;
73
+ text-align : left;
74
+ }
75
+
76
+ .md h1 ::before {
77
+ content : counter (h1) ". " ;
78
+ }
79
+
80
+ .md h2 ::before {
81
+ content : counter (h1) "." counter (h2) ". " ;
82
+ }
83
+
84
+
85
+ /* Code */
86
+
87
+ .md code {
88
+ font-family : Consolas, Menlo, monospace;
89
+ color : # 000 ;
90
+ }
91
+
92
+ .md p code {
93
+ padding : 0 .4ex ;
94
+ background : # e4e4e0 ;
95
+ }
96
+
97
+ .md pre .listing .tilde {
98
+ border : solid 3px # d4d4d4 ;
99
+ background : # e4e4e0 ;
100
+ }
101
+
102
+ .md pre .listing .tilde code {
103
+ font-size : 86% ;
104
+ }
105
+
106
+ .md .hljs-meta {
107
+ color : # d60 ;
108
+ }
109
+
110
+ .md .hljs-title {
111
+ font-weight : bold;
112
+ }
113
+
114
+ .md .hljs-number {
115
+ color : # 0a0 ;
116
+ }
117
+
118
+
31
119
/* Images and Figures */
32
120
33
121
.md img {
You can’t perform that action at this time.
0 commit comments