Skip to content

Commit

Permalink
Changes in Sphere and color
Browse files Browse the repository at this point in the history
  • Loading branch information
AveAng02 committed Aug 4, 2023
1 parent 33bcfd9 commit fe4586c
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,23 +209,29 @@ class ray



// HIT SPHERE Function
// SPHERE

struct SPHERE
{
point3 center;
double radius;
color sph_color;
std::string name;

SPHERE()
{
this->center = point3(0,0,0);
this->radius = 0.0;
this->sph_color = color(0,0,0);
this->name = "SPHERE";
}

SPHERE(point3 center_, double radius_)
SPHERE(point3 center_, double radius_, color col_, std::string name_)
{
this->center = center_;
this->radius = radius_;
this->sph_color = col_;
this->name = name_;
}
};

Expand All @@ -246,6 +252,26 @@ double hit_sphere(const SPHERE sp, const ray& r)
// Defining Ray Color

color ray_color(const ray& r, SPHERE sp)
{
if(hit_sphere(sp, r) >= 0.0)
{
return sp.sph_color;
}

vec3 unit_direc = unit(r.direction());

auto t = 0.5 * (unit_direc.y() + 1.0);

return (1.0 - t)*color(1,1,1) + t*color(0.5,0.7,1);
}





// Normal

color ray_normal_color(const ray& r, SPHERE sp)
{
auto t = hit_sphere(sp, r);

Expand All @@ -264,12 +290,14 @@ color ray_color(const ray& r, SPHERE sp)





// Main

int main()
{
// Scene objects
SPHERE sp1(point3(-1, 0, -2), 1);
SPHERE sp1(point3(-1, 0, -2), 1, color(1,0,0), "RED_SPHERE");

// Image
const double aspect_ratio = 16.0 / 9.0;
Expand Down Expand Up @@ -309,7 +337,7 @@ int main()
auto v = double(j) / (image_height - 1);

ray r(origin, lower_left_corner + u*horizontal + v*vertical - origin);
color pixelCol = ray_color(r, sp1);
color pixelCol = ray_normal_color(r, sp1);
write_color(std::cout, pixelCol);

/*
Expand Down

0 comments on commit fe4586c

Please sign in to comment.