ページ

2013年12月14日土曜日

Basic Tutorial 4 をやってみる

Basic Tutorial 4 は要約するとマウスボタンやキーボードの操作を受け取って、
カメラの位置を変える方法のような気がします。

/*
-----------------------------------------------------------------------------
Filename:    TutorialApplication.cpp
-----------------------------------------------------------------------------

This source file is part of the
   ___                 __    __ _ _    _ 
  /___\__ _ _ __ ___  / / /\ \ (_) | _(_)
 //  // _` | '__/ _ \ \ \/  \/ / | |/ / |
/ \_// (_| | | |  __/  \  /\  /| |   <| |
\___/ \__, |_|  \___|   \/  \/ |_|_|\_\_|
      |___/                              
      Tutorial Framework
      http://www.ogre3d.org/tikiwiki/
-----------------------------------------------------------------------------
*/
#include "TutorialApplication.h"

//-------------------------------------------------------------------------------------
TutorialApplication::TutorialApplication(void)
{
}
//-------------------------------------------------------------------------------------
TutorialApplication::~TutorialApplication(void)
{
}

//-------------------------------------------------------------------------------------
void TutorialApplication::createScene(void)
{
 // create your scene here :)

 mSceneMgr->setAmbientLight(Ogre::ColourValue(0.25, 0.25, 0.25));

 Ogre::Entity* ninjaEntity = mSceneMgr->createEntity("Ninja", "ninja.mesh");
 Ogre::SceneNode *node = mSceneMgr->getRootSceneNode()->createChildSceneNode("NinjaNode");
 node->attachObject(ninjaEntity);

 Ogre::Light* pointLight = mSceneMgr->createLight("pointLight");
 pointLight->setType(Ogre::Light::LT_POINT);
 pointLight->setPosition(Ogre::Vector3(0, 0, 0));
 pointLight->setDiffuseColour(Ogre::ColourValue::White);
 pointLight->setSpecularColour(Ogre::ColourValue::White);

}

//-------------------------------------------------------------------------------------
bool TutorialApplication::processUnbufferedInput(const Ogre::FrameEvent& evt)
{

 static bool mMouseDown = false;     // マウスボタンが押された時 // If a mouse button is depressed
 static Ogre::Real mToggle = 0.0;    // 次のトグルまでの時間 // The time left until next toggle
 static Ogre::Real mRotate = 0.13;   // 回転定数 // The rotate constant
 static Ogre::Real mMove = 250;      // 移動定数 // The movement constant

 // 左クリックでライトの点灯消灯切り替え
 bool currMouse = mMouse->getMouseState().buttonDown(OIS::MB_Left);

 if (currMouse && ! mMouseDown)
 {
  Ogre::Light* light = mSceneMgr->getLight("pointLight");
  light->setVisible(! light->isVisible());
 }

 mMouseDown = currMouse; // マウスの状態を設定

 // キーボードの1でライトの切り替え 0.5以上の間隔
 mToggle -= evt.timeSinceLastFrame;

 if ((mToggle < 0.0f ) && mKeyboard->isKeyDown(OIS::KC_1))
 {
  mToggle  = 0.5;
  Ogre::Light* light = mSceneMgr->getLight("pointLight");
  light->setVisible(! light->isVisible());
 }

 // カメラの位置
 Ogre::Vector3 transVector = Ogre::Vector3::ZERO;

 if (mKeyboard->isKeyDown(OIS::KC_I)) // Forward
 {
  transVector.z -= mMove;    // Z軸の移動 マイナス方向 小さくなる
 }
 if (mKeyboard->isKeyDown(OIS::KC_K)) // Backward
 {
  transVector.z += mMove;    // Z軸の移動 プラス方向 小さくなる
 }

 if (mKeyboard->isKeyDown(OIS::KC_J)) // Left - yaw or strafe
 {
  if(mKeyboard->isKeyDown( OIS::KC_LSHIFT )) 
  {
   // Yaw left // J + 左シフト 忍者が左回転 
   mSceneMgr->getSceneNode("NinjaNode")->yaw(Ogre::Degree(mRotate * 5));
  } else {
   transVector.x -= mMove; // Strafe left // 忍者が左に移動 
  }
 }

 if (mKeyboard->isKeyDown(OIS::KC_L)) // Right - yaw or strafe
 {
  if(mKeyboard->isKeyDown( OIS::KC_LSHIFT ))
  {
   // Yaw right // L + 左シフト 忍者が右回転 
   mSceneMgr->getSceneNode("NinjaNode")->yaw(Ogre::Degree(-mRotate * 5));
  } else {
   transVector.x += mMove; // Strafe right // 忍者が右に移動
  }
 }

 if (mKeyboard->isKeyDown(OIS::KC_U)) // Up
 {
  transVector.y += mMove;    // 忍者が上に移動
 }
 if (mKeyboard->isKeyDown(OIS::KC_O)) // Down
 {
  transVector.y -= mMove;    // 忍者が下に移動
 }

 //
 mSceneMgr->getSceneNode("NinjaNode")->translate(transVector * evt.timeSinceLastFrame, Ogre::Node::TS_LOCAL);


 return true;
}
//-------------------------------------------------------------------------------------
bool TutorialApplication::frameRenderingQueued(const Ogre::FrameEvent& evt)
{
 bool ret = BaseApplication::frameRenderingQueued(evt);

 if(!processUnbufferedInput(evt)) return false;

 return ret;
}
//-------------------------------------------------------------------------------------

#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
  TutorialApplication 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



0 件のコメント:

コメントを投稿