|
楼主 |
发表于 2012-10-30 11:12:57
|
显示全部楼层
array 发表于 2012-10-30 10:40
您觉得这么一大堆话和一个能说明问题的完整简单程序+资源,哪个更合适一些。。。
嗯,我确实想用程序表达,可是又不知道如何表达!程序太多了,那我就挑我认为这个问题最简单核心的代码吧!
天空盒以及海洋渲染部分:
osgOcean::ShaderManager::instance().enableShaders(!disableShaders);
m_pOceanSceneNode = new OceanSceneModel(sky_type,windDirection, windSpeed, depth, reflectionDamping, scale, isChoppy, choppyFactor, crestFoamHeight);
m_pOceanSceneNode->getOceanScene()->setOceanSurfaceHeight(fOceanHeight);
m_pOceanSceneNode->getOceanScene()->setNodeMask( m_pOceanSceneNode->getOceanScene()->getNormalSceneMask()
| m_pOceanSceneNode->getOceanScene()->getReflectedSceneMask()
| m_pOceanSceneNode->getOceanScene()->getRefractedSceneMask() );
//初始化天空及天气类型
m_pSkyDome = new SkyDome( 1900.f, 16, 16,sky_type);//osgocean里边的SkyDome函数
m_pSkyDome->setNodeMask( m_pOceanSceneNode->getOceanScene()->getReflectedSceneMask() | m_pOceanSceneNode->getOceanScene()->getNormalSceneMask() );
m_pOceanSceneNode->getScene()->setNodeMask(TRN_OCEAN);
m_pOceanSceneNode->getScene()->setName("oceanF");
m_pOceanSceneNode->getOceanScene()->addChild(m_pSkyDome);
//设置相机属性
m_pskyCamera->addChild(m_pOceanSceneNode->getScene());//m_pskyCamera是单独渲染天空盒与海洋的相机
m_pskyCamera->setRenderOrder(osg::Camera:RE_RENDER);
m_pRoot->addChild(m_pskyCamera);
天空盒代码:
SkyDome::SkyDome( float radius, unsigned int longSteps, unsigned int latSteps,SKY_TYPE _skyType):_sceneType(SCENE_SUNNY)
{
_cubemapDirs.push_back( "sky_SUNNY" );
_cubemapDirs.push_back( "sky_MSUNNY" );
_cubemapDirs.push_back( "sky_CLOUD" );
_cubemapDirs.push_back( "sky_MCLOUD" );
_cubemapDirs.push_back( "sky_FOGGY" );
buildSky(radius,longSteps,latSteps,_skyType);
}
void SkyDome::buildSky(float radius, unsigned int longSteps, unsigned int latSteps,SKY_TYPE _skyType)
{
_sceneType = changeSceneType(_skyType);
_cubemap = loadCubeMapTextures(_cubemapDirs[_sceneType]);
compute( radius, longSteps, latSteps, 90.f, 180.f, 0.f, 360.f );
setupStateSet(_cubemap);
}
osg::ref_ptr<osg::TextureCubeMap> SkyDome::loadCubeMapTextures( const std::string& dir )
{
enum {POS_X, NEG_X, POS_Y, NEG_Y, POS_Z, NEG_Z};
std::string filenames[6];
filenames[POS_X] = "../media/Ocean/textures/" + dir + "/east.png";
filenames[NEG_X] = "../media/Ocean/textures/" + dir + "/west.png";
filenames[POS_Z] = "../media/Ocean/textures/" + dir + "/north.png";
filenames[NEG_Z] = "../media/Ocean/textures/" + dir + "/south.png";
filenames[POS_Y] = "../media/Ocean/textures/" + dir + "/down.png";
filenames[NEG_Y] = "../media/Ocean/textures/" + dir + "/up.png";
osg::ref_ptr<osg::TextureCubeMap> cubeMap = new osg::TextureCubeMap;
cubeMap->setInternalFormat(GL_RGBA);
cubeMap->setFilter( osg::Texture::MIN_FILTER, osg::Texture:INEAR_MIPMAP_LINEAR);
cubeMap->setFilter( osg::Texture::MAG_FILTER, osg::Texture::LINEAR);
cubeMap->setWrap ( osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
cubeMap->setWrap ( osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE);
cubeMap->setImage(osg::TextureCubeMap::NEGATIVE_X, osgDB::readImageFile( filenames[NEG_X] ) );
cubeMap->setImage(osg::TextureCubeMap::POSITIVE_X, osgDB::readImageFile( filenames[POS_X] ) );
cubeMap->setImage(osg::TextureCubeMap::NEGATIVE_Y, osgDB::readImageFile( filenames[NEG_Y] ) );
cubeMap->setImage(osg::TextureCubeMap::POSITIVE_Y, osgDB::readImageFile( filenames[POS_Y] ) );
cubeMap->setImage(osg::TextureCubeMap::NEGATIVE_Z, osgDB::readImageFile( filenames[NEG_Z] ) );
cubeMap->setImage(osg::TextureCubeMap::POSITIVE_Z, osgDB::readImageFile( filenames[POS_Z] ) );
return cubeMap;
}
void SkyDome::setupStateSet( osg::TextureCubeMap* cubemap )
{
osg::StateSet* ss = new osg::StateSet;
ss->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
ss->setTextureAttributeAndModes( 0, cubemap, osg::StateAttribute::ON );
ss->setAttributeAndModes( createShader().get(), osg::StateAttribute::ON );
ss->addUniform( new osg::Uniform("uEnvironmentMap", 0) );
setStateSet(ss);
}
场景模型部分:
m_pCourseNode = osgDB::readNodeFile("../course.ive");//包含了花草树木等大场景模型
m_pRoot->addChild(m_pCourseNode);
主相机部分(main函数里边):
osg::ref_ptr<osgViewer::Viewer> viewer=new osgViewer::Viewer();
viewer->setThreadingModel(osgViewer::Viewer::SingleThreaded);
//场景漫游器
osg::ref_ptr<TravelManipulator> mainCamera =new TravelManipulator(physicsMgr,viewer);//这是一个相机操纵器,class TravelManipulator:public osgGA::MatrixManipulator
viewer->setCameraManipulator(mainCamera);
viewer->getCamera()->setClearColor(osg::Vec4(0.0,0.0,0.0,0.5));
viewer->getCamera()->setClearMask(GL_DEPTH_BUFFER_BIT);
viewer->setSceneData( root );//root跟上边的m_pRoot是一样的,上边m_pRoot是root传递到另外一个类中的表示(说明一下,怕引起误会)
程序很大,别的内容太多,不知道还需要什么信息!实在不好意思!有需要后续可以补上一些可以说明问题的代码!谢谢array大哥!
不知道在解决树叶白边问题上,能不能从天空盒的代码上下功夫,修改天空盒的渲染参数什么的!之前试过setRenderBinDetails()没有什么用! |
|