从《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->createRenderWindow(windowTitle, width, height, false, &miscParams);
//
// SoundPlay module: IrrKlang is almost the same as FMOD
/
// FMOD: play sound
string strSoundFile = "./Media/1.flac";
//float volume = 1.0f;
FMOD::System* fmodSystem = NULL;
FMOD::Sound* fmodSound = NULL;
FMOD::Channel* fmodChannel = NULL;
//FMOD::DSP* dspEcho = NULL;
FMOD::DSP* dspDisto = NULL;
System_Create(&fmodSystem);
fmodSystem->init(10, FMOD_INIT_NORMAL, NULL);
fmodSystem->createSound(strSoundFile.c_str(),
FMOD_LOOP_NORMAL | FMOD_2D, NULL, &fmodSound);
//fmodSystem->createDSPByType(FMOD_DSP_TYPE_ECHO, &dspEcho);
fmodSystem->createDSPByType(FMOD_DSP_TYPE_DISTORTION, &dspDisto);
fmodSystem->playSound(fmodSound, NULL, true, &fmodChannel);
//fmodChannel->setVolume(volume);
//dspEcho->setBypass(false);
dspDisto->setBypass(false);
//fmodChannel->addDSP(0, dspEcho);
fmodChannel->addDSP(0, dspDisto);
fmodChannel->setPaused(false);
fmodSystem->update(); // !!! IMPORTANT !!!
// Show window and fall into message loop
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
MSG msg = { 0 };
while (msg.message != WM_QUIT)
{
if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
// Render Loop
}
}
// Clean up
fmodSound->release();
fmodSystem->close();
m_pRoot->shutdown();
delete m_pRoot;
delete m_pLog;
return 0;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
char key = 0;
char msg[32] = "You pressed key: ";
int pos = strlen(msg);
switch (message)
{
case WM_PAINT:
{
ValidateRect(hwnd, NULL);
break;
}
case WM_KEYDOWN:
{
if (wParam == VK_ESCAPE) // ESC
{
DestroyWindow(hwnd);
}
break;
}
case WM_CHAR:
{
key = LOWORD(wParam);
if (IsCharAlpha(key))
{
msg[pos] = key;
MessageBox(hwnd, msg, "Info", MB_OK | MB_ICONINFORMATION);
}
break;
}
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
default:
{
return DefWindowProc(hwnd, message, wParam, lParam);
}
}
return 0;
}
// EOF
然后是执行结果:
首次运行时会弹出配置对话框
惯例不能添加动态图只能分别贴几张连续的图
这里设置1280*720可能会与富窗口尺寸不一致,这里暂且不讨论这个问题。
配置完成后
看看配置文件里面都有些什么内容
下面这张图我修改了某某文字(乱修改,这态度真不负责任),亮点自己找
示例代码中Win32窗口的创建比较简单,可参见
应用FMOD播放音乐不是重点,应用实例可参见
《音频引擎FMOD学习记录02:控制暂停/继续,调节音量,设置声效》
重点关注Ogre相关的部分。
LogManager* logMgr = new LogManager;
Log* m_pLog = LogManager::getSingleton().createLog("LoggerFile.log", true, true, false);
上面这两行代码创建日志
第二个参数设置为true表示这是默认的日志
Root* m_pRoot = new Root("plugins.cfg", "config.cfg");
构造函数其中的一种形式,第3各参数保持默认,因为上面创建了默认日志,这里的会被覆盖掉。
接下来是渲染系统(RenderSystem)设置
遍历可用的RenderSystem列表,找到匹配项"Direct3D9 Rendering Subsystem"
然后设置m_pRoot->setRenderSystem(pRenderSubystem);
if (!m_pRoot->restoreConfig())
{
m_pRoot->showConfigDialog();
}
首先尝试从文件(前述构造函数设置的config.cfg)载入
如果没有(首次运行时就没有)就弹出配置对话框
设置完毕后进行初始化
m_pRoot->initialise(false);
第一个参数false表示不自动创建窗口,其余参数保持默认
接下来进行额外配置
NameValuePairList miscParams;
miscParams["parentWindowHandle"] = StringConverter::toString((ULONG)hWnd);
其中"parentWindowHandle"(父窗口)参数被设置为之前创建的Win32窗口hWnd
m_pRoot->createRenderWindow(windowTitle, width, height, false, &miscParams);
这一语句通过最后一个参数miscParams指定了父窗口
该函数参见http://www.ogre3d.org/docs/api/1.9/class_ogre_1_1_root.html#a537b7d1d0937f799cfe4936f6b672620
因为Ogre窗口嵌入到了Win32窗口,因此外部事件需要有回电函数来处理。
窗口关闭(父窗口被关闭)后进行清理工作。
日志文件内容(部分省略)
16:18:51: Creating resource group General
16:18:51: Creating resource group Internal
16:18:51: Creating resource group Autodetect
16:18:51: SceneManagerFactory for type 'DefaultSceneManager' registered.
16:18:51: Registering ResourceManager for type Material
16:18:51: Registering ResourceManager for type Mesh
16:18:51: Registering ResourceManager for type Skeleton
16:18:51: MovableObjectFactory for type 'ParticleSystem' registered.
16:18:51: OverlayElementFactory for type Panel registered.
16:18:51: OverlayElementFactory for type BorderPanel registered.
16:18:51: OverlayElementFactory for type TextArea registered.
16:18:51: Registering ResourceManager for type Font
16:18:51: ArchiveFactory for archive type FileSystem registered.
16:18:51: ArchiveFactory for archive type Zip registered.
16:18:51: ArchiveFactory for archive type EmbeddedZip registered.
16:18:51: DDS codec registering
16:18:51: FreeImage version: 3.15.3
16:18:51: This program uses FreeImage, a free, open source image library supporting all common bitmap formats. See http://freeimage.sourceforge.net for details
16:18:51: Supported formats: bmp,ico,jpg,jif,jpeg,jpe,jng,koa,iff,lbm,mng,pbm,pbm,pcd,pcx,pgm,pgm,png,ppm,ppm,ras,tga,targa,tif,tiff,wap,wbmp,wbm,psd,cut,xbm,xpm,gif,hdr,g3,sgi,exr,j2k,j2c,jp2,pfm,pct,pict,pic
16:18:51: Registering ResourceManager for type HighLevelGpuProgram
16:18:51: Registering ResourceManager for type Compositor
16:18:51: MovableObjectFactory for type 'Entity' registered.
16:18:51: MovableObjectFactory for type 'Light' registered.
16:18:51: MovableObjectFactory for type 'BillboardSet' registered.
16:18:51: MovableObjectFactory for type 'ManualObject' registered.
16:18:51: MovableObjectFactory for type 'BillboardChain' registered.
16:18:51: MovableObjectFactory for type 'RibbonTrail' registered.
16:18:51: Loading library .\RenderSystem_Direct3D9
16:18:51: Installing plugin: D3D9 RenderSystem
16:18:51: D3D9 : Direct3D9 Rendering Subsystem created.
16:18:51: D3D9: Driver Detection Starts
16:18:51: D3D9: Driver Detection Ends
16:18:51: Plugin successfully installed
16:18:51: *-*-* OGRE Initialising
16:18:51: *-*-* Version 1.8.1 (Byatis)
16:18:51: D3D9 : RenderSystem Option: Allow NVPerfHUD = No
16:18:51: D3D9 : RenderSystem Option: FSAA = 0
16:18:51: D3D9 : RenderSystem Option: Fixed Pipeline Enabled = Yes
16:18:51: D3D9 : RenderSystem Option: Floating-point mode = Fastest
16:18:51: D3D9 : RenderSystem Option: Full Screen = No
....................省略........................
16:18:51: * Texture Compression: yes
16:18:51: - DXT: yes
16:18:51: - VTC: no
16:18:51: - PVRTC: no
16:18:51: * Scissor Rectangle: yes
16:18:51: * Hardware Occlusion Query: yes
16:18:51: * User clip planes: yes
16:18:51: * VET_UBYTE4 vertex element type: yes
16:18:51: * Infinite far plane projection: yes
16:18:51: * Hardware render-to-texture: yes
16:18:51: * Floating point textures: yes
16:18:51: * Non-power-of-two textures: yes
16:18:51: * Volume textures: yes
16:18:51: * Multiple Render Targets: 4
16:18:51: - With different bit depths: yes
16:18:51: * Point Sprites: yes
16:18:51: * Extended point parameters: yes
16:18:51: * Max Point Size: 8192
16:18:51: * Vertex texture fetch: yes
16:18:51: * Number of world matrices: 0
16:18:51: * Number of texture units: 8
16:18:51: * Stencil buffer depth: 8
16:18:51: * Number of vertex blend matrices: 0
16:18:51: - Max vertex textures: 4
16:18:51: - Vertex textures shared: no
16:18:51: * Render to Vertex Buffer : no
16:18:51: * DirectX per stage constants: yes
16:18:51: DefaultWorkQueue('Root') initialising on thread main.
16:18:51: Particle Renderer Type 'billboard' registered
16:19:22: DefaultWorkQueue('Root') shutting down on thread main.
16:19:22: *-*-* OGRE Shutdown
16:19:22: *-*-* OGRE Shutdown
16:19:22: Unregistering ResourceManager for type Compositor
16:19:22: Unregistering ResourceManager for type Font
16:19:22: Unregistering ResourceManager for type Skeleton
16:19:22: Unregistering ResourceManager for type Mesh
16:19:22: Unregistering ResourceManager for type HighLevelGpuProgram
16:19:22: Uninstalling plugin: D3D9 RenderSystem
16:19:22: D3D9 : Shutting down cleanly.
16:19:22: Unregistering ResourceManager for type Texture
16:19:22: Unregistering ResourceManager for type GpuProgram
16:19:22: D3D9 : Direct3D9 Rendering Subsystem destroyed.
16:19:22: Plugin successfully uninstalled
16:19:22: Unloading library .\RenderSystem_Direct3D9
16:19:22: Unregistering ResourceManager for type Material
注:代码中可能存在问题,仅供参考。如有建议、话题等欢迎评论指正。
本文原创,博文地址
http://blog.csdn.net/fengyhack/article/details/44565493