| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- #include "anki/resource/LightRsrc.h"
- #include "anki/resource/Texture.h"
- #include "anki/misc/PropertyTree.h"
- #include "anki/core/Logger.h"
- #include "anki/core/Globals.h"
- #include <cstring>
- #include <string>
- #include <boost/property_tree/ptree.hpp>
- #include <boost/property_tree/xml_parser.hpp>
- #include <boost/foreach.hpp>
- #include <boost/lexical_cast.hpp>
- namespace anki {
- //==============================================================================
- LightRsrc::LightRsrc()
- {
- diffuseCol = Vec3(0.5);
- specularCol = Vec3(0.5);
- castsShadowFlag = false;
- radius = 1.0;
- distance = 3.0;
- fovX = fovY = Math::PI / 4.0;
- width = height = 1.0;
- spotLightCameraType = SLCT_PERSPECTIVE;
- }
- //==============================================================================
- void LightRsrc::load(const char* filename)
- {
- try
- {
- using namespace boost::property_tree;
- ptree rpt;
- read_xml(filename, rpt);
- const ptree& pt = rpt.get_child("light");
- //
- // type
- //
- std::string type_ = pt.get<std::string>("type");
- if(type_ == "POINT")
- {
- type = LT_POINT;
- }
- else if(type_ == "SPOT")
- {
- type = LT_SPOT;
- }
- else
- {
- throw ANKI_EXCEPTION("Incorrect type: " + type_);
- }
- //
- // diffuseCol
- //
- boost::optional<const ptree&> diffColTree =
- pt.get_child_optional("diffuseCol");
- if(diffColTree)
- {
- diffuseCol = PropertyTree::getVec3(diffColTree.get());
- }
- //
- // specularCol
- //
- boost::optional<const ptree&> specColTree =
- pt.get_child_optional("specularCol");
- if(specColTree)
- {
- specularCol = PropertyTree::getVec3(specColTree.get());
- }
- //
- // castsShadow
- //
- boost::optional<bool> castsShadow_ =
- PropertyTree::getBoolOptional(pt, "castsShadow");
- if(castsShadow_)
- {
- castsShadowFlag = castsShadow_.get();
- }
- //
- // radius
- //
- boost::optional<float> radius_ = pt.get_optional<float>("radius");
- if(radius_)
- {
- radius = radius_.get();
- if(type == LT_SPOT)
- {
- ANKI_WARNING("File \"" << filename <<
- "\": No radius for spot lights");
- }
- }
- //
- // distance
- //
- boost::optional<float> distance_ = pt.get_optional<float>("distance");
- if(distance_)
- {
- distance = distance_.get();
- if(type == LT_POINT)
- {
- ANKI_WARNING("File \"" << filename <<
- "\": No distance for point lights");
- }
- }
- //
- // fovX
- //
- boost::optional<float> fovX_ = pt.get_optional<float>("fovX");
- if(fovX_)
- {
- fovX = fovX_.get();
- if(type == LT_POINT)
- {
- ANKI_WARNING("File \"" << filename <<
- "\": No fovX for point lights");
- }
- }
- //
- // fovY
- //
- boost::optional<float> fovY_ = pt.get_optional<float>("fovY");
- if(fovY_)
- {
- fovY = fovY_.get();
- if(type == LT_POINT)
- {
- ANKI_WARNING("File \"" << filename <<
- "\": No fovY for point lights");
- }
- }
- //
- // width
- //
- boost::optional<float> width_ = pt.get_optional<float>("width");
- if(width_)
- {
- width = width_.get();
- if(type == LT_POINT)
- {
- ANKI_WARNING("File \"" << filename <<
- "\": No width for point lights");
- }
- }
- //
- // height
- //
- boost::optional<float> height_ = pt.get_optional<float>("height");
- if(height_)
- {
- height = height_.get();
- if(type == LT_POINT)
- {
- ANKI_WARNING("File \"" << filename <<
- "\": No height for point lights");
- }
- }
- //
- // texture
- //
- boost::optional<std::string> tex =
- pt.get_optional<std::string>("texture");
- if(tex)
- {
- texture.load(tex.get().c_str());
- if(type == LT_POINT)
- {
- ANKI_WARNING("File \"" << filename <<
- "\": No texture for point lights");
- }
- }
- //
- // cameraType
- //
- boost::optional<std::string> cam =
- pt.get_optional<std::string>("cameraType");
- if(cam)
- {
- if(cam.get() == "PERSPECTIVE")
- {
- spotLightCameraType = SLCT_PERSPECTIVE;
- }
- else if(cam.get() == "ORTHOGRAPHIC")
- {
- spotLightCameraType = SLCT_ORTHOGRAPHIC;
- }
- else
- {
- throw ANKI_EXCEPTION("Incorrect cameraYype: " + cam.get());
- }
- }
- }
- catch(std::exception& e)
- {
- throw ANKI_EXCEPTION_R("Material \"" + filename + "\": ", e);
- }
- }
- } // end namespace
|