Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of the first 9 chapters of "Ray Tracing in a Weekend" #10

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
33 changes: 33 additions & 0 deletions Graphics/Sarthak_Gupta/1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include<iostream>
#include<fstream>
#include<cstdio>

int frac_to_rgb(float i){
int f = int(255.99*i);
// if(f>255) f = 255;
// if(f<0) f = 0;
return f;
}

using namespace std;

int main(){
int npx = 200;
int npy = 100;
ofstream ofs("first.ppm", ios::out | ios::app);
ofs<<"P3 \n"<<npx<<" "<<npy<<"\n255"<<endl;
for(int j = 0 ; j<npy ; j++ ){
for(int i = 0 ; i<npx ; i++){
float b = float(i)/float(npx);
float r = 0.9;
float g = float(j)/float(npy);
ofs<<frac_to_rgb(r) << " " << frac_to_rgb(g) << " " << frac_to_rgb(b)<<endl;
}
}

ofs.close();
}

/* Played around with the loops to see what and how i can influence the actal image output,8
*/

Binary file added Graphics/Sarthak_Gupta/1.exe
Binary file not shown.
21 changes: 21 additions & 0 deletions Graphics/Sarthak_Gupta/2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include<fstream>
#include "vec3.h"

int main(){
int npx = 200;
int npy = 100;
ofstream ofs ("second.ppm" , ios::out | ios::app);
ofs<<"P3 \n"<<npx<<" "<<npy<<"\n255"<<endl;

for(int j = 0 ; j<npy ; j++ ){
for(int i = 0 ; i<npx ; i++){
vec3 col( 0.9 , float(j)/float(npy) , float(i)/float(npx));
int ir = int(255.99*col[0]);
int ig = int(255.99*col[1]);
int ib = int(255.99*col[2]);
ofs<<ir << " " << ig << " " << ib <<endl;
}
}

ofs.close();
}
Binary file added Graphics/Sarthak_Gupta/2.exe
Binary file not shown.
39 changes: 39 additions & 0 deletions Graphics/Sarthak_Gupta/3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include<fstream>
#include "ray.h"

vec3 color(const ray& r){
vec3 unit_direction = unit_vector(r.direction());
float yd = 0.5*(unit_direction.y() + 1.0);
// float xd = 0.5*(unit_direction.x() + 1.0);
// return (1-yd-xd)*vec3(1.0 , 0.4 , 0.8) + yd*vec3(1.0 , 0.5 , 0.2);
return (1.0 - yd)*vec3(1.0 , 1.0 , 1.0) + yd*vec3(0.5 , 0.7 , 1.0);
}

int main(){
int npx = 200;
int npy = 100;
ofstream ofs("Third.ppm" , ios::out | ios::app);
ofs << "P3\n" << npx << " " << npy << "\n255" <<endl;
vec3 bottom_left_corner(-2.0 , -1.0 , -1.0);
vec3 x_motion(4.0 , 0.0 , 0.0);
vec3 y_motion(0.0 , 2.0 , 0.0);
vec3 origin(0.0 , 0.0 , 0.0);

for(int j = npy-1 ; j >= 0 ; j--)
{
for(int i = 0 ; i < npx ; i++)
{
float u = float(i)/float(npx);
float v = float(j)/float(npy);
ray r(origin , bottom_left_corner + u*x_motion + v*y_motion);
vec3 col = color(r).toColor();
ofs << col.e[0] << " " << col.e[1] << " " << col.e[2] << endl;
}
}

ofs.close();
}

/* Made use of ray and uderstood how to use eye/camera and how to move form point to point using ray
also addeda function to ray.h to suit my use case scenario
*/
Binary file added Graphics/Sarthak_Gupta/3.exe
Binary file not shown.
50 changes: 50 additions & 0 deletions Graphics/Sarthak_Gupta/4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include<fstream>
#include "ray.h"

bool hit_sphere(const vec3& center , float radius , const ray& r)
{
vec3 oc = r.origion() - center;
float a = dot(r.direction() , r.direction());
float b = 2.0 * dot(oc , r.direction());
float c = dot(oc , oc) - radius*radius;
float discriminant = b*b - 4.0*a*c;
return (discriminant > 0);
}

vec3 color(const ray& r) {
if (hit_sphere(vec3(0.0 , 0.0 ,-1) , 0.5 , r)){
return vec3(1.0 , 0.0 , 0.0);
}
// vec3 unit_direction = unit_vector(r.direction());
// float yd = 0.5*(unit_direction.y() + 1.0);
// return (1.0 - yd)*vec3(1.0 , 1.0 , 1.0) + yd*vec3(0.5 , 0.7 , 1.0);
return vec3(1.0 , 1.0 , 1.0);
}

int main(){
int npx = 200;
int npy = 100;
ofstream ofs("Fourth.ppm" , ios::out | ios::app);
ofs << "P3\n" << npx << " " << npy << "\n255" <<endl;
vec3 bottom_left_corner(-2.0 , -1.0 , -1.0);
vec3 x_motion(4.0 , 0.0 , 0.0);
vec3 y_motion(0.0 , 2.0 , 0.0);
vec3 origin(0.0 , 0.0 , 0.0);

for(int j = npy-1 ; j >= 0 ; j--)
{
for(int i = 0 ; i < npx ; i++)
{
float u = float(i)/float(npx);
float v = float(j)/float(npy);
ray r(origin , bottom_left_corner + u*x_motion + v*y_motion);
vec3 col = color(r).toColor();
ofs << col.e[0] << " " << col.e[1] << " " << col.e[2] << endl;
}
}

ofs.close();
}

/* Made a function whihc checks if there it hits a sphere at hte center and if it does then make that ray red. and rest is same*/

Binary file added Graphics/Sarthak_Gupta/4.exe
Binary file not shown.
54 changes: 54 additions & 0 deletions Graphics/Sarthak_Gupta/5_1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include<fstream>
#include "ray.h"

float hit_sphere(const vec3& center , float radius , const ray& r)
{
vec3 oc = r.origion() - center;
float a = dot(r.direction() , r.direction());
float b = 2.0 * dot(oc , r.direction());
float c = dot(oc , oc) - radius*radius;
float discriminant = b*b - 4.0*a*c;
if (discriminant < 0) {
return -1.0;
}
else return (-b - sqrt(discriminant)) / (2.0*a);
}

vec3 color(const ray& r){
float t = hit_sphere(vec3(0.0 , 0.0 , -1.0) , 0.5 , r);
if(t > 0.0){
vec3 N = unit_vector(r.point_at_parameter(t) - vec3(0.0 , 0.0 , -1.0));
return 0.5*vec3(N.x() + 1 , N.y() + 1 , N.z() + 1);
}

vec3 unit_direction = unit_vector(r.direction());
float yd = 0.5*(unit_direction.y() + 1.0);
return (1.0 - yd)*vec3(1.0 , 1.0 , 1.0) + yd*vec3(0.5 , 0.7 , 1.0);
}

int main(){
int npx = 200;
int npy = 100;
ofstream ofs("Fifth.ppm" , ios::out | ios::app);
ofs << "P3\n" << npx << " " << npy << "\n255" <<endl;
vec3 bottom_left_corner(-2.0 , -1.0 , -1.0);
vec3 x_motion(4.0 , 0.0 , 0.0);
vec3 y_motion(0.0 , 2.0 , 0.0);
vec3 origin(0.0 , 0.0 , 0.0);

for(int j = npy-1 ; j >= 0 ; j--)
{
for(int i = 0 ; i < npx ; i++)
{
float u = float(i)/float(npx);
float v = float(j)/float(npy);
ray r(origin , bottom_left_corner + u*x_motion + v*y_motion);
vec3 col = color(r).toColor();
ofs << col.e[0] << " " << col.e[1] << " " << col.e[2] << endl;
}
}

ofs.close();
}

/* we added a normal vector and assigned color acc to it so as to get some shading! */
Binary file added Graphics/Sarthak_Gupta/5_1.exe
Binary file not shown.
47 changes: 47 additions & 0 deletions Graphics/Sarthak_Gupta/5_2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include<fstream>

#include "sphere.h"
#include "hitable_list.h"
#include "float.h"

vec3 color(const ray& r , hitable *world){
hit_record rec;
if( world->hit(r , 0.0 , FLT_MAX , rec)) {
return 0.5*vec3(rec.normal.x() +1 , rec.normal.y() +1 , rec.normal.z() +1 );
}
else{
vec3 unit_direction = unit_vector(r.direction());
float yd = 0.5*(unit_direction.y() + 1.0);
return (1.0 - yd)*vec3(1.0 , 1.0 , 1.0) + yd*vec3(0.5 , 0.7 , 1.0);
}
}

int main() {
int npx = 200;
int npy = 100;
ofstream ofs("Fifth-final.ppm" , ios::out | ios::app);
ofs << "P3\n" << npx << " " << npy << "\n255" <<endl;
vec3 bottom_left_corner(-2.0 , -1.0 , -1.0);
vec3 x_motion(4.0 , 0.0 , 0.0);
vec3 y_motion(0.0 , 2.0 , 0.0);
vec3 origin(0.0 , 0.0 , 0.0);
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);

for(int j = npy-1 ; j >= 0 ; j--)
{
for(int i = 0 ; i < npx ; i++)
{
float u = float(i)/float(npx);
float v = float(j)/float(npy);
ray r(origin , bottom_left_corner + u*x_motion + v*y_motion);

vec3 col = color(r,world).toColor();
ofs << col.e[0] << " " << col.e[1] << " " << col.e[2] << endl;
}
}
}

/*created hitable class, hitable list class, sphere classs and used them to have an image showing two spheres using the concepts of OOP! */
Binary file added Graphics/Sarthak_Gupta/5_2.exe
Binary file not shown.
55 changes: 55 additions & 0 deletions Graphics/Sarthak_Gupta/6.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include<fstream>
#include<stdlib.h>

#include "sphere.h"
#include "hitable_list.h"
#include "float.h"
#include "camera.h"

vec3 color(const ray& r , hitable *world){
hit_record rec;
if( world->hit(r , 0.0 , FLT_MAX , rec)) {
return 0.5*vec3(rec.normal.x() +1 , rec.normal.y() +1 , rec.normal.z() +1 );
}
else{
vec3 unit_direction = unit_vector(r.direction());
float yd = 0.5*(unit_direction.y() + 1.0);
return (1.0 - yd)*vec3(1.0 , 1.0 , 1.0) + yd*vec3(0.5 , 0.7 , 1.0);
}
}

int main() {
int npx = 200;
int npy = 100;
int ns = 100;
ofstream ofs("Sixth.ppm" , ios::out | ios::app);
ofs << "P3\n" << npx << " " << npy << "\n255" <<endl;

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);
camera cam;

for(int j = npy-1 ; j >= 0 ; j--)
{
for(int i = 0 ; i < npx ; i++)
{
vec3 col(0 , 0 , 0);
for(int s = 0; s < ns; s++){
float x_s = float(rand() % 1000) / float(1000);
float y_s = float(rand() % 1000) / float(1000);
// cout<< x_s << " " << y_s << endl;
float u = float(i + x_s)/ float(npx);
float v = float(j + y_s)/ float(npy);
ray r = cam.get_ray(u,v);
col += color(r,world);
}
col /= float(ns);
col = col.toColor();
ofs << col.e[0] << " " << col.e[1] << " " << col.e[2] << endl;
}
}
}

/* used random digits to implement anti-aliasing */
Binary file added Graphics/Sarthak_Gupta/6.exe
Binary file not shown.
64 changes: 64 additions & 0 deletions Graphics/Sarthak_Gupta/7_1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include<fstream>
#include<stdlib.h>

#include "sphere.h"
#include "hitable_list.h"
#include "float.h"
#include "camera.h"

vec3 random_in_unit_sphere() {
vec3 p;
do{
p = 2.0*vec3(float(rand() % 1000) / float(1000), float(rand() % 1000) / float(1000) , float(rand() % 1000) / float(1000)) - vec3(1,1,1);
} while (p.square_length() >= 1.0);
return p;
}

vec3 color(const ray& r , hitable *world){
hit_record rec;
if( world->hit(r , 0.0 , FLT_MAX , rec)) {
vec3 target = rec.p + rec.normal + random_in_unit_sphere();
return 0.5*color( ray(rec.p , target-rec.p) , world );
}
else{
vec3 unit_direction = unit_vector(r.direction());
float yd = 0.5*(unit_direction.y() + 1.0);
return (1.0 - yd)*vec3(1.0 , 1.0 , 1.0) + yd*vec3(0.5 , 0.7 , 1.0);
}
}

int main() {
int npx = 200;
int npy = 100;
int ns = 100;
ofstream ofs("Seventh1.ppm" , ios::out | ios::app);
ofs << "P3\n" << npx << " " << npy << "\n255" <<endl;

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);
camera cam;

for(int j = npy-1 ; j >= 0 ; j--)
{
for(int i = 0 ; i < npx ; i++)
{
vec3 col(0 , 0 , 0);
for(int s = 0; s < ns; s++){
float x_s = float(rand() % 1000) / float(1000);
float y_s = float(rand() % 1000) / float(1000);
// cout<< x_s << " " << y_s << endl;
float u = float(i + x_s)/ float(npx);
float v = float(j + y_s)/ float(npy);
ray r = cam.get_ray(u,v);
col += color(r,world);
}
col /= float(ns);
col = col.toColor();
ofs << col.e[0] << " " << col.e[1] << " " << col.e[2] << endl;
}
}
}

/* used random digits to implement MATTE finsih of any material!.*/
Binary file added Graphics/Sarthak_Gupta/7_1.exe
Binary file not shown.
Loading