00001 /*************************************************************************** 00002 *cr 00003 *cr (C) Copyright 1995-2019 The Board of Trustees of the 00004 *cr University of Illinois 00005 *cr All Rights Reserved 00006 *cr 00007 *cr VRJuggler patches contributed by Martijn Kragtwijk: m.kragtwijk@rug.nl 00008 *cr 00009 ***************************************************************************/ 00010 00011 /*************************************************************************** 00012 * RCS INFORMATION: 00013 * 00014 * $RCSfile: VRJugglerApp.C,v $ 00015 * $Author: johns $ $Locker: $ $State: Exp $ 00016 * $Revision: 1.4 $ $Date: 2019年01月17日 21:21:02 $ 00017 * 00018 *************************************************************************** 00019 * DESCRIPTION: 00020 * VRJuggler application initialization code for VMD 00021 ***************************************************************************/ 00022 #include <vrj/vrjConfig.h> 00023 00024 #include <M_VRJapp.h> 00025 #include <VRJugglerScene.h> 00026 #include <VRJugglerRoutines.h> 00027 #include <VMDApp.h> 00028 #include <UIText.h> 00029 #include <Matrix4.h> 00030 //#include <vrj/vrjConfig.h> 00031 00032 #include <iostream> 00033 #include <iomanip> 00034 //#include <GL/gl.h> 00035 00036 #include <vrj/Draw/OGL/GlApp.h> 00037 00038 #include <gadget/Type/PositionInterface.h> 00039 #include <gadget/Type/DigitalInterface.h> 00040 00041 //#include <math.h> 00042 #include <GL/gl.h> 00043 //#include <GL/glu.h> 00044 00045 #include <gmtl/Matrix.h> 00046 #include <gmtl/Generate.h> 00047 #include <gmtl/Vec.h> 00048 00049 #include "Inform.h" 00050 00051 using namespace gmtl; 00052 using namespace vrj; 00053 00054 void M_VRJapp::init() 00055 { 00056 msgInfo << "---------- M_VRJapp::init() ---------------" << sendmsg; 00057 // Initialize devices 00058 mWand.init("VJWand"); 00059 mHead.init("VJHead"); 00060 mButton0.init("VJButton0"); 00061 mButton1.init("VJButton1"); 00062 00063 msgInfo << " initialising shared data " << sendmsg; 00064 vpr::GUID new_guid("44ab594b-1dfb-40c1-8bf3-9af7b7c0ac8a"); 00065 sharedData.init(new_guid); 00066 00067 msgInfo << " initialising shared data done" << sendmsg; 00068 } 00069 00070 void M_VRJapp::contextInit() 00071 { 00072 initGLState(); // Initialize the GL state information. (lights, shading, etc) 00073 } 00074 00075 void M_VRJapp::setScene(VRJugglerScene* s) 00076 { 00077 scene = s; 00078 } 00079 00080 void M_VRJapp::appendCommand(const char* str) 00081 { 00082 //msgInfo << "M_VRJapp::appendCommand(const char* str)" << sendmsg; 00083 cmdQueue.push_back(std::string(str)); 00084 //msgInfo << "queue size now " << (int)cmdQueue.size() << sendmsg; 00085 } 00086 00092 void M_VRJapp::preFrame() 00093 { 00094 00095 // add all commands in our queue to the userData so they can be sync'ed 00096 if (scene){ 00097 if (scene->app){ 00098 if (scene->app->jugglerMode == VRJ_MASTER){ 00099 if(sharedData.isLocal()){ 00100 //msgInfo << "M_VRJapp::preFrame(), I'm local" << sendmsg; 00101 //msgInfo << "queue size now " << (int) cmdQueue.size() << sendmsg; 00102 // TODO: make a mutex around cmdQueue, as the main app can write to it in parallel 00103 // to us reading from it 00104 for (unsigned i = 0; i < cmdQueue.size(); i++){ 00105 if(!strupcmp(cmdQueue[i].c_str(), "tool adddevice vrjugglerbuttons 0")) { 00106 msgInfo << "M_VRJapp::preFrame(), skipping string " << cmdQueue[i].c_str() << sendmsg; 00107 msgInfo << "The slaves should not attach the vrjugglerbuttons to the tool," << sendmsg; 00108 msgInfo << "otherwise translating molecules with the tool is done twice on the slaves:" << sendmsg; 00109 msgInfo << "first from their own calculations and then as a commmand from the master." << sendmsg; 00110 } else { 00111 msgInfo << "M_VRJapp::preFrame(), sending string " << cmdQueue[i].c_str() << sendmsg; 00112 sharedData->commandStrings.push_back(cmdQueue[i]); 00113 } 00114 } 00115 } 00116 } else { 00117 //msgInfo << "M_VRJapp::preFrame(), I'm not the master" << sendmsg; 00118 } 00119 } else { 00120 msgErr << "M_VRJapp::draw(), scene->app NULL" << sendmsg; 00121 } 00122 } else { 00123 msgErr << "M_VRJapp::draw(), scene NULL" << sendmsg; 00124 } 00125 cmdQueue.clear(); 00126 00127 // std::cout << "M_VRJapp::preFrame()" << std::endl; 00128 if (mButton0->getData()) 00129 { 00130 // std::cout << "Button 0 pressed" << std::endl; 00131 } 00132 00133 if (mButton1->getData()) 00134 { 00135 // std::cout << "Button 1 pressed" << std::endl; 00136 } 00137 //scene->app->VMD_VRJ_preframeUpdate(); // do the preframe stuff, don't draw yet 00138 //scene->app->VMDupdate(true); 00139 // std::cout << " M_VRJapp::preFrame() done" << std::endl; 00140 } 00141 00145 void M_VRJapp::latePreFrame() 00146 { 00147 unsigned nrOfCommands = sharedData->commandStrings.size(); 00148 //if (nrOfCommands>0){ 00149 //msgInfo << "latePreFrame(), got " << (long int)nrOfCommands << " commands" << sendmsg; 00150 //} 00151 if(!sharedData.isLocal()){ 00152 //msgInfo << "M_VRJapp::latePreFrame(), I'm a slave" << sendmsg; 00153 for (unsigned i = 0; i< nrOfCommands; i++){ 00154 msgInfo << "latePreFrame(), evaluating " << sharedData->commandStrings[i].c_str() << sendmsg; 00155 scene->app->uiText->get_interp()->evalString(sharedData->commandStrings[i].c_str()); 00156 } 00157 } 00158 } 00159 // Clears the viewport. Put the call to glClear() in this 00160 // method so that this application will work with configurations 00161 // using two or more viewports per display window. 00162 void M_VRJapp::bufferPreDraw() 00163 { 00164 // std::cout << "M_VRJapp::bufferPreDraw()" << std::endl; 00165 glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 00166 glClear(GL_COLOR_BUFFER_BIT); 00167 } 00168 00169 void M_VRJapp::draw() 00170 { 00171 //msgInfo << "M_VRJapp::draw()" << sendmsg; 00172 //msgInfo << "|"; 00173 00174 //msgInfo << "scene->app->jugglerMode" << scene->app->jugglerMode << sendmsg; 00175 //scene->draw(); 00176 if (scene){ 00177 if (scene->app){ 00178 if (scene->app->jugglerMode == VRJ_SLAVE){ 00179 //std::cout << "jugglerMode == VRJ_SLAVE" << std::endl; 00180 vrjuggler_renderer(); 00181 } else { 00182 // in master mode, don't do anything here 00183 //msgInfo << "M_VRJapp::draw() on Master, do nothing" << sendmsg; 00184 } 00185 } else { 00186 msgErr << "M_VRJapp::draw(), scene->app NULL" << sendmsg; 00187 } 00188 } else { 00189 msgErr << "M_VRJapp::draw(), scene NULL" << sendmsg; 00190 } 00191 //std::cout << " M_VRJapp::draw() done" << std::endl; 00192 } 00199 void M_VRJapp::intraFrame() 00200 { 00201 // std::cout << "M_VRJapp::intraFrame()" << std::endl; 00202 } 00203 00210 void M_VRJapp::postFrame() 00211 { 00212 // std::cout << "M_VRJapp::postFrame()" << std::endl; 00213 //scene->app->VMDupdate(true); 00214 if (scene){ 00215 if (scene->app){ 00216 if (scene->app->jugglerMode == VRJ_SLAVE){ 00217 scene->app->VRJ_VMDupdate(true); 00218 } 00219 } else { 00220 msgErr << "M_VRJapp::postFrame(), scene->app NULL" << sendmsg; 00221 } 00222 } else { 00223 msgErr << "M_VRJapp::postFrame(), scene NULL" << sendmsg; 00224 } 00225 } 00226 00227 void M_VRJapp::initGLState() 00228 { 00229 GLfloat light0_ambient[] = { 0.1f, 0.1f, 0.1f, 1.0f}; 00230 GLfloat light0_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f}; 00231 GLfloat light0_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f}; 00232 GLfloat light0_position[] = {0.0f, 0.75f, 0.75f, 0.0f}; 00233 00234 GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f }; 00235 GLfloat mat_diffuse[] = { 1.0f, 0.5f, 0.8f, 1.0f }; 00236 GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0}; 00237 GLfloat mat_shininess[] = { 50.0}; 00238 // GLfloat mat_emission[] = { 1.0, 1.0, 1.0, 1.0}; 00239 GLfloat no_mat[] = { 0.0, 0.0, 0.0, 1.0}; 00240 00241 glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient); 00242 glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse); 00243 glLightfv(GL_LIGHT0, GL_SPECULAR, light0_specular); 00244 glLightfv(GL_LIGHT0, GL_POSITION, light0_position); 00245 00246 glMaterialfv( GL_FRONT, GL_AMBIENT, mat_ambient ); 00247 glMaterialfv( GL_FRONT, GL_DIFFUSE, mat_diffuse ); 00248 glMaterialfv( GL_FRONT, GL_SPECULAR, mat_specular ); 00249 glMaterialfv( GL_FRONT, GL_SHININESS, mat_shininess ); 00250 glMaterialfv( GL_FRONT, GL_EMISSION, no_mat); 00251 00252 glEnable(GL_DEPTH_TEST); 00253 glEnable(GL_NORMALIZE); 00254 glEnable(GL_LIGHTING); 00255 glEnable(GL_LIGHT0); 00256 glEnable(GL_COLOR_MATERIAL); 00257 glShadeModel(GL_SMOOTH); 00258 } 00259 00260 void M_VRJapp::getWandXYZ(float& x, float& y, float& z){ 00261 00262 float units = 1.0; //getDrawScaleFactor(); 00263 gmtl::Matrix44f wand_matrix = mWand->getData(units); 00264 gmtl::Point3f wand_pos = gmtl::makeTrans<gmtl::Point3f>(wand_matrix); 00265 x= wand_pos[0]; 00266 y= wand_pos[1]; 00267 z= wand_pos[2]; 00268 } 00269 00270 // retrieve euler angle rotation of the Wanda 00271 void M_VRJapp::getWandRotMat(Matrix4& rot){ 00272 00273 float units = 1.0; //getDrawScaleFactor(); 00274 gmtl::Matrix44f wand_matrix = mWand->getData(units); 00275 gmtl::Quatf wand_quat = gmtl::make<gmtl::Quatf>(wand_matrix); 00276 gmtl::Matrix44f wand_rot_matrix = gmtl::make<gmtl::Matrix44f>(wand_quat); 00277 // msgInfo << "M_VRJapp::getwandRotMat "; 00278 for (unsigned i = 0; i < 16; i++){ 00279 //rot.mat[i] = wand_rot_matrix[i/4][i%4]; 00280 rot.mat[i] = wand_rot_matrix[i%4][i/4]; 00281 //msgInfo << rot.mat[i] << " "; 00282 } 00283 //msgInfo << sendmsg; 00284 } 00285 00286 // retrieve state of the specified wanda button 00287 bool M_VRJapp::getWandButton(unsigned nr){ 00288 if (nr==0){ 00289 return mButton0->getData(); 00290 } else if (nr==1){ 00291 return mButton1->getData(); 00292 } else { 00293 msgErr << "M_VRJapp::getWandButton: unknown button index " << (int)nr << sendmsg; 00294 } 00295 return false; 00296 }