OGER SDK研究之六 Demo_Terrain 地形跟随

长平狐 发布于 2013/03/19 18:40
阅读 31
收藏 0

 Terrain.h

/*
-----------------------------------------------------------------------------
This source file is part of OGRE
    (Object-oriented Graphics Rendering Engine)
For the latest info, see http://www.ogre3d.org/

Copyright (c) 2000-2006 Torus Knot Software Ltd
Also see acknowledgements in Readme.html

You may use this sample code for anything you like, it is not covered by the
LGPL like the rest of the engine.

-----------------------------------------------------------------------------
*/

/**
    /file
        Terrain.h
    /brief
        Specialisation of OGRE's framework application to show the
        terrain rendering plugin
   载入地形场景,并加入随地形高度跟随的摄像机进行场景漫游
*/

#include "ExampleApplication.h"
//射线碰撞查询器
RaySceneQuery* raySceneQuery = 0;

// Event handler to add ability to alter curvature
class TerrainFrameListener : public ExampleFrameListener
{
public:
    TerrainFrameListener(RenderWindow* win, Camera* cam)
        : ExampleFrameListener(win, cam)
    {
        // Reduce move speed
        mMoveSpeed = 50;

    }

    bool frameStarted(const FrameEvent& evt)
    {
        if( ExampleFrameListener::frameStarted(evt) == false )
  return false;

        // clamp to terrain
        //定义一个射线变量
  static Ray updateRay;
  //设置原点为摄像机的位置
        updateRay.setOrigin(mCamera->getPosition());
  //设置方向为Y轴反方向
        updateRay.setDirection(Vector3::NEGATIVE_UNIT_Y);
  //碰撞查询器设置射线
        raySceneQuery->setRay(updateRay);
  //执行碰撞查询
        RaySceneQueryResult& qryResult = raySceneQuery->execute();
  //遍历查询结果
        RaySceneQueryResult::iterator i = qryResult.begin();
        if (i != qryResult.end() && i->worldFragment)
        {
   //将摄像机设置成地表的位置偏移10
            mCamera->setPosition(mCamera->getPosition().x,
                i->worldFragment->singleIntersection.y + 10,
                mCamera->getPosition().z);
        }

        return true;

    }

};

 

class TerrainApplication : public ExampleApplication
{
public:
    TerrainApplication() {}

    ~TerrainApplication()
    {
  //删除射线碰撞查询器
        delete raySceneQuery;
    }

protected:


    virtual void chooseSceneManager(void)
    {
        // Get the SceneManager, in this case a generic one
  // 创建场景管理器
        mSceneMgr = mRoot->createSceneManager("TerrainSceneManager");
    }

    virtual void createCamera(void)
    {
        // Create the camera
  // 创建摄像机
        mCamera = mSceneMgr->createCamera("PlayerCam");

        // Position it at 500 in Z direction
  // 设置摄像机的位置
        mCamera->setPosition(Vector3(128,25,128));
        // Look back along -Z
  // 摄像机的目标点
        mCamera->lookAt(Vector3(0,0,-300));
  // 设置摄像机切距
        mCamera->setNearClipDistance( 1 );
  // 设置摄像机远距
        mCamera->setFarClipDistance( 1000 );

    }
  
    // Just override the mandatory create scene method
    void createScene(void)
    {
  //表面
        Plane waterPlane;

        // Set ambient light
  // 设置场景的环境光
        mSceneMgr->setAmbientLight(ColourValue(0.5, 0.5, 0.5));

        // Create a light
  // 创建灯光
        Light* l = mSceneMgr->createLight("MainLight");
        // Accept default settings: point light, white diffuse, just set position
        // NB I could attach the light to a SceneNode if I wanted it to move automatically with
        //  other objects, but I don't
  // 设置灯光的位置
        l->setPosition(20,80,50);

        // Fog
        // NB it's VERY important to set this before calling setWorldGeometry
        // because the vertex program picked will be different
  // 色彩值
        ColourValue fadeColour(0.93, 0.86, 0.76);
  // 设置雾
        mSceneMgr->setFog( FOG_LINEAR, fadeColour, .001, 500, 1000);
  //设置视口的背景色
        mWindow->getViewport(0)->setBackgroundColour(fadeColour);
  //字符串
        std::string terrain_cfg("terrain.cfg");
  //设置世界几何体
        mSceneMgr -> setWorldGeometry( terrain_cfg );
        // Infinite far plane?
  // 是否支持无限远的面投影
        if (mRoot->getRenderSystem()->getCapabilities()->hasCapability(RSC_INFINITE_FAR_PLANE))
        {
            mCamera->setFarClipDistance(0);
        }

        // Define the required skyplane
  // 定义一个必需的天空面
        Plane plane;
  //高度
        // 5000 world units from the camera
        plane.d = 5000;
        // Above the camera, facing down
  // 法向量为Y轴反向
        plane.normal = -Vector3::UNIT_Y;

        // Set a nice viewpoint
  // 设置摄像机的位置
        mCamera->setPosition(707,2500,528);
  //设置摄像机的方向
        mCamera->setOrientation(Quaternion(-0.3486, 0.0122, 0.9365, 0.0329));
        //mRoot -> showDebugOverlay( true );
  //创建射线碰撞查询器,由摄像机位置向Y轴负向发射线进行查询
        raySceneQuery = mSceneMgr->createRayQuery(
            Ray(mCamera->getPosition(), Vector3::NEGATIVE_UNIT_Y));


    }
    // Create new frame listener
    void createFrameListener(void)
    {
  //创建帧监听
        mFrameListener= new TerrainFrameListener(mWindow, mCamera);
        mRoot->addFrameListener(mFrameListener);
    }

};

Terrain.cpp:

/*
-----------------------------------------------------------------------------
This source file is part of OGRE
    (Object-oriented Graphics Rendering Engine)
For the latest info, see http://www.ogre3d.org/

Copyright (c) 2000-2006 Torus Knot Software Ltd
Also see acknowledgements in Readme.html

You may use this sample code for anything you like, it is not covered by the
LGPL like the rest of the engine.
-----------------------------------------------------------------------------
*/

/**
    @file
        Terrain.cpp
    @brief
        Shows OGRE's terrain rendering plugin.
*/

#include "Terrain.h"

#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
#define WIN32_LEAN_AND_MEAN
#include "windows.h"
#endif

#ifdef __cplusplus
extern "C" {
#endif

#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
#else
int main(int argc, char *argv[])
#endif
{
    // Create application object
    TerrainApplication app;

    try {
        app.go();
    } catch( Ogre::Exception& e ) {
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
        MessageBox( NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
#else
        std::cerr << "An exception has occured: " <<
            e.getFullDescription().c_str() << std::endl;
#endif
    }

    return 0;
}

#ifdef __cplusplus
}
#endif


原文链接:http://blog.csdn.net/honghaier/article/details/3188807
加载中
返回顶部
顶部