OGRE最简单的射线拾取详解

射线拾取原理是从摄像机发射一条经过鼠标位置的射线,计算射线和哪些三角形相交,然后计算这些三角形所属的场景对象(可以是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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值