00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include <vrj/Draw/Pf/Config.h>
00034
00035 #include <gmtl/Matrix.h>
00036 #include <gmtl/Generate.h>
00037 #include <gmtl/MatrixOps.h>
00038 #include <gmtl/Vec.h>
00039 #include <gmtl/VecOps.h>
00040 #include <gmtl/Xforms.h>
00041 #include <gmtl/Output.h>
00042
00043 #include <Performer/pf.h>
00044 #include <Performer/pfdu.h>
00045 #include <Performer/pf/pfDCS.h>
00046 #include <Performer/pf/pfScene.h>
00047 #include <Performer/pf/pfChannel.h>
00048 #include <Performer/pf/pfLightSource.h>
00049 #include <Performer/pfdu.h>
00050 #include <Performer/pf/pfTraverser.h>
00051
00052 #include <vpr/Util/FileUtils.h>
00053 #include <jccl/Config/ConfigElement.h>
00054
00055 #include <vrj/Kernel/User.h>
00056
00057 #include <vrj/Draw/Pf/PfDrawManager.h>
00058
00059 #include <vrj/Display/DisplayManager.h>
00060 #include <vrj/Display/Projection.h>
00061 #include <vrj/Display/Viewport.h>
00062 #include <vrj/Display/SimViewport.h>
00063 #include <vrj/Display/SurfaceViewport.h>
00064
00065 #include <vrj/Draw/Pf/PfSimInterfaceFactory.h>
00066 #include <vrj/Draw/Pf/PfBasicSimulator.h>
00067
00068 #include <boost/concept_check.hpp>
00069
00070
00071 namespace vrj
00072 {
00073
00074 VRJ_REGISTER_PF_SIM_INTERFACE_CREATOR(PfBasicSimulator);
00075
00076 PfBasicSimulator::PfBasicSimulator() : mRootWithSim(NULL),
00077 mSimTree(NULL), mHeadDCS(NULL), mWandDCS(NULL)
00078 {
00079
00080
00081 }
00082
00088 bool PfBasicSimulator::config(jccl::ConfigElementPtr element)
00089 {
00090 vprASSERT(element.get() != NULL);
00091 vprASSERT(element->getID() == std::string("default_simulator"));
00092
00093 std::string camera_proxy_str = element->getProperty<std::string>("camera_pos");
00094 std::string wand_proxy_str = element->getProperty<std::string>("wand_pos");
00095
00096 mCamera.init(camera_proxy_str);
00097 mWand.init(wand_proxy_str);
00098
00099 if(!mCamera.isConnected())
00100 {
00101 vprDEBUG(vprDBG_ERROR, vprDBG_CRITICAL_LVL)
00102 << clrOutNORM(clrRED,"ERROR:")
00103 << "PfBasicSimulator:: Fatal Error: Camera not found named: "
00104 << camera_proxy_str.c_str() << vprDEBUG_FLUSH;
00105 vprASSERT(false);
00106 }
00107
00108
00109 mDrawProjections = element->getProperty<bool>("draw_projections");
00110 mSurfaceColor[0] = element->getProperty<float>("surface_color", 0);
00111 mSurfaceColor[1] = element->getProperty<float>("surface_color", 1);
00112 mSurfaceColor[2] = element->getProperty<float>("surface_color", 2);
00113
00114 configPerformerAPI(element);
00115
00116 return true;
00117 }
00118
00123 void PfBasicSimulator::setKeyboardMouse(gadget::KeyboardMouseInterface kmInterface)
00124 {
00125 boost::ignore_unused_variable_warning(kmInterface);
00126 }
00127
00128
00129 void PfBasicSimulator::updateProjectionData(const float positionScale,
00130 Projection* leftProj, Projection* rightProj)
00131 {
00132 updateInternalData(positionScale);
00133
00134 gmtl::Matrix44f camera_pos = getCameraPos();
00135 gmtl::Vec3f camera_trans = gmtl::makeTrans<gmtl::Vec3f>(camera_pos);
00136
00137 gmtl::Matrix44f left_eye_pos, right_eye_pos;
00138
00139
00140 vprDEBUG(vprDBG_ALL, vprDBG_HEX_LVL)
00141 << "[vrj::PfBasicSimulator::updateProjectionData()] Getting cam position"
00142 << std::endl << vprDEBUG_FLUSH;
00143 vprDEBUG(vprDBG_ALL, vprDBG_HEX_LVL)
00144 << "CamPos:" << camera_trans << std::endl << vprDEBUG_FLUSH;
00145
00146
00147 float interocular_dist = mSimViewport->getUser()->getInterocularDistance();
00148 interocular_dist *= positionScale;
00149 float eye_offset = interocular_dist / 2.0f;
00150
00151 left_eye_pos = camera_pos * gmtl::makeTrans<gmtl::Matrix44f>( gmtl::Vec3f(-eye_offset, 0.0f, 0.0f) );
00152 right_eye_pos = camera_pos * gmtl::makeTrans<gmtl::Matrix44f>( gmtl::Vec3f(eye_offset, 0.0f, 0.0f) );
00153
00154 leftProj->calcViewMatrix(left_eye_pos, positionScale);
00155 rightProj->calcViewMatrix(right_eye_pos, positionScale);
00156 }
00157
00159 void PfBasicSimulator::updateInternalData(float positionScale)
00160 {
00161 mHeadPos = mSimViewport->getUser()->getHeadPosProxy()->getData(positionScale);
00162 mWandPos = mWand->getData(positionScale);
00163
00164 mCameraPos = mCamera->getData(positionScale);
00165 gmtl::invert(mCameraPos);
00166 }
00167
00168
00169
00170
00171 bool PfBasicSimulator::configPerformerAPI(jccl::ConfigElementPtr element)
00172 {
00173
00174
00175 vprDEBUG(vrjDBG_DRAW_MGR,vprDBG_CONFIG_LVL) << "PfBasicSimulator::configPerformerAPI:"
00176 << " Configuring Performer\n" << vprDEBUG_FLUSH;
00177
00178
00179 std::string head_file = element->getProperty<std::string>("head_model");
00180 std::string wand_file = element->getProperty<std::string>("wand_model");
00181 if(head_file.empty())
00182 {
00183 vprDEBUG(vprDBG_ALL,vprDBG_CONFIG_LVL)
00184 << "WARNING: PfBasicSimulator::config: simHeadModel not set."
00185 << std::endl << vprDEBUG_FLUSH;
00186 }
00187 if(wand_file.empty())
00188 {
00189 vprDEBUG(vprDBG_ALL,vprDBG_CONFIG_LVL)
00190 << "WARNING: PfBasicSimulator::config: simWandModel not set."
00191 << std::endl << vprDEBUG_FLUSH;
00192 }
00193
00194 mHeadModel = vpr::replaceEnvVars(head_file);
00195 mWandModel = vpr::replaceEnvVars(wand_file);
00196
00197 vprDEBUG(vrjDBG_DRAW_MGR,vprDBG_CONFIG_LVL)
00198 << "Head Model: " << mHeadModel.c_str() << std::endl
00199 << "Wand Model: " << mWandModel.c_str() << std::endl << vprDEBUG_FLUSH;
00200
00201 mRootWithSim = PfDrawManager::instance()->getRootWithSim();
00202 if (NULL != mRootWithSim)
00203 {
00204 initSimulatorGraph();
00205 return true;
00206 }
00207 return false;
00208 }
00209
00210 void PfBasicSimulator::initSimulatorGraph()
00211 {
00212 pfNode* head_node(NULL);
00213 pfNode* wand_node(NULL);
00214
00215 if(!mHeadModel.empty())
00216 {
00217 head_node = pfdLoadFile(mHeadModel.c_str());
00218 vprDEBUG(vrjDBG_DRAW_MGR,vprDBG_CONFIG_LVL)
00219 << "[vrj::PfBasicSimulator::initSimulatorGraph()] Loaded head model: "
00220 << mHeadModel.c_str() << std::endl << vprDEBUG_FLUSH;
00221 }
00222 else
00223 {
00224 vprDEBUG(vrjDBG_DRAW_MGR,vprDBG_CONFIG_LVL)
00225 << "[vrj::PfBasicSimulator::initSimulatorGraph()] "
00226 << "No wand head specified.\n" << vprDEBUG_FLUSH;
00227 }
00228
00229 if(!mWandModel.empty())
00230 {
00231 wand_node = pfdLoadFile(mWandModel.c_str());
00232 vprDEBUG(vrjDBG_DRAW_MGR,vprDBG_CONFIG_LVL)
00233 << "[vrj::PfBasicSImulator::initSimulatorGraph()] "
00234 << "Loaded wand model: " << mWandModel << std::endl << vprDEBUG_FLUSH;
00235 }
00236 else
00237 {
00238 vprDEBUG(vrjDBG_DRAW_MGR,vprDBG_CONFIG_LVL)
00239 << "[vrj::PfDrawManager::initSimulatorGraph()] "
00240 << "No wand model specified.\n" << vprDEBUG_FLUSH;
00241 }
00242
00243 mSimTree = new pfGroup;
00244
00245
00246
00247 mHeadDCS = new pfDCS;
00248 mWandDCS = new pfDCS;
00249 mSimTree->addChild(mHeadDCS);
00250 mSimTree->addChild(mWandDCS);
00251 if(NULL != head_node)
00252 {
00253 mHeadDCS->addChild(head_node);
00254 }
00255 if(NULL != wand_node)
00256 {
00257 mWandDCS->addChild(wand_node);
00258 }
00259
00260 if((head_node != NULL) && (wand_node != NULL))
00261 {
00262 mRootWithSim->addChild(mSimTree);
00263 }
00264 }
00265
00266 void PfBasicSimulator::updateSimulatorSceneGraph()
00267 {
00268 gmtl::Matrix44f vj_head_mat = getHeadPos();
00269 gmtl::Matrix44f vj_wand_mat = getWandPos();
00270 pfMatrix head_mat = GetPfMatrix(vj_head_mat);
00271 pfMatrix wand_mat = GetPfMatrix(vj_wand_mat);
00272 mHeadDCS->setMat(head_mat);
00273 mWandDCS->setMat(wand_mat);
00274 }
00275
00276 }