从《OGRE3D引擎入门重拾》这篇文章开始,本系列关注OGRE3D渲染引擎及其周边应用。
本文以一个简单的示例来说明如何将Ogre的渲染窗口嵌入到另一个父窗口中,
为了增加趣味性(就是这么任性哈)我特意增加了FMOD的音乐播放支持。
首先将代码贴上来吧
/
// CREATED by fengyhack [@<163.com><gmail.com><qq.com><live.cn>]
// OGRE Example: Embeded window, Playing sound with FMOD.
/
#include <Windows.h>
#include <OgreRoot.h>
#include <OgreRenderSystem.h>
#include <fmod.hpp>
#include <string.h>
#pragma comment(lib,"OgreMain.lib")
#pragma comment(lib,"fmod_vc.lib")
using namespace Ogre;
using namespace std;
#define STR_RENDER_SYSTEM_DirectX9 "Direct3D9 Rendering Subsystem"
#define STR_RENDER_SYSTEM_OpenGL "OpenGL Rendering Subsystem"
#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR strCmdLine, INT nCmdShow)
{
string rsName = STR_RENDER_SYSTEM_DirectX9; // Using DirectX9
string windowTitle = string("OGRE Example Window - ") + rsName;
int width = WINDOW_WIDTH, height = WINDOW_HEIGHT;
// Ogre Objects: Log, Root, RenderSystem, etc.
LogManager* logMgr = new LogManager;
Log* m_pLog = LogManager::getSingleton().createLog("LoggerFile.log", true, true, false);
Root* m_pRoot = new Root("plugins.cfg", "config.cfg");
RenderSystemList rsList = m_pRoot->getAvailableRenderers();
RenderSystemList::iterator it = rsList.begin();
RenderSystem* pRenderSubystem = NULL;
while (it != rsList.end())
{
pRenderSubystem = *it;
if (pRenderSubystem->getName() == rsName)
{
m_pRoot->setRenderSystem(pRenderSubystem);
break;
}
++it;
}
pRenderSubystem = m_pRoot->getRenderSystem();
if (pRenderSubystem == NULL)
{
string msg = "FAILED to set Rendering Subsystem: " + rsName;
MessageBox(NULL, msg.c_str(), "ERROR", MB_OK);
}
if (!m_pRoot->restoreConfig())
{
m_pRoot->showConfigDialog();
}
//
// Do somethig (i.e. initialization) here
// E.G.
// Read properties from config.cfg
// and then set the parameters such as width/height, etc.
//
// Win32 Window - Parent/External window
LPCSTR wcexName = "MainWindow";
WNDCLASSEX wndClass = { 0 };
wndClass.cbSize = sizeof(WNDCLASSEX);
wndClass.style = CS_HREDRAW | CS_VREDRAW;
wndClass.lpfnWndProc = WndProc;
wndClass.cbClsExtra = 0;
wndClass.cbWndExtra = 0;
wndClass.hInstance = hInst;
wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION); //LoadIcon(hInst, IDI_ICON1);
wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndClass.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH);
wndClass.lpszMenuName = NULL;
wndClass.lpszClassName = wcexName;
RegisterClassEx(&wndClass);
DWORD dwStyle = WS_SYSMENU | WS_SIZEBOX;
HWND hWnd = CreateWindow(wcexName, windowTitle.c_str(), dwStyle,
100, 100, width, height, NULL, NULL, hInst, NULL);
// Ogre Window will be embedded on the Parent/External Window(aka. Win32 Window)
m_pRoot->initialise(false);
NameValuePairList miscParams;
// HWND hWnd: Ogre Window's parent window's handle
miscParams["parentWindowHandle"] = StringConverter::toString((ULONG)hWnd);
m_pRoot->createRe