3 Commits

Author SHA1 Message Date
Godzil
18965fe1bd They say it is better to do this way. 2020-03-06 21:59:16 +00:00
Godzil
57eff4830e Sample scene for CSG \o/ 2020-03-06 21:55:32 +00:00
Godzil
b5ee92c544 And CSG! \o/
Still working on a nice scene for it.
2020-03-06 19:00:31 +00:00
24 changed files with 734 additions and 78 deletions

View File

@@ -17,101 +17,83 @@ as texture, also use [NanoJPEG](https://keyj.emphy.de/nanojpeg/) to use jpeg fil
Examples outputs
----------------
**From chapter 05 - Sphere intersections:**
#### From chapter 05 - Sphere intersections:
![Chapter 5 rendering test](output/ch5_test.png)
**From Chapter 06 - Phong shading:**
#### From Chapter 06 - Phong shading:
![Chapter 6 rendering test](output/ch6_test.png)
**From Chapter 07 - World / Camera / Scenes:**
#### From Chapter 07 - World / Camera / Scenes:
![Chapter 7 rendering test](output/ch7_test.png)
**From Chapter 08 - Shadows:**
#### From Chapter 08 - Shadows:
![Chapter 8 rendering test](output/ch8_test.png)
**From Chapter 09 - Planes:**
#### From Chapter 09 - Planes:
![Chapter 9 rendering test](output/ch9_test.png)
**From Chapter 10 - Patterns:**
#### From Chapter 10 - Patterns:
![Chapter 10 rendering test](output/ch10_test.png)
**From Chapter 11 - Reflections, Transparency & Refractions:**
#### From Chapter 11 - Reflections, Transparency & Refractions:
![Chapter 11 reflections rendering test](output/ch11_reflection.png)
Bonus: Zooming on a reflective ball:
###### Bonus: Zooming on a reflective ball:
![Chapter 11 zooming on a ball](output/ch11_zooming_on_reflective_ball.png)
Zooming on a reflection on that ball:
###### Zooming on a reflection on that ball:
![Chapter 11 zooming on a reflection](output/ch11_reflection_on_ball.png)
![Chapter 11 refraction rendering test](output/ch11_refraction.png)
![Chapter 11 rendering test](output/ch11_test.png)
**From Chapter 12 - Cubes:**
#### From Chapter 12 - Cubes:
![Chapter 12 rendering test](output/ch12_test.png)
**From Chapter 13 - Cylinders:**
#### From Chapter 13 - Cylinders:
![Chapter 13 rendering test](output/ch13_test.png)
Bonus:
###### Bonus:
![Chapter 13 cone test](output/ch13_cone.png)
**From Chapter 14 - Groups & Bounding boxes:**
#### From Chapter 14 - Groups & Bounding boxes:
![Chapter 14 rendering test](output/ch14_test.png)
**From Chapter 15 - Triangles, Wavefrom OBJ files - Smooth trianges:**
#### From Chapter 15 - Triangles, Wavefrom OBJ files - Smooth trianges:
![Chapter 15 Triangles and teapots](output/ch15_teapot_objfile.png)
**Bonus (from the forum):**
#### From Chapter 16 - Constructive Solid Geomety:
![Chapter 16 CSG](output/ch16_test.png)
#### Bonus (from the forum):
[Merry Christmas](https://forum.raytracerchallenge.com/thread/16/merry-christmas-scene-description)
![Merry Christmas](output/christmasball.png)
(about 1min render time using OpenMP on a 2.6Ghz Core i7 3720QM)
**Bonus chapter - Soft shadow / Area light**
Without jitter:
#### Bonus chapter - Soft shadow / Area light
###### Without jitter:
![Area light without jitter](output/arealight_test_nojitter.png)
With jitter:
###### With jitter:
![Area light witht jitter](output/arealight_test.png)
**Bonus chapter - Texture mapping**
Spherical mapping:
#### Bonus chapter - Texture mapping
###### Spherical mapping:
![Spherical mapping](output/uvmap_checkeredsphere.png)
Planar mapping:
######Planar mapping:
![Planar mapping](output/uvmap_checkeredplane.png)
Cylindrical mapping:
###### Cylindrical mapping:
![Cylindrical mapping](output/uvmap_checkeredcylinder.png)
Aligncheck plane:
###### Aligncheck plane:
![Aligncheck plane](output/uvmap_aligncheckplane.png)
Cubical mapping:
###### Cubical mapping:
![Cubical mapping](output/uvmap_checkeredcube.png)
Image mapping:
###### Image mapping:
![Image mapping](output/uvmap_earth.png)
Skybox:
###### Skybox:
![Skybox](output/uvmap_skybox.png)

BIN
output/ch16_test.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 112 KiB

View File

@@ -62,9 +62,9 @@ Canvas Camera::render(World world, uint32_t depth)
uint32_t x, y;
Canvas image = Canvas(this->horizontalSize, this->verticalSize);
#pragma omp parallel private(x, y) shared(image, stats)
#pragma omp parallel default(shared) private(x, y) shared(image, stats)
{
#pragma omp for
#pragma omp for schedule(dynamic, 5)
for (y = 0 ; y < this->verticalSize ; y++)
{
for (x = 0 ; x < this->horizontalSize ; x++)

View File

@@ -132,12 +132,12 @@ Canvas::~Canvas()
}
}
void Canvas::putPixel(uint32_t x, uint32_t y, Tuple colour)
void Canvas::putPixel(uint32_t x, uint32_t y, Tuple c)
{
uint32_t offset = y * this->stride + x * BytePP;
this->bitmap[offset + 0] = MAX(MIN(colour.x * 255, 255), 0);
this->bitmap[offset + 1] = MAX(MIN(colour.y * 255, 255), 0);
this->bitmap[offset + 2] = MAX(MIN(colour.z * 255, 255), 0);
this->bitmap[offset + 0] = MAX(MIN(c.x * 255, 255), 0);
this->bitmap[offset + 1] = MAX(MIN(c.y * 255, 255), 0);
this->bitmap[offset + 2] = MAX(MIN(c.z * 255, 255), 0);
}
Colour Canvas::getPixel(uint32_t x, uint32_t y)

54
source/include/csg.h Normal file
View File

@@ -0,0 +1,54 @@
/*
* DoRayMe - a quick and dirty Raytracer
* CSG header
*
* Created by Manoël Trapier
* Copyright (c) 2020 986-Studio.
*
*/
#ifndef DORAYME_CSG_H
#define DORAYME_CSG_H
#include <shape.h>
class CSG : public Shape
{
public:
enum OperationType
{
UNION,
DIFFERENCE,
INTERSECTION
};
protected:
Shape *left;
Shape *right;
OperationType operation;
BoundingBox bounds;
protected:
Intersect localIntersect(Ray r);
Tuple localNormalAt(Tuple point, Intersection *hit = nullptr);
BoundingBox getLocalBounds();
bool intersectionAllowed(bool leftHit, bool inLeft, bool inRight);
Intersect filterIntersections(Intersect &xs);
void updateBoundingBox();
BoundingBox getBounds();
public:
CSG(OperationType operation, Shape *left, Shape *right);
Intersect intersect(Ray r);
bool includes(Shape *b);
void updateTransform();
void dumpMe(FILE *fp);
};
#endif /* DORAYME_CSG_H */

View File

@@ -44,6 +44,8 @@ public:
void updateBoundingBox();
void updateTransform();
bool includes(Shape *b);
Group();
void dumpMe(FILE * fp);

View File

@@ -61,6 +61,8 @@ public:
BoundingBox getLocalBounds();
BoundingBox getBounds();
bool includes(Shape *b);
void updateBoundingBox();
void updateTransform();

View File

@@ -24,6 +24,7 @@ private:
uint64_t triangleCount; /* Total number of triangle */
uint64_t smoothTriangleCount; /* Total number of smooth triangle */
uint64_t objfileCount; /* Total number of OBJ File */
uint64_t csgCount; /* Total number of CSG */
uint64_t pixelCount; /* Total number of rendered pixels */
uint64_t rayCount; /* Total number of rays */
@@ -42,7 +43,7 @@ public:
RenderStats() : coneCount(0), cylinderCount(0), cubeCount(0), groupCount(0), lightCount(0), planeCount(0), sphereCount(0), triangleCount(0),
pixelCount(0), rayCount(0), lightRayEmitedCount(0), reflectionRayCount(0), refractedRayCount(0),
intersectCount(0), intersectionCount(0), reallocCallCount(0), mallocCallCount(0), smoothTriangleCount(0),
discardedIntersectCount(0), maxDepthAttained(UINT64_MAX), maxIntersectOnARay(0), objfileCount(0) {};
discardedIntersectCount(0), maxDepthAttained(UINT64_MAX), maxIntersectOnARay(0), objfileCount(0), csgCount(0) {};
#ifdef RENDER_STATS
void addCone();
void addCylinder();
@@ -51,6 +52,7 @@ public:
void addLight();
void addPlane();
void addSphere();
void addCsg();
void addOBJFile();
void addTriangle();
void addSmoothTriangle();
@@ -90,7 +92,8 @@ public:
static void addMalloc() {};
static void addRealloc() {};
static void setMaxIntersect(uint32_t count) {};
static void void RenderStats::addOBJFile() {};
static void addOBJFile() {};
static void addCsg() {};
#endif
};

View File

@@ -31,6 +31,7 @@ enum ShapeType
SHAPE_TRIANGLE,
SHAPE_OBJFILE,
SHAPE_SMOOTHTRIANGLE,
SHAPE_CSG,
};
/* Base class for all object that can be presented in the world */
@@ -58,7 +59,6 @@ public:
Shape(ShapeType = SHAPE_NONE);
virtual Intersect intersect(Ray r);
virtual Intersect intersectOOB(Ray r) { return this->intersect(r); };
Tuple normalAt(Tuple point, Intersection *hit = nullptr);
/* Bounding box points are always world value */
@@ -68,6 +68,8 @@ public:
virtual void updateTransform();
virtual bool includes(Shape *b) { return this == b; };
virtual void dumpMe(FILE *fp);
Tuple worldToObject(Tuple point) { return this->inverseTransform * point; };
@@ -75,9 +77,9 @@ public:
Tuple normalToWorld(Tuple normalVector);
void setTransform(Matrix transform);
void setMaterial(Material material) { this->material = material; };
Ray transform(Ray r) { return Ray(this->transformMatrix * r.origin, this->transformMatrix * r.direction); };
Ray invTransform(Ray r) { return Ray(this->inverseTransform * r.origin, this->inverseTransform * r.direction); };
void setMaterial(Material material) { this->material = material; this->materialSet = true; };
Ray transform(Ray r) { return Ray(this->transformMatrix * r.origin, this->transformMatrix * r.direction); };
Ray invTransform(Ray r) { return Ray(this->inverseTransform * r.origin, this->inverseTransform * r.direction); };
bool operator==(const Shape &b) const { return this->material == b.material &&
this->type == b.type &&

View File

@@ -45,7 +45,7 @@ Computation Intersection::prepareComputation(Ray r, Intersect *xs)
if ((xs != nullptr) && (xs->hit().object->material.transparency > 0))
{
List containers;
int j, k;
int j;
for (j = 0 ; j < xs->count() ; j++)
{

View File

@@ -153,18 +153,17 @@ public:
double u,v;
if (this->type == CUBIC_MAP)
{
CubeFaces face = this->faceFromPoint(point);
CubeFaces face = TextureMap::faceFromPoint(point);
UVPattern *facePat;
double u, v;
switch(face)
{
default:
case CUBE_LEFT: facePat = this->leftPat; this->cubeUBLeft(point, u, v); break;
case CUBE_RIGHT: facePat = this->rightPat; this->cubeUBRight(point, u, v); break;
case CUBE_FRONT: facePat = this->frontPat; this->cubeUBFront(point, u, v); break;
case CUBE_BACK: facePat = this->backPat; this->cubeUBBack(point, u, v); break;
case CUBE_UP: facePat = this->upPat; this->cubeUBUp(point, u, v); break;
case CUBE_DOWN: facePat = this->downPat; this->cubeUBDown(point, u, v); break;
case CUBE_LEFT: facePat = this->leftPat; TextureMap::cubeUBLeft(point, u, v); break;
case CUBE_RIGHT: facePat = this->rightPat; TextureMap::cubeUBRight(point, u, v); break;
case CUBE_FRONT: facePat = this->frontPat; TextureMap::cubeUBFront(point, u, v); break;
case CUBE_BACK: facePat = this->backPat; TextureMap::cubeUBBack(point, u, v); break;
case CUBE_UP: facePat = this->upPat; TextureMap::cubeUBUp(point, u, v); break;
case CUBE_DOWN: facePat = this->downPat; TextureMap::cubeUBDown(point, u, v); break;
}
return facePat->uvPatternAt(u, v);

View File

@@ -133,6 +133,13 @@ void RenderStats::addDiscardedIntersect()
this->discardedIntersectCount++;
};
void RenderStats::addCsg()
{
#pragma omp atomic
this->csgCount++;
};
void RenderStats::setMaxDepth(uint32_t depth)
{
if (this->maxDepthAttained > depth)
@@ -162,6 +169,7 @@ void RenderStats::printStats()
printf("Triangles : %lld\n", this->triangleCount);
printf("Smooth Triangles : %lld\n", this->smoothTriangleCount);
printf("OBJ File : %lld\n", this->objfileCount);
printf("CSG : %lld\n", this->csgCount);
printf("==================================================\n");
printf("Pixel rendered : %lld\n", this->pixelCount);
printf("Ray casted : %lld\n", this->rayCount);

138
source/shapes/csg.cpp Normal file
View File

@@ -0,0 +1,138 @@
/*
* DoRayMe - a quick and dirty Raytracer
* Constructive Solid Geometry (CSG) implementation
*
* Created by Manoël Trapier
* Copyright (c) 2020 986-Studio.
*
*/
#include <tuple.h>
#include <ray.h>
#include <shape.h>
#include <csg.h>
#include <math_helper.h>
CSG::CSG(OperationType operation, Shape *left, Shape *right) : Shape(SHAPE_CSG), operation(operation), left(left), right(right)
{
stats.addCsg();
this->left->parent = this;
this->right->parent = this;
this->bounds | this->left->getBounds();
this->bounds | this->right->getBounds();
}
Intersect CSG::localIntersect(Ray r)
{
int i;
Intersect leftxs = this->left->intersect(r);
Intersect rightxs = this->right->intersect(r);
for(i = 0; i < rightxs.count(); i++)
{
leftxs.add(rightxs[i]);
}
Intersect ret = this->filterIntersections(leftxs);
return ret;
}
Intersect CSG::intersect(Ray r)
{
return localIntersect(r);
}
Tuple CSG::localNormalAt(Tuple point, Intersection *hit)
{
return Vector(1, 0, 0);
}
BoundingBox CSG::getLocalBounds()
{
return this->bounds;
}
BoundingBox CSG::getBounds()
{
if (this->bounds.isEmpty()) { this->updateBoundingBox(); }
return this->bounds;
}
void CSG::updateBoundingBox()
{
this->bounds.reset();
this->bounds | this->left->getBounds();
this->bounds | this->right->getBounds();
}
void CSG::updateTransform()
{
Shape::updateTransform();
this->left->updateTransform();
this->right->updateTransform();
/* Once the full stack being notified of the changes, let's update the
* bounding box
*/
this->updateBoundingBox();
}
bool CSG::includes(Shape *b)
{
if (this->left->includes(b)) { return true; }
if (this->right->includes(b)) { return true; }
if (this == b) { return true; }
return false;
}
bool CSG::intersectionAllowed(bool leftHit, bool inLeft, bool inRight)
{
switch(this->operation)
{
case CSG::UNION: return (leftHit && !inRight) || (!leftHit && !inLeft);
case CSG::INTERSECTION: return (!leftHit && inLeft) || (leftHit && inRight);
case CSG::DIFFERENCE: return (leftHit && !inRight) || (!leftHit && inLeft);
}
return false;
}
Intersect CSG::filterIntersections(Intersect &xs)
{
bool inl = false;
bool inr = false;
Intersect ret = Intersect();
int i;
for(i = 0; i < xs.count(); i++)
{
bool lhit = this->left->includes(xs[i].object);
if (this->intersectionAllowed(lhit, inl, inr))
{
ret.add(xs[i]);
}
if (lhit)
{
inl = !inl;
}
else
{
inr = !inr;
}
}
return ret;
}
void CSG::dumpMe(FILE *fp)
{
}

View File

@@ -18,11 +18,11 @@ Group::Group() : Shape(SHAPE_GROUP)
{
stats.addGroup();
this->allocatedObjectCount = MIN_ALLOC;
this->objectList = (Shape **)calloc(sizeof(Shape *), MIN_ALLOC);
this->objectList = (Shape **)calloc(sizeof(Shape **), MIN_ALLOC);
this->objectCount = 0;
this->allocatedUnboxableObjectCount = MIN_ALLOC;
this->unboxableObjectList = (Shape **)calloc(sizeof(Shape *), MIN_ALLOC);
this->unboxableObjectList = (Shape **)calloc(sizeof(Shape **), MIN_ALLOC);
this->unboxableObjectCount = 0;
}
@@ -67,6 +67,35 @@ Intersect Group::intersect(Ray r)
return ret;
}
bool Group::includes(Shape *b)
{
if (this->objectCount > 0)
{
int i;
for (i = 0 ; i < this->objectCount ; i++)
{
if (this->objectList[i] == b)
{
return true;
}
}
}
/* We are force to do them all the time */
if (this->unboxableObjectCount > 0)
{
int i;
for(i = 0; i < this->unboxableObjectCount; i++)
{
if (this->unboxableObjectList[i] == b)
{
return true;
}
}
}
return false;
}
Intersect Group::localIntersect(Ray r)
{
return Intersect();

View File

@@ -41,7 +41,6 @@ double Light::intensityAt(World &w, Tuple point)
}
}
return total / this->samples;
break;
}
}

View File

@@ -130,6 +130,22 @@ Intersect OBJFile::intersect(Ray r)
return ret;
}
bool OBJFile::includes(Shape *b)
{
int i;
if (this->faceGroupCount > 0)
{
for (i = 0 ; i < this->faceGroupCount ; i++)
{
if (this->faceGroupList[i] == b)
{
return true;
}
}
}
return false;
}
Intersect OBJFile::localIntersect(Ray r)
{
return Intersect();
@@ -283,7 +299,6 @@ static int parseFaceVertex(char *buf, uint32_t &v, uint32_t &vt, uint32_t &vn)
{
uint32_t bufPos = 0;
uint32_t lineLength = strlen(buf);
char *tmp = buf;
vt = INT32_MAX;
vn = INT32_MAX;
int ret = 0;

View File

@@ -10,7 +10,7 @@ link_libraries(rayonnement)
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 cone_test.cpp group_test.cpp
boundingbox_test.cpp triangle_test.cpp sequence_test.cpp objfile_test.cpp smoothtriangle_test.cpp)
boundingbox_test.cpp triangle_test.cpp sequence_test.cpp objfile_test.cpp smoothtriangle_test.cpp csg_test.cpp)
add_executable(testMyRays)
target_include_directories(testMyRays PUBLIC ${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR})
@@ -81,6 +81,9 @@ add_custom_command(
${CMAKE_CURRENT_BINARY_DIR}/
)
add_executable(ch16_test)
target_sources(ch16_test PRIVATE ch16_test.cpp)
add_executable(arealight_test)
target_sources(arealight_test PRIVATE arealight_test.cpp)
@@ -148,6 +151,7 @@ add_test(NAME Chapter13_Test COMMAND $<TARGET_FILE:ch13_test>)
add_test(NAME Chapter13_ConeBonus COMMAND $<TARGET_FILE:ch13_cone>)
add_test(NAME Chapter14_Test COMMAND $<TARGET_FILE:ch14_test>)
add_test(NAME Chapter15_Teapots COMMAND $<TARGET_FILE:ch15_teapot_objfile>)
add_test(NAME Chapter16_Test COMMAND $<TARGET_FILE:ch16_test>)
add_test(NAME AreaLight_Test COMMAND $<TARGET_FILE:arealight_test>)
add_test(NAME UVMap_CheckeredSphere COMMAND $<TARGET_FILE:uvmap_checkeredsphere>)
add_test(NAME UVMap_CheckeredPlane COMMAND $<TARGET_FILE:uvmap_checkeredplane>)

194
tests/ch16_test.cpp Normal file
View File

@@ -0,0 +1,194 @@
/*
* DoRayMe - a quick and dirty Raytracer
* Render test for CSG in chapter 16.
*
* Created by Manoël Trapier
* Copyright (c) 2020 986-Studio.
*
*/
#include <world.h>
#include <light.h>
#include <sphere.h>
#include <plane.h>
#include <cube.h>
#include <cylinder.h>
#include <material.h>
#include <colour.h>
#include <canvas.h>
#include <camera.h>
#include <group.h>
#include <cone.h>
#include <csg.h>
#include <pattern.h>
#include <strippattern.h>
#include <gradientpattern.h>
#include <checkerspattern.h>
#include <ringpattern.h>
#include <transformation.h>
int main()
{
World w = World();
/* Add lights */
Light light1 = Light(POINT_LIGHT, Point(6, 10, 10), Colour(0.5, 0.4, 0.5));
w.addLight(&light1);
/* Add lights */
Light light2 = Light(POINT_LIGHT, Point(6, 10, -2.5), Colour(0.5, 0.6, 0.5));
w.addLight(&light2);
/* ----------------------------- */
/* Floor */
Plane p = Plane();
CheckersPattern checkered = CheckersPattern(Colour(0.35, 0.35, 0.35), Colour(0.4, 0.4, 0.4));
p.material.pattern = &checkered;
p.material.ambient = 0.2;
p.material.diffuse = 1;
p.material.specular = 0;
p.material.reflective = 0.1;
p.setTransform(translation(0, 0, 0));
w.addObject(&p);
Plane p2 = Plane();
p2.setTransform(translation(0, 0, -3) * rotationX(M_PI/2));
p2.material.pattern = &checkered;
p2.material.ambient = 0.2;
p2.material.diffuse = 1;
p2.material.specular = 0;
w.addObject(&p2);
/* ----------------------------- */
/* Funky cube */
Cylinder c1 = Cylinder();
c1.minCap = -2;
c1.maxCap = 2;
c1.isClosed = true;
c1.material.colour = Colour(1, 0, 0);
c1.setTransform(scaling(0.4, 1, 0.4));
c1.materialSet = true;
Cylinder c2 = Cylinder();
c2.minCap = -2;
c2.maxCap = 2;
c2.isClosed = true;
c2.material.colour = Colour(0, 1, 0);
c2.setTransform(rotationX(M_PI/2) * scaling(0.4, 1, 0.4));
c2.materialSet = true;
CSG leaf1 = CSG(CSG::UNION, &c1, &c2);
Cylinder c3 = Cylinder();
c3.minCap = -2;
c3.maxCap = 2;
c3.isClosed = true;
c3.material.colour = Colour(0, 0, 1);
c3.setTransform(rotationZ(M_PI/2) * scaling(0.4, 1, 0.4));
c3.materialSet = true;
CSG leaf2 = CSG(CSG::UNION, &leaf1, &c3);
Cube cb = Cube();
cb.materialSet = true;
cb.material.reflective = 0.5;
cb.material.colour = Colour(0.3, 0.3, 0.3);
cb.material.ambient = 0;
cb.material.diffuse = 0.3;
cb.material.specular = 0.3;
cb.material.shininess = 20;
Sphere sp = Sphere();
sp.setTransform(scaling(1.35, 1.35, 1.35));
sp.materialSet = true;
sp.material.colour = Colour(0, 0, 0);
sp.material.ambient = 0;
sp.material.specular = 0.3;
sp.material.shininess = 20;
sp.material.reflective = 0.05;
sp.material.diffuse = 0.3;
CSG leaf3 = CSG(CSG::INTERSECTION, &sp, &cb);
CSG leaf4 = CSG(CSG::DIFFERENCE, &leaf3, &leaf2);
leaf4.setTransform(translation(0, 1, 0.8) * rotationY(-0.45));
w.addObject(&leaf4);
/* ----------------------------- */
/* Tricylinder weirdy */
Cylinder sp1 = Cylinder();
sp1.minCap = -2;
sp1.maxCap = 2;
sp1.isClosed = true;
sp1.materialSet = true;
sp1.material.colour = Colour(1, 0, 0);
Cylinder sp2 = Cylinder();
sp2.minCap = -2;
sp2.maxCap = 2;
sp2.isClosed = true;
sp2.materialSet = true;
sp2.setTransform(rotationX(M_PI/2));
sp2.material.colour = Colour(0, 1, 0);
Cylinder sp3 = Cylinder();
sp3.minCap = -2;
sp3.maxCap = 2;
sp3.isClosed = true;
sp3.materialSet = true;
sp3.setTransform(rotationZ(M_PI/2));
sp3.material.colour = Colour(0, 0, 1);
CSG spleaf1 = CSG(CSG::INTERSECTION, &sp1, &sp2);
CSG spleaf2 = CSG(CSG::INTERSECTION, &spleaf1, &sp3);
spleaf2.setTransform(translation(4, 1, -0.1) * rotationY(0.35));
w.addObject(&spleaf2);
/* ----------------------------- */
Group grp = Group();
int i;
#define SLICE_NUM (12)
for(i = 0; i < SLICE_NUM; i++)
{
Cube *c = new Cube();
c->setTransform(rotationY((2*M_PI / SLICE_NUM) * i) * scaling(0.1, 1.1, 0.7) * translation(0, 0, 0.9));
c->dropShadow = false;
grp.addObject(c);
}
grp.materialSet = true;
grp.dropShadow = false;
grp.material.ambient = 0;
grp.material.diffuse = 0.1;
grp.material.specular = 0;
grp.material.transparency = 1;
grp.material.reflective = 1;
grp.material.refractiveIndex = 1;
Sphere ballSp = Sphere();
ballSp.materialSet = true;
ballSp.material.colour = Colour(0.7, 0.2, 0.1);
CSG ballLeaf = CSG(CSG::INTERSECTION, &grp, &ballSp);
ballLeaf.setTransform(translation(-4, 1, -0.1) * rotationY(-0.35) * rotationZ(0.1));
w.addObject(&ballLeaf);
/* ----------------------------- */
/* Set the camera */
Camera camera = Camera(80, 40, M_PI / 2);
camera.setTransform(viewTransform(Point(0, 3, 5),
Point(0, 1, 0),
Vector(0, 1, 0)));
/* Now render it */
Canvas image = camera.render(w, 5);
image.SaveAsPNG("ch16_test.png");
return 0;
}

226
tests/csg_test.cpp Normal file
View File

@@ -0,0 +1,226 @@
/*
* DoRayMe - a quick and dirty Raytracer
* CSG unit tests
*
* Created by Manoël Trapier
* Copyright (c) 2020 986-Studio.
*
*/
#include <intersect.h>
#include <intersection.h>
#include <sphere.h>
#include <cube.h>
#include <csg.h>
#include <transformation.h>
#include <gtest/gtest.h>
/* Proxy class to get access to protected functions / members */
class CSGTest : public CSG
{
public:
Intersect doLocalIntersect(Ray r) {
return this->localIntersect(r);
};
Tuple doLocalNormalAt(Tuple point, Intersection *hit = nullptr) {
return this->localNormalAt(point, hit);
};
BoundingBox doGetLocalBounds() {
return this->getLocalBounds();
};
bool doIntersectionAllowed(bool leftHit, bool inLeft, bool inRight) {
return this->intersectionAllowed(leftHit, inLeft, inRight);
};
Intersect doFilterIntersections(Intersect &xs) {
return this->filterIntersections(xs);
}
CSGTest(OperationType operation, Shape *left, Shape *right) : CSG(operation, left, right) {};
Shape *getLeft() { return this->left; };
Shape *getRight() { return this->right; };
OperationType getOperation() { return this->operation; };
void setOperation(OperationType operation) { this->operation = operation; };
};
TEST(CSGTest, Csg_is_created_with_an_operation_and_two_shape)
{
Sphere s1 = Sphere();
Cube s2 = Cube();
CSGTest c = CSGTest(CSG::UNION, &s1, &s2);
ASSERT_EQ(c.getOperation(), CSG::UNION);
ASSERT_EQ(*c.getLeft(), s1);
ASSERT_EQ(*c.getRight(), s2);
ASSERT_EQ(*s1.parent, c);
ASSERT_EQ(*s2.parent, c);
}
TEST(CSGTest, Evaluating_the_rules_for_a_csg_operation)
{
Sphere s1 = Sphere();
Cube s2 = Cube();
CSGTest c = CSGTest(CSG::UNION, &s1, &s2);
CSG::OperationType testList2[] = {
CSG::UNION, CSG::UNION,
CSG::UNION, CSG::UNION,
CSG::UNION, CSG::UNION,
CSG::UNION, CSG::UNION,
CSG::INTERSECTION, CSG::INTERSECTION,
CSG::INTERSECTION, CSG::INTERSECTION,
CSG::INTERSECTION, CSG::INTERSECTION,
CSG::INTERSECTION, CSG::INTERSECTION,
CSG::DIFFERENCE, CSG::DIFFERENCE,
CSG::DIFFERENCE, CSG::DIFFERENCE,
CSG::DIFFERENCE, CSG::DIFFERENCE,
CSG::DIFFERENCE, CSG::DIFFERENCE,
};
bool testList[][3] = {
/* lhit, inl, inr */
/* UNION */ { true, true, true },
{ true, true, false },
{ true, false, true },
{ true, false, false },
{ false, true, true },
{ false, true, false },
{ false, false, true },
{ false, false, false },
/* INTER */ { true, true, true },
{ true, true, false },
{ true, false, true },
{ true, false, false },
{ false, true, true },
{ false, true, false },
{ false, false, true },
{ false, false, false },
/* DIFFE */ { true, true, true },
{ true, true, false },
{ true, false, true },
{ true, false, false },
{ false, true, true },
{ false, true, false },
{ false, false, true },
{ false, false, false },
};
bool testResults[] {
/* Unions */
false,
true,
false,
true,
false,
false,
true,
true,
/* Intersection */
true,
false,
true,
false,
true,
true,
false,
false,
/* difference */
false,
true,
false,
true,
true,
true,
false,
false
};
int testCount = sizeof(testList)/sizeof((testList)[0]);
int i;
for(i = 0; i < testCount; i++)
{
c.setOperation(testList2[i]);
ASSERT_EQ(c.doIntersectionAllowed(testList[i][0], testList[i][1], testList[i][2]), testResults[i]);
}
}
TEST(CSGTest, Filtering_a_list_of_intersections)
{
Sphere s1 = Sphere();
Cube s2 = Cube();
CSGTest c = CSGTest(CSG::UNION, &s1, &s2);
CSG::OperationType testList[] = {
CSG::UNION,
CSG::INTERSECTION,
CSG::DIFFERENCE,
};
uint32_t testResults[][2] = {
{0, 3},
{1, 2},
{0, 1},
};
int testCount = sizeof(testList)/sizeof((testList)[0]);
int i;
Intersect xs = Intersect();
Intersection i1 = Intersection(1, &s1);
Intersection i2 = Intersection(2, &s2);
Intersection i3 = Intersection(3, &s1);
Intersection i4 = Intersection(4, &s2);
xs.add(i1);
xs.add(i2);
xs.add(i3);
xs.add(i4);
for(i = 0; i < testCount; i++)
{
c.setOperation(testList[i]);
Intersect result = c.doFilterIntersections(xs);
ASSERT_EQ(result.count(), 2);
ASSERT_EQ(result[0], xs[testResults[i][0]]);
ASSERT_EQ(result[1], xs[testResults[i][1]]);
}
}
TEST(CSGTest, A_ray_misses_a_csg_object)
{
Sphere s1 = Sphere();
Cube s2 = Cube();
CSGTest c = CSGTest(CSG::UNION, &s1, &s2);
Ray r = Ray(Point(0, 2, -5), Vector(0, 0, 1));
Intersect xs = c.doLocalIntersect(r);
ASSERT_EQ(xs.count(), 0);
}
TEST(CSGTest, A_ray_hits_a_csg_object)
{
Sphere s1 = Sphere();
Sphere s2 = Sphere();
s2.setTransform(translation(0, 0, 0.5));
CSGTest c = CSGTest(CSG::UNION, &s1, &s2);
Ray r = Ray(Point(0, 0, -5), Vector(0, 0, 1));
Intersect xs = c.doLocalIntersect(r);
ASSERT_EQ(xs.count(), 2);
ASSERT_TRUE(double_equal(xs[0].t, 4));
ASSERT_EQ(xs[0].object, &s1);
ASSERT_TRUE(double_equal(xs[1].t, 6.5));
ASSERT_EQ(xs[1].object, &s2);
}

View File

@@ -59,7 +59,7 @@ TEST(CylinderTest, A_ray_hit_a_cylinder)
double t0s[] = { 5, 4, 6.80798 };
double t1s[] = { 5, 6, 7.08872 };
int i, j;
int i;
for(i = 0; i < 3; i++)
{
Tuple direction = Directions[i].normalise();

View File

@@ -73,7 +73,6 @@ TEST(PlaneTest, A_ray_intersecting_a_plane_from_below)
TEST(PlaneTest, The_bounding_box_of_a_plane)
{
Plane t = Plane();
BoundingBox b = BoundingBox(Point(-8, -5, -8), Point(8, 8, 8));
BoundingBox res = t.getBounds();
ASSERT_FALSE(res.min.isRepresentable());

View File

@@ -54,7 +54,7 @@ int main()
Plane p4 = Plane();
p3.setTransform(translation(6, 0, 0) * rotationZ(M_PI/2) );
p3.material.pattern = new CheckersPattern(Colour(1, 1, 1), Colour(0.1, 0.1, 0.1));
w.addObject(&p3);
w.addObject(&p4);
Plane p5 = Plane();
p5.material.pattern = new CheckersPattern(Colour(1, 1, 1), Colour(0.1, 0.1, 0.1));

View File

@@ -31,7 +31,7 @@ int main()
Light light = Light(POINT_LIGHT, Point(0, 100, 0), Colour(1, 1, 1));
w.addLight(&light);
Sphere sp = Sphere();;
Sphere sp = Sphere();
sp.setTransform(translation(0, 0, 5) * scaling(0.75, 0.75, 0.75));
sp.material.diffuse = 0.4;
sp.material.specular = 0.6;

View File

@@ -44,7 +44,7 @@ TEST(WorldTest, The_default_world)
ASSERT_TRUE(w.lightIsIn(l));
ASSERT_TRUE(w.objectIsIn(s1));
ASSERT_TRUE(w.objectIsIn(s2));
};
}
TEST(WorldTest, Intersect_a_world_with_a_ray)
{
@@ -169,7 +169,7 @@ TEST(WorldTest, Shade_hit_is_given_an_intersection_in_shadow)
Tuple c = w.shadeHit(comps);
ASSERT_EQ(c, Colour(0.1, 0.1, 0.1));
};
}
TEST(WorldTest, The_reflected_colour_for_a_non_reflective_material)
{