PLSM2+Hydrax0.5.1+Ogre1.6.3

HydraxManage.h


 

#pragma once

#include "Ogre.h"
using namespace  Ogre;

class Input;

class HydraxManageListener:public Ogre::FrameListener
{
public:
 HydraxManageListener(Input* pInput,SceneManager* pSceneMgr);
 ~HydraxManageListener();
  virtual bool frameStarted(const FrameEvent& evt);

  virtual bool frameEnded(const FrameEvent& evt);

  void changeSkyBox();

private:
  Input*      mInput;
   Real mKeyBuffer;
   SceneManager* mSceneMgr;

};

class HydraxManage
{
public:
 HydraxManage(Root *pRoot,SceneManager* pSceneMgr,Camera* pCamera,RenderWindow* pWindow,Input* pInput);
 ~HydraxManage(void);

 void CreateHydraxSence();

protected:
 Root *mRoot;
    Camera* mCamera;
 RenderWindow* mWindow;
 SceneManager* mSceneMgr;
 Input*      mInput;
 
};

 

HydraxManage.cpp


 

#include "HydraxManage.h"
#include "Input.h"

// ----------------------------------------------------------------------------
// Include the Hydrax plugin headers
// Main base headers (Hydrax.h) and especific headers (Noise/Water modules)
// ----------------------------------------------------------------------------
#include "Hydrax.h"
#include "Noise/Perlin/Perlin.h"
#include "Modules/ProjectedGrid/ProjectedGrid.h"

#define _def_SkyBoxNum 3

// Hydrax pointer
Hydrax::Hydrax *mHydrax = 0;

Ogre::String mSkyBoxes[_def_SkyBoxNum] =
{"Sky/ClubTropicana",
"Sky/EarlyMorning",
"Sky/Clouds"};

Ogre::Vector3 mSunPosition[_def_SkyBoxNum] =
{Ogre::Vector3(0,10000,0),
Ogre::Vector3(0,10000,90000),
Ogre::Vector3(0,10000,0)};

Ogre::Vector3 mSunColor[_def_SkyBoxNum] =
{Ogre::Vector3(1, 0.9, 0.6),
Ogre::Vector3(1,0.6,0.4),
Ogre::Vector3(0.45,0.45,0.45)};

int mCurrentSkyBox = 0;

 

HydraxManageListener::HydraxManageListener(Input* pInput,SceneManager* pSceneMgr)
{
    mInput=pInput;
  mKeyBuffer=-1;
 mSceneMgr=pSceneMgr;
}

HydraxManageListener::~HydraxManageListener()
{

}
void HydraxManageListener::changeSkyBox()
{
 // Change skybox
 mSceneMgr->setSkyBox(true, mSkyBoxes[mCurrentSkyBox], 99999*3, true);

 // Update Hydrax sun position and colour
 mHydrax->setSunPosition(mSunPosition[mCurrentSkyBox]);
 mHydrax->setSunColor(mSunColor[mCurrentSkyBox]);

 // Update light 0 light position and colour
 mSceneMgr->getLight("Light0")->setPosition(mSunPosition[mCurrentSkyBox]);
 mSceneMgr->getLight("Light0")->setSpecularColour(mSunColor[mCurrentSkyBox].x,mSunColor[mCurrentSkyBox].y,mSunColor[mCurrentSkyBox].z);

 // Update text area
 //mTextArea->setCaption("Hydrax 0.5.1 demo application/nCurrent water preset: "  + Ogre::StringUtil::split(mSkyBoxes[mCurrentSkyBox],"/")[1] + " (" +Ogre::StringConverter::toString(mCurrentSkyBox+1) + "/3). Press 'm' to switch water presets.");

 // Log
 LogManager::getSingleton().logMessage("Skybox " + mSkyBoxes[mCurrentSkyBox] + " selected. ("+Ogre::StringConverter::toString(mCurrentSkyBox+1)+"/"+Ogre::StringConverter::toString(_def_SkyBoxNum)+")");
}

 bool HydraxManageListener::frameStarted(const FrameEvent& evt)
 {
  // Update Hydrax
  mHydrax->update(evt.timeSinceLastFrame);

  if (mInput->IsKeyDown(OIS::KC_M) && mKeyBuffer < 0)
  {
   mCurrentSkyBox++;

   if(mCurrentSkyBox > (_def_SkyBoxNum-1))
   {
    mCurrentSkyBox = 0;
   }

   changeSkyBox();

   mKeyBuffer = 0.5f;
  }

  mKeyBuffer -= evt.timeSinceLastFrame;

  return true;
 }

 bool HydraxManageListener::frameEnded(const FrameEvent& evt)
 {
  return true;
 }

 float seed_ = 801;
 float rnd_(const float& min, const float& max)
 {
  seed_ += Ogre::Math::PI*2.8574f + seed_*(0.3424f - 0.12434f + 0.452345f);
  if (seed_ > 10000000000) seed_ -= 10000000000;
  return ((max-min)*Ogre::Math::Abs(Ogre::Math::Sin(Ogre::Radian(seed_))) + min);
 }

 void createPalms(Ogre::SceneManager *mSceneMgr)
 {
  const int NumberOfPalms = 12;

  Ogre::SceneNode* mPalmsSceneNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();

  for (int k = 0; k < NumberOfPalms; k++)
  {
   Ogre::Vector3 RandomPos = Ogre::Vector3(rnd_(500,2500),
    0,
    rnd_(500,2500));

   Ogre::RaySceneQuery * raySceneQuery = mSceneMgr->
    createRayQuery(Ogre::Ray(RandomPos + Ogre::Vector3(0,1000000,0),
    Ogre::Vector3::NEGATIVE_UNIT_Y));

   Ogre::RaySceneQueryResult& qryResult = raySceneQuery->execute();
   Ogre::RaySceneQueryResult::iterator i = qryResult.begin();

  /* if (i != qryResult.end() && i->worldFragment)
   {
    if (i->worldFragment->singleIntersection.y>105 || i->worldFragment->singleIntersection.y<20)
    {
     k--;
     continue;
    }

    RandomPos.y = i->worldFragment->singleIntersection.y;
   }
   else
   {
    k--;
    continue;
   }*/

   Ogre::Entity *mPalmEnt = mSceneMgr->createEntity("Palm"+Ogre::StringConverter::toString(k), "Palm.mesh");
   Ogre::SceneNode *mPalmSN = mPalmsSceneNode->createChildSceneNode();

   mPalmSN->rotate(Ogre::Vector3(-1,0,rnd_(-0.3,0.3)), Ogre::Degree(90));
   mPalmSN->attachObject(mPalmEnt);
   Ogre::Real Scale = rnd_(50,75);
   mPalmSN->scale(Scale,Scale,Scale);
   mPalmSN->setPosition(RandomPos);
  }
 }
//----------------------------------------------------------------------------------------------

HydraxManage::HydraxManage(Root *pRoot,SceneManager* pSceneMgr,Camera* pCamera,RenderWindow* pWindow,Input* pInput)

 mRoot=pRoot;
 mSceneMgr=pSceneMgr;
 mWindow=pWindow;
 mCamera=pCamera;
    mInput=pInput;

}

void HydraxManage::CreateHydraxSence()
 {
  
  // Create the SkyBox
  mSceneMgr->setSkyBox(true, mSkyBoxes[mCurrentSkyBox], 99999*3, true);

  // Light
  Ogre::Light *mLight = mSceneMgr->createLight("Light0");
  mLight->setPosition(mSunPosition[mCurrentSkyBox]);
  mLight->setDiffuseColour(1, 1, 1);
  mLight->setSpecularColour(mSunColor[mCurrentSkyBox].x,
  mSunColor[mCurrentSkyBox].y,
  mSunColor[mCurrentSkyBox].z);

  // Hydrax initialization code ---------------------------------------------
  // ------------------------------------------------------------------------

  // Create Hydrax object
  mHydrax = new Hydrax::Hydrax(mSceneMgr, mCamera, mWindow->getViewport(0));

  // Create our projected grid module 
  Hydrax::Module::ProjectedGrid *mModule
   = new Hydrax::Module::ProjectedGrid(// Hydrax parent pointer
   mHydrax,
   // Noise module
   new Hydrax::Noise::Perlin(/*Generic one*/),
   // Base plane
   Ogre::Plane(Ogre::Vector3(0,1,0), Ogre::Vector3(0,0,0)),
   // Normal mode
   Hydrax::MaterialManager::NM_VERTEX,
   // Projected grid options
   Hydrax::Module::ProjectedGrid::Options(/*264 /*Generic one*/));

  // Set our module
  mHydrax->setModule(static_cast<Hydrax::Module::Module*>(mModule));

  // Load all parameters from config file
  // Remarks: The config file must be in Hydrax resource group.
  // All parameters can be set/updated directly by code(Like previous versions),
  // but due to the high number of customizable parameters, since 0.4 version, Hydrax allows save/load config files.
  mHydrax->loadCfg("HydraxDemo.hdx");

  // Create water
  mHydrax->create();

  // Create palmiers
  createPalms(mSceneMgr);

  // Hydrax initialization code end -----------------------------------------
  // ------------------------------------------------------------------------

  // Add frame listener
  mRoot->addFrameListener(new HydraxManageListener(mInput, mSceneMgr));


 }

HydraxManage::~HydraxManage(void)
{

}

 

注意HydraxManage对象的调用要在PLSM2初始化后面


运行界面

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值