Skip to content

Commit

Permalink
And cones !
Browse files Browse the repository at this point in the history
  • Loading branch information
Godzil committed Feb 23, 2020
1 parent 0650ac7 commit f226664
Show file tree
Hide file tree
Showing 9 changed files with 509 additions and 3 deletions.
4 changes: 3 additions & 1 deletion README.md
Expand Up @@ -52,4 +52,6 @@ From Chapter 12 - Cubes:

From Chapter 13 - Cylinders:

![Chapter 13 rendering test](output/ch13_test.png)
![Chapter 13 rendering test](output/ch13_test.png)
Bonus:
![Chapter 13 cone test](output/ch13_cone.png)
Binary file added output/ch13_cone.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions source/include/cone.h
@@ -0,0 +1,33 @@
/*
* DoRayMe - a quick and dirty Raytracer
* Cone header
*
* Created by Manoël Trapier
* Copyright (c) 2020 986-Studio.
*
*/
#ifndef DORAYME_CONE_H
#define DORAYME_CONE_H

#include <shape.h>
#include <ray.h>
#include <intersect.h>

class Cone : public Shape {
protected:
Intersect localIntersect(Ray r);

Tuple localNormalAt(Tuple point);

bool checkCap(Ray r, double t, double y);
void intersectCaps(Ray r, Intersect &xs);

public:
bool isClosed;
double minCap;
double maxCap;

Cone() : minCap(-INFINITY), maxCap(INFINITY), isClosed(false), Shape(SHAPE_CONE) {};
};

#endif /* DORAYME_CONE_H */
124 changes: 124 additions & 0 deletions source/shapes/cone.cpp
@@ -0,0 +1,124 @@
/*
* DoRayMe - a quick and dirty Raytracer
* Cone implementation
*
* Created by Manoël Trapier
* Copyright (c) 2020 986-Studio.
*
*/
#include <tuple.h>
#include <ray.h>
#include <shape.h>
#include <cone.h>
#include <math_helper.h>

bool Cone::checkCap(Ray r, double t, double y)
{
/* Helping function to reduce duplication.
* Checks to see if the intersection ot t is within a radius
* of 1 (the radius of our Cone from the y axis
*/
double x = r.origin.x + t * r.direction.x;
double z = r.origin.z + t * r.direction.z;
return (x * x + z * z) <= fabs(y);
}

void Cone::intersectCaps(Ray r, Intersect &xs)
{
/* Caps only mattter is the Cone is closed, and might possibly be
* intersected by the ray
*/
if ((this->isClosed) && (fabs(r.direction.y) > getEpsilon()))
{
double t;
/* Check for an intersection with the lower end cap by intersecting
* the ray with the plan at y = this->minCap
*/
t = (this->minCap - r.origin.y) / r.direction.y;
if (this->checkCap(r, t, this->minCap))
{
xs.add(Intersection(t, this));
}

/* Check for an intersection with the upper end cap by intersecting
* the ray with the plan at y = this->maxCap
*/
t = (this->maxCap - r.origin.y) / r.direction.y;
if (this->checkCap(r, t, this->maxCap))
{
xs.add(Intersection(t, this));
}
}
}

Intersect Cone::localIntersect(Ray r)
{
Intersect ret;

double A = pow(r.direction.x, 2) -
pow(r.direction.y, 2) +
pow(r.direction.z, 2);
double B = (2 * r.origin.x * r.direction.x) -
(2 * r.origin.y * r.direction.y) +
(2 * r.origin.z * r.direction.z);

double C = pow(r.origin.x, 2) -
pow(r.origin.y, 2) +
pow(r.origin.z, 2);

if ((fabs(A) <= getEpsilon()) && (fabs(B) >= getEpsilon()))
{
double t = -C / (2*B);
ret.add(Intersection(t, this));
}
else if (fabs(A) >= getEpsilon())
{

double disc = pow(B, 2) - 4 * A * C;

if (disc >= 0)
{
double t0 = (-B - sqrt(disc)) / (2 * A);
double t1 = (-B + sqrt(disc)) / (2 * A);

double y0 = r.origin.y + t0 * r.direction.y;
if ((this->minCap < y0) && (y0 < this->maxCap))
{
ret.add(Intersection(t0, this));
}

double y1 = r.origin.y + t1 * r.direction.y;
if ((this->minCap < y1) && (y1 < this->maxCap))
{
ret.add(Intersection(t1, this));
}
}
}

this->intersectCaps(r, ret);

return ret;
}

Tuple Cone::localNormalAt(Tuple point)
{
/* Compute the square of the distance from the Y axis */
double dist = point.x * point.x + point.z * point.z;

if ((dist < 1) && (point.y >= (this->maxCap - getEpsilon())))
{
return Vector(0, 1, 0);
}

if ((dist < 1) && (point.y <= this->minCap + getEpsilon()))
{
return Vector(0, -1, 0);
}

double y = sqrt(point.x * point.x + point.z * point.z);
if (point.y > 0)
{
y = -y;
}
return Vector(point.x, y, point.z);
}
5 changes: 5 additions & 0 deletions source/tuple.cpp
Expand Up @@ -19,6 +19,11 @@ double Tuple::magnitude()
Tuple Tuple::normalise()
{
double mag = this->magnitude();
if (mag == 0)
{
return Tuple(0, 0, 0, 0);
}

return Tuple(this->x / mag, this->y / mag, this->z / mag, this->w / mag);
}

Expand Down
8 changes: 7 additions & 1 deletion tests/CMakeLists.txt
Expand Up @@ -5,7 +5,7 @@ find_package(Threads REQUIRED)

set(TESTS_SRC math_test.cpp tuple_test.cpp colour_test.cpp canvas_test.cpp matrix_test.cpp transformation_test.cpp
ray_test.cpp intersect_test.cpp sphere_test.cpp light_test.cpp material_test.cpp world_test.cpp camera_test.cpp
shape_test.cpp plane_test.cpp pattern_test.cpp cube_test.cpp cylinder_test.cpp)
shape_test.cpp plane_test.cpp pattern_test.cpp cube_test.cpp cylinder_test.cpp cone_test.cpp)

add_executable(testMyRays)
target_include_directories(testMyRays PUBLIC ${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR})
Expand Down Expand Up @@ -74,6 +74,11 @@ target_include_directories(ch13_test PUBLIC ../source/include)
target_sources(ch13_test PRIVATE ch13_test.cpp)
target_link_libraries(ch13_test rayonnement)

add_executable(ch13_cone)
target_include_directories(ch13_cone PUBLIC ../source/include)
target_sources(ch13_cone PRIVATE ch13_cone.cpp)
target_link_libraries(ch13_cone rayonnement)

add_test(NAME Chapter05_Test COMMAND $<TARGET_FILE:ch5_test>)
add_test(NAME Chapter06_Test COMMAND $<TARGET_FILE:ch6_test>)
add_test(NAME Chapter07_Test COMMAND $<TARGET_FILE:ch7_test>)
Expand All @@ -84,4 +89,5 @@ add_test(NAME Chapter11_Refraction COMMAND $<TARGET_FILE:ch11_refraction>)
add_test(NAME Chapter11_Test COMMAND $<TARGET_FILE:ch11_test>)
add_test(NAME Chapter12_Test COMMAND $<TARGET_FILE:ch12_test>)
add_test(NAME Chapter13_Test COMMAND $<TARGET_FILE:ch13_test>)
add_test(NAME Chapter13_ConeBonus COMMAND $<TARGET_FILE:ch13_cone>)
add_test(NAME Hw3Render COMMAND $<TARGET_FILE:hw3render> ${CMAKE_CURRENT_SOURCE_DIR}/test.hw3scene)
2 changes: 1 addition & 1 deletion tests/ch12_test.cpp
Expand Up @@ -199,7 +199,7 @@ int main()
/* ----------------------------- */

/* Set the camera */
Camera camera = Camera(1000, 500, 0.785);
Camera camera = Camera(400, 200, 0.785);
camera.setTransform(viewTransform(Point(8, 6, -8),
Point(0, 3, 0),
Vector(0, 1, 0)));
Expand Down

0 comments on commit f226664

Please sign in to comment.