ogre1.7 cookbook_OgreWin32Application

环境配置: win32Application

ogre1.7 cookbook_OgreWin32Application

ogre1.7 cookbook_OgreWin32Application

ogre1.7 cookbook_OgreWin32Application

新工程的debug目录下需要:

ogre.cfg

resources_d.cfg

plugins_d.cfg

OgreMain_d.dll

RenderSystem_Direct3D9_d.dll(RenderSystem_GL_d.dll)

Plugin_ParticleFX_d.dll

 

#pragma once



#include "Ogre.h"



class CEngine

{

public:

    CEngine(HWND hWnd);

    ~CEngine();



public:

    Ogre::Root* m_Root;

    Ogre::SceneManager* m_sceneMgr;

    Ogre::RenderWindow* m_renderWindow;

    Ogre::Camera* m_camera;

};
#include "stdafx.h"

#include "Engine.h"



#include <string>



CEngine::CEngine(HWND hWnd)

{

    TCHAR szPath[MAX_PATH];

    GetModuleFileName(NULL, szPath, MAX_PATH);



    std::string path(szPath);

    int pos = path.find_last_of("\\");

    if (pos != std::string::npos)

    {

        path = path.substr(0, pos);

        path += "\\";

    }



    m_Root = new Ogre::Root("", "", Ogre::String(path) + Ogre::String("OgreWin32.log"));



    Ogre::ConfigFile ogreConfigFile;

    ogreConfigFile.load(Ogre::String(path) + Ogre::String("ogre.cfg"));



    Ogre::String renderSystemName = ogreConfigFile.getSetting("Render System");

    if (renderSystemName == "Direct3D9 Rendering Subsystem")

    {

#ifdef _DEBUG

        m_Root->loadPlugin("RenderSystem_Direct3D9_d");

#else

        m_Root->loadPlugin("RenderSystem_Direct3D9");

#endif

    }

    else if(renderSystemName == "OpenGL Rendering Subsystem")

        {

#ifdef _DEBUG

            m_Root->loadPlugin("RenderSystem_GL_d");

#else

            m_Root->loadPlugin("RenderSystem_GL");

#endif

        }

        else

        {

#ifdef _DEBUG         

            m_Root->loadPlugin("RenderSystem_Direct3D9_d");

#else

            m_Root->loadPlugin("RenderSystem_Direct3D9");

#endif

        }



#ifdef _DEBUG

    m_Root->loadPlugin("Plugin_ParticleFX_d");

#else

    m_Root->loadPlugin("Plugin_ParticleFX");

#endif



        Ogre::RenderSystemList renderList = m_Root->getAvailableRenderers();

        m_Root->setRenderSystem(renderList[0]);



        m_Root->initialise(false);

        m_sceneMgr = m_Root->createSceneManager(Ogre::ST_GENERIC, "ogreWin32");



        Ogre::NameValuePairList params;

        params["externalWindowHandle"] = Ogre::StringConverter::toString((long)hWnd);

        params["vsync"] = "true";



        RECT rect;

        GetClientRect(hWnd, &rect);

        Ogre::RenderTarget* renderWindow = NULL;

        try

        {

            m_renderWindow = m_Root->createRenderWindow("ogreWin32", rect.right - rect.left,

                rect.bottom - rect.top, false, &params);

        }

        catch (...)

        {

            MessageBox(hWnd, "failed create ogre render window", "initialize rendr system",

                MB_OK | MB_ICONSTOP);

            exit(EXIT_SUCCESS);

        }



        Ogre::ConfigFile cf;

        cf.load(Ogre::String(path) + Ogre::String("resources_d.cfg"));



        Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();

        Ogre::String secName, typeName, archName;

        while (seci.hasMoreElements())

        {

            secName = seci.peekNextKey();

            Ogre::ConfigFile::SettingsMultiMap* settings = seci.getNext();

            Ogre::ConfigFile::SettingsMultiMap::iterator ite;

            for (ite = settings->begin(); ite != settings->end(); ++ite)

            {

                typeName = ite->first;

                archName = ite->second;

                Ogre::ResourceGroupManager::getSingleton().addResourceLocation(archName, typeName, secName);

            }

        }



        Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();



        m_camera = m_sceneMgr->createCamera("ogrewin32 camera");

        m_camera->setNearClipDistance(0.5);

        m_camera->setFarClipDistance(5000);

        m_camera->setCastShadows(false);

        m_camera->setUseRenderingDistance(true);

        m_camera->setPosition(Ogre::Vector3(200.0, 50.0, 100.0));

        Ogre::SceneNode* camerNode = NULL;

        camerNode = m_sceneMgr->getRootSceneNode()->createChildSceneNode("camer node");



        Ogre::Viewport* viewPort = NULL;

        if (0 == m_renderWindow->getNumViewports())

        {

            viewPort = m_renderWindow->addViewport(m_camera);

            viewPort->setBackgroundColour(Ogre::ColourValue(0.8, 1.0, 0.8));

        }



        m_camera->setAutoAspectRatio(true);



        Ogre::MeshManager::getSingleton().createPlane("floor", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,

            Ogre::Plane(Ogre::Vector3::UNIT_Y, -30), 1000, 1000, 10, 10, true, 1, 8, 8, Ogre::Vector3::UNIT_Z);



        // create a floor entity, give it a material, and place it at the origin

        Ogre::Entity* floor = m_sceneMgr->createEntity("Floor", "floor");

        floor->setMaterialName("Examples/BumpyMetal");

        m_sceneMgr->getRootSceneNode()->attachObject(floor);



        Ogre::Entity* entity = m_sceneMgr->createEntity("Sinbad", "Sinbad.mesh");

        Ogre::SceneNode* sinbadNode = m_sceneMgr->getRootSceneNode()->createChildSceneNode();

        sinbadNode->attachObject(entity);

        sinbadNode->scale(10.0f, 10.0f, 10.0f);

        sinbadNode->setPosition(Ogre::Vector3(0, 30, 0));



        Ogre::AxisAlignedBox robotBox = entity->getBoundingBox();

        Ogre::Vector3 center = robotBox.getCenter();

        m_camera->lookAt(center);



        m_sceneMgr->setAmbientLight(Ogre::ColourValue(0.3, 0.3, 0.3));

        Ogre::Light* light = m_sceneMgr->createLight();

        light->setType(Ogre::Light::LT_POINT);

        light->setPosition(-10, 40, 20);

        light->setSpecularColour(Ogre::ColourValue::White);

}



CEngine::~CEngine()

{

    //clear scene..

    if (m_sceneMgr)

        m_sceneMgr->clearScene();



    //unload resource..

    Ogre::ResourceGroupManager::ResourceManagerIterator resMgrs =

        Ogre::ResourceGroupManager::getSingleton().getResourceManagerIterator();



    while (resMgrs.hasMoreElements())

    {

        resMgrs.getNext()->unloadUnreferencedResources();

    }



    if (m_sceneMgr)

    {

        m_Root->destroySceneManager(m_sceneMgr);

        m_sceneMgr = 0;

    }



    if (m_renderWindow)

    {

        m_renderWindow->removeAllViewports();

    }



    if (m_Root)

    {

        delete m_Root;

        m_Root = 0;

    }

}

////////////////////////////////////////////////////////////////

CEngine* pEngine = NULL;
void InitEngine(HWND hwnd);
void CloseEngine();

//////////////////////////////////////////////////////////////////

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;

hInst = hInstance; // 将实例句柄存储在全局变量中

hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

if (!hWnd)
{
return FALSE;
}

InitEngine(hWnd);

ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

 

return TRUE;
}

///////////////////////////////////////////

case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: 在此添加任意绘图代码...
pEngine->m_Root->renderOneFrame();
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);

CloseEngine();

break;

///////////////////////////////////////////////

void InitEngine(HWND hwnd)
{
pEngine = new CEngine(hwnd);
}

void CloseEngine()
{
if (pEngine)
{
delete pEngine;
pEngine = NULL;
}
}

注意的事:

初次加入.mesh到一个节点后,只画出了模型,但是没有皮肤,是因为没有正确的载入材质的原因.

要想正确的载入材质,Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();

必须被调用.

而且locateResource(读取resources_d.cfg)和上面的loadResource必须在创建了Ogre::RenderTarget* renderWindow之后调用。

另外就是需要Plugin_ParticleFX_d.dll这个dll,以为内在initialiseAllResourceGroups中会用到.

你可能感兴趣的:(application)