Skip to content

Commit c21dbb8

Browse files
committed
CHAPTER 8
- fix the issue with sphere.h - add a Chapter 7 HD photo before gamma correction for fun
1 parent c1ee3af commit c21dbb8

File tree

9 files changed

+20084
-21
lines changed

9 files changed

+20084
-21
lines changed
1.22 MB
Loading

Graphics/Saumya_Talera/Chapter8.png

25.1 KB
Loading

Graphics/Saumya_Talera/Chapter8.ppm

+20,003
Large diffs are not rendered by default.

Graphics/Saumya_Talera/hitable.h

+3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33

44
#include "ray.h"
55

6+
class material;
7+
68
struct hit_record {
79
float t;
810
vec3 p;
911
vec3 normal;
12+
material *mat_ptr;
1013
};
1114

1215
class hitable {

Graphics/Saumya_Talera/main.cpp

+18-20
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "float.h"
66
#include "camera.h"
77
#include "lodepng.h"
8+
#include "material.h"
89
using namespace std;
910

1011
void encodeOneStep(const char* filename, std::vector<unsigned char>& image, unsigned width, unsigned height) {
@@ -16,22 +17,17 @@ void encodeOneStep(const char* filename, std::vector<unsigned char>& image, unsi
1617
if(error) std::cout << "encoder error " << error << ": "<< lodepng_error_text(error) << std::endl;
1718
}
1819

19-
float drand() {
20-
return (float)rand()/(RAND_MAX+1.0);
21-
}
22-
vec3 random_in_unit_sphere(){
23-
vec3 p;
24-
do {
25-
p = 2.0*vec3(drand(),drand(),drand()) - vec3(1,1,1);
26-
} while (p.squared_length() >= 1.0);
27-
return p;
28-
}
2920

30-
vec3 color(const ray& r, hitable *world) {
21+
vec3 color(const ray& r, hitable *world, int depth) {
3122
hit_record rec;
3223
if(world->hit(r, 0.001, FLT_MAX, rec)) {
33-
vec3 target = rec.normal + random_in_unit_sphere();
34-
return 0.5*color(ray(rec.p,target),world);
24+
ray scattered;
25+
vec3 attenuation;
26+
if(depth<50 && rec.mat_ptr->scatter(r,rec,attenuation,scattered)) {
27+
return attenuation*color(scattered,world,depth+1);
28+
} else {
29+
return vec3(0,0,0);
30+
}
3531
}
3632
vec3 unit_direction = unit_vector(r.direction());
3733
float t = 0.5*(unit_direction.y()+1.0);
@@ -46,10 +42,12 @@ int main() {
4642
vector<unsigned char> image;
4743
image.resize(nx*ny*3);
4844
cout<<"P3\n"<<nx<<" "<<ny<<"\n255\n";
49-
hitable *list[2];
50-
list[0] = new sphere(vec3(0,0,-1), 0.5);
51-
list[1] = new sphere(vec3(0,-100.5,-1), 100);
52-
hitable *world = new hitable_list(list,2);
45+
hitable *list[4];
46+
list[0] = new sphere(vec3(0,0,-1), 0.5, new lambertian(vec3(0.8,0.3,0.3)));
47+
list[1] = new sphere(vec3(0,-100.5,-1), 100, new lambertian(vec3(0.8,0.8,0.0)));
48+
list[2] = new sphere(vec3(1,0,-1), 0.5, new metal(vec3(0.8, 0.6, 0.2),0.3));
49+
list[3] = new sphere(vec3(-1,0,-1), 0.5, new metal(vec3(0.8, 0.8, 0.8),1.0));
50+
hitable *world = new hitable_list(list,4);
5351
camera cam;
5452
for(int j=ny-1;j>=0;j--) {
5553
for(int i=0;i<nx;i++) {
@@ -59,7 +57,7 @@ int main() {
5957
float v = ((float)j+drand())/(float)ny;
6058
ray r = cam.get_ray(u,v);
6159
vec3 p =r.point_at_parameter(2.0);
62-
col += color(r,world);
60+
col += color(r,world,0);
6361
}
6462
col /= (float)ns;
6563
col = vec3(sqrt(col[0]),sqrt(col[1]),sqrt(col[2]));
@@ -72,6 +70,6 @@ int main() {
7270
cout<<ir<<" "<<ig<<" "<<ib<<"\n";
7371
}
7472
}
75-
encodeOneStep("Chapter7.png", image, nx, ny);
73+
encodeOneStep("Chapter8.png", image, nx, ny);
74+
return 0;
7675
}
77-

Graphics/Saumya_Talera/main.exe

2.43 KB
Binary file not shown.

Graphics/Saumya_Talera/material.h

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#ifndef MATERIALH
2+
#define MATERIALH
3+
4+
#include "sphere.h"
5+
6+
7+
float drand() {
8+
return (float)rand()/(RAND_MAX+1.0);
9+
}
10+
vec3 random_in_unit_sphere(){
11+
vec3 p;
12+
do {
13+
p = 2.0*vec3(drand(),drand(),drand()) - vec3(1,1,1);
14+
} while (p.squared_length() >= 1.0);
15+
return p;
16+
}
17+
class material {
18+
public:
19+
virtual bool scatter(const ray& r_in, const hit_record& rec, vec3& attenuation, ray& scattered) const = 0;
20+
};
21+
22+
vec3 reflect(const vec3& v, const vec3& n) {
23+
return v - 2*dot(v,n)*n;
24+
}
25+
26+
class lambertian : public material {
27+
public:
28+
lambertian(const vec3& a) : albedo(a) {}
29+
virtual bool scatter(const ray& r_in, const hit_record& rec, vec3& attenuation, ray& scattered) const {
30+
vec3 target = rec.normal + random_in_unit_sphere();
31+
scattered = ray(rec.p, target);
32+
attenuation = albedo;
33+
return true;
34+
}
35+
vec3 albedo;
36+
};
37+
38+
class metal : public material {
39+
public:
40+
metal(const vec3& a, float f) : albedo(a) {if (f<1) fuzz=f; else fuzz=1;}
41+
virtual bool scatter(const ray& r_in, const hit_record& rec, vec3& attenuation, ray& scattered) const {
42+
vec3 reflected = reflect(unit_vector(r_in.direction()), rec.normal);
43+
scattered = ray(rec.p,reflected+fuzz*random_in_unit_sphere());
44+
attenuation = albedo;
45+
return (dot(scattered.direction(), rec.normal)>0);
46+
}
47+
48+
vec3 albedo;
49+
float fuzz;
50+
};
51+
52+
#endif

Graphics/Saumya_Talera/sphere.h

+8-1
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,19 @@
22
#define SPHEREH
33

44
#include "hitable.h"
5+
#include "material.h"
6+
7+
58

69
class sphere: public hitable {
710
public:
811
sphere(){}
9-
sphere(vec3 cen, float r) : center(cen), radius(r) {};
12+
sphere(vec3 cen, float r, material* m) : center(cen), radius(r) {mat=m;};
1013
virtual bool hit(const ray& r, float t_min, float t_max, hit_record& rec) const;
1114
vec3 center;
1215
float radius;
16+
material* mat;
17+
1318
};
1419

1520
bool sphere::hit(const ray& r, float t_min, float t_max, hit_record& rec) const {
@@ -24,13 +29,15 @@ bool sphere::hit(const ray& r, float t_min, float t_max, hit_record& rec) const
2429
rec.t = temp;
2530
rec.p = r.point_at_parameter(temp);
2631
rec.normal = (rec.p-center)/radius;
32+
rec.mat_ptr = mat;
2733
return true;
2834
}
2935
temp = (-b+discriminant)/a;
3036
if(temp<t_max && temp>t_min) {
3137
rec.t = temp;
3238
rec.p = r.point_at_parameter(temp);
3339
rec.normal = (rec.p-center)/radius;
40+
rec.mat_ptr = mat;
3441
return true;
3542
}
3643
}

Graphics/Saumya_Talera/test.png

-27.3 KB
Binary file not shown.

0 commit comments

Comments
 (0)