From 0fc1e468fef5d58bfe8477c1b1f085f3bceef337 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Tue, 10 Feb 2009 01:09:07 +0000 Subject: [PATCH] removed test directory, converted Shape's material handle to a refptr instead of a Material *, made refptr::operator*() and operator->() const members git-svn-id: svn://anubis/fart/trunk@96 7f9b0f55-74a9-4bce-be96-3c2cd072584d --- Makefile | 3 --- main/Lighting.cc | 10 +++++----- main/Lighting.h | 3 ++- main/Scene.cc | 6 ++---- parser/parser.yy | 6 +++++- shapes/Shape.cc | 2 +- shapes/Shape.h | 7 ++++--- test/Makefile | 20 -------------------- test/tests.cc | 37 ------------------------------------- util/refptr.h | 8 ++++---- 10 files changed, 23 insertions(+), 79 deletions(-) delete mode 100644 test/Makefile delete mode 100644 test/tests.cc diff --git a/Makefile b/Makefile index ada4317..d493b13 100644 --- a/Makefile +++ b/Makefile @@ -18,11 +18,8 @@ $(TARGET): make -C main make -C parser $(CXX) -o $@ main/*.o util/*.o shapes/*.o parser/*.o $(CXXFLAGS) $(LDFLAGS) - make -C test - $(CXX) -o tests test/*.o util/*.o shapes/*.o parser/*.o $(CXXFLAGS) $(LDFLAGS) clean: - make -C test clean make -C parser clean make -C main clean make -C shapes clean diff --git a/main/Lighting.cc b/main/Lighting.cc index 0b50d77..24fd6a8 100644 --- a/main/Lighting.cc +++ b/main/Lighting.cc @@ -4,19 +4,19 @@ #include using namespace std; -Color Lighting::computePhong(const Material & material, +Color Lighting::computePhong(const refptr material, const std::vector & lights, const Ray & viewRay, const Vector & surfacePoint, const Vector & surfaceNormal, const Color & ambientLight) { - Color result = ambientLight * material.getAmbientColor(); + Color result = ambientLight * material->getAmbientColor(); Vector viewDirection = -viewRay.getDirection(); - double shininess = material.getShininess(); - const Color & diffuseColor = material.getDiffuseColor(); - const Color & specularColor = material.getSpecularColor(); + double shininess = material->getShininess(); + const Color & diffuseColor = material->getDiffuseColor(); + const Color & specularColor = material->getSpecularColor(); for (std::vector::const_iterator it = lights.begin(); it != lights.end(); diff --git a/main/Lighting.h b/main/Lighting.h index 7201006..cb66f03 100644 --- a/main/Lighting.h +++ b/main/Lighting.h @@ -6,12 +6,13 @@ #include "Material.h" #include "util/Ray.h" #include "util/Color.h" +#include "util/refptr.h" #include class Lighting { public: - static Color computePhong(const Material & material, + static Color computePhong(const refptr material, const std::vector & lights, const Ray & viewRay, const Vector & surfacePoint, diff --git a/main/Scene.cc b/main/Scene.cc index aef9989..09b43ae 100644 --- a/main/Scene.cc +++ b/main/Scene.cc @@ -176,13 +176,11 @@ Color Scene::traceRay(const Ray & ray) it++) { /* compute the Phong lighting for each hit */ - const Material * material = it->first->getMaterial(); - if (material == NULL) - material = &Material::white; + refptr material = it->first->getMaterial(); Vector surfacePoint = ray[it->second]; - color = Lighting::computePhong(*material, + color = Lighting::computePhong(material, m_lights, ray, surfacePoint, diff --git a/parser/parser.yy b/parser/parser.yy index 6c26db7..1ac8a52 100644 --- a/parser/parser.yy +++ b/parser/parser.yy @@ -191,7 +191,11 @@ shape_items: /* empty */ | material { $$ = $1; } ; -sphere: SPHERE LPAREN number RPAREN LCURLY sphere_items RCURLY +sphere: SPHERE LPAREN number RPAREN LCURLY sphere_items RCURLY { + Sphere * sphere = new Sphere(*($3->the_double)); + $$ = new Node(); + $$->the_Shape = sphere; + } ; sphere_items: shape_items diff --git a/shapes/Shape.cc b/shapes/Shape.cc index 7acce71..d6adadd 100755 --- a/shapes/Shape.cc +++ b/shapes/Shape.cc @@ -4,5 +4,5 @@ Shape::Shape() { m_transparency = 0.0; - m_material = NULL; + m_material = new Material(Material::white); } diff --git a/shapes/Shape.h b/shapes/Shape.h index dfb78d0..d072c03 100644 --- a/shapes/Shape.h +++ b/shapes/Shape.h @@ -7,6 +7,7 @@ #include "util/Vector.h" #include "util/Transform.h" #include "main/Material.h" +#include "util/refptr.h" #include class Shape @@ -28,14 +29,14 @@ class Shape void setTransparency(double t) { m_transparency = t; } double getTransparency() const { return m_transparency; } - void setMaterial(Material * material) { m_material = material; } - Material * getMaterial() const { return m_material; } + void setMaterial(refptr material) { m_material = material; } + refptr getMaterial() const { return m_material; } protected: Transform m_transform; Transform m_inverse; double m_transparency; - Material * m_material; + refptr m_material; }; #include "Sphere.h" diff --git a/test/Makefile b/test/Makefile deleted file mode 100644 index 701e218..0000000 --- a/test/Makefile +++ /dev/null @@ -1,20 +0,0 @@ - -OBJS := $(patsubst %.cc,%.o,$(wildcard *.cc)) - -all: $(OBJS) - -%.o: %.cc - $(CXX) -c -o $@ $(CPPFLAGS) $(CXXFLAGS) $< - -# Make dependency files -%.dep: %.cc - @set -e; rm -f $@; \ - $(CXX) -MM $(CPPFLAGS) $< > $@.$$$$; \ - sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \ - rm -f $@.$$$$ - -clean: - -$(RM) -f *.o *.dep - -# Include dependency files -include $(OBJS:.o=.dep) diff --git a/test/tests.cc b/test/tests.cc deleted file mode 100644 index f2fb312..0000000 --- a/test/tests.cc +++ /dev/null @@ -1,37 +0,0 @@ - -#include -#include -#include "util/Transform.h" -#include "shapes/Sphere.h" -#include "util/refptr.h" -using namespace std; - -class S -{ - public: - S(int val) - { - cout << "S constructor, val " << val << endl; - this->val = val; - } - ~S() { cout << "S destructor, val " << val << endl; } - int val; -}; - -int main() -{ - S * a = new S(42); - S * b = new S(33); - cout << "a: " << a << endl; - cout << "b: " << b << endl; - cout << "Creating arp" << endl; - refptr arp(a); - cout << "Creating brp" << endl; - refptr brp(b); - cout << "Going to assign brp to arp" << endl; - arp = brp; - cout << "Going to assign a new S to arp" << endl; - arp = new S(21); - cout << "ok, exiting" << endl; - return 0; -} diff --git a/util/refptr.h b/util/refptr.h index 6eed839..b53c897 100644 --- a/util/refptr.h +++ b/util/refptr.h @@ -14,8 +14,8 @@ class refptr refptr & operator=(const refptr & orig); refptr & operator=(T * ptr); ~refptr(); - T & operator*(); - T * operator->(); + T & operator*() const; + T * operator->() const; private: void cloneFrom(const refptr & orig); @@ -88,12 +88,12 @@ template void refptr::destroy() } } -template T & refptr::operator*() +template T & refptr::operator*() const { return *m_ptr; } -template T * refptr::operator->() +template T * refptr::operator->() const { return m_ptr; }