Working on groups
This commit is contained in:
38
source/include/group.h
Normal file
38
source/include/group.h
Normal file
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* DoRayMe - a quick and dirty Raytracer
|
||||
* Group header
|
||||
*
|
||||
* Created by Manoël Trapier
|
||||
* Copyright (c) 2020 986-Studio.
|
||||
*
|
||||
*/
|
||||
#ifndef DORAYME_GROUP_H
|
||||
#define DORAYME_GROUP_H
|
||||
|
||||
#include <shape.h>
|
||||
|
||||
class Group : public Shape
|
||||
{
|
||||
private:
|
||||
uint32_t allocatedObjectCount;
|
||||
Shape* *objectList;
|
||||
uint32_t objectCount;
|
||||
|
||||
protected:
|
||||
Intersect localIntersect(Ray r);
|
||||
Tuple localNormalAt(Tuple point);
|
||||
|
||||
|
||||
|
||||
public:
|
||||
bool isEmpty();
|
||||
|
||||
void addObject(Shape *s);
|
||||
Shape *operator[](const int p) { return this->objectList[p]; }
|
||||
|
||||
Intersect intersect(Ray r);
|
||||
|
||||
Group();
|
||||
};
|
||||
|
||||
#endif /* DORAYME_GROUP_H */
|
||||
@@ -25,6 +25,8 @@ enum ShapeType
|
||||
SHAPE_CUBE,
|
||||
SHAPE_CYLINDER,
|
||||
SHAPE_CONE,
|
||||
SHAPE_GROUP,
|
||||
|
||||
};
|
||||
|
||||
/* Base class for all object that can be presented in the world */
|
||||
@@ -32,16 +34,19 @@ class Shape
|
||||
{
|
||||
private:
|
||||
ShapeType type;
|
||||
|
||||
private:
|
||||
Matrix localTransformMatrix;
|
||||
protected:
|
||||
virtual Intersect localIntersect(Ray r) = 0;
|
||||
virtual Tuple localNormalAt(Tuple point) = 0;
|
||||
|
||||
public:
|
||||
Matrix transformMatrix;
|
||||
Matrix inverseTransform;
|
||||
Matrix transposedInverseTransform;
|
||||
|
||||
Material material;
|
||||
bool dropShadow;
|
||||
Shape *parent;
|
||||
|
||||
public:
|
||||
Shape(ShapeType = SHAPE_NONE);
|
||||
@@ -49,6 +54,12 @@ public:
|
||||
Intersect intersect(Ray r);
|
||||
Tuple normalAt(Tuple point);
|
||||
|
||||
//virtual Bounds getBounds();
|
||||
|
||||
void updateTransform();
|
||||
Tuple worldToObject(Tuple point) { return this->inverseTransform * point; };
|
||||
Tuple normalToWorld(Tuple normalVector) { return (this->transposedInverseTransform * normalVector).normalise(); };
|
||||
|
||||
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); };
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
|
||||
class Sphere : public Shape
|
||||
{
|
||||
private:
|
||||
protected:
|
||||
Intersect localIntersect(Ray r);
|
||||
Tuple localNormalAt(Tuple point);
|
||||
|
||||
|
||||
73
source/shapes/group.cpp
Normal file
73
source/shapes/group.cpp
Normal file
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* DoRayMe - a quick and dirty Raytracer
|
||||
* Group implementation
|
||||
*
|
||||
* Created by Manoël Trapier
|
||||
* Copyright (c) 2020 986-Studio.
|
||||
*
|
||||
*/
|
||||
#include <tuple.h>
|
||||
#include <ray.h>
|
||||
#include <group.h>
|
||||
#include <cone.h>
|
||||
#include <math_helper.h>
|
||||
|
||||
#define MIN_ALLOC (2)
|
||||
|
||||
Group::Group() : Shape(SHAPE_GROUP)
|
||||
{
|
||||
this->allocatedObjectCount = MIN_ALLOC;
|
||||
this->objectList = (Shape **)calloc(sizeof(Shape *), MIN_ALLOC);
|
||||
this->objectCount = 0;
|
||||
}
|
||||
|
||||
Intersect Group::intersect(Ray r)
|
||||
{
|
||||
Intersect ret;
|
||||
int i, j;
|
||||
if (this->objectCount > 0)
|
||||
{
|
||||
for(i = 0; i < this->objectCount; i++)
|
||||
{
|
||||
Intersect xs = this->objectList[i]->intersect(r);
|
||||
if (xs.count() > 0)
|
||||
{
|
||||
for(j = 0; j < xs.count(); j++)
|
||||
{
|
||||
ret.add(xs[j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
Intersect Group::localIntersect(Ray r)
|
||||
{
|
||||
return Intersect();
|
||||
}
|
||||
|
||||
Tuple Group::localNormalAt(Tuple point)
|
||||
{
|
||||
return Vector(1, 0, 0);
|
||||
}
|
||||
|
||||
void Group::addObject(Shape *s)
|
||||
{
|
||||
if ((this->objectCount + 1) > this->allocatedObjectCount)
|
||||
{
|
||||
this->allocatedObjectCount *= 2;
|
||||
this->objectList = (Shape **)realloc(this->objectList, sizeof(Shape **) * this->allocatedObjectCount);
|
||||
}
|
||||
|
||||
s->parent = this;
|
||||
s->updateTransform();
|
||||
|
||||
this->objectList[this->objectCount++] = s;
|
||||
}
|
||||
|
||||
bool Group::isEmpty()
|
||||
{
|
||||
return (this->objectCount == 0);
|
||||
}
|
||||
@@ -15,10 +15,11 @@
|
||||
|
||||
Shape::Shape(ShapeType type)
|
||||
{
|
||||
this->parent = nullptr;
|
||||
this->dropShadow = true;
|
||||
this->type = type;
|
||||
this->transformMatrix = Matrix4().identity();
|
||||
this->inverseTransform = this->transformMatrix.inverse();
|
||||
this->localTransformMatrix = Matrix4().identity();
|
||||
this->updateTransform();
|
||||
}
|
||||
|
||||
Intersect Shape::intersect(Ray r)
|
||||
@@ -32,7 +33,7 @@ Tuple Shape::normalAt(Tuple point)
|
||||
|
||||
Tuple local_normal = this->localNormalAt(local_point);
|
||||
|
||||
Tuple world_normal = this->inverseTransform.transpose() * local_normal;
|
||||
Tuple world_normal = this->transposedInverseTransform * local_normal;
|
||||
|
||||
/* W may get wrong, so hack it. This is perfectly normal as we are using a 4x4 matrix instead of a 3x3 */
|
||||
world_normal.w = 0;
|
||||
@@ -40,8 +41,20 @@ Tuple Shape::normalAt(Tuple point)
|
||||
return world_normal.normalise();
|
||||
}
|
||||
|
||||
void Shape::updateTransform()
|
||||
{
|
||||
this->transformMatrix = this->localTransformMatrix;
|
||||
if (this->parent != nullptr)
|
||||
{
|
||||
this->transformMatrix = this->parent->transformMatrix * this->transformMatrix;
|
||||
}
|
||||
|
||||
this->inverseTransform = this->transformMatrix.inverse();
|
||||
this->transposedInverseTransform = this->inverseTransform.transpose();
|
||||
}
|
||||
|
||||
void Shape::setTransform(Matrix transform)
|
||||
{
|
||||
this->transformMatrix = transform;
|
||||
this->inverseTransform = transform.inverse();
|
||||
this->localTransformMatrix = transform;
|
||||
this->updateTransform();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user