专注于互联网--专注于架构

最新标签
网站地图
文章索引
Rss订阅

首页 »游戏开发 » ogre教程:OGRE初学者引导 »正文

ogre教程:OGRE初学者引导

来源: 发布时间:星期四, 2009年2月12日 浏览:334次 评论:0


1 初学者如何开始OGRE 作者:Antiarc

  欢迎进入OGRE世界你在看这篇文章介绍说明你在开始学习OGRE了希望这篇文件对你有用

  这篇文件是假设你有下面知识情况下如C编程窍门技巧基本操作系统使用知识和些有关3D方面知识我在这篇文章中只是描述我知道东西对于其他东西请在OGRE论坛中讨论对于高级用户这篇文件对你也许不适合这是我第次使用OGRE些想法那时C不是很好对VS也不是很了解甚至不知道3D编程

(注意:这篇文章是有关VC7)

  首先我们需要到OGRE下载页面下载“stable releases(完整版)”下载后将他解压到你建立目录下(如:F:/OGRE)文件将自动生成到OGRENEW目录下这是必须如果你乱改了目录也许会发生

  如果你是使用WINDOWS请到OGRE 下载页面下载\"extras\"版本将他解压到你建立目录下如:F:/OGRE/OGRENEW这些文件适合LINUX平台并不包括对WINDOWS支持

  如果你在WINDOWS LINUX下使用GCC 2.9X你将需要下载更多东西如STLPORT如果你使用是GCC 3.0你将不需要做什么


2 何时需要STLPORT 为什么?

  如果你使用是VC 6/7你将需要STLPORTSTL在VC6/7中执行很慢并有许多OGRE是使用STL通过DLL使用MS STL;这将引起崩溃OGRE是非常面向DLL因此MS STL不适合而STLPORT很适合他运行也比MS STL快

  注意在VS 20032005中不需要STLPORT

  如果你使用是GCC 2.9X你将需要STL;但在GCC 3.0中不需要STL得到了更新

STLPORT是标准模版“beefed up\"版本包含许多你所需要基本有关STLPORT使用和安装在下面介绍

2.1 如何安装STLPORT?

* 从www.stlport.org下载最新STLPORT(4.6.2)并解压他
* 打开个 命令行(注意:在WINDOWS NT/2000/XP中是cmd.exe 而不是commamd.com)并改变他路径STLPORT-4.6.2/SRC
* 如果你使用VC请确定‘VCVARS32BAT’是否运行在devstudio/vc6/bin or visualstudio.net/vc7/bin目录下这些操作用于设置你环境参数并确定你‘NMAKE’文件位置

注意:这些命令必须在在同行执行VCVARS32BAT只设置运行中SHELL环境因此如果你得到有关NMAKE请确定VCVARS32BAT版本是否和SHELL版本相同

* 如果你使用VC 6
* 键入‘nmake -f vc6.mak clean all\'
* 键入 ’nmake -f vc6.mak \'
* 如果你使用是VC NET
* 键入 ‘nmake -f vc7.mak clean all\'
* 键入 ’nmake -f vc7.mak \'

注意:如果你觉得麻烦你可以添加NMAKEEXE路径到你路径变量中如果你这样做了返回了”NOTE:“比如如下:\"..\\\\stlport\\ctime(25) : fatal error C1083: Cannot open file:\'..//ctime\': No such file or directory\". 发生了这样你可以把VCVARS32BAT拷贝到你命令行相同路径下然后执行如果你有其他问题请到OGRE论坛中讨论

* 如果你使用VS 2003
* 你将不需要做任何事情
* 如果你使用是GCC 2.9X
* 键入 ‘./configure\'
* 键入 ’make\'
* 键入 ‘make \'
* 打开VC然后选择Tools/options...将弹出个对话框
* 在VC6中电击\'Directories\' 选行在.NET中是在Project/vc下面
* 确定STLPORT路径在路径列表最上面他必须在最上面
*(比如头文件路径可以如下:\\STLport-4.6.2\\stlport\\)
*(比如库文件路径可以如下:\\STLport-4.6.2\\lib\\)
* 保存并关闭
* 恭喜STLPORT已经设置好了


3 Visual Studio.NET (VC7)

  我们现在可以用OGRE开发了在开发的前请安装DX SDK然后建立OGRE引擎和例子他在OGRENEW/OGREDSW(VC6)或OGRENEW/OGRESLN(VC7)下电击“BUILD ALL”等待大约10-15分钟后引擎plugins,工具和例子将建立

  如果你想运行这些例子切换到ogre\\Samples\\Common\\bin\\Debug (在这里我们使用是DEBUG模式如果你是RELEASE模式就切换到RELEASE)先将OGREMAINDLL(ogre/ogre/lib/{debug|release})和其他支持拷贝到ogre/samples/common/bin/debug 下然后就可以运行例子了

  OGRE实际上是个应用类库他允许你很快速开发你需要应用他提供了你开发3D基础特别适合建立应用

我们首先建立个\"Empty Project\"然后进行下面设置:
* 右击项目名选择”Properties\".
* 添加the ogre\\OgreMain\\ 和ogre\\Samples\\Common\\ 路径到\"additional s\"
* 添加ogre\\OgreMain\\lib\\(Debug|Release) 到\"library search paths\"
* 添加OgreMain.lib 到\"Additional Dependencies\"
* 在C/Code Generation模式下选择\"Runtime Library\" to \"Multi-threaded Debug DLL\" (for debug-mode apps) 和 \"Multi-threaded DLL\" (For release-mode apps).

  你也可以把\"Output Directory\" 和\"Working Directory\" (under Debugging) 选上他将把文件放到和已经运行OGRE DLL相同目录下如果你不这样做你将拷贝许多OGRE DLL到output directory下

  设置好后我们就开始了.cpp文件


4 GCC 3.3/3.4

如果你使用LINUX或MingW for windows那么下面内容对你有用先到OGRE下载LINUX版本然后解压:
* tar xjf ./ogre*
* ./bootstrap (I\'ve found not doing this in 1.0.0 RC1 causes later errors)
* ./configure && make
* make (you may have to be root to do this)
设置好后你能通过./ogre/Samples/Common/bin 访问

设置Anjuta IDE

你可能想用IDE来便于你开发OGRE这些还在讨论中重点如下:

* 建立个新 Generic / Terminal
* change your Project->Project Options Configuration tab and Libraries sub-tab and add:



PKG_CHECK_MODULES(ogre, [OGRE >= 1.0.0])
AC_SUBST(ogre_CFLAGS)
AC_SUBST(ogre_LIBS)

* 然后进入Settings -> Compiler 将连接选行添加${ogre_CFLAGS} to CFLAGS and ${ogre_LIBS}
* 最后到Build->Autogenerate 这将自动更新你make and config文件


5 GCC 3.3/3.4 Dev C

如果你想在DC中使用OGRE请按照下面步骤:

1) 得到DC
2) 得到 DEVPAK (http://the-agency.sourceforge.net/xoops/modules/mydownloads/visit.php?cid=20&;;lid=64)
3) 得到OGRE 相关 DEVPAK (http://the-agency.sourceforge.net/xoops/modules/mydownloads/visit.php?cid=18&;;lid=65)
4) 安装DC
5) 安装Ogre DevPak
6) 安装Ogre Dependencies
7) 到例程目录例如:C:\\Development\\Dev-Cpp\\MyProjects\\ogre\\the-agency\\Samples\\ParticleFX
   然后打开DEV文件如:Sample_ParticleFX.dev
   将有个警告出现ignore 他
   如果你得到另个警告介绍说明你文件在“build\" 目录下
8) 电击Project -> Project Options -> Directories 然后设置:
   在\"Libraries\" 行设置:
   ..\\..\\OgreMain\\build

  ..\\..\\..\\Dependencies\\build

在\"Include\" tab如下:
   ..\\..\\..\\OgreMain\\

  ..\\..\\..\\dependencies\\devil\\

  ..\\..\\..\\dependencies\\freetype\\

  ..\\..\\..\\dependencies\\zlib\\

  ..\\..\\..\\Samples\\Common\\

  ..\\..\\..\\Samples\\ParticleFX\\

9) Hit Rebuild All (Ctrl + F11) -这将让Sample_ParticleFX.e xe 去建立个目录拷贝文件到C:\\Development\\Dev-Cpp\\MyProje cts\\ogre\\Samples\\Common\\bin\\Debug
10) 将dependencies\\build 所有DLL和C:\\Development\\Dev-Cpp\\MyProjects\\ogre\\the-agency (use the Windows Search function) 下文件拷贝到C:\\Development\\Dev-Cpp\\MyProje cts\\ogre\\Samples\\Common\\bin\\Debug
11) Rename PlatformManager_SDL.dll to OgrePlatform.dll
12) 通过下面设置修改the Plugins.cfg 文件:
   1Plugin=Render_Direct3D7
   1Plugin=Render_Direct3D9
13) 完成后运行Sample_ParticleFX.exe


6 GCC 3.3 Cygwin/MinGW

Use the following you don\'t want to have to up the whole Dev-C, but would like to have just a pure Cygwin/MinGW environment. Cygwin is used for it\'s building system, and since it can nicely cross-compile for MinGW, you will have a build that doesn\'t depend _disibledevent=>1) Full cygwin . Might need to add correct path to /etc/profile - it wasn\'t created _disibledevent=>3) get the MingW/Cygwin OGRE dependencies from the OGRE website. Either extract it to the root (thus creating /mingw) or to another directory and create a symlink /mingw poing to the correct location (that\'s what I did).

4) download and libtool 1.5.12 (or er). Version 1.5.10 (the _disibledevent=>5.2) export ACLOCAL_FLAGS=\"-I /mingw/share/aclocal\"

5.3) export PKG_CONFIG_PATH=/mingw/lib/pkgconfig

5.4) ./bootstrap

5.5) CC=\"gcc -mno-cygwin\" CXX=\"g -mno-cygwin\" CXXFLAGS=\"-O2 -I/mingw/\" LDFLAGS=\"-L/mingw/lib -L/lib/w32api\" ./configure --with-ft-prefix=/mingw --with-platform=Win32 --with-gl-support=Win32 --disable- --build=\"i686-pc-mingw32\" --enable-direct3d

5.6) make

5.7) make

6) up your development tools.



6.1) passing the arguments \"-lOgreMain -Wl,--enable-runtime-pseudo-reloc\" to the linker will work.

6.2) as far as library directories go, you can link to cygwin/usr/local/lib and be happy about it.

6.3) directories: cygwin/usr/local/, cygwin/usr/local//OGRE and ogre/Samples/Common/ ( you are using the ExampleApplication ). Where I say cygwin/ I actually mean the cygwin path (such as c:/cygwin), and the same goes for ogre/ (c:/cygwin/usr/local/ogre/ogre, for example)

6.4) this is not required, but I tend to copy all required DLLs to my build directory: the _disibledevent=>mSceneMgr->AmbientLight(c);
Entity *e = mSceneMgr->createEntity(\"ogrehead\", \"ogrehead.mesh\");
SceneNode *node = _cast<SceneNode *>(mSceneMgr->getRootSceneNode->createChild);
node->attachObject(e);
node->Position(0,0,0);
}

// Create frame listener
void Dome::createFrameListener(void)
{
mFrameListener = Input(mWindow, mCamera);
mRoot->addFrameListener(mFrameListener);
}

Input.h:

#ndef _INPUT_H_
# _INPUT_H_
# \"ExampleApplication.h\"
Input : public ExampleFrameListener
{
public:
Input(RenderWindow* win, Camera* cam) : ExampleFrameListener(win, cam) {}
~Input;
// Input erface
bool frameStarted(const FrameEvent& evt);
// TODO: add member function declarations...
protected:
};
#end //_INPUT_H_

Input.cc:

# \"Input.h\"
bool Input::frameStarted(const FrameEvent& evt)
{
mInputDevice->capture;
(mInputDevice->isKeyDown(Ogre::KC_ESCAPE))
false;
true;
}

按F11编译他如果得到有关OgreSharedPtr.h 警告这是正常
8) Go to the Projects folder in your home directory and enter the folder for the Ogre project, which I called Dome.

9) Create a symlink in the MyApp folder to the Media files that came with ogre. ln -s (ogre)/Samples/Media

10) Go o the src folder and create a folder named bin.

11) copy all the .cfg files from the ogre Samples o the bin folder. cp (ogre)/Samples/Common/bin/*.cfg Myapp/src/bin

12) go o the bin folder and create a symlink to you executable in the src folder. ln -s ../Dome

13) Run it. You need to run it from the bin folder. That way the executable has access to all the .cfg files in the folder with it. It also will look for the Media folder in ../../Media, which is why we put the symlink there.



8 你

# \"Ogre.h\"
# \"ExampleApplication.h\"
# <windows.h>
// First, we declare a sub of the ExampleApplication
FirstApp : public ExampleApplication
{
public:
FirstApp { }

/** createScene is pure virtual in ExampleApplication, so we just override it to do nothing.
* What this means is that we\'ll be creating an empty scene at first.
**/
void createScene(void) { }
};

INT WINAPI WinMain(HINSTANCE,HINSTANCE,LPSTR,INT) {
FirstApp myApp; // Instanciate our sub
try { // This try-catch provides a nice way of popping up errors they occur.
myApp.go; // ExampleApplication provides a go method, which starts the rendering.
} catch (Ogre::Exception& e) {
MessageBox( NULL, e.getFullDescription.c_str, \"An exception has occured!\",
MB_OK | MB_IConERROR | MB_TASKMODAL);
}


}

8.1 编译 运行

  试着运行他他将显示OGRE安装对话框并开始个包含OGRE LOGE和FPS深黑屏幕我在这里没有添加任何东西当按ESC键退出

  ExampleApplication 非常重要他用于建立窗口建立基本场景摄象机和缓冲器监视器(frame listener.)因此你不得不在工作时加上他

  这只是个空如果你想知道更多东西请参考OGRE教程

8.2 添加第个物体

前面我们建立了个黑色屏幕下面我们将添加个ogrehead mesh到在createScene中添加下面代码:

Entity *e = mSceneMgr->createEntity(\"ogrehead\", \"ogrehead.mesh\");
SceneNode *node = mSceneMgr->getRootSceneNode->createChildSceneNode;
node->attachObject(e);
node->Position(0,0,0);

ExampleApplication 为我们提供了个“场景管理者”----mSceneMgr.

  mScenemgr用于在OGRE管理我们场景SceneNodes是OGRE中个基本结构在场景中大多东西都和SceneNodes有联系你能通过控制这些节点控制场景每个场景都建立了个根场景节点你将把子节点光和网格值联系到根节点每个节点又能建立他自己子节点这样更新场景将会更容易例如你有个角色在场景中并且他检起个小棍角色和小棍将分别给予不可见场景节点要使角色检起小棍你将使角色动画弯腰并检起小棍这样你必须使小棍场景节点成为角色场景节点子节点我们都知道3D世界是建立在3D坐标系统上因此你有个角色在坐标(0.0.0)作为角色场景节点子节点小棍场景节点是坐标(10.10.10)小棍节点将在父节点下保持这个偏移这就意味着当你移动角色节点小棍节点也将跟着移动在这里并不是移动两个节点而是移动其中而他子节点将跟着移动这容易吗我认为很难

  在这个例子中网格值很简单为了建立可见物体我们简单用sceneManager建立了全部然后我们把他们给予场景节点在你计算机上个值想象成个窗口把sceneManager看着标题栏你能通过点击并拖动标题栏来移动窗口如果窗口有子窗口点击并移动主窗口将移动子窗口

  现在你将添加这些东西到场景中你将可能编译并运行他没有看见任何东西吗?原因是我们没有建立灯光因此我们场景就是黑我们有两种思路方法解决这个问题

8.3 添加灯光

  首先我们可以在场景中假如些环境灯光这将使整个场景达到亮度我们可以建立个灯光物体并设置个灯光场景节点并添加这个灯光到场景世界中

让我们来建立个环境灯光吧他是很容易
在你场景管理者中添加下面代码:

Colourvalue c = Colourvalue(0.5, 0.5, 0.5);
mSceneMgr->AmbientLight(c);

美国读者建议:

  sinbadOGRE创始者是英国人因此他拼写思路方法和美国区别比如\"Colour\" 替代了\"Color\" \"Initialise\" 替代了\"Initialize\". 因此在编译时有可能出现请大家注意

  mSceneMgr 用个成员去设置环境灯光主要是通过设置Colourvalue 值来实现Colourvalue 有 3个变量:red,green,blue.取值范围为0——1在标准RGB下0是01是255因此我在这个例子中建立了白光我们建立Colourvalue对象并把他添加到SceneManagerAmbientLight 思路方法中

代码如下:

# \"Ogre.h\"
# \"ExampleApplication.h\"

// First, we declare a sub of the ExampleApplication
FirstApp : public ExampleApplication
{
public:
FirstApp { }

/** createScene is pure virtual in ExampleApplication, so we just override
* to add what we just discussed.
* What this means is that we\'ll now can see the ogre head in the scene.
**/
void createScene(void) {
Colourvalue c = Colourvalue(0.5, 0.5, 0.5);
mSceneMgr->AmbientLight(c);
Entity *e = mSceneMgr->createEntity(\"ogrehead\", \"ogrehead.mesh\");
SceneNode *node = _cast<SceneNode *>(mSceneMgr->getRootSceneNode->createChild);
node->attachObject(e);
node->Position(0,0,0);
}
};

( argc, char **argv)
{
FirstApp myApp; // Instanciate our sub
try { // This try-catch provides a nice way of popping up errors they occur.
myApp.go; // ExampleApplication provides a go method, which starts the rendering.
} catch (Ogre::Exception& e) {
MessageBox( NULL, e.getFullDescription.c_str, \"An exception has occured!\",
MB_OK | MB_IConERROR | MB_TASKMODAL);
}
}

编译并运行它将产生个OGRE
现在让我们建立点光源
在createScene 思路方法中添加下面代码:

Light *l = mSceneMgr->createLight(\"aLight\"); // Create a light and give it a name
SceneNode *lightNode = _cast<SceneNode *>(mSceneMgr->getRootSceneNode->createChild);
lightNode->attachObject(l);
// We\'re going to create the light at the camera\'s position.
lightNode->Position(mCamera->getPosition);

就如你所见到我们先建立了个光个用于光场景节点并把光联系到节点上并设置节点坐标你可能注意到我用mCamera->getPosition 设置坐标ExampleApplication为我们提供了另个定义指针在这里我们用于设置摄象机你将在复杂中发现他很有用但在这里我们只是用他来取得摄象机坐标并设置灯光

Lights 值为\"po\" 光并是白光你可以随意更改但在这里我们不需要有 3中类型光:po, directional, and spotlightpo光是从个点向 4周发散directional 光没有只是个指向特定方向spotlight有点和方向



现在我们建立了物体光和控制他们节点


9 Frame Listeners

  Frame Listeners 是什么?实际上是个允许你更新你世界物体他有两种思路方法来实现:frameStarted and frameEndedframeStarted帧动画开始frameEnded动画结束你能用这些对象和思路方法更新你世界

  OGRE提供了个基本frame listener 叫ExampleFrameListener他允许你在世界中自由移动对于每个动画他都接受鼠标输入和键盘输入并允许你改变世界这些都是通过移动摄象机实现鼠标移动决定了你摄象机位置当你按每个键将决定按键后动作Frame listeners 不仅控制你摄象机移动并是个游戏循环当游戏运行时他将控制你所有操作如果你感兴趣打开ExampleFrameListener (in ogre\\Samples\\Common\\) 浏览他如果你知道些3D数学你将知道他是如何工作在这里我们只涉及到frameStarted and frameEnded


10 整理总结

  这只是有关OGRE篇引导去玩例程并阅读其他教程你将获得更多知识希望这篇文章对你有用如果有任何问题请写信给我不要客气

  译者注:经过两个星期 6时间终于完成了这篇文章翻译对于其中67节我没有翻译主要是国内使用这两个工具人很少(至少我觉是这样 :) )如有任何问题写信给我[email protected] 还是那句话希望这篇文章对你有用



0

相关文章

读者评论

发表评论

  • 昵称:
  • 内容: