Skip to content

Commit

Permalink
Added comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Werozel committed Mar 25, 2020
1 parent 2e37654 commit da68305
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ check:
test -r result.ppm && echo "Result generated" || (echo "Result not generated!" & exit 1)

clean:
rm result.* rt -f
rm rt -f
49 changes: 29 additions & 20 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,38 @@ const float fov = 90;


RGB cast_ray(const Ray &ray, const std::vector<Sphere> &objects, const std::vector<Light> &lights) {
Point no_intersection(-1, -1, -1);
for (int i = 0; i < objects.size(); i++) {
Point intersection_point = objects[i].ray_intersection(ray);
if (intersection_point != Point(-1, -1, -1)) {

// Returns Point(-1, -1, -1) if no intersection detected
Point intersection_point = objects[i].ray_intersection(ray); // Intersection point of ray
if (intersection_point != no_intersection) {

Vector norm = Vector(objects[i].get_position(), intersection_point).normalize();
float brightness = 0;
float brightness = 0; // Brightnesss of intersection point
for (auto light: lights) {
Vector vector_of_incidence(intersection_point, light.position);

Vector vector_of_incidence(intersection_point, light.position); // To light direction
Ray to_light(intersection_point, vector_of_incidence.normalize());
bool shade_flag = false;
bool shade_flag = false; // Flag indicating if point is in shade for current light
for (int j = 0; j < objects.size(); j++) {
if (j == i) continue;
if (objects[j].radius != 30 && objects[j].ray_intersection(to_light) != Point(-1, -1, -1)) {
if (j == i) continue; // Skipping current object
if (objects[j].ray_intersection(to_light) != no_intersection) {
shade_flag = true;
break;
}
}
if (shade_flag) continue;
if (shade_flag) continue; // If in shade skipping brightness calculation

float angle_of_incidence = get_angle(norm, vector_of_incidence);
if (angle_of_incidence > 0) {
brightness += angle_of_incidence * light.intensity;

// No point calculating glares in points hidden from light
// Calculating glares
// Direction to camera from intersection point
Vector to_camera = 2 * (vector_of_incidence * norm) * norm - vector_of_incidence;
float angle_of_reflection = get_angle(norm, to_camera);
brightness += 0.7 * std::pow(angle_of_reflection, objects[i].get_shininess()) * light.intensity;
// Calculating a glare --- K * (n * to_camera)^p
brightness += light.intensity * std::pow(angle_of_reflection, objects[i].get_shininess());
}

}
Expand All @@ -58,20 +62,19 @@ RGB cast_ray(const Ray &ray, const std::vector<Sphere> &objects, const std::vect
void render (const std::vector<Sphere> &objects, const std::vector<Light> &lights,
const int &w = width, const int &h = height) {

// pix - pixel matrix for picture generation
std::vector<std::vector<RGB> > pix(h, std::vector<RGB>(w));
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (j == 960 && i == 540) {
int k = 0;
}
Point start = Point(w/2, h/2, -w/2);
Vector direction = Vector(start, Point(j, i, 0)).normalize();
Point start = Point(w/2, h/2, -w/2); // Point of view
Vector direction = Vector(start, Point(j, i, 0)).normalize(); //current ray direction
Ray ray = Ray(start, direction);
pix[i][j] = cast_ray(ray, objects, lights);
}
}


// Generating picture in ppm P6 format
std::ofstream out;
out.open(output_file);

Expand All @@ -86,6 +89,7 @@ void render (const std::vector<Sphere> &objects, const std::vector<Light> &light
}


// Comparator for objects - closest first
bool comparator (const Sphere &s1, const Sphere &s2) {
return s1.get_position().get_z() <= s2.get_position().get_z();
}
Expand All @@ -97,24 +101,29 @@ int main (int argc, char **argv) {
std::vector<Sphere> objects;
std::vector<Light> lights;

lights.push_back(Light(Point( 3 * width/4, height/6, -100), 0.6));
lights.push_back(Light(Point(width/5, 0, 100), 0.6));
lights.push_back(Light(Point(width/2, height/2, -200), 0.3));
// Adding lights
lights.push_back(Light(Point( 3 * width/4, height/6, -100), 0.5));
lights.push_back(Light(Point(width/5, 0, 100), 0.5));
lights.push_back(Light(Point(width/2, height/2, -200), 0.25));
RGB white = RGB(255, 255, 255);

// Adding objects
objects.push_back(Sphere(300, RGB(255, 0, 0), Point(300, 540, 800), OPAQUE)); // Red
objects.push_back(Sphere(150, RGB(162, 1, 202), Point(1400, 800, 600), OPAQUE)); // Purple
objects.push_back(Sphere(200, RGB(0, 255, 0), Point(500, 540, 500), OPAQUE)); // Green

// for (auto light: lights) {
// objects.push_back(Sphere(30, white, light.get_position(), OPAQUE));
// }

// Closest objects first
std::sort(objects.begin(), objects.end(), comparator);

auto start_time = std::chrono::steady_clock::now();
// Start rendering
render(objects, lights, 1920, 1080);

// Timer
std::cout << "Ready!" << std::endl;
auto end_time = std::chrono::steady_clock::now();
std::chrono::duration<float> calculation_time = end_time - start_time;
Expand Down
7 changes: 6 additions & 1 deletion objects.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ class Object {
Point get_position() const;
SurfaceType get_stype() const;
float get_shininess() const;


// Returns an intersection point of ray and current object
virtual Point ray_intersection(const Ray &ray) const;

virtual ~Object ();
Expand All @@ -48,6 +49,7 @@ class Sphere: public Object {

void operator= (const Sphere &s);

// Returns an intersection point of ray and current object
virtual Point ray_intersection(const Ray &ray) const;
};

Expand All @@ -65,6 +67,7 @@ class Parallelepiped: public Object {
Object(col, pos, surf_type, shine), a(ta), b(tb), c(tc) {}
Parallelepiped (const Parallelepiped &p);

// Returns an intersection point of ray and current object
virtual Point ray_intesection(const Ray &ray) const = 0;

float get_a() const;
Expand All @@ -82,8 +85,10 @@ class Ray {

Ray(const Point &start_point, const Vector &dir);

// Gets a point on a ray closest to given point
Point get_closest_point_to_point (const Point &p) const;

// Gets a point on a ray closest to given object
Point get_closest_point_to_object (const Object &o) const;

~Ray ();
Expand Down
6 changes: 0 additions & 6 deletions vectors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ float distance(const Point &p1, const Point &p2) {
return sqrt(x*x + y*y + z*z);
}

float plain_distance(const Point &p1, const Point &p2) {
float x = p1.get_x() - p2.get_x();
float y = p1.get_y() - p2.get_y();
return sqrt(x*x + y*y);
}

float get_angle(const Vector &v1, const Vector &v2) {
return v1 * v2 / v1.get_length() / v2.get_length();
}
Expand Down
5 changes: 4 additions & 1 deletion vectors.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ class RGB;
class Ray;


// Calculates distance between 2 points
float distance(const Point &p1, const Point &p2);
float plain_distance(const Point &p1, const Point &p2);
// Calculates a cos of an angle between 2 vectors
float get_angle(const Vector &v1, const Vector &v2);


Expand Down Expand Up @@ -53,8 +54,10 @@ class Vector: public Triplet {
Vector(const Triplet &v);
Vector(const Point &from, const Point &to);

// Calculates a length of a current vector
float get_length() const;

// Returns a normilized current vector
Vector normalize() const;
};

Expand Down

0 comments on commit da68305

Please sign in to comment.