Change World to use a base group instead of storing all the world object.
Makes the code simpler and avoid some potential issues. Add to change a bit some test to make them pass properly.
This commit is contained in:
@@ -46,6 +46,8 @@ public:
|
|||||||
|
|
||||||
bool includes(Shape *b);
|
bool includes(Shape *b);
|
||||||
|
|
||||||
|
uint32_t getObjectCount() { return this->objectCount + this->unboxableObjectCount; };
|
||||||
|
|
||||||
Group();
|
Group();
|
||||||
|
|
||||||
void dumpMe(FILE * fp);
|
void dumpMe(FILE * fp);
|
||||||
|
|||||||
@@ -15,6 +15,7 @@
|
|||||||
#include <intersect.h>
|
#include <intersect.h>
|
||||||
#include <ray.h>
|
#include <ray.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <group.h>
|
||||||
|
|
||||||
#ifdef ENABLE_LUA_SUPPORT
|
#ifdef ENABLE_LUA_SUPPORT
|
||||||
extern "C" {
|
extern "C" {
|
||||||
@@ -25,15 +26,13 @@ extern "C" {
|
|||||||
class World
|
class World
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
uint32_t objectCount;
|
|
||||||
uint32_t lightCount;
|
uint32_t lightCount;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
uint32_t allocatedObjectCount;
|
|
||||||
uint32_t allocatedLightCount;
|
uint32_t allocatedLightCount;
|
||||||
|
|
||||||
Light* *lightList;
|
Light* *lightList;
|
||||||
Shape* *objectList;
|
|
||||||
|
Group worldGroup;
|
||||||
|
|
||||||
#ifdef ENABLE_LUA_SUPPORT
|
#ifdef ENABLE_LUA_SUPPORT
|
||||||
lua_State *L;
|
lua_State *L;
|
||||||
@@ -50,9 +49,12 @@ public:
|
|||||||
bool lightIsIn(Light &l);
|
bool lightIsIn(Light &l);
|
||||||
bool objectIsIn(Shape &s);
|
bool objectIsIn(Shape &s);
|
||||||
|
|
||||||
Shape *getObject(int i) { return this->objectList[i]; };
|
Shape *getObject(int i) { return this->worldGroup[i]; };
|
||||||
Light *getLight(int i) { return this->lightList[i]; };
|
Light *getLight(int i) { return this->lightList[i]; };
|
||||||
|
|
||||||
|
uint32_t getObjectCount() { return this->worldGroup.getObjectCount(); };
|
||||||
|
uint32_t getLightCount() { return this->lightCount; };
|
||||||
|
|
||||||
Tuple shadeHit(Computation comps, uint32_t depthCount = 4);
|
Tuple shadeHit(Computation comps, uint32_t depthCount = 4);
|
||||||
Tuple colourAt(Ray r, uint32_t depthCount = 4);
|
Tuple colourAt(Ray r, uint32_t depthCount = 4);
|
||||||
bool isShadowed(Tuple point, Tuple lightPosition);
|
bool isShadowed(Tuple point, Tuple lightPosition);
|
||||||
|
|||||||
@@ -22,16 +22,12 @@ extern "C" {
|
|||||||
|
|
||||||
#define MIN_ALLOC (2)
|
#define MIN_ALLOC (2)
|
||||||
|
|
||||||
World::World() : objectCount(0), lightCount(0)
|
World::World() : lightCount(0)
|
||||||
{
|
{
|
||||||
this->allocatedLightCount = MIN_ALLOC;
|
this->allocatedLightCount = MIN_ALLOC;
|
||||||
this->lightList = (Light **)calloc(sizeof(Light *), MIN_ALLOC);
|
this->lightList = (Light **)calloc(sizeof(Light *), MIN_ALLOC);
|
||||||
this->lightCount = 0;
|
this->lightCount = 0;
|
||||||
|
|
||||||
this->allocatedObjectCount = MIN_ALLOC;
|
|
||||||
this->objectList = (Shape **)calloc(sizeof(Shape *), MIN_ALLOC);
|
|
||||||
this->objectCount = 0;
|
|
||||||
|
|
||||||
#ifdef ENABLE_LUA_SUPPORT
|
#ifdef ENABLE_LUA_SUPPORT
|
||||||
this->L = luaL_newstate(); /* opens Lua */
|
this->L = luaL_newstate(); /* opens Lua */
|
||||||
luaL_openlibs(L); /* opens the basic library */
|
luaL_openlibs(L); /* opens the basic library */
|
||||||
@@ -45,12 +41,10 @@ World::~World()
|
|||||||
|
|
||||||
void World::addObject(Shape *s)
|
void World::addObject(Shape *s)
|
||||||
{
|
{
|
||||||
if ((this->objectCount + 1) > this->allocatedObjectCount)
|
/* Cheaty but need to be done for now */
|
||||||
{
|
s->materialSet = true;
|
||||||
this->allocatedObjectCount *= 2;
|
|
||||||
this->objectList = (Shape **)realloc(this->objectList, sizeof(Shape **) * this->allocatedObjectCount);
|
this->worldGroup.addObject(s);
|
||||||
}
|
|
||||||
this->objectList[this->objectCount++] = s;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void World::addLight(Light *l)
|
void World::addLight(Light *l)
|
||||||
@@ -78,33 +72,12 @@ bool World::lightIsIn(Light &l)
|
|||||||
|
|
||||||
bool World::objectIsIn(Shape &s)
|
bool World::objectIsIn(Shape &s)
|
||||||
{
|
{
|
||||||
int i;
|
return this->worldGroup.includes(&s);
|
||||||
for(i = 0; i < this->objectCount; i++)
|
|
||||||
{
|
|
||||||
if (*this->objectList[i] == s)
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Intersect World::intersect(Ray r)
|
Intersect World::intersect(Ray r)
|
||||||
{
|
{
|
||||||
Intersect ret;
|
return this->worldGroup.intersect(r);
|
||||||
int i, j;
|
|
||||||
|
|
||||||
for(i = 0; i < this->objectCount; i++)
|
|
||||||
{
|
|
||||||
Intersect xs = this->objectList[i]->intersect(r);
|
|
||||||
|
|
||||||
for(j = 0; j < xs.count(); j++)
|
|
||||||
{
|
|
||||||
ret.add(xs[j]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Tuple World::shadeHit(Computation comps, uint32_t depthCount)
|
Tuple World::shadeHit(Computation comps, uint32_t depthCount)
|
||||||
@@ -230,12 +203,7 @@ void World::dumpMe(FILE *fp)
|
|||||||
fprintf(fp, "},\n");
|
fprintf(fp, "},\n");
|
||||||
|
|
||||||
fprintf(fp, "\"Objects\": {\n");
|
fprintf(fp, "\"Objects\": {\n");
|
||||||
for(i = 0; i < this->objectCount; i++)
|
this->worldGroup.dumpMe(fp);
|
||||||
{
|
|
||||||
fprintf(fp, "\"%d\": {\n", i);
|
|
||||||
this->objectList[i]->dumpMe(fp);
|
|
||||||
fprintf(fp, "},\n");
|
|
||||||
}
|
|
||||||
fprintf(fp, "},\n");
|
fprintf(fp, "},\n");
|
||||||
|
|
||||||
/* JSON Closing */
|
/* JSON Closing */
|
||||||
|
|||||||
@@ -22,8 +22,8 @@ TEST(WorldTest, Creating_a_world)
|
|||||||
{
|
{
|
||||||
World w;
|
World w;
|
||||||
|
|
||||||
ASSERT_EQ(w.lightCount, 0);
|
ASSERT_EQ(w.getLightCount(), 0);
|
||||||
ASSERT_EQ(w.objectCount, 0);
|
ASSERT_EQ(w.getObjectCount(), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(WorldTest, The_default_world)
|
TEST(WorldTest, The_default_world)
|
||||||
@@ -41,9 +41,12 @@ TEST(WorldTest, The_default_world)
|
|||||||
|
|
||||||
s2.setTransform(scaling(0.5, 0.5,0.5));
|
s2.setTransform(scaling(0.5, 0.5,0.5));
|
||||||
|
|
||||||
|
Shape *obj0 = w.getObject(0);
|
||||||
|
Shape *obj1 = w.getObject(1);
|
||||||
|
|
||||||
ASSERT_TRUE(w.lightIsIn(l));
|
ASSERT_TRUE(w.lightIsIn(l));
|
||||||
ASSERT_TRUE(w.objectIsIn(s1));
|
ASSERT_EQ(*obj0, s1);
|
||||||
ASSERT_TRUE(w.objectIsIn(s2));
|
ASSERT_EQ(*obj1, s2);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(WorldTest, Intersect_a_world_with_a_ray)
|
TEST(WorldTest, Intersect_a_world_with_a_ray)
|
||||||
|
|||||||
Reference in New Issue
Block a user