2012年7月18日 星期三

NeoOgreBase

NeoOgreBase.h


#ifndef _NEO_OGRE_BASE_H_
#define _NEO_OGRE_BASE_H_
#include <Ogre.h>
class NeoOgreBase
{
public:
     NeoOgreBase() : m_root(NULL), m_render_win(NULL), m_scene_mgr(NULL), m_camera(NULL){}
    ~NeoOgreBase(){if (m_root) delete m_root;}
    void Run(void)
    {
        if (!Initialize())
        {
            exit(-1);
        }
        CreateScene();
        m_root->startRendering();
    }
protected:
    bool Initialize(void)
    {
        m_root = new Ogre::Root("","");
        #ifdef _DEBUG
            m_root->loadPlugin( "RenderSystem_Direct3D9_d" );     // for a debug build
        #else
            m_root->loadPlugin( "RenderSystem_Direct3D9" );     // for a release build
        #endif
//------------------------------------------------------------------------------------
//   Ogre::RenderSystemList *render_sys_list = m_root->getAvailableRenderers();  //for 1.6
//   Ogre::RenderSystemList::iterator it_render_sys(render_sys_list->begin() );//for 1.6
//   while (it_render_sys != render_sys_list->end() )                          //for 1.6
//------------------------------------------------------------------------------------
    const Ogre::RenderSystemList &render_sys_list = m_root->getAvailableRenderers();//for 1.7
    Ogre::RenderSystemList::const_iterator it_render_sys( render_sys_list.begin() );//for 1.7
    while ( it_render_sys != render_sys_list.end() )                                //for 1.7
            {
                Ogre::RenderSystem* render_sys = *(it_render_sys++);
                if(render_sys->getName().find("Direct3D9") != Ogre::String::npos)
                {
                    m_root->setRenderSystem(render_sys);
                    break;
                }
            }
            if (m_root == NULL) 
            {
                delete m_root;
                return false;
            }
            m_root->initialise(false);//false表示不自動產生視窗,手動產生
            m_render_win = m_root->createRenderWindow("Neo Ogre", 320, 240, false); //false表非全螢幕模式
            m_scene_mgr = m_root->createSceneManager(Ogre::ST_GENERIC,"neo scene manager");
            m_camera = m_scene_mgr->createCamera("MainCamera");
            Ogre::Viewport* viewport = m_render_win->addViewport(m_camera);
            viewport->setBackgroundColour(Ogre::ColourValue::Black); //same as ColourValue( 0, 0, 0 )
          
            //設定camera
             m_camera->setAspectRatio( Ogre::Real( viewport->getActualWidth() ) /  Ogre::Real( viewport->getActualHeight() ) );
             m_camera->setNearClipDistance( 10 );
             m_camera->setFarClipDistance( 10000 );
             m_camera->setFOVy( Ogre::Degree( 45 ) );
             m_camera->setPosition( 0, 0, 1000 );

            //增加材質
            Ogre::MaterialPtr mtl;
            mtl = Ogre::MaterialManager::getSingleton().create("NeoMatrial_0", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
            mtl->setLightingEnabled(false);
            mtl = Ogre::MaterialManager::getSingleton().create("CheeseCube", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
            mtl->setLightingEnabled( true );
            mtl->setAmbient(Ogre::ColourValue( 0.87, 0.77, 0.46 ) );
            mtl->setDiffuse(Ogre::ColourValue( 0.87, 0.77, 0.46 ) );

            return true; 
    }
protected:
    virtual void CreateScene(void) = 0;
    Ogre::Root* m_root;
    Ogre::RenderWindow* m_render_win;
    Ogre::SceneManager* m_scene_mgr;
    Ogre::Camera* m_camera;
private:
};
#endif 

main.cpp
#include <Ogre.h>
#include <iostream>
#include "NeoOgreBase.h"
using namespace Ogre;

class myOgre : public NeoOgreBase
{
protected:
    void CreateScene(void)
    {
      Light *light = m_scene_mgr->createLight( "light0" );
      light->setType( Light::LT_DIRECTIONAL );
      light->setDirection( Vector3( 0, -1, -1 ) );
      m_scene_mgr->setAmbientLight( ColourValue( 0.5, 0.5, 0.5 ) );
      
      SceneNode *center_node = m_scene_mgr->getRootSceneNode()->createChildSceneNode();
            //--------------------------------     
            std::ostringstream entity_name;
            entity_name << "cube0";
            Entity *ent = m_scene_mgr->createEntity(entity_name.str(), SceneManager::PT_CUBE );
            ent->setMaterialName( "CheeseCube" );
            SceneNode *scene_node = center_node->createChildSceneNode();
            scene_node->rotate( Vector3::UNIT_Z, Degree( 0 ), SceneNode::TS_LOCAL );
            scene_node->translate( Vector3( 0, 0, 0 ), SceneNode::TS_LOCAL );
            scene_node->attachObject( ent );
            //---------------------------------
            entity_name << "cube1";
            Entity *ent2 = m_scene_mgr->createEntity(entity_name.str(), SceneManager::PT_CUBE );
            ent2->setMaterialName( "NeoMatrial_0" );
            SceneNode *scene_node2 = center_node->createChildSceneNode();
            scene_node2->rotate( Vector3::UNIT_Z, Degree( 0 ), SceneNode::TS_LOCAL );
            scene_node2->translate( Vector3( 300, 0, 0 ), SceneNode::TS_LOCAL );
            scene_node2->attachObject(ent2);
            //---------------------------------
    }
};

int main(int argc, char *argv[])
{
    myOgre a;
    a.Run();
    return true;
}

沒有留言:

張貼留言