ogre射线数组的拣选

这篇博客介绍了如何在Ogre 3D游戏引擎中实现射线拣选功能,通过鼠标点击事件获取场景中的物体。作者展示了如何创建射线查询,设置查询标志来区分不同类型的物体(如机器人和忍者),并根据拣选结果进行物体创建和交互。同时,还提到了使用CEGUI库进行UI交互。
摘要由CSDN通过智能技术生成
ogre射线数组的拣选
先来看代码。
bool TutorialApplication::mousePressed(const OIS::MouseEvent &arg, OIS::MouseButtonID id)
{
	CEGUI::GUIContext& context = CEGUI::System::getSingleton().getDefaultGUIContext();
	context.injectMouseButtonDown(convertButton(id));
	if (id == OIS::MB_Left)
	{
		CEGUI::Vector2f mousePos = context.getMouseCursor().getPosition();
		Ogre::Ray mouseRay =
			mCamera->getCameraToViewportRay(
			mousePos.d_x / float(arg.state.width),
			mousePos.d_y / float(arg.state.height));

		

		mRayScnQuery->setRay(mouseRay);
		mRayScnQuery->setSortByDistance(true);
		mRayScnQuery->setQueryMask(mRobotMode? ROBOT_MASK :NINJA_MASK );

		Ogre::RaySceneQueryResult& result = mRayScnQuery->execute();
		Ogre::RaySceneQueryResult::iterator it = result.begin();

		bool movableFound = false;
		for (;it != result.end();++it)
		{
			movableFound = 
				it->movable &&
				it->movable->getName() != ""&&
				it->movable->getName() != "PlayerCam";

			if (movableFound)
			{
				mCurObject = it->movable->getParentSceneNode();
				if (mCurObject)
					mCurObject->showBoundingBox(true);
				break;
			}
		}
		if(!movableFound)
		{
			Ogre::TerrainGroup::RayResult terrainResult = mTerrainGroup->rayIntersects(mouseRay);
			if (terrainResult.terrain)
			{
				Ogre::Entity* ent = NULL;			
				if(!mRobotMode)
				{
					ent = mSceneMgr->createEntity("ninja.mesh");
					ent->setQueryFlags(NINJA_MASK);
				}
				else
				{
					ent = mSceneMgr->createEntity("robot.mesh");
					ent->setQueryFlags(ROBOT_MASK);
				}

				mCurObject = mSceneMgr->getRootSceneNode()->createChildSceneNode();
				mCurObject->setPosition(terrainResult.position);
				mCurObject->setScale(0.2, 0.2, 0.2);
				mCurObject->attachObject(ent);
			}
		}
		else
		{
			CEGUI::Window* terrainLabel =
				CEGUI::System::getSingleton().getDefaultGUIContext().getRootWindow()->getChild(
				"TerrainLabel");
			std::string name = "robot";
			if(!mRobotMode)
				name = "ninja";

			std::string tips = "pick" + name;
			terrainLabel->setText(tips);
		}
		
	}
	else if (id == OIS::MB_Right)
	{
		mRobotMode = !mRobotMode;
	}

	//关联鼠标
	if(CEGUI::System::getSingleton().getDefaultGUIContext().injectMouseButtonDown(convertButton(id))) return true;
	return BaseApplication::mousePressed(arg,id);
}
在创建物体时,我们可以设置标志码。
enum QueryFlags
	{
		NINJA_MASK = 1 << 0,
		ROBOT_MASK = 1 << 1
	};

mRayScnQuery = mSceneMgr->createRayQuery(Ogre::Ray());
这样我们就可以按标志位来查找物体。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值