OGRE最简单的射线拾取详解
2011-03-16 21:21
387 查看
射线拾取原理是从摄像机发射一条经过鼠标位置的射线,计算射线和哪些三角形相交,然后计算这些三角形所属的场景对象(可以是Entity,Light,平面等等)
OGRE已经帮我们实现了基本的功能。
在这里实现的是最简单的鼠标射线拾取。
view plaincopy to clipboardprint?
bool GuiFrameListener::mousePressed( const OIS::MouseEvent &arg, OIS::MouseButtonID id )OGRE
{
// Left mouse button down
if (id == OIS::MB_Left)
{
// Setup the ray scene query, use CEGUI's mouse position
CEGUI::Point mousePos = CEGUI::MouseCursor::getSingleton().getPosition();
Ray mouseRay = mCamera->getCameraToViewportRay(
mousePos.d_x/float(arg.state.width),
mousePos.d_y/float(arg.state.height));
mRaySceneQuery->setRay(mouseRay);
// Execute query
mRaySceneQuery->setSortByDistance(true, 1);
RaySceneQueryResult &result = mRaySceneQuery->execute();
RaySceneQueryResult::iterator itr = result.begin();
// Get results, create a node/entity on the position
if (itr != result.end() && !itr->worldFragment)
{
String strName = itr->movable->getName();
String strTypeName = itr->movable->getMovableType();
SceneNode* pNode = itr->movable->getParentSceneNode();
pNode->scale(1.2, 1.2, 1.2);
}
}
return true;
}
bool GuiFrameListener::mousePressed( const OIS::MouseEvent &arg, OIS::MouseButtonID id )OGRE
{
// Left mouse button down
if (id == OIS::MB_Left)
{
// Setup the ray scene query, use CEGUI's mouse position
CEGUI::Point mousePos = CEGUI::MouseCursor::getSingleton().getPosition();
Ray mouseRay = mCamera->getCameraToViewportRay(
mousePos.d_x/float(arg.state.width),
mousePos.d_y/float(arg.state.height));
mRaySceneQuery->setRay(mouseRay);
// Execute query
mRaySceneQuery->setSortByDistance(true, 1);
RaySceneQueryResult &result = mRaySceneQuery->execute();
RaySceneQueryResult::iterator itr = result.begin();
// Get results, create a node/entity on the position
if (itr != result.end() && !itr->worldFragment)
{
String strName = itr->movable->getName();
String strTypeName = itr->movable->getMovableType();
SceneNode* pNode = itr->movable->getParentSceneNode();
pNode->scale(1.2, 1.2, 1.2);
}
}
return true;
}
这里有几点需要注意。
首先我们用到了OGRE的射线查询类RaySceneQuery(代码中的mRaySceneQuery)。
给这个类传入一个Ray(射线)对象,他在内部实现了射线相交的具体过程。
代码中没有给出的很重要的一步是对该类的初始化,可以写在GuiFrameListener的构造函数中:
mRaySceneQuery = m_pSceneMgr->createRayQuery(Ray());
相对的,在析构函数中要销毁它:
m_pSceneMgr->destroyQuery(mRaySceneQuery);
首先是坐标转换的问题。
OGRE的Camera类提供的函数getCameraToViewportRay可以根据摄像机的位置和一个x,y屏幕坐标建立并返回一条射线。
注意,鼠标点击在屏幕上,CEGUI捕获到的是个相对客户端窗口左上角相对像素的坐标。
然而getCameraToViewportRay需要传入的坐标是基于[0,1]的,所以我们看到代码中对坐标进行了处理。
mRaySceneQuery->setRay(mouseRay);
这一步把摄像机帮我们创建的射线传给了mRaySceneQuery,这样便可以进行查询了。
mRaySceneQuery->setSortByDistance(true, 1);
这一步是要求mRaySceneQuery把查询结果按照从近到远排序,最后保留1个结果。
RaySceneQueryResult &result = mRaySceneQuery->execute();
终于开始查询了。注意,execute返回了一个RaySceneQueryResult 对象。这个对象在Ogre中定义如下:
view plaincopy to clipboardprint?
/** This struct allows a single comparison of result data no matter what the type */
struct _OgreExport RaySceneQueryResultEntry
{
/// Distance along the ray
Real distance;
/// The movable, or NULL if this is not a movable result
MovableObject* movable;
/// The world fragment, or NULL if this is not a fragment result
SceneQuery::WorldFragment* worldFragment;
/// Comparison operator for sorting
bool operator < (const RaySceneQueryResultEntry& rhs) const
{
return this->distance < rhs.distance;
}
};
typedef std::vector<RaySceneQueryResultEntry> RaySceneQueryResult;
/** This struct allows a single comparison of result data no matter what the type */
struct _OgreExport RaySceneQueryResultEntry
{
/// Distance along the ray
Real distance;
/// The movable, or NULL if this is not a movable result
MovableObject* movable;
/// The world fragment, or NULL if this is not a fragment result
SceneQuery::WorldFragment* worldFragment;
/// Comparison operator for sorting
bool operator < (const RaySceneQueryResultEntry& rhs) const
{
return this->distance < rhs.distance;
}
};
typedef std::vector<RaySceneQueryResultEntry> RaySceneQueryResult;
这说明查询的结果可以是多个对象组成的集合。
对于每个对象,要么是MovableObject对象(Entity,Camera等都是其子类),要么是SceneQuery::WorldFragment对象。
这两者必有一个是NULL。
MovableObject类中有个函数setQueryFlags,可以给对象设置掩码。
mRaySceneQuery->setQueryTypeMask可以指定掩码查询物体,本文不作讨论。
这里还有个疑问。得到了如果是MovableObject指针,如何对他进行操作呢?
我们可以通过itr->movable->getName()得到Entity的名字,也可以通过itr->movable->getParentSceneNode();得到改物体所属的场景节点。
理论上说,得到了节点,可以对物体做任何空间移动。得到了Entity,可以对物体做任何修改。
在这里,我把选中的物体放大了1.2倍。现在可以运行程序观看效果了。
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/lsldd/archive/2010/03/28/5424970.aspx
OGRE已经帮我们实现了基本的功能。
在这里实现的是最简单的鼠标射线拾取。
view plaincopy to clipboardprint?
bool GuiFrameListener::mousePressed( const OIS::MouseEvent &arg, OIS::MouseButtonID id )OGRE
{
// Left mouse button down
if (id == OIS::MB_Left)
{
// Setup the ray scene query, use CEGUI's mouse position
CEGUI::Point mousePos = CEGUI::MouseCursor::getSingleton().getPosition();
Ray mouseRay = mCamera->getCameraToViewportRay(
mousePos.d_x/float(arg.state.width),
mousePos.d_y/float(arg.state.height));
mRaySceneQuery->setRay(mouseRay);
// Execute query
mRaySceneQuery->setSortByDistance(true, 1);
RaySceneQueryResult &result = mRaySceneQuery->execute();
RaySceneQueryResult::iterator itr = result.begin();
// Get results, create a node/entity on the position
if (itr != result.end() && !itr->worldFragment)
{
String strName = itr->movable->getName();
String strTypeName = itr->movable->getMovableType();
SceneNode* pNode = itr->movable->getParentSceneNode();
pNode->scale(1.2, 1.2, 1.2);
}
}
return true;
}
bool GuiFrameListener::mousePressed( const OIS::MouseEvent &arg, OIS::MouseButtonID id )OGRE
{
// Left mouse button down
if (id == OIS::MB_Left)
{
// Setup the ray scene query, use CEGUI's mouse position
CEGUI::Point mousePos = CEGUI::MouseCursor::getSingleton().getPosition();
Ray mouseRay = mCamera->getCameraToViewportRay(
mousePos.d_x/float(arg.state.width),
mousePos.d_y/float(arg.state.height));
mRaySceneQuery->setRay(mouseRay);
// Execute query
mRaySceneQuery->setSortByDistance(true, 1);
RaySceneQueryResult &result = mRaySceneQuery->execute();
RaySceneQueryResult::iterator itr = result.begin();
// Get results, create a node/entity on the position
if (itr != result.end() && !itr->worldFragment)
{
String strName = itr->movable->getName();
String strTypeName = itr->movable->getMovableType();
SceneNode* pNode = itr->movable->getParentSceneNode();
pNode->scale(1.2, 1.2, 1.2);
}
}
return true;
}
这里有几点需要注意。
首先我们用到了OGRE的射线查询类RaySceneQuery(代码中的mRaySceneQuery)。
给这个类传入一个Ray(射线)对象,他在内部实现了射线相交的具体过程。
代码中没有给出的很重要的一步是对该类的初始化,可以写在GuiFrameListener的构造函数中:
mRaySceneQuery = m_pSceneMgr->createRayQuery(Ray());
相对的,在析构函数中要销毁它:
m_pSceneMgr->destroyQuery(mRaySceneQuery);
首先是坐标转换的问题。
OGRE的Camera类提供的函数getCameraToViewportRay可以根据摄像机的位置和一个x,y屏幕坐标建立并返回一条射线。
注意,鼠标点击在屏幕上,CEGUI捕获到的是个相对客户端窗口左上角相对像素的坐标。
然而getCameraToViewportRay需要传入的坐标是基于[0,1]的,所以我们看到代码中对坐标进行了处理。
mRaySceneQuery->setRay(mouseRay);
这一步把摄像机帮我们创建的射线传给了mRaySceneQuery,这样便可以进行查询了。
mRaySceneQuery->setSortByDistance(true, 1);
这一步是要求mRaySceneQuery把查询结果按照从近到远排序,最后保留1个结果。
RaySceneQueryResult &result = mRaySceneQuery->execute();
终于开始查询了。注意,execute返回了一个RaySceneQueryResult 对象。这个对象在Ogre中定义如下:
view plaincopy to clipboardprint?
/** This struct allows a single comparison of result data no matter what the type */
struct _OgreExport RaySceneQueryResultEntry
{
/// Distance along the ray
Real distance;
/// The movable, or NULL if this is not a movable result
MovableObject* movable;
/// The world fragment, or NULL if this is not a fragment result
SceneQuery::WorldFragment* worldFragment;
/// Comparison operator for sorting
bool operator < (const RaySceneQueryResultEntry& rhs) const
{
return this->distance < rhs.distance;
}
};
typedef std::vector<RaySceneQueryResultEntry> RaySceneQueryResult;
/** This struct allows a single comparison of result data no matter what the type */
struct _OgreExport RaySceneQueryResultEntry
{
/// Distance along the ray
Real distance;
/// The movable, or NULL if this is not a movable result
MovableObject* movable;
/// The world fragment, or NULL if this is not a fragment result
SceneQuery::WorldFragment* worldFragment;
/// Comparison operator for sorting
bool operator < (const RaySceneQueryResultEntry& rhs) const
{
return this->distance < rhs.distance;
}
};
typedef std::vector<RaySceneQueryResultEntry> RaySceneQueryResult;
这说明查询的结果可以是多个对象组成的集合。
对于每个对象,要么是MovableObject对象(Entity,Camera等都是其子类),要么是SceneQuery::WorldFragment对象。
这两者必有一个是NULL。
MovableObject类中有个函数setQueryFlags,可以给对象设置掩码。
mRaySceneQuery->setQueryTypeMask可以指定掩码查询物体,本文不作讨论。
这里还有个疑问。得到了如果是MovableObject指针,如何对他进行操作呢?
我们可以通过itr->movable->getName()得到Entity的名字,也可以通过itr->movable->getParentSceneNode();得到改物体所属的场景节点。
理论上说,得到了节点,可以对物体做任何空间移动。得到了Entity,可以对物体做任何修改。
在这里,我把选中的物体放大了1.2倍。现在可以运行程序观看效果了。
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/lsldd/archive/2010/03/28/5424970.aspx
相关文章推荐
- OGRE最简单的射线拾取详解
- OGRE最简单的射线拾取详解
- unity3d射线的原理用法以及一个利用射线实现简单拾取的小例子
- 【unity3d游戏开发之基础篇】unity3d射线的原理用法以及一个利用射线实现简单拾取的小例子
- 【unity3d游戏开发之基础篇】unity3d射线的原理用法以及一个利用射线实现简单拾取的小例子
- unity3d射线的原理用法以及一个利用射线实现简单拾取的小例子
- Unity3d射线的原理用法以及一个利用射线实现简单拾取的小例子
- 射线的原理用法以及一个利用射线实现简单拾取的小例子
- unity3d射线的原理用法以及一个利用射线实现简单拾取的小例子
- 【unity3d游戏开发之基础篇】unity3d射线的原理用法以及一个利用射线实现简单拾取的小例子
- 用Ogre实现《天龙八部》场景中水面(TerrainLiquid)详解
- Console命令详解,让调试js代码变得更简单
- ios xmpp客户端简单程序详解
- Unity开发详解之射线运用(4/6)
- android 事件分发机制详解(最简单的解释)
- Android APK反编译就这么简单 详解(附图)
- 3DS Max plugin 编程七,简单了解Ogre的XML格式内容(DTD)
- Android WebView 详解及简单实例
- Unity3D之简单的点击拾取
- Ogre_初学指导_8_"模型动画:简单人物动画"