QT 和Ogre Demo

QT 和Ogre Demo

ogre的编译请参考链接https://blog.csdn.net/DdogYuan/article/details/79559667 或自行解决, 我用的vs2015 ,生成后将dll和头文件加入到QT的工程中即可(注意必须对应)

ogre自带的de'mo

 

 

参考链接:

http://wiki.ogre3d.org/Home

https://ogrecave.github.io/ogre/api/latest/tut__first_scene.html

 

该Demo使用的版本是ogre1.10.12 (32位) ,  Qt5.10.0 MSVC2015  32bit  Release  QT Creator 编译生成的

例程,完整ogre编译和使用步骤,

完整源码工程下载地址

https://download.csdn.net/download/gws09876/12136896

附qt ogreCamera操作类的代码,理解请看代码

#ifndef __SdkQtCameraMan_H__
#define __SdkQtCameraMan_H__

#include "OgreCamera.h"
#include "OgreSceneNode.h"
#include "OgreFrameListener.h"

#include <QKeyEvent>
#include <QMouseEvent>
#include <QDebug>

// enum CameraStyle should be in other namespace than OgreBites::CameraStyle
namespace OgreQtBites
{
    enum CameraStyle   // enumerator values for different styles of camera movement
    {
        CS_FREELOOK,
        CS_ORBIT,
        CS_MANUAL
    };

    /*=============================================================================
    | Utility class for controlling the camera in samples.
    =============================================================================*/
    class SdkQtCameraMan
    {
    public:
        SdkQtCameraMan(Ogre::Camera* cam)
        : mCamera(0)
        , mTarget(0)
        , mOrbiting(false)
        , mZooming(false)
        , mTopSpeed(150)
        , mVelocity(Ogre::Vector3::ZERO)
        , mGoingForward(false)
        , mGoingBack(false)
        , mGoingLeft(false)
        , mGoingRight(false)
        , mGoingUp(false)
        , mGoingDown(false)
        , mFastMove(false)
        {

            setCamera(cam);
            setStyle(CS_FREELOOK);
        }

        virtual ~SdkQtCameraMan() {}

        /*-----------------------------------------------------------------------------
        | Swaps the camera on our camera man for another camera.
        -----------------------------------------------------------------------------*/
        virtual void setCamera(Ogre::Camera* cam)
        {
            mCamera = cam;
        }

        virtual Ogre::Camera* getCamera()
        {
            return mCamera;
        }

        /*-----------------------------------------------------------------------------
        | Sets the target we will revolve around. Only applies for orbit style.
        -----------------------------------------------------------------------------*/
        virtual void setTarget(Ogre::SceneNode* target)
        {
            if (target != mTarget)
            {
                mTarget = target;
                if(target)
                {
                    setYawPitchDist(Ogre::Degree(0), Ogre::Degree(15), 150);
                    mCamera->setAutoTracking(true, mTarget);
                }
                else
                {
                    mCamera->setAutoTracking(false);
                }

            }


        }

        virtual Ogre::SceneNode* getTarget()
        {
            return mTarget;
        }

        /*-----------------------------------------------------------------------------
        | Sets the spatial offset from the target. Only applies for orbit style.
        -----------------------------------------------------------------------------*/
        virtual void setYawPitchDist(Ogre::Radian yaw, Ogre::Radian pitch, Ogre::Real dist)
        {
            mCamera->setPosition(mTarget->_getDerivedPosition());
            mCamera->setOrientation(mTarget->_getDerivedOrientation());
            mCamera->yaw(yaw);
            mCamera->pitch(-pitch);
            mCamera->moveRelative(Ogre::Vector3(0, 0, dist));
        }

        /*-----------------------------------------------------------------------------
        | Sets the camera's top speed. Only applies for free-look style.
        -----------------------------------------------------------------------------*/
        virtual void setTopSpeed(Ogre::Real topSpeed)
        {
            mTopSpeed = topSpeed;
        }

        virtual Ogre::Real getTopSpeed()
        {
            return mTopSpeed;
        }

        /*-----------------------------------------------------------------------------
        | Sets the movement style of our camera man.
        -----------------------------------------------------------------------------*/
        virtual void setStyle(CameraStyle style)
        {
            if (mStyle != CS_ORBIT && style == CS_ORBIT)
            {
                setTarget(mTarget ? mTarget : mCamera->getSceneManager()->getRootSceneNode());
                mCamera->setFixedYawAxis(true);
                manualStop();
                setYawPitchDist(Ogre::Degree(0), Ogre::Degree(15), 150);
            }
            else if (mStyle != CS_FREELOOK && style == CS_FREELOOK)
            {
                mCamera->setAutoTracking(false);
                mCamera->setFixedYawAxis(true);
            }
            else if (mStyle != CS_MANUAL && style == CS_MANUAL)
            {
                mCamera->setAutoTracking(false);
                manualStop();
            }
            mStyle = style;

        }

        virtual CameraStyle getStyle()
        {
            return mStyle;
        }

        /*-----------------------------------------------------------------------------
        | Manually stops the camera when in free-look mode.
        -----------------------------------------------------------------------------*/
        virtual void manualStop()
        {
            if (mStyle == CS_FREELOOK)
            {
                mGoingForward = false;
                mGoingBack = false;
                mGoingLeft = false;
                mGoingRight = false;
                mGoingUp = false;
                mGoingDown = false;
                mVelocity = Ogre::Vector3::ZERO;
            }
        }

        virtual bool frameRenderingQueued(const Ogre::FrameEvent& evt)
        {
            if (mStyle == CS_FREELOOK)
            {
                // build our acceleration vector based on keyboard input composite
                Ogre::Vector3 accel = Ogre::Vector3::ZERO;
                if (mGoingForward) accel += mCamera->getDirection();
                if (mGoingBack) accel -= mCamera->getDirection();
                if (mGoingRight) accel += mCamera->getRight();
                if (mGoingLeft) accel -= mCamera->getRight();
                if (mGoingUp) accel += mCamera->getUp();
                if (mGoingDown) accel -= mCamera->getUp();

                // if accelerating, try to reach top speed in a certain time
                Ogre::Real topSpeed = mFastMove ? mTopSpeed * 20 : mTopSpeed;
                if (accel.squaredLength() != 0)
                {
                    accel.normalise();
                    mVelocity += accel * topSpeed * evt.timeSinceLastFrame * 10;
                }
                // if not accelerating, try to stop in a certain time
                else mVelocity -= mVelocity * evt.timeSinceLastFrame * 10;

                Ogre::Real tooSmall = std::numeric_limits<Ogre::Real>::epsilon();

                // keep camera velocity below top speed and above epsilon
                if (mVelocity.squaredLength() > topSpeed * topSpeed)
                {
                    mVelocity.normalise();
                    mVelocity *= topSpeed;
                }
                else if (mVelocity.squaredLength() < tooSmall * tooSmall)
                    mVelocity = Ogre::Vector3::ZERO;

                if (mVelocity != Ogre::Vector3::ZERO) mCamera->move(mVelocity * evt.timeSinceLastFrame);
            }

            return true;
        }

        /*-----------------------------------------------------------------------------
        | Processes key presses for free-look style movement.
        -----------------------------------------------------------------------------*/
        virtual void injectKeyDown(const QKeyEvent& evt)
        {
            if (mStyle == CS_FREELOOK)
            {
                if (evt.key() == Qt::Key_W || evt.key() == Qt::Key_Up) mGoingForward = true;
                else if (evt.key() == Qt::Key_S || evt.key() == Qt::Key_Down) mGoingBack = true;
                else if (evt.key() == Qt::Key_A || evt.key() == Qt::Key_Left) mGoingLeft = true;
                else if (evt.key() == Qt::Key_D || evt.key() == Qt::Key_Right) mGoingRight = true;
                else if (evt.key() == Qt::Key_PageUp) mGoingUp = true;
                else if (evt.key() == Qt::Key_PageDown) mGoingDown = true;
                else if (evt.key() == Qt::Key_Shift) mFastMove = true;
            }
        }

        /*-----------------------------------------------------------------------------
        | Processes key releases for free-look style movement.
        -----------------------------------------------------------------------------*/
        virtual void injectKeyUp(const QKeyEvent& evt)
        {
            if (mStyle == CS_FREELOOK)
            {
                if (evt.key() == Qt::Key_W || evt.key() == Qt::Key_Up) mGoingForward = false;
                else if (evt.key() == Qt::Key_S || evt.key() == Qt::Key_Down) mGoingBack = false;
                else if (evt.key() == Qt::Key_A || evt.key() == Qt::Key_Left) mGoingLeft = false;
                else if (evt.key() == Qt::Key_D || evt.key() == Qt::Key_Right) mGoingRight = false;
                else if (evt.key() == Qt::Key_PageUp) mGoingUp = false;
                else if (evt.key() == Qt::Key_PageDown) mGoingDown = false;
                else if (evt.key() == Qt::Key_Shift) mFastMove = false;
            }
        }

        /*-----------------------------------------------------------------------------
        | Processes mouse movement differently for each style.
        -----------------------------------------------------------------------------*/
        virtual void injectMouseMove(int relX, int relY)
        {
//            static int lastX = evt.x();
//            static int lastY = evt.y();
//            int relX = evt.x() - lastX;
//            int relY = evt.y() - lastY;
//            lastX = evt.x();
//            lastY = evt.y();
            if (mStyle == CS_ORBIT)
            {
                Ogre::Real dist = (mCamera->getPosition() - mTarget->_getDerivedPosition()).length();

                if (mOrbiting)   // yaw around the target, and pitch locally
                {
                    mCamera->setPosition(mTarget->_getDerivedPosition());

                    mCamera->yaw(Ogre::Degree(-relX * 0.025f));
                    mCamera->pitch(Ogre::Degree(-relY * 0.025f));

                    mCamera->moveRelative(Ogre::Vector3(0, 0, dist));

                    // don't let the camera go over the top or around the bottom of the target
                }
                else if (mZooming)  // move the camera toward or away from the target
                {
                    // the further the camera is, the faster it moves
                    mCamera->moveRelative(Ogre::Vector3(0, 0, relY * 0.004f * dist));
                }
            }
            else if (mStyle == CS_FREELOOK)
            {
                mCamera->yaw(Ogre::Degree(-relX * 0.15f));
                mCamera->pitch(Ogre::Degree(-relY * 0.15f));
            }
        }

        /*-----------------------------------------------------------------------------
        | Processes mouse movement differently for each style.
        -----------------------------------------------------------------------------*/
        virtual void injectWheelMove(const QWheelEvent& evt)
        {
            int relZ = evt.delta();
            if (mStyle == CS_ORBIT)
            {
                Ogre::Real dist = (mCamera->getPosition() - mTarget->_getDerivedPosition()).length();

                if (relZ != 0)  // move the camera toward or away from the target
                {
                    // the further the camera is, the faster it moves
                    mCamera->moveRelative(Ogre::Vector3(0, 0, -relZ * 0.0008f * dist));
                }
            }
        }

        /*-----------------------------------------------------------------------------
        | Processes mouse presses. Only applies for orbit style.
        | Left button is for orbiting, and right button is for zooming.
        -----------------------------------------------------------------------------*/
        virtual void injectMouseDown(const QMouseEvent& evt)
        {
            if (mStyle == CS_ORBIT)
            {
                if (evt.buttons() & Qt::LeftButton) mOrbiting = true;
                else if (evt.buttons() & Qt::RightButton) mZooming = true;
            }
        }

        /*-----------------------------------------------------------------------------
        | Processes mouse releases. Only applies for orbit style.
        | Left button is for orbiting, and right button is for zooming.
        -----------------------------------------------------------------------------*/
        virtual void injectMouseUp(const QMouseEvent& evt)
        {
            if (mStyle == CS_ORBIT)
            {
                if (evt.buttons() & Qt::LeftButton) mOrbiting = false;
                else if (evt.buttons() & Qt::RightButton) mZooming = false;
            }
        }

    protected:

        Ogre::Camera* mCamera;
        CameraStyle mStyle;
        Ogre::SceneNode* mTarget;
        bool mOrbiting;
        bool mZooming;
        Ogre::Real mTopSpeed;
        Ogre::Vector3 mVelocity;
        bool mGoingForward;
        bool mGoingBack;
        bool mGoingLeft;
        bool mGoingRight;
        bool mGoingUp;
        bool mGoingDown;
        bool mFastMove;
    };
}



#endif

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

gws09876

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值