|
楼主 |
发表于 2012-10-30 17:41:32
|
显示全部楼层
array 发表于 2012-10-30 10:37
您就这么一说,是提供不了什么有用的信息的
代码在下方,具体遇到以下问题,恳请老师批评指正:
1)绘制方法思路是否正确?
2) 相机更新函数是否正确?
3)现在,单独添加speedtree节点,可以显示森林(但角度不正确),但是地形、草地不显示(控制台提示"CTerrainRI:Render,BindVertexBuffer(),failed",
"CGeometryBufferRI::BindVertexBuffer,the Vertexbuffer has not been set up ,yet","CGeometryBufferRI::OverWriteVertices,cannot be called
until a VB is established with AppendVertices()+EndVertices").同时鼠标拖动屏幕远离树木后会花屏,屏蔽掉地形、草地、天空后不会出现花屏
4)添加上osg节点后只能看到osg模型,看不到speedtree森林模型。
使用的speedtree SDK5.1 (32位),osg3.0.032位)
//全局变量
static CApplication* g_pApplication = NULL;
static osgViewer::Viewer viewer;
//1. 自定义Drawable类
class SpeedTreeDrawable:public osg:rawable
{
public:
SpeedTreeDrawable()
{
g_pApplication = st_new(CApplication, "CApplication");
//为方便跟踪调试,使用了以下固定参数
st_int32 argc = 3;
char* argv[] = {"E:\\我的日常工作\\练习\\speed\\SpeedTree\\Debug\\SpeedTree_OpenGL_App_MT_Static_d.exe","-cmdline_file","E:\\我的日常工作\\练习\\speed\\Forests\\Huangshan\\__Settings__.txt"};
if (argc > 0)
SetCurrentDirectoryA(CFixedString(argv[0]).Path( ).c_str( ));
g_pApplication->Init(argc, argv); //CApplication类里面,其实就是获取外部配置参数
setUseDisplayList(false); //关闭
}
SpeedTreeDrawable(const SpeedTreeDrawable& speedtreedrawable,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY):
osg::Drawable(speedtreedrawable,copyop) {}
META_Object(mySpeedTreeDrawableApp,SpeedTreeDrawable)
//bool g_bFirst = true;
//重写drawImplementation函数
virtual void drawImplementation(osg::RenderInfo& renderInfo)const
{
//设置状态参数
static st_bool bFirstDisplay = true;
if (bFirstDisplay) //第一次显示
{
//初始化speedtree
InitOpenGL( ); //必须加,否则不识别dds等纹理
assert(g_pApplication);
g_pApplication->InitGraphics( ); //CApplication类里面,初始化场景等并生成森林
bFirstDisplay = false;
return; //必须加,否则不显示
}
g_pApplication->Render( ); //CApplication类里面,渲染森林、地形、草地、天空等
}
protected:
virtual ~SpeedTreeDrawable() {}
};
//2.自定义更新回调
class SpeedTreeUpdateCallback:public osg::Drawable::UpdateCallback
{
public:
SpeedTreeUpdateCallback(){};
//重载update函数
virtual void update(osg::NodeVisitor*, osg::Drawable*)
{
if (g_pApplication->ReadyToRender())
{
//更新相机
ChangeSpeedTreeCamera(); //自定义更新相机函数
//更新视图
g_pApplication->Advance(); //CApplication类里面,更新全局风等(屏蔽其UpdateView(更新相机))
g_pApplication->Cull(); //CApplication类里面,各种裁剪
//(注:本来Cull()应放在自定义的裁剪回调类里面的,但是程序进入不了裁剪回调函数中就放在此处了)
}
}
};
//3.更新相机
void ChangeSpeedTreeCamera()
{
//获取osg视图投影矩阵
osg::Matrixd vprojectMatrix;
vprojectMatrix = viewer.getCamera()->getProjectionMatrix();
//获取视点矩阵
osg::Matrixd vViewerMatrix;
vViewerMatrix = viewer.getCamera()->getViewMatrix();
//获取视点位置
osg::Vec3 camePos;
osg::Vec3 center;
osg::Vec3 up;
float dis = 1.0f;
viewer.getCamera()->getViewMatrixAsLookAt(camePos,center,up,dis);
//逐参数赋值给SpeedTree中的相应变量
SpeedTree::Vec3 spcamePos(camePos.x(),camePos.y(),camePos.z());
const st_float32 afInit1[16]={vprojectMatrix(0,0),vprojectMatrix(0,1),vprojectMatrix(0,2),vprojectMatrix(0,3)
,vprojectMatrix(1,0),vprojectMatrix(1,1),vprojectMatrix(1,2),vprojectMatrix(1,3)
,vprojectMatrix(2,0),vprojectMatrix(2,1),vprojectMatrix(2,2),vprojectMatrix(2,3)
,vprojectMatrix(3,0),vprojectMatrix(3,1),vprojectMatrix(3,2),vprojectMatrix(3,3)};
SpeedTree::Mat4x4 spvprojectMatrix(afInit1);
const st_float32 afInit2[16]={vViewerMatrix(0,0),vViewerMatrix(0,1),vViewerMatrix(0,2),vViewerMatrix(0,3)
,vViewerMatrix(1,0),vViewerMatrix(1,1),vViewerMatrix(1,2),vViewerMatrix(1,3)
,vViewerMatrix(2,0),vViewerMatrix(2,1),vViewerMatrix(2,2),vViewerMatrix(2,3)
,vViewerMatrix(3,0),vViewerMatrix(3,1),vViewerMatrix(3,2),vViewerMatrix(3,3)};
SpeedTree::Mat4x4 spvViewerMatrix(afInit2);
//因为是外部更改参数,将CApplication中以下参数设为public
st_bool bViewChanged = g_pApplication->m_cView.Set(spcamePos,spvprojectMatrix,spvViewerMatrix,g_pApplication->GetUserSettings().m_fNearClip,g_pApplication->GetUserSettings().m_fVisibility);
g_pApplication->m_bCameraChanged |= bViewChanged;
g_pApplication->m_pCurrentNavigation->SetCameraPos(CCoordSys::ConvertFromStd(spcamePos));
}
//4.场景加载speedtree节点
main
{
//定义speedtree节点
osg::ref_ptr<osg::Geode> geode = new osg::Geode;
SpeedTreeDrawable* drable = new SpeedTreeDrawable();
drable->setUpdateCallback(new SpeedTreeUpdateCallback);
geode->addDrawable(drable);
//定义osg样例节点
osg::ref_ptr<osg::Node> node2 = osgDB::readNodeFile("cow.osg");
osg::ref_ptr<osg::Group> root = new osg::Group;
root->addChild(geode.get());
//root->addChild(node2.get());
viewer.addEventHandler(new osgViewer::StatsHandler());
osgUtil::Optimizer optimizer;
optimizer.optimize(root.get());
viewer.setSceneData(root.get());
viewer.run();
} |
|