void StandardShadowMap::ViewData::aimShadowCastingCamera( const osg::BoundingSphere &bs, const osg::Light *light, const osg::Vec4 &lightPos, const osg::Vec3 &lightDir, const osg::Vec3 &lightUpVector /* by default = osg::Vec3( 0, 1 0 )*/ ) { osg::Matrixd & view = _camera->getViewMatrix(); osg::Matrixd & projection = _camera->getProjectionMatrix(); osg::Vec3 up = lightUpVector; if( up.length2() <= 0 ) up.set( 0,1,0 ); osg::Vec3d position(lightPos.x(), lightPos.y(), lightPos.z()); if (lightPos[3]==0.0) // infinite directional light { // make an orthographic projection // set the position far away along the light direction position = bs.center() - lightDir * bs.radius() * 2; } float centerDistance = (position-bs.center()).length(); float znear = centerDistance-bs.radius(); float zfar = centerDistance+bs.radius(); float zNearRatio = 0.001f; if (znear<zfar*zNearRatio) znear = zfar*zNearRatio; if ( lightPos[3]!=0.0 ) { // positional light if( light->getSpotCutoff() < 180.0f) // also needs znear zfar estimates { float spotAngle = light->getSpotCutoff(); projection.makePerspective( spotAngle * 2, 1.0, znear, zfar); view.makeLookAt(position,position+lightDir,up); } else { // standard omnidirectional positional light float top = (bs.radius()/centerDistance)*znear; float right = top; projection.makeFrustum(-right,right,-top,top,znear,zfar); view.makeLookAt(position,bs.center(),up ); } } else // directional light { float top = bs.radius(); float right = top; projection.makeOrtho(-right, right, -top, top, znear, zfar); view.makeLookAt(position,bs.center(),up); } }
void CovisePlugin::expandBoundingSphere(osg::BoundingSphere &bsphere) { if (coVRMSController::instance()->isCluster() && coVRDistributionManager::instance().isActive()) { struct BSphere { double x, y, z, radius; }; BSphere b_sphere; b_sphere.x = bsphere.center()[0]; b_sphere.y = bsphere.center()[1]; b_sphere.z = bsphere.center()[2]; b_sphere.radius = bsphere.radius(); if (coVRMSController::instance()->isMaster()) { coVRMSController::SlaveData result(sizeof(b_sphere)); if (coVRMSController::instance()->readSlaves(&result) < 0) { std::cerr << "VRSceneGraph::getBoundingSphere err: sync error"; return; } BSphere bs; for (std::vector<void *>::iterator i = result.data.begin(); i != result.data.end(); ++i) { memcpy(&bs, *i, sizeof(bs)); osg::BoundingSphere otherBs = osg::BoundingSphere(osg::Vec3(bs.x, bs.y, bs.z), bs.radius); bsphere.expandBy(otherBs); } b_sphere.x = bsphere.center()[0]; b_sphere.y = bsphere.center()[1]; b_sphere.z = bsphere.center()[2]; b_sphere.radius = bsphere.radius(); coVRMSController::instance()->sendSlaves(&b_sphere, sizeof(b_sphere)); } else { coVRMSController::instance()->sendMaster(&b_sphere, sizeof(b_sphere)); coVRMSController::instance()->readMaster(&b_sphere, sizeof(b_sphere)); bsphere = osg::BoundingSphere(osg::Vec3(b_sphere.x, b_sphere.y, b_sphere.z), b_sphere.radius); } } }
bool DXFWriterNodeVisitor::writeHeader(const osg::BoundingSphere &bound) { if ( _layers.empty() ) { return false; } _fout << "999\n written by OpenSceneGraph" << std::endl; _fout << "0\nSECTION\n2\nHEADER\n"; _fout << "9\n$ACADVER\n1\nAC1006\n"; // specify minimum autocad version AC1006=R10 _fout << "9\n$EXTMIN\n10\n"<<bound.center().x()-bound.radius()<<"\n20\n"<<bound.center().y()-bound.radius()<<"\n30\n"<<bound.center().z()-bound.radius()<<"\n"; _fout << "9\n$EXTMAX\n10\n"<<bound.center().x()+bound.radius()<<"\n20\n"<<bound.center().y()+bound.radius()<<"\n30\n"<<bound.center().z()+bound.radius()<<"\n"; _fout << "0\nENDSEC\n0\nSECTION\n2\nTABLES\n"; _fout << "0\nTABLE\n2\nLAYER\n"; for (std::vector<Layer>::iterator itr=_layers.begin();itr!=_layers.end();itr++) { if ( itr->_color ) { _fout<<"0\nLAYER\n2\n"<<itr->_name<<"\n70\n0\n62\n"<<itr->_color<<"\n6\nContinuous\n"; // color by layer } else { _fout<<"0\nLAYER\n2\n"<<itr->_name<<"\n70\n0\n62\n255\n6\nContinuous\n"; // most apps won't read 24bit color without a color value in header } } _fout << "0\nENDTAB\n0\nENDSEC\n"; _fout << "0\nSECTION\n2\nENTITIES\n"; _firstPass=false; _count=0; return true; }
void OrbitCameraManipulator::zoomToBoundingSphere( const osg::BoundingSphere& bs, double ratio_w ) { osg::Vec3f bs_center( bs.center() ); double bs_radius = bs.radius(); if( bs_radius <= 0.0 ) { bs_radius = 2.0; bs_center._v[2] = 0.5; } m_rotate_center.set( bs.center() ); osg::Vec3d eye_lookat(m_eye - m_lookat); eye_lookat.normalize(); // define animation path if( m_animation_data ) { osg::Vec3d target_lookat( bs.center() ); double d_eye = bs_radius/sin( osg::DegreesToRadians(m_fovy*0.5) ); osg::Vec3d target_eye( target_lookat + eye_lookat*d_eye ); m_animation_data->m_start_eye.set( m_eye ); m_animation_data->m_start_lookat.set( m_lookat ); m_animation_data->m_start_up.set( m_up ); m_animation_data->m_target_eye.set( target_eye ); m_animation_data->m_target_lookat.set( target_lookat ); m_animation_data->m_target_up.set( m_up ); m_animation_data->_animationTime = 0.6; m_animation_data->start( -1 ); } }
ModelTransformCallback(const osg::BoundingSphere& bs) { _firstTime = 0.0; _period = 4.0f; _range = bs.radius()*0.5f; }
void OsgMainApp::loadModels(){ //LOGI ("OsgMainApp::loadModels 1111"); if(_vModelsToLoad.size()==0) return; //LOGI ("OsgMainApp::loadModels 22222"); osg::notify(osg::ALWAYS)<<"There are "<<_vModelsToLoad.size()<<" models to load"<<std::endl; LOGI ("OsgMainApp::loadModels _vModelsToLoad.size() %d", _vModelsToLoad.size()); Model newModel; for(unsigned int i=0; i<_vModelsToLoad.size(); i++){ newModel = _vModelsToLoad[i]; osg::notify(osg::ALWAYS)<<"Loading: "<<newModel.filename<<std::endl; osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFile(newModel.filename); if (loadedModel == 0) { osg::notify(osg::ALWAYS)<<"Model not loaded"<<std::endl; } else { osg::notify(osg::ALWAYS)<<"Model loaded"<<std::endl; _vModels.push_back(newModel); loadedModel->setName(newModel.name); osg::Uniform* samUniform = new osg::Uniform(osg::Uniform::SAMPLER_2D, "sam"); samUniform->set(0); loadedModel->getOrCreateStateSet()->addUniform(samUniform); // // osg::Uniform* pUnModelViewProjection = new osg::Uniform(osg::Uniform::FLOAT_MAT4, "gl_ModelViewProjectionMatrix"); // loadedModel->getOrCreateStateSet()->addUniform(pUnModelViewProjection); // // osg::Matrixf mModelView; // pUnModelViewProjection->get(mModelView); // // osg::Matrixf mModelViewInverse; // mModelViewInverse.invert(mModelView); // // osg::Uniform* pUnModelViewProjectionInverse = new osg::Uniform(osg::Uniform::FLOAT_MAT4, "uModelViewMatrixInverse"); // loadedModel->getOrCreateStateSet()->addUniform(pUnModelViewProjectionInverse); long iFileLen = 0; char* pszVertexShader = GetFileBuffer("/sdcard/osg/a.vs", iFileLen); char* pszFragmentShader = GetFileBuffer("/sdcard/osg/a.fs", iFileLen); osg::Shader * vshader = new osg::Shader(osg::Shader::VERTEX, pszVertexShader ); osg::Shader * fshader = new osg::Shader(osg::Shader::FRAGMENT, pszFragmentShader ); osg::Program * prog = new osg::Program; prog->addShader ( vshader ); prog->addShader ( fshader ); loadedModel->getOrCreateStateSet()->setAttribute ( prog ); delete [] pszVertexShader; delete [] pszFragmentShader; // osg::ref_ptr<osg::Image> image = osgDB::readImageFile("/sdcard/Present3D/sunset.bmp"); // //// osg::ref_ptr<osg::Image> image = new osg::Image; //// image->allocateImage(256, 256, 4, GL_RGBA, GL_UNSIGNED_BYTE); //// image->setInternalTextureFormat(GL_RGBA); // // if (image.get()) // { // //// unsigned char * pData = image->data(); //// memset(pData, 255, 256 * 256 * 4); // // osg::ref_ptr<osg::Texture2D> texture=new osg::Texture2D(); // texture->setDataVariance(osg::Object::DYNAMIC); // texture->setImage(image.get()); // // //设置自动生成纹理坐标 // osg::ref_ptr<osg::TexGen> texgen=new osg::TexGen(); // texgen->setMode(osg::TexGen::SPHERE_MAP); // // //设置纹理环境,模式为BLEND // osg::ref_ptr<osg::TexEnv> texenv=new osg::TexEnv; // texenv->setMode(osg::TexEnv::ADD); // texenv->setColor(osg::Vec4(0.6,0.6,0.6,0.0)); // // //启动单元一自动生成纹理坐标,并使用纹理 // osg::ref_ptr<osg::StateSet> state=new osg::StateSet; // state->setTextureAttributeAndModes(1,texture.get(),osg::StateAttribute::ON); // state->setTextureAttributeAndModes(1,texgen.get(),osg::StateAttribute::ON); // state->setTextureAttribute(1,texenv.get()); // // loadedModel->setStateSet(state.get()); // // LOGE("osgDB::readImageFile ok"); // } // else // { // LOGE("osgDB::readImageFile err bmp 3"); // } // osg::ref_ptr<osg::Node> theNode = createNode(); _trans =new osg::MatrixTransform; _trans->setMatrix(osg::Matrix::rotate(0,1,0,0)); _trans->addChild(loadedModel); // _trans->addChild(theNode); // 取模型的包围盒 const osg::BoundingSphere bs = _trans->getBound(); float radius = bs.radius(); _xFactor = radius / 100; // _root->addChild(loadedModel); _root->addChild(_trans); } } osgViewer::Viewer::Windows windows; _viewer->getWindows(windows); for(osgViewer::Viewer::Windows::iterator itr = windows.begin();itr != windows.end();++itr) { (*itr)->getState()->setUseModelViewAndProjectionUniforms(true); (*itr)->getState()->setUseVertexAttributeAliasing(true); } _viewer->setSceneData(NULL); _viewer->setSceneData(_root.get()); _manipulator->getNode(); _viewer->home(); _viewer->getDatabasePager()->clear(); _viewer->getDatabasePager()->registerPagedLODs(_root.get()); _viewer->getDatabasePager()->setUpThreads(3, 1); _viewer->getDatabasePager()->setTargetMaximumNumberOfPageLOD(2); _viewer->getDatabasePager()->setUnrefImageDataAfterApplyPolicy(true, true); _vModelsToLoad.clear(); }