And CSG! \o/
Still working on a nice scene for it.
This commit is contained in:
133
source/shapes/csg.cpp
Normal file
133
source/shapes/csg.cpp
Normal file
@@ -0,0 +1,133 @@
|
||||
/*
|
||||
* 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;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user