00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "sparkmonitor.h"
00023 #include <zeitgeist/logserver/logserver.h>
00024 #include <kerosin/sceneserver/singlematnode.h>
00025 #include <kerosin/materialserver/material.h>
00026 #include <oxygen/monitorserver/monitorcmdparser.h>
00027 #include <oxygen/sceneserver/sceneserver.h>
00028 #include <sstream>
00029
00030 using namespace kerosin;
00031 using namespace oxygen;
00032 using namespace zeitgeist;
00033 using namespace boost;
00034 using namespace salt;
00035 using namespace std;
00036
00037 SparkMonitor::SparkMonitor() : oxygen::MonitorSystem()
00038 {
00039 mFullState = true;
00040 }
00041
00042 SparkMonitor::~SparkMonitor()
00043 {
00044 }
00045
00046 void SparkMonitor::OnLink()
00047 {
00048
00049 mSceneServer = shared_dynamic_cast<SceneServer>
00050 (GetCore()->Get("/sys/server/scene"));
00051
00052 if (mSceneServer.get() == 0)
00053 {
00054 GetLog()->Error()
00055 << "(SparkMonitor) ERROR: SceneServer not found\n";
00056 }
00057 }
00058
00059 void SparkMonitor::OnUnlink()
00060 {
00061 mSceneServer.reset();
00062 mActiveScene.reset();
00063 }
00064
00065 void SparkMonitor::ParseMonitorMessage(const std::string& data)
00066 {
00067
00068 TLeafList items;
00069 ListChildrenSupportingClass<MonitorCmdParser>(items);
00070
00071 for (
00072 TLeafList::iterator iter = items.begin();
00073 iter != items.end();
00074 ++iter
00075 )
00076 {
00077 shared_static_cast<MonitorCmdParser>(*iter)
00078 ->ParseMonitorMessage(data);
00079 }
00080 }
00081
00082 string SparkMonitor::GetMonitorInfo(const PredicateList& pList)
00083 {
00084 stringstream ss;
00085 mFullState = false;
00086 DescribeCustomPredicates(ss,pList);
00087 DescribeActiveScene(ss);
00088 return ss.str();
00089 }
00090
00091 string SparkMonitor::GetMonitorHeaderInfo(const PredicateList& pList)
00092 {
00093 stringstream ss;
00094 mFullState = true;
00095 DescribeCustomPredicates(ss,pList);
00096 DescribeActiveScene(ss);
00097 return ss.str();
00098 }
00099
00100 void SparkMonitor::DescribeCustomPredicates(stringstream& ss,const PredicateList& pList)
00101 {
00102 ss << "(";
00103
00104 for (
00105 PredicateList::TList::const_iterator iter = pList.begin();
00106 iter != pList.end();
00107 ++iter
00108 )
00109 {
00110 const Predicate& pred = (*iter);
00111
00112 ss << "(";
00113 ss << pred.name;
00114
00115 const ParameterList& paramList = pred.parameter;
00116 ParameterList::TVector::const_iterator pIter = paramList.begin();
00117
00118 std::string param;
00119 while (
00120 (pIter != paramList.end()) &&
00121 (paramList.AdvanceValue(pIter, param))
00122 )
00123 {
00124 ss << " ";
00125 ss << param;
00126 }
00127
00128 ss << ")";
00129 }
00130
00131 ss << ")";
00132 }
00133
00134 void SparkMonitor::DescribeLight(stringstream& ss, shared_ptr<Light> light)
00135 {
00136 ss << "(node Light ";
00137
00138 const RGBA& diff = light->GetDiffuse();
00139 ss << "(setDiffuse " << diff.r() << " " << diff.g() << " "
00140 << diff.b() << " " << diff.a() << ") ";
00141
00142 const RGBA& amb = light->GetAmbient();
00143 ss << "(setAmbient " << amb.r() << " " << amb.g() << " "
00144 << amb.b() << " " << amb.a() << ") ";
00145
00146 const RGBA& spec = light->GetSpecular();
00147 ss << "(setSpecular " << spec.r() << " " << spec.g() << " "
00148 << spec.b() << " " << spec.a() << ")";
00149 }
00150
00151 void SparkMonitor::DescribeTransform(stringstream& ss, shared_ptr<Transform> transform)
00152 {
00153 const float precision = 0.005;
00154 const Matrix& mat = transform->GetLocalTransform();
00155
00156 if (mFullState)
00157 {
00158 ss << "(node Transform";
00159 } else
00160 {
00161 ss << "(node";
00162 }
00163
00164
00165
00166 bool update = false;
00167 if (mFullState)
00168 {
00169 update = true;
00170 } else
00171 {
00172 if (SceneServer::GetTransformMark() == transform->GetChangedMark())
00173 {
00174 const salt::Matrix& current = transform->GetLocalTransform();
00175 const salt::Matrix& old = transform->GetOldLocalTransform();
00176 for (int i=0;i<16;++i)
00177 {
00178 const float d = fabs(current.m[i] - old.m[i]);
00179
00180 if (d > precision)
00181 {
00182 update = true;
00183 break;
00184 }
00185 }
00186 }
00187 }
00188
00189
00190 if (update)
00191 {
00192 ss << " (setLocalTransform ";
00193
00194 for (int i=0;i<16;++i)
00195 {
00196 ss << mat.m[i] << " ";
00197 }
00198
00199 ss << ")";
00200 }
00201 }
00202
00203 void SparkMonitor::DescribeMesh(stringstream& ss, boost::shared_ptr<StaticMesh> mesh)
00204 {
00205 shared_ptr<SingleMatNode> singleMat =
00206 shared_dynamic_cast<SingleMatNode>(mesh);
00207
00208 if (singleMat.get() != 0)
00209 {
00210 ss << "(node SingleMatNode";
00211 } else
00212 {
00213 ss << "(node StaticMesh";
00214 }
00215
00216 ss << " (load " << mesh->GetMeshName();
00217
00218 const ParameterList& params = mesh->GetMeshParameter();
00219 for (
00220 ParameterList::TVector::const_iterator iter = params.begin();
00221 iter != params.end();
00222 ++iter
00223 )
00224 {
00225 string str;
00226 params.GetValue(iter,str);
00227 ss << " " << str;
00228 }
00229
00230 ss << ")";
00231
00232 const Vector3f& scale = mesh->GetScale();
00233
00234 ss << " (setScale "
00235 << scale[0] << " "
00236 << scale[1] << " "
00237 << scale[2] << ")";
00238
00239 if (singleMat.get() != 0)
00240 {
00241 shared_ptr<Material> mat = singleMat->GetMaterial();
00242 if (mat.get() != 0)
00243 {
00244 ss << " (setMaterial " << mat->GetName() << ")";
00245 }
00246 }
00247 }
00248
00249 void SparkMonitor::DescribeNode(stringstream& ss, shared_ptr<BaseNode> node)
00250 {
00251
00252 shared_ptr<Transform> transform =
00253 shared_dynamic_cast<Transform>(node);
00254
00255 if (transform.get() != 0)
00256 {
00257 DescribeTransform(ss,transform);
00258 return;
00259 }
00260
00261 if (mFullState)
00262 {
00263
00264 shared_ptr<StaticMesh> mesh =
00265 shared_dynamic_cast<StaticMesh>(node);
00266
00267 if (mesh.get() != 0)
00268 {
00269 DescribeMesh(ss,mesh);
00270 return;
00271 }
00272
00273
00274 shared_ptr<Light> light =
00275 shared_dynamic_cast<Light>(node);
00276
00277 if (light.get() != 0)
00278 {
00279 DescribeLight(ss,light);
00280 return;
00281 }
00282 }
00283
00284 if (mFullState)
00285 {
00286
00287 ss << "(node BaseNode";
00288 } else
00289 {
00290 ss << "(node";
00291 }
00292 }
00293
00294 void SparkMonitor::DescribeActiveScene(stringstream& ss)
00295 {
00296 if (mSceneServer.get() == 0)
00297 {
00298 return;
00299 }
00300
00301 mActiveScene = mSceneServer->GetActiveScene();
00302
00303 if (mActiveScene.get() != 0)
00304 {
00305 if (mActiveScene->GetModified())
00306 {
00307
00308
00309
00310 mFullState = true;
00311 }
00312
00313 if (mFullState)
00314 {
00315 ss << "(RubySceneGraph 0 1)";
00316 } else
00317 {
00318 ss << "(RubyDeltaScene 0 1)";
00319 }
00320
00321 DescribeScene(ss,mActiveScene);
00322 }
00323
00324 ss << endl;
00325 }
00326
00327 void SparkMonitor::DescribeScene(stringstream& ss, shared_ptr<BaseNode> node)
00328 {
00329 DescribeNode(ss, node);
00330
00331 for (TLeafList::iterator i = node->begin(); i!= node->end(); ++i)
00332 {
00333 shared_ptr<BaseNode> baseNode = shared_dynamic_cast<BaseNode>(*i);
00334 if (baseNode.get() == 0)
00335 {
00336 continue;
00337 }
00338
00339 DescribeScene(ss,baseNode);
00340 }
00341
00342 ss << ")";
00343 }
00344