00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #ifndef _SCENE_H_
00014 #define _SCENE_H_
00015
00016 #include <vector>
00017 #include <map>
00018 #include <stdexcept>
00019
00020 #include <libxml/xmlmemory.h>
00021 #include <libxml/parser.h>
00022
00023 #include "General/Debug.hh"
00024 #include "Render/Ray.hh"
00025
00026 #include "World/Color.hh"
00027 #include "World/Texture.hh"
00028 #include "World/Material.hh"
00029
00030 #include "World/Object.hh"
00031 #include "World/Plane.hh"
00032 #include "World/Sphere.hh"
00033
00034 #include "World/Light.hh"
00035 #include "World/Camera.hh"
00036
00037
00046 namespace World {
00048 static const Double NearestCollision(0.001);
00049
00060 class Scene {
00062 std::vector<Material *> Materials;
00063
00065 std::vector<Texture *> Textures;
00066
00070 std::vector<Object *> Objects;
00071
00074 std::vector<Light *> Lights;
00075
00077 Color Background;
00078
00080 Double AtmosphereIdx;
00081
00083 Camera C;
00084
00085
00089 void CreateLibrary();
00090
00092 void DumpLibrary();
00093
00095 Color ParseColor(xmlNodePtr Node);
00097 Math::Vector ParseVector(xmlNodePtr Node);
00099 Double ParseIdx(xmlNodePtr Node);
00100
00102 const Material *GetMaterial(const std::string &id);
00104 const Texture *GetTexture(const std::string &id);
00105
00107 void ParseTexture(xmlNodePtr Node);
00109 void ParseMaterial(xmlNodePtr Node);
00111 void ParseLight(xmlNodePtr Node);
00112
00114 void ParseCamera(xmlNodePtr Node);
00115
00117 void ParseSphere(xmlNodePtr Node);
00119 void ParsePlane(xmlNodePtr Node);
00123 std::map<std::string, Color> ColMap;
00124 std::map<std::string, const Texture *> TexMap;
00125 std::map<std::string, const Material *> MatMap;
00126 std::map<std::string, const Double> IdxMap;
00127
00128 typedef std::map<std::string, Color>::iterator ColIter;
00129 typedef std::map<std::string, const Texture *>::iterator TexIter;
00130 typedef std::map<std::string, const Material *>::iterator MatIter;
00131 typedef std::map<std::string, const Double>::iterator IdxIter;
00134 public:
00136 Scene(const Camera &C = Camera(),
00137 const Color &Background = ColLib::Black(),
00138 const Double AtmosphereIdx = MatLib::IdxAir)
00139 : Background(Background),
00140 AtmosphereIdx(AtmosphereIdx),
00141 C(C) {
00142 Materials.reserve(20);
00143 Objects.reserve(20);
00144 Lights.reserve(20);
00145 Textures.reserve(20);
00146 }
00147
00149 ~Scene();
00150
00152 void Purge();
00153
00156 inline void AddObject(Object *O) {
00157 if (DEBUG && O == NULL)
00158 throw std::invalid_argument
00159 ("Argument can't be a NULL pointer");
00160 Objects.push_back(O);
00161 }
00162
00165 inline void AddLight(Light *L) {
00166 if (DEBUG && L == NULL)
00167 throw std::invalid_argument
00168 ("Argument can't be a NULL pointer");
00169 Lights.push_back(L);
00170 }
00171
00174 inline void AddMaterial(Material *M) {
00175 if (DEBUG && M == NULL)
00176 throw std::invalid_argument
00177 ("Argument can't be a NULL pointer");
00178 Materials.push_back(M);
00179 }
00180
00183 inline void AddTexture(Texture *T) {
00184 if (DEBUG && T == NULL)
00185 throw std::invalid_argument
00186 ("Argument can't be a NULL pointer");
00187 Textures.push_back(T);
00188 }
00189
00194 Bool Collide(const Render::Ray &R, Double &RayPos, const Object* &O) const;
00195
00197 Bool ParseFile(const std::string &File);
00198
00200 inline const Color &GetBackground() const {
00201 return this->Background;
00202 }
00203
00205 inline Double GetAtmosphere() const {
00206 return this->AtmosphereIdx;
00207 }
00208
00210 inline const Camera &GetCamera() const {
00211 return this->C;
00212 }
00213
00215 template<typename T>
00216 class Iterator {
00217 protected:
00219 typename std::vector<T *>::const_iterator Cur;
00221 typename std::vector<T *>::const_iterator End;
00222
00223 public:
00225 Iterator(const Scene &S);
00226
00228 inline const T *Next() {
00229 if (Cur == End)
00230 return NULL;
00231 else return *(Cur++);
00232 }
00233
00234 friend class Scene;
00235 };
00236
00238 typedef Iterator<Light> LightIterator;
00239 typedef Iterator<Object> ObjectIterator;
00240 typedef Iterator<Texture> TextureIterator;
00241 typedef Iterator<Material> MaterialIterator;
00243 };
00244
00245 };
00246
00247 #endif