00001 00007 /* --------------------------------------------------------------------------- 00008 * Authors: Tomáš Burian (tburian _AT centrum.cz) 00009 * Thanks to: PCJohn (peciva _AT fit.vutbr.cz) 00010 * Contributors: 00011 * 00012 * THIS SOFTWARE IS NOT COPYRIGHTED 00013 * 00014 * This source code is offered for use in the public domain. 00015 * You may use, modify or distribute it freely. 00016 * 00017 * This source code is distributed in the hope that it will be useful but 00018 * WITHOUT ANY WARRANTY. ALL WARRANTIES, EXPRESS OR IMPLIED ARE HEREBY 00019 * DISCLAIMED. This includes but is not limited to warranties of 00020 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00021 * 00022 * If you find the source code useful, authors will kindly welcome 00023 * if you give them credit and keep their names with their source code, 00024 * but do not feel to be forced to do so. 00025 * --------------------------------------------------------------------------- 00026 */ 00027 00028 //---------------------------------------------------------------------------- 00029 // INCLUDEs 00030 //---------------------------------------------------------------------------- 00031 00032 #include "SoShadowCaster.h" 00033 00034 //---------------------------------------------------------------------------- 00035 // IMPLEMENTATION 00036 //---------------------------------------------------------------------------- 00037 00044 SoShadowCaster::SoShadowCaster(CShadowManager * sm, SoSeparator * or) 00045 : manager(sm), 00046 objectRoot(or) 00047 { 00048 path = NULL; 00049 moveSensor = NULL; 00050 shapeSensor = NULL; 00051 nameIndex = -1; 00052 objectModel = new CObjectModel(); 00053 this->getTransformationMatrix(); 00054 hasMoved = FALSE; 00055 } 00056 00057 00061 SoShadowCaster::~SoShadowCaster() 00062 { 00063 // sensors must be detached !!! 00064 this->moveSensor->detach(); 00065 this->shapeSensor->detach(); 00066 // this->removeAllChildren(); // has no children 00067 } 00068 00069 00073 SoSeparator * SoShadowCaster::getObjectNode() 00074 { 00075 return this->objectRoot; 00076 } 00077 00078 00084 void SoShadowCaster::setCasterName(int objectIndex) 00085 { 00086 SbString index(objectIndex); 00087 SbString name = "Caster_"; 00088 name += index; 00089 ONDEBUG( printf("-- caster name: %s\n",name.getString()) ); 00090 this->nameIndex = objectIndex; 00091 this->setName(name); 00092 } 00093 00094 00100 int SoShadowCaster::getCasterName() 00101 { 00102 return this->nameIndex; 00103 } 00104 00105 00111 void SoShadowCaster::setMoveSensor(SoPathSensor * sensor) 00112 { 00113 this->moveSensor = sensor; 00114 } 00115 00116 00122 SoPathSensor * SoShadowCaster::getMoveSensor() 00123 { 00124 return this->moveSensor; 00125 } 00126 00127 00133 void SoShadowCaster::setShapeSensor(SoNodeSensor * sensor) 00134 { 00135 this->shapeSensor = sensor; 00136 } 00137 00138 00144 SoNodeSensor * SoShadowCaster::getShapeSensor() 00145 { 00146 return this->shapeSensor; 00147 } 00148 00149 00153 void SoShadowCaster::createFaceModel() 00154 { 00155 objectModel->createFaceModel(this->objectRoot); 00156 #if SHOWFACEMODEL 00157 this->showFaceModel(); 00158 #endif 00159 } 00160 00161 00167 void SoShadowCaster::showFaceModel() 00168 { 00169 /* 00170 // delete old one 00171 if ( modelParent != NULL) 00172 this->manager->getSceneRoot()->removeChild(modelParent); 00173 */ 00174 SoTranslation *t1 = new SoTranslation; 00175 t1->translation.setValue(20.f, 0.f, 0.0f); 00176 SoSeparator * modelParent = new SoSeparator; 00177 modelParent->addChild(t1); 00178 00179 // Rotate object 00180 SoRotationXYZ *rX = new SoRotationXYZ; 00181 rX->axis = SoRotationXYZ::X; 00182 rX->angle.setValue(float(3.1415926/2)); 00183 modelParent->addChild(rX); 00184 00185 modelParent->addChild(objectModel->getFaceModelNode()); 00186 this->manager->getSceneRoot()->addChild(modelParent); // adds the model to scene 00187 /* 00188 // to write object model 00189 SoOutput output; 00190 output.openFile("object_model.iv"); 00191 SoWriteAction * writer = new SoWriteAction(&output); 00192 writer->apply(modelParent); 00193 delete writer; 00194 */ 00195 } 00196 00197 00202 bool SoShadowCaster::getTransformationMatrix() 00203 { 00204 // save old matrix 00205 this->trMatrixOld = trMatrix; 00206 00207 SoSearchAction * sa = new SoSearchAction; 00208 sa->setNode(this->objectRoot); 00209 sa->setSearchingAll(FALSE); 00210 sa->apply(this->manager->getSceneRoot()); 00211 00212 SoPath * nodePath = sa->getPath(); 00213 if ( nodePath == NULL ) { 00214 ONDEBUG( printf("MATRIX not FOUND\n") ); 00215 return false; 00216 } 00217 00218 const SbViewportRegion vpr; 00219 SoGetMatrixAction * getmatrixaction = new SoGetMatrixAction(vpr); 00220 getmatrixaction->apply(nodePath); 00221 trMatrix = getmatrixaction->getMatrix(); 00222 ONDEBUG( printf("MATRIX FOUND\n") ); 00223 00224 return true; 00225 } 00226 00227 00231 SbMatrix * SoShadowCaster::getMatrix() 00232 { 00233 return &this->trMatrix; 00234 } 00235 00236 00240 CObjectModel * SoShadowCaster::getObjectModel() 00241 { 00242 return this->objectModel; 00243 } 00244 00245 00251 void SoShadowCaster::setMoved(bool m) 00252 { 00253 this->hasMoved = m; 00254 } 00255 00256 00262 bool SoShadowCaster::moved() 00263 { 00264 return this->hasMoved; 00265 } 00266 00267 00271 void SoShadowCaster::createShadowVolumes() 00272 { 00273 ONDEBUG( printf("--SoShadowCaster::createShadowVolumes()\n") ); 00274 00275 // object has moved, get new transformation m. 00276 this->getTransformationMatrix(); 00277 this->manager->createCasterVolumes(this->nameIndex); 00278 }