转载请说明出处
http://blog.csdn.net/zhuyingqingfen/article/details/8096345
/*
本例子主要说明了:
1.怎样使用dtABC::Application这个类和Delta3D的效果管理
2.怎样获得鼠标事件,怎样注册一个新的函数来处理鼠标
3.怎样处理键盘事件
4.点击屏幕可以产生爆炸效果
4.具体里面的原理可以参看一些我的其他的Delta3D目录下的文章
*/
#include<dtABC/application.h>
#include<dtCore/effectmanager.h>
#include<dtCore/keyboard.h>
#include<dtCore/object.h>
#include<dtCore/scene.h>
#include<dtCore/particlesystem.h>
#include<dtCore/transform.h>
#include<dtCore/mouse.h>
#include<dtCore/camera.h>
#include<dtCore/system.h>
#include<dtUtil/datapathutils.h>
#include<iostream>
//爆炸和烟的类型
const std::string kHighExplosiveDetonation="HightExplosiveDetonation";
const std::string kSmokeDetonation="SmokeDetonation";
class MouseHandler:public dtCore::Base//主要负责鼠标的控制
{
public:
MouseHandler(dtCore::Scene*scene):_scene(scene) {
init();
}
void init()
{
_effectManger=new dtCore::EffectManager;
_effectManger->AddDetonationTypeMapping(kHighExplosiveDetonation, "effects/explosion.osg");
_effectManger->AddDetonationTypeMapping(kSmokeDetonation, "effects/smoke.osg");
_scene->AddDrawable(_effectManger.get());
}
bool Pressed(const dtCore::Mouse *mouse, dtCore::Mouse::MouseButton button)
{//鼠标按下,会触发此函数
float x,y;
mouse->GetPosition(x,y);
detonation(osg::Vec3(x*36,50,y*36));
return true;
}
void detonation(osg::Vec3 location)
{
_effectManger->AddDetonation(location,kHighExplosiveDetonation);
}
private:
dtCore::RefPtr<dtCore::EffectManager>_effectManger;
dtCore::RefPtr<dtCore::Scene>_scene;
};
class Updater:public dtCore::Base//负责飞机飞行的更新
{
public:
Updater(dtCore::Scene *scene)
:Base("Updater")
,_scene(scene)
,_angle(0)
{
AddSender(&dtCore::System::GetInstance());
init();
}
void init()
{
_entity=new dtCore::Object("UH-1N");
_entity->LoadFile("models/uh-1n.ive");
_scene->AddDrawable(_entity.get());
_smoke=new dtCore::ParticleSystem;
_smoke->LoadFile("effects/smoke.osg");
_entity->AddChild(_smoke.get());
}
virtual void OnMessage(MessageData *data)
{
if(data->message==dtCore::System::MESSAGE_PRE_FRAME)
{
round();
}
}
void round()
{
_angle = _angle + 1;
if (_angle > 360)
{
_angle -= 360.0f;
}
_position.SetTranslation(30 * cosf(osg::DegreesToRadians(_angle)),
100 + 30 * sinf(osg::DegreesToRadians(_angle)),
0 );
_position.SetRotation(_angle, 0, -45.0);
_entity->SetTransform(_position);
}
private:
dtCore::RefPtr<dtCore::Scene>_scene;
dtCore::RefPtr<dtCore::Object>_entity;
dtCore::RefPtr<dtCore::ParticleSystem>_smoke;
dtCore::Transform _position;
float _angle;
};
class TestEffectApp:public dtABC::Application
{
public:
TestEffectApp(const std::string &configfilename="config.xml")
:Application(configfilename)
{
}
/*application中的AddSender在基类BaseABC中调用了
System* sys = &dtCore::System::GetInstance();
assert(sys);
AddSender(sys);*/
virtual void OnMessage(MessageData* data)
{
//要在dtABC中重写OnMessage函数,必须先执行基类的这个函数,因为像 EventTraversal,PreFrame,Frame,PostFrame,Pause
//等函数都是在基类中被调用的
BaseABC::OnMessage(data);
if(GetKeyboard()->GetKeyState(osgGA::GUIEventAdapter::KEY_Escape))
{
dtCore::System::GetInstance().Stop();
}
}
virtual void Config()
{
dtABC::Application::Config();
dtCore::Transform position(0,-50,-0,0);//相机向外退50个单位
GetCamera()->SetTransform(position);
dtCore::RefPtr<dtCore::Object> terrain = new dtCore::Object("Terrain");
terrain->LoadFile("models/terrain_simple.ive");
AddDrawable(terrain.get());
//调整地形位置,向下20,向左20,向里100个单位
osg::Vec3 terrain_position(-20, 100.0f, -20.0f);
dtCore::Transform trans;
trans.SetTranslation(terrain_position);
terrain->SetTransform(trans);
_update=new Updater(GetScene());//负责一个飞机的旋转
MouseHandler *mh = new MouseHandler(GetScene());//负责鼠标的控制,当鼠标点击的时候产生一个爆炸效果
//下面这句就是怎样写一个捕获鼠标的时间方法
GetMouseListener()->SetPressedCallback(dtCore::GenericMouseListener::ButtonCallbackType(mh, &MouseHandler::Pressed));
}
private:
dtCore::RefPtr<Updater> _update;
};
int main()
{
std::string dataPath = dtUtil::GetDeltaDataPathList();
dtUtil::SetDataFilePathList(dataPath + ";" +
dtUtil::GetDeltaRootPath() + "/examples/data" + ";");
dtCore::RefPtr<TestEffectApp>app=new TestEffectApp("config.xml");
app->Config();
app->Run();
return 0;
}
![](https://img-my.csdn.net/uploads/201210/21/1350829217_6038.jpg)