forked from RayTracing/TheRestOfYourLife
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsphereimp.cc
41 lines (34 loc) · 1.24 KB
/
sphereimp.cc
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
//==================================================================================================
// Written in 2016 by Peter Shirley <[email protected]>
//
// To the extent possible under law, the author(s) have dedicated all copyright and related and
// neighboring rights to this software to the public domain worldwide. This software is distributed
// without any warranty.
//
// You should have received a copy (see file COPYING.txt) of the CC0 Public Domain Dedication along
// with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
//==================================================================================================
#include <math.h>
#include <stdlib.h>
#include <iostream>
#include "vec3.h"
vec3 random_on_unit_sphere() {
vec3 p;
do {
p = 2.0*vec3(drand48(),drand48(),drand48()) - vec3(1,1,1);
} while (dot(p,p) >= 1.0);
return unit_vector(p);
}
inline float pdf(const vec3& p) {
return 1 / (4*M_PI);
}
int main() {
int N = 1000000;
float sum;
for (int i = 0; i < N; i++) {
vec3 d = random_on_unit_sphere();
float cosine_squared = d.z()*d.z();
sum += cosine_squared / pdf(d);
}
std::cout << "I =" << sum/N << "\n";
}