PfBasicSimulator.cpp

Go to the documentation of this file.
00001 /*************** <auto-copyright.pl BEGIN do not edit this line> **************
00002  *
00003  * VR Juggler is (C) Copyright 1998-2005 by Iowa State University
00004  *
00005  * Original Authors:
00006  *   Allen Bierbaum, Christopher Just,
00007  *   Patrick Hartling, Kevin Meinert,
00008  *   Carolina Cruz-Neira, Albert Baker
00009  *
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Library General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2 of the License, or (at your option) any later version.
00014  *
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Library General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU Library General Public
00021  * License along with this library; if not, write to the
00022  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00023  * Boston, MA 02111-1307, USA.
00024  *
00025  * -----------------------------------------------------------------
00026  * File:          $RCSfile$
00027  * Date modified: $Date: 2006-12-09 16:10:39 -0600 (Sat, 09 Dec 2006) $
00028  * Version:       $Revision: 19606 $
00029  * -----------------------------------------------------------------
00030  *
00031  *************** <auto-copyright.pl END do not edit this line> ***************/
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    //setDrawWandFunctor(new GlDrawConeWandFunctor());
00080    //setDrawWandFunctor(new GlDrawRightAngleWandFunctor());
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);      // Configure the wand to use
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    // Get drawing parameters
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;     // NOTE: Eye coord system is -z forward, x-right, y-up
00138 
00139    // -- Calculate camera (eye) Positions -- //
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    // Compute location of left and right eyes
00147    float interocular_dist = mSimViewport->getUser()->getInterocularDistance();
00148    interocular_dist *= positionScale;               // Scale into correct units
00149    float eye_offset = interocular_dist / 2.0f;      // Distance to move eye
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    //vprASSERT(Element->getID() == std::string("apiPerformer"));
00174 
00175    vprDEBUG(vrjDBG_DRAW_MGR,vprDBG_CONFIG_LVL) << "PfBasicSimulator::configPerformerAPI:"
00176                                             << " Configuring Performer\n" << vprDEBUG_FLUSH;
00177 
00178    // --- Get simulator model info --- //
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());     // Load head model
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());     // Load wand model
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    //mRootWithSim = new pfScene;
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);      // Put sim stuff in the graph
00263    }
00264 }
00265 
00266 void PfBasicSimulator::updateSimulatorSceneGraph()
00267 {
00268       gmtl::Matrix44f vj_head_mat = getHeadPos();          // Get Juggler matrices
00269       gmtl::Matrix44f vj_wand_mat = getWandPos();
00270       pfMatrix head_mat = GetPfMatrix(vj_head_mat);    // Convert to Performer
00271       pfMatrix wand_mat = GetPfMatrix(vj_wand_mat);
00272       mHeadDCS->setMat(head_mat);                        // Set the DCS nodes
00273       mWandDCS->setMat(wand_mat);
00274 }
00275 
00276 }

Generated on Thu Jan 4 10:56:51 2007 for VR Juggler by  doxygen 1.5.1