Ogre学习笔记(一)
2012-1-30
Kagula
概要
记录Ogre学习过程中要注意的地方。我的学习环境是
[1]WinXPSP3(简中版)
[2]VS2008SP1(英语版)
[3] boost 1.48
[4] Ogre 1.7.4
[5] CMake 2.8.7
为方便学习和调试,本文档中生成的库和程序是以Debug方式编译和运行。
正文
建立Ogre开发环境
参考资料[2]的Prerequisites准备BuildingOgre。
[S1]下载ogre_src_v1-7-4.exe后解压到D:\workspace\Ogre\ogre_src_v1-7-4路径。
[S2]下载CMake的Windows安装版,然后运行安装。
[S3]Building前确保以下环境变量已经正确设置,我的OGRE环境变量(有五个)如下:
DXSDK_DIR=D:\Microsoft DirectXSDK (February 2010)
BOOST_ROOT=E:\SDK\boost_1_48_0
BOOST_INCLUDEDIR=E:\SDK\boost_1_48_0
BOOST_LIBRARYDIR=E:\SDK\boost_1_48_0\stage\lib
OGRE_DEPENDENCIES_DIR=E:\SDK\Ogre174\Dependencies
[S4]下载安装MicrosoftDirectX SDK (February 2010)
现在Building Ogre。
[S1]启动CMake,[开始]->[所有程序]->[CMake2.8]->[CMake(cmake-gui)]
[S2]“Where is thesource code”栏里填入“D:\workspace\Ogre\ogre_src_v1-7-4”。
“Where to buildbinaries”栏里填入编译出来的依赖库放在哪里,我这里放在了“D:\workspace\Ogre\ogre_build_v1-7-4”位置。
[S3]点击“Configure”按钮,“Specifythe generator for this project”一栏里选择“Visual Studio 9 2008”,然后“Finish”。
[S4]现在你看见了红色的列表框,再点击“Configure”按钮,变成灰色列表框,然后点击“Generate”,现在你应该能看到“Generating Done”提示。
[S5]现在“D:\workspace\Ogre\ogre_build_v1-7-4”路径里已经生成“OGRE.sln”文件,打开它,BuildSolution,现在耐心等待就可以了。编译完成,“D:\workspace\Ogre\ogre_build_v1-7-4\bin\debug”目录下已经生成了我们需要的库文件和EXE文件。我们可以开始写第一个Ogre程序了。
第一个Ogre程序
目的是建立一个Ogre空项目。
先配置Ogre项目环境
[S1]在VS2008里配置Ogre头文件搜索路径为
“D:\workspace\Ogre\ogre_src_v1-7-4\OgreMain\include”
“D:\workspace\Ogre\ogre_src_v1-7-4\Samples\Common\include”
“D:\workspace\Ogre\ogre_build_v1-7-4\include”
“E:\SDK\Ogre174\Dependencies\include”
“E:\SDK\boost_1_48_0”
库文件搜索路径为
“D:\workspace\Ogre\ogre_build_v1-7-4\lib\Debug”
“E:\SDK\Ogre174\Dependencies\lib\Debug”
“E:\SDK\boost_1_48_0\stage\lib”
(如果要发行Release版本Debug路径得改为Release路径)。
“E:\SDK\boost_1_48_0\stage\lib”
[S2] 参考资料[3] 可以了解Ogre使用方式。
[S3]在VS2008里新建Win32Console工程,这里的Solution名字为“testOgre2”
[S4]复制“D:\workspace\Ogre\ogre_build_v1-7-4\inst\bin\debug”目录下的Resources_d.cfg和plugins_d.cfg文件到当前项目下面。我的当前项目路径是“D:\workspace\testOgre2\testOgre2”。
复制“D:\workspace\Ogre\ogre_src_v1-7-4\Samples”下的“Media”目录到当前项目路径下。
复制“D:\workspace\Ogre\ogre_build_v1-7-4\bin\debug”路径下的DLL文件到当前项目路径 下面。
DLL文件列表如下(下面几个DLL是必须的,否则无法启动程序)
cg.dll
OgreMain_d.dll
OIS_d.dl
Plugin_BSPSceneManager_d.dll
Plugin_CgProgramManager_d.dll
Plugin_OctreeSceneManager_d.dll
Plugin_OctreeZone_d.dll
Plugin_ParticleFX_d.dll
Plugin_PCZSceneManager_d.dll
RenderSystem_Direct3D9_d.dll
RenderSystem_GL_d.dll
[S5] 修改resources_d.cfg文件中的路径指向,原来是“../../Media”的路径改为“Media/”路径,否则会找不到刚才复制到项目中来的“Media”文件夹。
修改后的Resources_d.cfg文件内容如下
# Resources required by the sample browser and most samples.
[Essential]
Zip=media/packs/SdkTrays.zip
FileSystem=media/thumbnails
# Common sample resources needed by many of the samples.
# Rarely used resources should be separately loaded by the
# samples which require them.
[Popular]
FileSystem=media/fonts
FileSystem=media/materials/programs
FileSystem=media/materials/scripts
FileSystem=media/materials/textures
FileSystem=media/materials/textures/nvidia
FileSystem=media/models
FileSystem=media/particle
FileSystem=media/DeferredShadingMedia
FileSystem=media/PCZAppMedia
FileSystem=media/RTShaderLib
FileSystem=media/RTShaderLib/materials
Zip=media/packs/cubemap.zip
Zip=media/packs/cubemapsJS.zip
Zip=media/packs/dragon.zip
Zip=media/packs/fresneldemo.zip
Zip=media/packs/ogretestmap.zip
Zip=media/packs/ogredance.zip
Zip=media/packs/Sinbad.zip
Zip=media/packs/skybox.zip
[General]
FileSystem=media
[S6]修改项目向导自动生成的testOgre2.cpp文件内容
修改后的testOgre2.cpp文件内容如下
// testOgre2.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "MyApp.h"
#ifdef _DEBUG
#pragma comment(lib,"OgreMain_d.lib")
#pragma comment(lib,"OIS_d.lib")
#else
#pragma comment(lib,"OgreMain.lib")
#pragma comment(lib,"OIS.lib")
#endif
int _tmain(int argc, _TCHAR* argv[])
{
MyApp app;
app.go();
return 0;
}
[S7]添加MyApp Class
MyApp类的头文件内容如下
#pragma once
#include <ExampleApplication.h>
class MyApp:public ExampleApplication
{
public:
MyApp(void);
~MyApp(void);
private:
void createScene();
};
MyApp类的CPP文件内容如下
#include "StdAfx.h"
#include "MyApp.h"
MyApp::MyApp(void)
{
}
MyApp::~MyApp(void)
{
}
void MyApp::createScene()
{
}
[S8]在VS2008里以Debug方式启动就能看到Ogre对话框,进入后黑屏,按Esc可以退出,在项目的当前路径里会自动生成Ogre.log文件,你可以看一下运行日志。
[S9]把复杂到项目里的cfg文件全部看一遍,比如,你可能需要修改plugins_d.cfg因为我们不需要那么多的插件,这样可以少拷贝DLL文件。
程序运行后会产生Ogre.cfg文件。
[S10]我们可以在MyApp::createScene中添加代码
MyApp.cpp源文件清单如下
#include "StdAfx.h"
#include "MyApp.h"
MyApp::MyApp(void)
{
}
MyApp::~MyApp(void)
{
}
void MyApp::createScene()
{
Ogre::Entity* ogreHead = mSceneMgr->createEntity("Head", "ogrehead.mesh");
Ogre::SceneNode* headNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
headNode->attachObject(ogreHead);
Ogre::Entity* ogreHead2 = mSceneMgr->createEntity( "Head2", "ogrehead.mesh" );
Ogre::SceneNode* headNode2 = mSceneMgr->getRootSceneNode()->createChildSceneNode( "HeadNode2", Ogre::Vector3( 100, 0, 0 ) );
headNode2->attachObject( ogreHead2 );
// Set ambient light
mSceneMgr->setAmbientLight(Ogre::ColourValue(0.5, 0.5, 0.5));
// Create a light
Ogre::Light* l = mSceneMgr->createLight("MainLight");
l->setPosition(20,80,50);
}
[S11]使用F5快捷键,运行程序,我们将会看到一个鬼怪的头随着鼠标而动。
[S12]源代码全部理一遍,包括“ExampleApplication.h”文件,理解下Ogre使用方式。参考资料[4]进一步学习
调整库文件搜索路径
为了方便Release和Debug两种编译方式之间的切换,我们可以把这两种库放在一个文件夹里,因为文件名不同所以是不会冲突的。
[S1]“ E:\SDK\Ogre174\Dependencies\lib\Debug”文件夹下的文件复制到上面一层,“E:\SDK\Ogre174\Dependencies\lib\Release”文件夹下的文件复制到上面一层,只有cg.dll文件名为重复,保留原来的cg.dll即可。
[S2]相应修改VS2008中,库文件的搜索路径。
[S3]“D:\workspace\Ogre\ogre_build_v1-7-4\lib\Debug”文件夹下的文件复制到上面一层,“D:\workspace\Ogre\ogre_build_v1-7-4\lib\Release” 文件夹下的文件复制到上面一层。
[S4]相应修改VS2008,库文件的搜索路径。
参考资料
[1]《OgreMax使用技巧-轻松绕过限制》
http://hi.baidu.com/myard/blog/item/eab78ffdb1a8271a08244d2c.html
[2]《Building Ogre》
http://www.ogre3d.org/tikiwiki/Building+Ogre
[3]《Ogre Wiki TutorialFramework》
http://www.ogre3d.org/tikiwiki/Ogre+Wiki+Tutorial+Framework
[4]《Basic tutorial1》
http://www.ogre3d.org/tikiwiki/Basic+Tutorial+1&structure=Tutorials