Trang

24/12/12

Dùng mouse trong Irrlicht tác động vật thể
- Bây giờ mình đưa lên blog một hướng dẫn dùng mouse để tác động lên các vật trong chương trình Irrlicht.
- Các bạn dùng Irrlicht hãy tự tạo project cho mình và sau đây là tệp tin main.cpp (như các ví dụ của Irrlicht). Các bạn khi dùng nhớ đổi lại đường dẫn các tệp tin hình ảnh cho phù hợp nhé ( trong thư mục media của Irrlicht đó) . Có gì liên hệ với mình.
////////////////////////////////
#include
using namespace irr;
#pragma comment(lib, "Irrlicht.lib")
bool mouseArr[2];
class TMouseClass : public IEventReceiver
{
public:
TMouseClass()
{
for (int i=0;i<2 br="br" i="i" mousearr="mousearr">}
~TMouseClass(){};
virtual bool OnEvent(SEvent event)
{
if (event.EventType==irr::EET_MOUSE_INPUT_EVENT)
{
mouseArr[0]= (event.MouseInput.Event==EMIE_LMOUSE_PRESSED_DOWN) ;
mouseArr[1]= (event.MouseInput.Event==EMIE_RMOUSE_PRESSED_DOWN) ;
return false;
}
return false;
}
};
int main()
{
video::E_DRIVER_TYPE driverType = video::EDT_DIRECT3D9;
TMouseClass receiver;
IrrlichtDevice* device = createDevice(driverType, core::dimension2d(640, 480),32,false,false,false,&receiver);
if (device == 0)
return 1; // could not create selected driver.
/*
First, we add standard stuff to the scene: A nice irrlicht engine
logo, a small help text, a user controlled camera, and we disable
the mouse cursor.
*/
video::IVideoDriver* driver = device->getVideoDriver();
scene::ISceneManager* smgr = device->getSceneManager();
gui::IGUIEnvironment* env = device->getGUIEnvironment();
driver->setTextureCreationFlag(video::ETCF_ALWAYS_32_BIT, true);
// add camera
scene::ICameraSceneNode* camera =
smgr->addCameraSceneNodeMaya();
camera->setPosition(core::vector3df(1900*2,255*2,3700*2));
camera->setTarget(core::vector3df(2397*2,343*2,2700*2));
camera->setFarValue(12000.0f);
// add terrain scene node
scene::ITerrainSceneNode* terrain = smgr->addTerrainSceneNode(
"terrain-heightmap.bmp",
0, // parent node
-1, // node id
core::vector3df(0.f, 0.f, 0.f), // position
core::vector3df(0.f, 0.f, 0.f), // rotation
core::vector3df(40.f, 4.4f, 40.f), // scale
video::SColor ( 255, 255, 255, 255 ), // vertexColor,
5, // maxLOD
scene::ETPS_17, // patchSize
4 // smoothFactor
);

terrain->setMaterialFlag(video::EMF_LIGHTING, false);
terrain->setMaterialTexture(1, driver->getTexture("terrain-texture.jpg"));
terrain->setMaterialTexture(0, driver->getTexture("detailmap3.jpg"));
terrain->setMaterialType(video::EMT_DETAIL_MAP);
terrain->scaleTexture(10.0f, 1.0f);
terrain->setID(1000); //®Æt ID cho terrain
scene::IMetaTriangleSelector* meta = smgr->createMetaTriangleSelector();
// create triangle selector for the terrain
scene::ITriangleSelector* selector
= smgr->createTerrainTriangleSelector(terrain, 0);
terrain->setTriangleSelector(selector);
meta->addTriangleSelector(selector);
selector->drop();
//
// add billboard
scene::IBillboardSceneNode * bill = smgr->addBillboardSceneNode();
bill->setMaterialType(video::EMT_TRANSPARENT_ADD_COLOR );
bill->setMaterialTexture(0, driver->getTexture("particlewhite.bmp"));
bill->setMaterialFlag(video::EMF_LIGHTING, false);
bill->setMaterialFlag(video::EMF_ZBUFFER, false);
bill->setSize(core::dimension2d(100.0f, 100.0f));
bill->setID(2000);
/*
To make the user be able to switch between normal and wireframe mode, we create
an instance of the event reciever from above and let Irrlicht know about it. In
addition, we add the skybox which we already used in lots of Irrlicht examples.
*/
// create skybox
driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, false);
smgr->addSkyBoxSceneNode(
driver->getTexture("irrlicht2_up.jpg"),
driver->getTexture("irrlicht2_dn.jpg"),
driver->getTexture("irrlicht2_lf.jpg"),
driver->getTexture("irrlicht2_rt.jpg"),
driver->getTexture("irrlicht2_ft.jpg"),
driver->getTexture("irrlicht2_bk.jpg"));
driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, true);
/*
That's it, draw everything. Now you know how to use terrain in Irrlicht.
*/
//thªm mé sè h×nh trßn
scene::ISceneNode* node[10];
for (int i=0;i<10 br="br" i="i">{
node[i] = smgr->addSphereSceneNode(100);
node[i]->setPosition(core::vector3df(2397*2-1000+200*i,343*10,2700*2-500+200*i));
node[i]->setMaterialTexture(0,driver->getTexture("water.jpg"));
node[i]->setMaterialFlag(video::EMF_LIGHTING, false);
selector = smgr->createTriangleSelectorFromBoundingBox(node[i]);
meta->addTriangleSelector(selector);
}
selector->drop();
// create collision response animator and attach it to the camera
//lµm cho qu¶ cÇu r¬i
scene::ISceneNodeAnimator* anim;
for (i=0;i<10 br="br" i="i">{
core::aabbox3df box = node[i]->getBoundingBox();
core::vector3df radius = box.MaxEdge;
anim = smgr->createCollisionResponseAnimator(
meta, node[i], radius,
core::vector3df(0,-1.0f,0),
core::vector3df(0,1,0));
node[i]->addAnimator(anim);
//anim->drop();
}
anim = smgr->createCollisionResponseAnimator(
meta, camera, core::vector3df(60,100,60),
core::vector3df(0,-1,0),
core::vector3df(0,50,0));
camera->addAnimator(anim);
anim->drop();
//
scene::ISceneNode* n;
//
int lastFPS = -1;

while(device->run())
if (device->isWindowActive())
{
driver->beginScene(true, true, 0 );
smgr->drawAll();
env->drawAll();
driver->endScene();
core::position2dpos = device->getCursorControl()->getPosition(); //lÊy täa ®é mouse
core::line3df ray = smgr->getSceneCollisionManager()-> //lÊy tia dß tõ camera theo vÞ trÝ mouse
getRayFromScreenCoordinates(pos,camera);
n = smgr->getSceneCollisionManager()->getSceneNodeFromRayBB(ray);
if (n)
{
if ((n->getID()!=1000)&&(n->getID()!=2000))
{
n->setMaterialFlag(video::EMF_LIGHTING, false);
core::vector3df p = n->getPosition();
core::vector3df r = n->getRotation();
r.Y+=50;
n->setRotation(r);
bill->setPosition(p);
if (mouseArr[0])
{
p.Y +=1000;
n->removeAnimators();
n->setPosition(p);
core::aabbox3df box = n->getBoundingBox();
core::vector3df radius = box.MaxEdge;
anim = smgr->createCollisionResponseAnimator(
meta, n, radius,core::vector3df(0,-1.0f,0), core::vector3df(0,50,0));
n->addAnimator(anim);
anim->drop();
mouseArr[0] = false;
}
}
}
// display frames per second in window title
int fps = driver->getFPS();
if (lastFPS != fps)
{
core::stringw str = L"Pick Item - Irrlicht Engine [";
str += driver->getName();
str += "] FPS:";
str += fps;
// Also print terrain height of current camera position
// We can use camera position because terrain is located at coordinate origin
str += " Height: ";
str += terrain->getHeight(camera->getAbsolutePosition().X, camera->getAbsolutePosition().Z);
device->setWindowCaption(str.c_str());
lastFPS = fps;
}
}
device->drop();
return 0;
}
/////////////////////////////////////////////

Không có nhận xét nào:

Đăng nhận xét

Bài đăng phổ biến