|
源代码:
#include<hgl/win.h>
#include<hgl/FlowObject.H>
#include<hgl/OpenGL.H>
#include"../osg.h"
#include <osg/Config>
#include <osg/ComputeBoundsVisitor>
#include <osg/ShadeModel>
#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>
#include <osgGA/TrackballManipulator>
#include <osgDB/ReadFile>
#include <osgShadow/ShadowedScene>
#include <osgShadow/ShadowMap>
#include <osgUtil/Optimizer>
using namespace hgl;
class TestObject:public FlowObject
{
osg::ref_ptr<osgViewer::Viewer> viewer;
osg::ref_ptr<osgViewer::GraphicsWindow> window;
osg::ref_ptr<osgGA::EventQueue> event_queue;
osg::ref_ptr<osg::Node> model;
osg::ref_ptr<osgShadow::ShadowedScene> shadow_scene;
osg::ref_ptr<osgShadow::ShadowMap> sm;
private:
bool MouseLeftDownProc (int x,int y,uint) { event_queue->mouseButtonPress ( x, y, 1); return(true); }
bool MouseLeftUpProc (int x,int y,uint) { event_queue->mouseButtonRelease ( x, y, 1); return(true); }
bool MouseMidDownProc (int x,int y,uint) { event_queue->mouseButtonPress ( x, y, 2); return(true); }
bool MouseMidUpProc (int x,int y,uint) { event_queue->mouseButtonRelease ( x, y, 2); return(true); }
bool MouseRightDownProc (int x,int y,uint) { event_queue->mouseButtonPress ( x, y, 3); return(true); }
bool MouseRightUpProc (int x,int y,uint) { event_queue->mouseButtonRelease ( x, y, 3); return(true); }
bool MouseMoveProc (int x,int y,uint) { event_queue->mouseMotion ( x, y ); return(true); }
void SetMouse()
{
SetEventCall(OnMouseLeftDown, this,MouseLeftDownProc );
SetEventCall(OnMouseLeftUp, this,MouseLeftUpProc );
SetEventCall(OnMouseMidDown, this,MouseMidDownProc );
SetEventCall(OnMouseMidUp, this,MouseMidUpProc );
SetEventCall(OnMouseRightDown, this,MouseRightDownProc );
SetEventCall(OnMouseRightUp, this,MouseRightUpProc );
SetEventCall(OnMouseMove, this,MouseMoveProc );
}
private:
osg::ref_ptr<osg::Node> CreateLight(osg::ref_ptr<osg::Node> model)
{
osg::ComputeBoundsVisitor cbbv;
model->accept(cbbv);
osg::BoundingBox bb=cbbv.getBoundingBox();
osg::Vec4 lightpos;
float radius=bb.radius()*2.0f;
lightpos.set( bb.xMax()+radius,
bb.yMax()+radius,
bb.zMax()+radius,
1.0f);
osg::ref_ptr<osg:ightSource> ls=new osg::LightSource();
osg::Light *l=ls->getLight();
l->setPosition(lightpos);
l->setAmbient(osg::Vec4(1.0,1.0,1.0,1.0));
l->setDiffuse(osg::Vec4(1.0,1.0,1.0,1.0));
osg::StateSet *state=model->getOrCreateStateSet();
return ls.get();
}
void CreateShadowScene()
{
shadow_scene=new osgShadow::ShadowedScene();
sm=new osgShadow::ShadowMap();
shadow_scene->setShadowTechnique(sm);
shadow_scene->addChild(CreateLight(model.get()));
shadow_scene->addChild(model.get());
}
void CreateViewer()
{
osgUtil::Optimizer opt;
opt.optimize(shadow_scene.get());
viewer=new osgViewer::Viewer;
window = viewer->setUpViewerAsEmbeddedInWindow(0,0,GetScreenWidth(),GetScreenHeight());
event_queue=window->getEventQueue();
viewer->setSceneData(shadow_scene.get());
viewer->setCameraManipulator(new osgGA::TrackballManipulator);
viewer->realize();
}
public:
TestObject(const char *filename)
{
model = osgDB::readNodeFile(filename);
if(model)
{
CreateShadowScene();
CreateViewer();
SetMouse();
}
}
void Draw()
{
if(viewer)
viewer->frame();
}
};
void GameMain(int argc,wchar_t **argv)
{
SystemInitInfo sii;
GameApplication app;
sii.info.ProjectName=L"OpenSceneGraph 查看器";
sii.info.ProjectCode=L"OpenSceneGraph Viewer";
sii.graphics.gl.MultiSample=4;
if(!app.Init(&sii))return;
char filename[1024];
UnicodeToAnsi(filename,1024,argv[1]);
app.SetStart(new TestObject(filename));
app.Run();
}
|
|