Skip to content

Commit

Permalink
CHAPTER 8
Browse files Browse the repository at this point in the history
- fix the issue with sphere.h
- add a Chapter 7 HD photo before gamma correction for fun
  • Loading branch information
renauga committed May 9, 2021
1 parent c1ee3af commit c21dbb8
Show file tree
Hide file tree
Showing 9 changed files with 20,084 additions and 21 deletions.
Binary file added Graphics/Saumya_Talera/Chapter7 HD.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Graphics/Saumya_Talera/Chapter8.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20,003 changes: 20,003 additions & 0 deletions Graphics/Saumya_Talera/Chapter8.ppm

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions Graphics/Saumya_Talera/hitable.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@

#include "ray.h"

class material;

struct hit_record {
float t;
vec3 p;
vec3 normal;
material *mat_ptr;
};

class hitable {
Expand Down
38 changes: 18 additions & 20 deletions Graphics/Saumya_Talera/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "float.h"
#include "camera.h"
#include "lodepng.h"
#include "material.h"
using namespace std;

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

float drand() {
return (float)rand()/(RAND_MAX+1.0);
}
vec3 random_in_unit_sphere(){
vec3 p;
do {
p = 2.0*vec3(drand(),drand(),drand()) - vec3(1,1,1);
} while (p.squared_length() >= 1.0);
return p;
}

vec3 color(const ray& r, hitable *world) {
vec3 color(const ray& r, hitable *world, int depth) {
hit_record rec;
if(world->hit(r, 0.001, FLT_MAX, rec)) {
vec3 target = rec.normal + random_in_unit_sphere();
return 0.5*color(ray(rec.p,target),world);
ray scattered;
vec3 attenuation;
if(depth<50 && rec.mat_ptr->scatter(r,rec,attenuation,scattered)) {
return attenuation*color(scattered,world,depth+1);
} else {
return vec3(0,0,0);
}
}
vec3 unit_direction = unit_vector(r.direction());
float t = 0.5*(unit_direction.y()+1.0);
Expand All @@ -46,10 +42,12 @@ int main() {
vector<unsigned char> image;
image.resize(nx*ny*3);
cout<<"P3\n"<<nx<<" "<<ny<<"\n255\n";
hitable *list[2];
list[0] = new sphere(vec3(0,0,-1), 0.5);
list[1] = new sphere(vec3(0,-100.5,-1), 100);
hitable *world = new hitable_list(list,2);
hitable *list[4];
list[0] = new sphere(vec3(0,0,-1), 0.5, new lambertian(vec3(0.8,0.3,0.3)));
list[1] = new sphere(vec3(0,-100.5,-1), 100, new lambertian(vec3(0.8,0.8,0.0)));
list[2] = new sphere(vec3(1,0,-1), 0.5, new metal(vec3(0.8, 0.6, 0.2),0.3));
list[3] = new sphere(vec3(-1,0,-1), 0.5, new metal(vec3(0.8, 0.8, 0.8),1.0));
hitable *world = new hitable_list(list,4);
camera cam;
for(int j=ny-1;j>=0;j--) {
for(int i=0;i<nx;i++) {
Expand All @@ -59,7 +57,7 @@ int main() {
float v = ((float)j+drand())/(float)ny;
ray r = cam.get_ray(u,v);
vec3 p =r.point_at_parameter(2.0);
col += color(r,world);
col += color(r,world,0);
}
col /= (float)ns;
col = vec3(sqrt(col[0]),sqrt(col[1]),sqrt(col[2]));
Expand All @@ -72,6 +70,6 @@ int main() {
cout<<ir<<" "<<ig<<" "<<ib<<"\n";
}
}
encodeOneStep("Chapter7.png", image, nx, ny);
encodeOneStep("Chapter8.png", image, nx, ny);
return 0;
}

Binary file modified Graphics/Saumya_Talera/main.exe
Binary file not shown.
52 changes: 52 additions & 0 deletions Graphics/Saumya_Talera/material.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#ifndef MATERIALH
#define MATERIALH

#include "sphere.h"


float drand() {
return (float)rand()/(RAND_MAX+1.0);
}
vec3 random_in_unit_sphere(){
vec3 p;
do {
p = 2.0*vec3(drand(),drand(),drand()) - vec3(1,1,1);
} while (p.squared_length() >= 1.0);
return p;
}
class material {
public:
virtual bool scatter(const ray& r_in, const hit_record& rec, vec3& attenuation, ray& scattered) const = 0;
};

vec3 reflect(const vec3& v, const vec3& n) {
return v - 2*dot(v,n)*n;
}

class lambertian : public material {
public:
lambertian(const vec3& a) : albedo(a) {}
virtual bool scatter(const ray& r_in, const hit_record& rec, vec3& attenuation, ray& scattered) const {
vec3 target = rec.normal + random_in_unit_sphere();
scattered = ray(rec.p, target);
attenuation = albedo;
return true;
}
vec3 albedo;
};

class metal : public material {
public:
metal(const vec3& a, float f) : albedo(a) {if (f<1) fuzz=f; else fuzz=1;}
virtual bool scatter(const ray& r_in, const hit_record& rec, vec3& attenuation, ray& scattered) const {
vec3 reflected = reflect(unit_vector(r_in.direction()), rec.normal);
scattered = ray(rec.p,reflected+fuzz*random_in_unit_sphere());
attenuation = albedo;
return (dot(scattered.direction(), rec.normal)>0);
}

vec3 albedo;
float fuzz;
};

#endif
9 changes: 8 additions & 1 deletion Graphics/Saumya_Talera/sphere.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@
#define SPHEREH

#include "hitable.h"
#include "material.h"



class sphere: public hitable {
public:
sphere(){}
sphere(vec3 cen, float r) : center(cen), radius(r) {};
sphere(vec3 cen, float r, material* m) : center(cen), radius(r) {mat=m;};
virtual bool hit(const ray& r, float t_min, float t_max, hit_record& rec) const;
vec3 center;
float radius;
material* mat;

};

bool sphere::hit(const ray& r, float t_min, float t_max, hit_record& rec) const {
Expand All @@ -24,13 +29,15 @@ bool sphere::hit(const ray& r, float t_min, float t_max, hit_record& rec) const
rec.t = temp;
rec.p = r.point_at_parameter(temp);
rec.normal = (rec.p-center)/radius;
rec.mat_ptr = mat;
return true;
}
temp = (-b+discriminant)/a;
if(temp<t_max && temp>t_min) {
rec.t = temp;
rec.p = r.point_at_parameter(temp);
rec.normal = (rec.p-center)/radius;
rec.mat_ptr = mat;
return true;
}
}
Expand Down
Binary file removed Graphics/Saumya_Talera/test.png
Binary file not shown.

0 comments on commit c21dbb8

Please sign in to comment.