Fix an issue with groups, and add Chapter 14 example!

This commit is contained in:
Godzil
2020-02-26 00:30:09 +00:00
parent aded6bb943
commit 5e4cfb84e6
7 changed files with 244 additions and 3 deletions

View File

@@ -54,4 +54,7 @@ From Chapter 13 - Cylinders:
![Chapter 13 rendering test](output/ch13_test.png) ![Chapter 13 rendering test](output/ch13_test.png)
Bonus: Bonus:
![Chapter 13 cone test](output/ch13_cone.png) ![Chapter 13 cone test](output/ch13_cone.png)
From Chapter 14 - Groups & Bounding boxes
![Chapter 14 rendering test](output/ch14_test.png)

BIN
output/ch14_test.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 112 KiB

View File

@@ -37,10 +37,10 @@ public:
Shape *operator[](const int p) { return this->objectList[p]; } Shape *operator[](const int p) { return this->objectList[p]; }
Intersect intersect(Ray r); Intersect intersect(Ray r);
BoundingBox getBounds(); BoundingBox getBounds();
void updateBoundingBox(); void updateBoundingBox();
void updateTransform();
Group(); Group();
}; };

View File

@@ -61,7 +61,8 @@ public:
virtual BoundingBox getBounds(); virtual BoundingBox getBounds();
virtual bool haveFiniteBounds() { return true; }; virtual bool haveFiniteBounds() { return true; };
void updateTransform(); virtual void updateTransform();
Tuple worldToObject(Tuple point) { return this->inverseTransform * point; }; Tuple worldToObject(Tuple point) { return this->inverseTransform * point; };
Tuple objectToWorld(Tuple point) { return this->transformMatrix * point; }; Tuple objectToWorld(Tuple point) { return this->transformMatrix * point; };
Tuple normalToWorld(Tuple normalVector); Tuple normalToWorld(Tuple normalVector);

View File

@@ -136,4 +136,32 @@ void Group::updateBoundingBox()
} }
} }
} }
}
void Group::updateTransform()
{
int i;
Shape::updateTransform();
if (this->objectCount > 0)
{
for (i = 0 ; i < this->objectCount ; i++)
{
this->objectList[i]->updateTransform();
}
}
/* We are force to do them all the time */
if (this->unboxableObjectCount > 0)
{
for(i = 0; i < this->unboxableObjectCount; i++)
{
this->unboxableObjectList[i]->updateTransform();
}
}
/* Once the full stack being notified of the changes, let's update the
* bounding box
*/
this->updateBoundingBox();
} }

View File

@@ -85,6 +85,11 @@ target_include_directories(ch13_cone PUBLIC ../source/include)
target_sources(ch13_cone PRIVATE ch13_cone.cpp) target_sources(ch13_cone PRIVATE ch13_cone.cpp)
target_link_libraries(ch13_cone rayonnement) target_link_libraries(ch13_cone rayonnement)
add_executable(ch14_test)
target_include_directories(ch14_test PUBLIC ../source/include)
target_sources(ch14_test PRIVATE ch14_test.cpp)
target_link_libraries(ch14_test rayonnement)
add_test(NAME Chapter05_Test COMMAND $<TARGET_FILE:ch5_test>) add_test(NAME Chapter05_Test COMMAND $<TARGET_FILE:ch5_test>)
add_test(NAME Chapter06_Test COMMAND $<TARGET_FILE:ch6_test>) add_test(NAME Chapter06_Test COMMAND $<TARGET_FILE:ch6_test>)
add_test(NAME Chapter07_Test COMMAND $<TARGET_FILE:ch7_test>) add_test(NAME Chapter07_Test COMMAND $<TARGET_FILE:ch7_test>)
@@ -96,6 +101,7 @@ add_test(NAME Chapter11_Test COMMAND $<TARGET_FILE:ch11_test>)
add_test(NAME Chapter12_Test COMMAND $<TARGET_FILE:ch12_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_Test COMMAND $<TARGET_FILE:ch13_test>)
add_test(NAME Chapter13_ConeBonus COMMAND $<TARGET_FILE:ch13_cone>) add_test(NAME Chapter13_ConeBonus COMMAND $<TARGET_FILE:ch13_cone>)
add_test(NAME Chapter14_Test COMMAND $<TARGET_FILE:ch14_test>)
add_test(NAME Test_Rendering COMMAND $<TARGET_FILE:test_render>) add_test(NAME Test_Rendering COMMAND $<TARGET_FILE:test_render>)
add_test(NAME Hw3Render COMMAND $<TARGET_FILE:hw3render> ${CMAKE_CURRENT_SOURCE_DIR}/test.hw3scene) add_test(NAME Hw3Render COMMAND $<TARGET_FILE:hw3render> ${CMAKE_CURRENT_SOURCE_DIR}/test.hw3scene)
add_test(NAME Hw3RenderAllCmds COMMAND $<TARGET_FILE:hw3render> ${CMAKE_CURRENT_SOURCE_DIR}/test_keys.hw3scene) add_test(NAME Hw3RenderAllCmds COMMAND $<TARGET_FILE:hw3render> ${CMAKE_CURRENT_SOURCE_DIR}/test_keys.hw3scene)

203
tests/ch14_test.cpp Normal file
View File

@@ -0,0 +1,203 @@
/*
* DoRayMe - a quick and dirty Raytracer
* Render test for reflection in chapter 13.
*
* 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 <pattern.h>
#include <strippattern.h>
#include <gradientpattern.h>
#include <checkerspattern.h>
#include <ringpattern.h>
#include <transformation.h>
Shape *leg()
{
Group *ret = new Group();
Sphere *s = new Sphere();
s->setTransform(translation(0, 0, -1) * scaling(0.25, 0.25, 0.25));
ret->addObject(s);
Cylinder *cyl = new Cylinder();
cyl->minCap = 0;
cyl->maxCap = 1;
cyl->isClosed = false;
cyl->setTransform(translation(0, 0, -1) * rotationY(-0.5236) * rotationZ(-1.5708) * scaling(0.25, 1, 0.25));
ret->addObject(cyl);
return ret;
}
Shape *cap()
{
Group *ret = new Group();
Cone *c = new Cone();
c->minCap = -1;
c->maxCap = 0;
c->isClosed = false;
c->setTransform(rotationX(-0.7854) * scaling(0.24606, 1.37002, 0.24606));
ret->addObject(c);
c = new Cone();
c->minCap = -1;
c->maxCap = 0;
c->isClosed = false;
c->setTransform(rotationY(1.0472) * rotationX(-0.7854) * scaling(0.24606, 1.37002, 0.24606));
ret->addObject(c);
c = new Cone();
c->minCap = -1;
c->maxCap = 0;
c->isClosed = false;
c->setTransform(rotationY(2.0944) * rotationX(-0.7854) * scaling(0.24606, 1.37002, 0.24606));
ret->addObject(c);
c = new Cone();
c->minCap = -1;
c->maxCap = 0;
c->isClosed = false;
c->setTransform(rotationY(3.1416) * rotationX(-0.7854) * scaling(0.24606, 1.37002, 0.24606));
ret->addObject(c);
c = new Cone();
c->minCap = -1;
c->maxCap = 0;
c->isClosed = false;
c->setTransform(rotationY(4.1888) * rotationX(-0.7854) * scaling(0.24606, 1.37002, 0.24606));
ret->addObject(c);
c = new Cone();
c->minCap = -1;
c->maxCap = 0;
c->isClosed = false;
c->setTransform(rotationY(5.236) * rotationX(-0.7854) * scaling(0.24606, 1.37002, 0.24606));
ret->addObject(c);
return ret;
}
Shape *wacky()
{
Group *ret = new Group();
Shape *s;
s = leg();
ret->addObject(s);
s = leg();
s->setTransform(rotationY(1.0472));
ret->addObject(s);
s = leg();
s->setTransform(rotationY(2.0944));
ret->addObject(s);
s = leg();
s->setTransform(rotationY(3.1416));
ret->addObject(s);
s = leg();
s->setTransform(rotationY(4.1888));
ret->addObject(s);
s = leg();
s->setTransform(rotationY(5.236));
ret->addObject(s);
s = cap();
s->setTransform(translation(0, 1, 0));
ret->addObject(s);
s = cap();
s->setTransform(rotationX(3.1416) * translation(0, 1, 0));
ret->addObject(s);
return ret;
}
int main()
{
World w = World();
/* Add lights */
Light light1 = Light(POINT_LIGHT, Point(10000, 10000, -10000), Colour(0.25, 0.25, 0.25));
w.addLight(&light1);
Light light2 = Light(POINT_LIGHT, Point(-10000, 10000, -10000), Colour(0.25, 0.25, 0.25));
w.addLight(&light2);
Light light3 = Light(POINT_LIGHT, Point(10000, -10000, -10000), Colour(0.25, 0.25, 0.25));
w.addLight(&light3);
Light light4 = Light(POINT_LIGHT, Point(-10000, -10000, -10000), Colour(0.25, 0.25, 0.25));
w.addLight(&light4);
/* ----------------------------- */
/* White background */
Plane p = Plane();
p.setTransform(translation(0, 0, 100) * rotationX(1.5708));
p.material.colour = Colour(1, 1, 1);
p.material.ambient = 1;
p.material.diffuse = 0;
p.material.specular = 0;
w.addObject(&p);
Shape *wa;
wa = wacky();
wa->setTransform(translation(-2.8, 0, 0) * rotationX(0.4363) * rotationY(0.1745));
wa->material.colour = Colour(0.9, 0.2, 0.4);
wa->material.ambient = 0.2;
wa->material.diffuse = 0.8;
wa->material.specular = 0.7;
wa->material.shininess = 20;
w.addObject(wa);
wa = wacky();
wa->setTransform(rotationY(0.1745));
wa->material.colour = Colour(0.2, 0.9, 0.6);
wa->material.ambient = 0.2;
wa->material.diffuse = 0.8;
wa->material.specular = 0.7;
wa->material.shininess = 20;
w.addObject(wa);
wa = wacky();
wa->setTransform(translation(2.8, 0, 0) * rotationX(-0.4363) * rotationY(-0.1745));
wa->material.colour = Colour(0.2, 0.3, 1.0);
wa->material.ambient = 0.2;
wa->material.diffuse = 0.8;
wa->material.specular = 0.7;
wa->material.shininess = 20;
w.addObject(wa);
/* ----------------------------- */
/* Set the camera */
Camera camera = Camera(1000, 334, 0.9);
camera.setTransform(viewTransform(Point(0, 0, -9),
Point(0, 0, 0),
Vector(0, 1, 0)));
/* Now render it */
Canvas image = camera.render(w, 5);
image.SaveAsPNG("ch14_test.png");
return 0;
}