ページ

2013年12月12日木曜日

Basic Tutorial 2 をやってみる

Basic Tutorial 2 では、カメラとライト、影の使い方。

前回のソースコードを使う。
TutorialApplication.h
/*
-----------------------------------------------------------------------------
Filename:    TutorialApplication.h
-----------------------------------------------------------------------------

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

#include "BaseApplication.h"

class TutorialApplication : public BaseApplication
{
public:
    TutorialApplication(void);
    virtual ~TutorialApplication(void);

protected:
    virtual void createScene(void);

 virtual void createCamera(void);
 virtual void createViewports(void);
 
};

#endif // #ifndef __TutorialApplication_h_


TutorialApplication.cpp
/*
-----------------------------------------------------------------------------
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 :)


 // Set the scene's ambient light
 // シーンに周囲光をセット
    mSceneMgr->setAmbientLight(Ogre::ColourValue(0.5f, 0.5f, 0.5f));
 
    // Create an Entity
 // エンティティの作成
 Ogre::Entity* ogreHead = mSceneMgr->createEntity("Head", "ogrehead.mesh");

 // Create a SceneNode and attach the Entity to it
 // シーンノードの作成とエンティティのアタッチ
 Ogre::SceneNode* headNode = mSceneMgr->getRootSceneNode()->createChildSceneNode("HeadNode");

    headNode->attachObject(ogreHead);
 
    // Create a Light and set its position
 // ライトの作成と位置の指定
    Ogre::Light* light = mSceneMgr->createLight("MainLight");
    light->setPosition(20.0f, 80.0f, 50.0f);

 
}
//-------------------------------------------------------------------------------------
void TutorialApplication::createCamera(void)
{
 // create the camera
 // カメラを作成
 mCamera = mSceneMgr->createCamera("PlayerCam");
 // set its position, direction  
 // 位置と方向設定
 mCamera->setPosition(Ogre::Vector3(0,10,500));
 mCamera->lookAt(Ogre::Vector3(0,0,0));
 // set the near clip distance
 // ニアクリッピング距離の設定
 mCamera->setNearClipDistance(5);

 //デフォルトのカメラコントローラの作成
 mCameraMan = new OgreBites::SdkCameraMan(mCamera);   // create a default camera controller
}

//-------------------------------------------------------------------------------------
void TutorialApplication::createViewports(void)
{
 // Create one viewport, entire window
 // 1つのビューポートを作成、ウインドウ全体
 Ogre::Viewport* vp = mWindow->addViewport(mCamera);
 vp->setBackgroundColour(Ogre::ColourValue(0,0,0));//ビューポートの背景:黒
 // Alter the camera aspect ratio to match the viewport
 // ビューポートに一致するように、カメラアスペクト比を変更
 mCamera->setAspectRatio(Ogre::Real(vp->getActualWidth()) / Ogre::Real(vp->getActualHeight()));    

}


#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


ライトと影
/*
-----------------------------------------------------------------------------
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)
{
 // 影
 mSceneMgr->setAmbientLight(Ogre::ColourValue(0, 0, 0));// 周囲光の設定
 mSceneMgr->setShadowTechnique(Ogre::SHADOWTYPE_STENCIL_ADDITIVE);//影の種類を指定

 Ogre::Entity* entNinja = mSceneMgr->createEntity("Ninja", "ninja.mesh");// 忍者オブジェクトを作成
 entNinja->setCastShadows(true);//影を表示させる
 mSceneMgr->getRootSceneNode()->createChildSceneNode()->attachObject(entNinja);// オブジェクトを表示させる

 Ogre::Plane plane(Ogre::Vector3::UNIT_Y, 0);//原点から0の地点にパネルを作成

 // 平面のメッシュと作成して上のパネルを登録
 Ogre::MeshManager::getSingleton().createPlane("ground", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
  plane, 1500, 1500, 20, 20, true, 1, 5, 5, Ogre::Vector3::UNIT_Z);

 Ogre::Entity* entGround = mSceneMgr->createEntity("GroundEntity", "ground");//パネルのエンティティを作成
 mSceneMgr->getRootSceneNode()->createChildSceneNode()->attachObject(entGround);//スクリーンマネージャーにパネルのエンティティを設定

 entGround->setMaterialName("Examples/Rockwall");// パネルのテクスチャ
 entGround->setCastShadows(false);// パネルの影は表示しない

 //ライト
 Ogre::Light* pointLight = mSceneMgr->createLight("pointLight");//ライトの作成
 pointLight->setType(Ogre::Light::LT_POINT);// ポイントライトに指定
 pointLight->setPosition(Ogre::Vector3(0, 150, 250));// ライトの位置

 pointLight->setDiffuseColour(1.0, 0.0, 0.0);// 拡散反射の色を指定
 pointLight->setSpecularColour(1.0, 0.0, 0.0);// 鏡面反射の色を指定

 Ogre::Light* directionalLight = mSceneMgr->createLight("directionalLight");//二つ目のライトを作成
 directionalLight->setType(Ogre::Light::LT_DIRECTIONAL);// 指向性光源
 directionalLight->setDiffuseColour(Ogre::ColourValue(.25, .25, 0));
 directionalLight->setSpecularColour(Ogre::ColourValue(.25, .25, 0));

 directionalLight->setDirection(Ogre::Vector3( 0, -1, 1 ));// ライトの位置

 Ogre::Light* spotLight = mSceneMgr->createLight("spotLight");//三つ目のライト
 spotLight->setType(Ogre::Light::LT_SPOTLIGHT);//スポットライトに指定
 spotLight->setDiffuseColour(0, 0, 1.0);
 spotLight->setSpecularColour(0, 0, 1.0);

 spotLight->setDirection(-1, -1, 0);
 spotLight->setPosition(Ogre::Vector3(300, 300, 0));

 spotLight->setSpotlightRange(Ogre::Degree(35), Ogre::Degree(50));

}
//-------------------------------------------------------------------------------------
void TutorialApplication::createCamera(void)
{
 // create the camera
 // カメラを作成
 mCamera = mSceneMgr->createCamera("PlayerCam");
 // set its position, direction  
 // 位置と方向設定
 mCamera->setPosition(Ogre::Vector3(0,400,500));// 左右,高さ,前後
 mCamera->lookAt(Ogre::Vector3(0,0,0));
 // set the near clip distance
 // ニアクリッピング距離の設定
 mCamera->setNearClipDistance(10);

 //デフォルトのカメラコントローラの作成
 mCameraMan = new OgreBites::SdkCameraMan(mCamera);   // create a default camera controller
}

//-------------------------------------------------------------------------------------
void TutorialApplication::createViewports(void)
{
 // Create one viewport, entire window
 // 1つのビューポートを作成、ウインドウ全体
 Ogre::Viewport* vp = mWindow->addViewport(mCamera);
 vp->setBackgroundColour(Ogre::ColourValue(0,0,0));//ビューポートの背景:黒
 // Alter the camera aspect ratio to match the viewport
 // ビューポートに一致するように、カメラアスペクト比を変更
 mCamera->setAspectRatio(Ogre::Real(vp->getActualWidth()) / Ogre::Real(vp->getActualHeight()));    

}


#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 件のコメント:

コメントを投稿