00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <stdio.h>
00025 #include "light.hh"
00026
00027 using namespace A3DL;
00028
00029 Light::Light(MetaObject* superobject)
00030 : MetaObject(superobject), accesscontrol(0)
00031 {
00032 }
00033
00034 Light::~Light()
00035 {
00036 }
00037
00038 void Light::setPosition(double x, double y, double z) throw(NoSuchObjectError)
00039 {
00040 char c[256];
00041 sprintf(c, "%.15g %.15g %.15g", x, y, z);
00042
00043 try {
00044 Property::setProperty(*this, "a3dl:position", string(c), "text/x-vector-float", accesscontrol);
00045 } catch(bad_cast e) {
00046 throw NoSuchObjectError(string("Error casting \"position\" object to type \"property\" (") + e.what() + ")");
00047 }
00048 }
00049
00050 void Light::getPosition(double& x, double& y, double& z) throw(NoSuchObjectError)
00051 {
00052 try {
00053 vRef<Property> p = meta_cast<Property*>(&findObject("a3dl:position"));
00054 if(!&p) throw bad_cast();
00055 string n;
00056 p->read(n);
00057 sscanf(n.c_str(), "%lg %lg %lg", &x, &y, &z);
00058 } catch(Vobject::NoSuchObjectError e) {
00059 LOG("light", 2, "Warning: Error finding \"position\" subobject in A3DL::Light::getPosition()! " << e.what());
00060 throw e;
00061 } catch(bad_cast e) {
00062 LOG("light", 2, "Warning: Error casting \"position\" subobject to type property in A3DL::Light::getPosition()! "
00063 << e.what() );
00064 throw NoSuchObjectError(string("Error casting \"position\" object to type \"property\" (") + e.what() + ")");
00065 } catch(exception& e) {
00066 LOG("light", 2, "Warning: Runtime error in A3DL::Light::getPosition()! " << e.what());
00067 }
00068 }
00069
00070 void Light::setRadius(double r) throw(NoSuchObjectError)
00071 {
00072 try {
00073 Property::setProperty(*this, "a3dl:radius", r, accesscontrol);
00074 } catch(bad_cast& e) {
00075 throw NoSuchObjectError(string("Error casting \"radius\" object to type \"property\" (") + e.what() + ")");
00076 }
00077 }
00078
00079 double Light::getRadius() throw(NoSuchObjectError)
00080 {
00081 double ret = 0;
00082 try {
00083 vRef<Property> p = meta_cast<Property*>(&findObject("a3dl:radius"));
00084 if(!&p) throw bad_cast();
00085 string n;
00086 p->read(n);
00087 sscanf(n.c_str(), "%lg", &ret);
00088 } catch(bad_cast& e) {
00089 throw NoSuchObjectError(string("Error casting \"radius\" object to type \"property\" (") + e.what() + ")");
00090 }
00091 return ret;
00092 }
00093
00094 void Light::setColor(double r, double g, double b) throw(NoSuchObjectError)
00095 {
00096 char c[256];
00097 snprintf(c, sizeof(c), "%.15g %.15g %.15g", r, g, b);
00098 try {
00099 Property::setProperty(*this, "a3dl:color", string(c), "text/x-vector-float", accesscontrol);
00100 } catch(bad_cast& e) {
00101 throw NoSuchObjectError(string("Error casting \"color\" object to type \"property\" (") + e.what() + ")");
00102 }
00103 }
00104
00105 void Light::getColor(double& r, double& g, double& b) throw(NoSuchObjectError)
00106 {
00107 try {
00108 vRef<Property> p = meta_cast<Property*>(&findObject("a3dl:color"));
00109 if(!&p) throw bad_cast();
00110 string n;
00111 p->read(n);
00112 sscanf(n.c_str(), "%lg %lg %lg", &r, &g, &b);
00113 } catch(bad_cast& e) {
00114 throw NoSuchObjectError(string("Error casting \"color\" object to type \"property\" (") + e.what() + ")");
00115 }
00116 }
00117
00118 void Light::setStatic(bool s) throw(NoSuchObjectError)
00119 {
00120 string val;
00121 if(s)
00122 val = "yes";
00123 else
00124 val = "no";
00125 try {
00126 Property::setProperty(*this, "a3dl:static", val, "text/x-yes-no", accesscontrol);
00127 } catch(bad_cast& e) {
00128 throw NoSuchObjectError(string("Error casting \"static\" object to type \"property\" (") + e.what() + ")");
00129 }
00130 }
00131
00132 bool Light::getStatic() throw(NoSuchObjectError)
00133 {
00134 bool val;
00135 try {
00136 rREF(Vobject&, v, findObject("a3dl:static"),
00137 Property* p = meta_cast<Property*>(&v);
00138 if(!p) throw bad_cast();
00139 val = ( p->read() == "yes" );
00140 );
00141 } catch(exception& e) {
00142 throw NoSuchObjectError( string("Error getting \"static\" property: ") + e.what() );
00143 }
00144 return val;
00145 }
00146
00147
00148
00149 void Light::initialize(bool is_static, PropertyAccessControl* access) {
00150 if(access == NULL)
00151 accesscontrol = &NoPropertyAccessControl::static_;
00152 else
00153 accesscontrol = access;
00154 setStatic(is_static);
00155 setPosition(0.0, 0.0, 0.0);
00156 setRadius(1.0);
00157 setColor(1.0, 1.0, 1.0);
00158 }
00159
00160 MetaObject* Light::new_Light(MetaObject* superobject, const string& type)
00161 {
00162 return new Light(superobject);
00163 }
00164
00165 const string Light::getType()
00166 {
00167 return "a3dl:light";
00168 }
00169
00170 void Light::registerExtenders()
00171 {
00172 LocalSite::addLocalObjectExtension(typeid(Light).name(), &Light::new_Light);
00173 LocalSite::addLocalObjectExtension("a3dl:light", &Light::new_Light);
00174 RemoteSite::addRemoteObjectExtension(typeid(Light).name(), &Light::new_Light);
00175 RemoteSite::addRemoteObjectExtension("a3dl:light", &Light::new_Light);
00176 }