文档:创建第一个程序
出自Ogre3D开放资源地带
(本页面只翻译了前面的Visual Studio部分,对于之后的Code::Blocks、Linux GCC、Eclipse等环境请熟悉它们的大大们进行补充。)
本页面将会引导你建立第一个应用程序。请确保你已经依照安装SDK或者从源代码构建安装了OGRE。作为一个新手最好选择第一种方式。
前置工作
此刻你应该已经看完了安装SDK或者从源代码构建并且把OGRE安装在了你的电脑里。否则,请回到适当的页面来完成对OGRE的安装配置。
为了执行程序,有一系列文件需要适当配置,让你的操作系统可以定位他们。下方的列表详细列举了这些文件。[_d]标明了一个指向debug库的引用可供选择(例:OgreMain_d.dll对应OgreMain.dll)。这些文件应该放置在你的工作目录内,或者从适当的途径能够访问到。
在Mac OS X操作系统上,你的项目必须从一个应用程序包来运行(又叫 ".app"),否则Ogre无法正常的初始化。XCode会自动的为你搞定,不过如果你不使用XCode,那你必须手动的构建程序包。
其实想要把一切都配置好的最简单方式,是把一个现有的OGRE示例程序和media目录复制到你的工作目录下。如果你使用Windows,把所有必要的DLL都复制到这个目录。编辑 plugins.cfg和resource.cfg,让它们里面的的文件路径与您的新位置相符
- OGRE库与文件
- OGRE库(OgreMain[_d].dll, 对应的linux版本是libOgreMain.so)
- plugins.cfg - 文件中指定了Ogre可选的渲染库(例:DX9, OpenGL)
- 所有plugins.cfg列出的插件库(如果你不想要它们,你可以直接从plugins.cfg中移除)
- resource.cfg - 如果你使用例子程序(ExampleApplication),这个文件指定了材质、纹理、模型等东西。
- OgreCore.zip - 要是你打算使用OGRE调试面板和筛选器,你要确保resource.cfg中对此文件的指向是正确有效的。
- 在你的程序中用到的其他资源(*.zip; *.png; *.particle; *.mesh; ...)
- 第三方程序库
如果要从源代码构建(非SDK)的话,则需要下面这些包。你需要库文件(.lib; .a)和头文件(.h)来链接,需要运行时库(win32 .dll; linux .so)来运行。如果在Winodws平台上,我们极度推荐你使用precompiled dependecies,它可以通过Source releases获得。为你的编译器下载一个正确的包吧。
以下东西是必须的:
- Zlib: zlib1.dll; libz.co (debian; zlib1g, zlib1g-dev)
- Direct X SDK (样例程序所需的OSI库会需要这个)
以下东西是可选的:
- CEGUI: OgreGUIRenderer[_d].dll, CEGUIBase[_d].dll, CEGUITaharezLook[_d].dll, CEGUIWindowsLook[_d].dll, xerces-c_2_5_0.dll
- CEGUI: libCEGUIBase.so, libCEGUIOgreRenderer.so, libxerces-c.so (debian: libcegui-mk2-0, libcegui-mk2-dev, libxerces26, libxerces26-dev)
- Cg: cg.dll; libCg.so (debian: nvidia-cg-toolkit)
- OpenEXR: openexr.dll??; (debian: libopenexr-dev libopenexr2 )
- ReferenceApp: ReferenceAppLayer.dll
- Microsoft Visual C++ 6 (SP3+)的额外需求
- stlport_vc6[_stldebug]46.dll
- msvcp60[D].dll
- msvcrt[D].dll
- Microsoft Visual C++.Net 2002的额外需求
- stlport_vc7[_stldebug]46.dll
- msvcp70[d].dll
- msvcr70[d].dll
- Microsoft Visual C++.Net 2003的额外需求
- msvcp71[d].dll
- msvcr71[d].dll
- Mingw + STLPort的额外需求
- libstlport[stlg].5.0.dll
- mingwm10.dll
环境设置
你可能打算注册一些环境变量指向你安装OGRE的根目录,这样你可以把你的所有路径与这个变量关联。这样的话当你移动这些东西到别处或者别的机器上时可以非常简单的重新定位这些路径。关于查看与修改环境变量,请参阅以下文档:Windows, Linux以及 Mac OSX。
- 如果你下载并安装了预编译版(precompiled) OGRE SDK,那你应该已经拥有一个名为'OGRE_HOME'的环境变量了,它指向了你安装OGRE的那个文件夹。如果没有的话,你也许是在系统的另一个用户下安装的OGRE,那么手动定义一下 OGRE_HOME 环境变量就行了。
- 如果你下载的是源代码版本,你应该注册一个新的环境变量 OGRE_SRC 指向OGRE源代码中的'ogrenew'文件夹。
创建一个项目文件
这一小节并不深入探讨编译器/IDE集成开发环境的设置。它只是为了让你能够在很少的几个步骤之内就建立起来一个新项目。这应该在任何IDE集成开发环境中都是非常容易做到的。您将来的所有代码都会保存在如下的歌项目里。创建一个工作目录来存储您的源代码。一个符合标准的目录结构必须分离开头文件、源文件和工程文件,如下所示:
work_dir
include
*.h
src
*.cpp
scripts
*.vcproj
Microsoft Visual C++ 6 (SP3+)
Ogre并不向VC6提供支持。它是1998年甚至比那更古老的集成开发工具,它并不支持ISO C++,只会为你带来痛苦。
我们推荐您使用Visual C++ 2005 Express(又称VC8)。
如果你一定要使用那种上古之物的话,请看这里。你不会得到任何支持,相反大家都会劝你换个高版本。
Microsoft Visual C++ .NET
非常重要:
如果你使用的是Visual Studio 2005 (VC8),那么在进行下面的操作之前,请确保你已经安装了service pack #1。
提示: 如果你正在使用VC++ .NET 2003或者2005,你可以使用Ogre应用程序创建向导创建Ogre程序——就像Visual Studio自带的创建应用程序向导一样。你也可以参看从零开始学用Ogre应用程序创建向导。
另外你也可以按照如下的方法手动配置IDE:
- 创建一个'Visual C++ Projects' / 'Win32' / 'Win32 Project'类型的工程。
http://www.ogre3d.org/wiki/images/2/23/VC_shot01.png http://www.ogre3d.org/wiki/images/2/23/VC_shot02.png
- 给你的工程命名然后改变她的位置使她指向一个工作目录(和运行目录分开)。
- 选择'Windows Application'和'Empty project'来新建一个某个类型的工程。
http://www.ogre3d.org/wiki/images/6/6d/VC_shot03.png http://www.ogre3d.org/wiki/images/7/75/VC_shot04.png
- 通过选择'Project -> Add New Item...'来新建一个源代码文件。要确定文件是在工作目录 。这步必须在编辑工程属性之前完成(否则'C/C++'页无法获得)
- 保存工程。
如果你把工程命名为"Testproject"然后选择一个类似"C:\Work"这样的位置,再选择一个叫"Create directory for solution"选项,选择一个解决问题的名字叫"Testsolution",最后目录结构就变成了C:\Work\Testsolution\Testproject. 如果你没有为这个解决方案创建目录,这个结构就简化为C:\Work\Testproject。
要弄清DLL文件应该放在哪,一个简单的方法是从OGRE安装目录复制\bin文件夹(包含 \debug and \release)和\media文件夹到你的Testproject文件夹(注意:在这一步中你可能会复制一些没有用的DLLS,但是这就是一个自由的悖论。)如果你遵循上面所展示的约定并创建了一些文件夹来放置、脚本和源文件,你就拥有了如下文件夹在\Testproject下(不包括子文件夹):'bin', 'include', 'media', 'testsolution', 'scripts', 和 'src'. 但是,如果你不打算在你用VS运行Testproject的时候也要往Debug或者Release目录里复制这些东西的话,只需要把这些DLL文件的目录添加到项目的%PATH%系统变量中。如下所示: (按:这句翻译的不是很有把握。原文是 However, if you don't want to copy them while running your Testproject from Visual Studio just add the path to OGRE's DLL files to the %PATH% global environment variable like this:)
Debugging : Environment = PATH=%PATH%;%OGRE_HOME%\bin\debug
Realese版本:
Debugging : Environment = PATH=%PATH%;%OGRE_HOME%\bin\release
如果你这样做的话并不会永久性的修改%PATH%环境变量, 而只是在运行的时候才生效,所以它很安全。你也可以从你的Windows直接修改变量,就像这样:
My Computer -> Properties -> Advanced -> Environment Variables -> System Variables -> Path += ;%OGRE_HOME%\bin\debug
或者
My Computer -> Properties -> Advanced -> Environment Variables -> System Variables -> Path += ;%OGRE_HOME%\bin\release
但是这样做的话如果你安装了多个不同版本的OGRE就会发生混乱,这是不被推荐的。
如果你仔细遵循如下的几步,你的项目便会顺利通过编译。
按照下面的设置来配置你的工程。所有的这些都是为了Debug版本的配置。设置Release版本方式也是几乎相同的,仅仅把目录'\Debug\'换成'\Release\'就可以了。 选择"project properties"然后在"configuration properties"下添加如下设置:
Debugging : Command = $(OutDir)\$(ProjectName).exe Debugging : Working Directory = $(OutDir) C/C++ : Preprocessor : Preprocessor Definitions += _STLP_DEBUG (only in Debug mode, not needed for .Net 2003 and 2005) C/C++ : Code Generation : Use runtime library = Multithreaded Debug DLL (Multithreaded DLL in Release) Linker : General : Output File = ..\bin\Debug\[appname].exe
以下设置用于Ogre3D 1.4.x
Linker : Input : Additional Dependencies += OgreMain_d.lib OIS_d.lib (OgreMain.lib OIS.lib in Release)
如果你还用了CEGUI,你需要告诉Visual Studio链接下面的几个lib:
Linker : Input : Additional Dependencies += CEGUIBase_d.lib OgreGUIRenderer_d.lib (CEGUIBase.lib OgreGUIRenderer.lib in Release)
以下设置用于SDK:
C/C++ : General : Additional Include Directories = ..\include;$(OGRE_HOME)\include;$(OGRE_HOME)\samples\include Linker : General : Additional Library Directories = $(OGRE_HOME)\lib
或者以下设置用于源代码:
C/C++ : General : Additional Include Directories = ..\include;$(OGRE_SRC)\OgreMain\include;$(OGRE_SRC)\Samples\Common\Include Linker : General : Additional Library Directories = $(OGRE_SRC)\OgreMain\Lib\Debug
更多可以让你在IDE里顺利运行你的程序的提示请参考:
Visual Studio Debugging Settings
Microsoft Visual C++ 2008
暂时没有- -! (保留位置,等待添加。)
Code::Blocks + MinGW 3.4.5 + STLPort 5.0
Have a look at the build options for Demo_CameraTrack, most of the demos use the same build options.
Note: with MinGW gcc 3.4.5 you can link directly against dll's built by gcc 3.4.5 and do not require an import lib. This is the same setup on Linux where you link directly to the shared object (.so). This is why the SDK does not come with import libs for Ogre. You don't need them.
Build options that you should use:
Normally you will have two targets: Debug and Release.
To create a new target:
- Right click on the project in the Management window and select Properties in the popup and this will open up the properties window for your project.
- Select the Target tab.
- Select Add to define a new target.
- Type in the target name ie Debug.
- Select OK.
Select Build Options to modify build options for a target.
Project build options
will be used for both Debug and Release target build options:
ensure Selected compiler is: GNU GCC Compiler
- Compiler
- Other options
- -mthreads
- -fmessage-length=0
- -fexceptions
- -fident
- '#defines'
- _STLP_NO_CUSTOM_IO
If your app does not define any custom IO templates then this define will speed up compiles. - _STLP_USE_DYNAMIC_LIB
this ensures that the DLL version of STLPort is used.
- _STLP_NO_CUSTOM_IO
- Other options
- Linker
- Other linker options:
- -Wl,--enable-auto-image-base
- -Wl,--add-stdcall-alias
- Other linker options:
- Directories
- Compiler
- $(OGRE_HOME)\stlport\stlport
VERY IMPORTANT: The stlport include directory has to be first in the include directories. If it is not then headers used for libstdc++ could get included which will cause conflicts with stlport during the link stage. You will most likely get undefined reference error messages with member methods saying they require std::string (method parameters). You should never see std:: in an error message and if you do then it means that the STLPort headers were not found and libstdc++ headers were used instead. If you do end up in this scenerio, after you correct the directory problem you must do a rebuild so that everything gets recompiled with the proper headers. Selecting build won't do it since CB will just attempt to do the link stage again since it doesn't detect any changes in the source and will fail again. - $(OGRE_HOME)\include
- Your project include directories
- $(OGRE_HOME)\stlport\stlport
- Linker
- $(OGRE_HOME)\bin\$(TARGET_NAME)
this is the path to OgreMain.dll or OgreMain_d.dll along with stlport.5.0.dll or stlportstlg.5.0.dll depending on TARGET_NAME state. $(TARGET_NAME) is a built in Code::Blocks macro that you can use in your project settings. It gives you the active project target build name.
- $(OGRE_HOME)\bin\$(TARGET_NAME)
- Compiler
Debug build target options:
ensure Selected compiler is: GNU GCC Compiler
set Policy to 'Append target options to project options'
- Compiler
- '#defines'
- WIN32
- _DEBUG
- _WINDOWS
- _STLP_DEBUG
this tells STLPort to use the special debug build version of the dll
- '#defines'
- Linker
- Link libraries:
- Your project dll's or static libs (.a)
- OgreMain_d
this is the name of the debug build of OgreMain dll. - stlportstlg.5.0
this must be the last entry
- Link libraries:
Release build target options:
ensure Selected compiler is: GNU GCC Compiler
set Policy to 'Append target options to project options'
- Compiler
- '#defines'
- WIN32
- NDEBUG
- _WINDOWS
- '#defines'
- Linker
- Link libraries:
- Your project dll's or static libs (.a)
- OgreMain
this is the name of the release build of OgreMain dll. - stlport.5.0
this must be the last entry
- Link libraries:
Note: OGRE_HOME is a pre-defined environment variable setup by the SDK installer. OGRE_HOME points to the directory where the SDK is installed. You can use $(OGRE_HOME) within your build options when setting up paths to specific directories with the SDK that you will need to access.
WARNING Do not mix dll and static object builds using different STL implementations. Here is an example: You build a dll using stdlibc++ but your exe is built using STLPort. Everything that is c++ must be built using STLPort. This does not apply to C static libs and dll's.
Code::Blocks on Linux
See section 5 of the tutorial on the Ubuntu forums here.
Gnu Compiler Collection (gcc) v3.x (using make)
Make sure you are using gcc 3.x. Many Linux distributions still come with gcc 2.95.x which does not have as good support for the ISO C++ standard and will probably cause you problems. Please install the latest stable version of gcc 3.x.
Create a file called 'Makefile' in your application folder, with the following contents. Later you can replace SampleApp with your own program file name.
DEFINES = LIBS = OGRE CXX = g++ CXXFLAGS = $(shell pkg-config --cflags $(LIBS)) $(DEFINES) LD = g++ LDFLAGS = $(shell pkg-config --libs $(LIBS)) all: $(CXX) $(CXXFLAGS) $(LDFLAGS) -o SampleApp SampleApp.cpp clean: rm -f SampleApp
When you are ready to start working with multiple files and even incorporating CEGUI, your Makefile might look like the following example. Make will expect to find SampleApp.cpp, camera.cpp and listener.cpp:
## Compiler and linker settings DEFINES = -g LIBS = OGRE CEGUI-OGRE CEGUI CXX = g++ CXXFLAGS = $(shell pkg-config --cflags $(LIBS)) $(DEFINES) LD = g++ LDFLAGS = $(shell pkg-config --libs $(LIBS)) ## Files to include in yourApp project SAMPLEAPP = SampleApp.o camera.o listener.o ## Compilation and linking occurs here all: SampleApp $(LD) $(LDFLAGS) $(SAMPLEAPP) -o SampleApp SampleApp: $(SAMPLEAPP) ## Clean up clean: rm -f $(SAMPLEAPP) SampleApp
XCode
- In XCode, select menu File > New project > Carbon Application.
- Place your project wherever you want and name it, then click finish
- Remove main.nib and *_Prefix.pch (note: if you wish to use a precompiled prefix header, you may, just adapt the instructions as needed)
- rename main.c to main.cpp. Remove its content and replace it with Ogre code (for instance http://www.ogre3d.org/wiki/index.php/BasicTutorial2Source).
- Add Ogre.framework and Cg.framework to your project by right-clicking on your project name in the tree and selecting Add > Existing frameworks. Add the IOKit framework from system directories the same way.
- Add static library libois.a the same way (libFreeImage.a, libfreetype.a libzzip.a were also previously required but apparently are no more).
(These libraries are all provided in the Ogre dependencies download except Ogre.framework that you either got from the mac SDK or built from source.) (As of Ogre 1.4.5, I had better success with the CVS version of OIS on mac. OIS versions greater than 1.0 should be all okay though.)
Now you must add the required config files to your project. You can find these files in Ogre/Mac/Samples/config Just drag and drop them to the file tree and they should automatically be copied to the Resources directory of your app. Whenever you add data or source files to the project, make sure you check the copy items checkbox in the dialog that pops-up before confirming file addition because you don't want to modify the original files. There is no need to do that with libraries though.
- plugins.cfg
- media.cfg
- resources.cfg : you will need to edit this one to point inside your application. here is an example of resources.cfg file to work with the samples and tutorials
# Resource locations to be added to the 'boostrap' path # This also contains the minimum you need to use the Ogre example framework [Bootstrap] Zip=Contents/Resources/Media/packs/OgreCore.zip # Resource locations to be added to the default path [General] FileSystem=Contents/Resources/Media FileSystem=Contents/Resources/Media/fonts FileSystem=Contents/Resources/Media/materials/programs FileSystem=Contents/Resources/Media/materials/scripts FileSystem=Contents/Resources/Media/materials/textures FileSystem=Contents/Resources/Media/models FileSystem=Contents/Resources/Media/overlays FileSystem=Contents/Resources/Media/particle FileSystem=Contents/Resources/Media/gui FileSystem=Contents/Resources/Media/DeferredShadingMedia Zip=Contents/Resources/Media/packs/cubemap.zip Zip=Contents/Resources/Media/packs/cubemapsJS.zip Zip=Contents/Resources/Media/packs/dragon.zip Zip=Contents/Resources/Media/packs/fresneldemo.zip Zip=Contents/Resources/Media/packs/ogretestmap.zip Zip=Contents/Resources/Media/packs/skybox.zip
- Media : found in Ogre/Samples (if you want to run the samples or tutorials. make sure you create folder references and not group references - you can choose that in the dialog that pops-up just after you dragged and dropped the folder onto the tree)
- Open targets in the file tree
- Right-click on your target, select get info, go in tab build
- In configuration, make sure to select all configurations when you make changes that should apply to both debug and release
- In collection architectures, double-click on element Architectures in the list and select the architectures you wish to build (and have built Ogre for). If you build Universal binary, make sure the SDK path in build locations is a universal one (for instance /Developer/SDKs/MacOSX10.4u.sdk. This template should set it to universal by default.).
- In linking, disable ZeroLink
- In language, remove the file in Prefix Header and uncheck Precompile prefix header
- In search paths, add the path to includes provided in Ogre dependencies (double-click on header search paths, drag and drop the OgreDependencies-1.4.5/include folder there)
- Note: Be sure to leave the "Recursive" check box unchecked, otherwise you may experience compiler errors. (ex: http://www.ogre3d.org/phpBB2/viewtopic.php?p=279172#279172)
Existing projects will try to include Ogre.h and not <Ogre/Ogre.h> like frameworks usually require, so you may want to set up an additional header search path pointing inside your Ogre framework headers directory.
If you want to use the samples or tutorials you will also need to copy example application header files from /ogre-1.4.5/Samples/Common/include/
You will now need a copy files build step in your target:
- Right-click on your target
- select Add > New Build Phase > New Copy File Build Phase
- In the window that pops up, select Frameworks in the destination field, don't change any other settings.
- Now expand your target in the tree view and you should see the new step you just added as a child of your target, by befault named something like Copy Files.
- Drag and drop all Ogre-related frameworks (including those from Ogre dependencies) inside the new build step you just created.
- Warning, files you add to the project from now on will tend to add themselves to the copy files build step, make sure only frameworks find their way there.
You are now ready to build and run!
Once you're done, put a copy of your project in ~/Library/Application Support/Developer/Shared/Xcode/Project Templates/Application/, so next time you can just select New project and choose this one from the template list (in older versions of OSX/Xcode, the templates directoy was at ~/Library/Application Support/Apple/Developer Tools/Project Templates)
Autotools
To build your application using the ubiquitous GNU autotools (autoconf + automake + libtool), create these files in your project folder with the following content:
- bootstrap
#!/bin/sh rm -rf autom4te.cache libtoolize --force --copy && \ aclocal && \ autoheader && \ automake --add-missing --force-missing --copy --foreign && \ autoconf
- configure.ac
AC_INIT(configure.ac) AM_INIT_AUTOMAKE(SampleApp, 0.1) AM_CONFIG_HEADER(config.h) AC_LANG_CPLUSPLUS AC_PROG_CXX AM_PROG_LIBTOOL PKG_CHECK_MODULES(OGRE, [OGRE >= 1.2]) AC_SUBST(OGRE_CFLAGS) AC_SUBST(OGRE_LIBS) PKG_CHECK_MODULES(OIS, [OIS >= 1.0]) AC_SUBST(OIS_CFLAGS) AC_SUBST(OIS_LIBS) AC_CONFIG_FILES(Makefile include/Makefile src/Makefile) AC_OUTPUT
- Makefile.am
SUBDIRS = include src EXTRA_DIST = bootstrap AUTOMAKE_OPTIONS = foreign
- include/Makefile.am
noinst_HEADERS= SampleApp.h
- src/Makefile.am
bin_PROGRAMS= SampleApp SampleApp_CPPFLAGS= -I$(top_srcdir)/include SampleApp_SOURCES= SampleApp.cpp SampleApp_CXXFLAGS= $(OGRE_CFLAGS) $(OIS_CFLAGS) SampleApp_LDADD= $(OGRE_LIBS) $(OIS_LIBS)
See Your First Application or Minimal Application to create include/SampleApp.h and src/SampleApp.cpp.
You probably want to add plugins.cfg and resources.cfg to the executable folder (src) (see Prerequisites).
To build your app run ./bootstrap && ./configure && make. Subsequent builds only need running make.
To execute your app go to the src folder and run ./SampleApp.
Scons
Scons is a multiplatform building tool programmed in Python, designed to replace GNU Make and other similar tools (autoconf, automake, etc).
For compiling using scons, you'll only need to type:
scons
in your command line. The scons building tool will automatically check for all needed libraries and include files in your system before trying to compile.
For using scons, you'll need to create a SConstruct file. You'll had to pay a visit to scons.org to learn the directives for building with that tool. But here we provide a sample SConstruct file, that will build both in Ubuntu Linux and in Windows (using Visual Studio 2003 at least).
For a simple program composed by a file named main.cpp, using Ogre Eihort and OIS, this would be the SConstruct file needed:
platform = ARGUMENTS.get('OS', Platform())
mode = ARGUMENTS.get('mode', "release")
if platform.name == "linux":
listinc=[
# our main include directory
'include',
# location of ogre includes
'/usr/local/include/OGRE',
# location of ois includes
'/usr/local/include/OIS',
]
else:
listinc=[
# our main include directory
'include',
# location of ogre includes
'E:/ogre-eihort/ogrenew/OgreMain/include',
# location of ois includes
'E:/ogre-eihort/ogrenew/Dependencies/include/OIS',
]
if platform.name == "linux":
debugcflags=['-g']
releasecflags=[]
if mode=="debug":
env=Environment(CPPPATH=listinc, LIBPATH='.', CCFLAGS = debugcflags)
else:
env=Environment(CPPPATH=listinc, LIBPATH='.', CCFLAGS = releasecflags)
else:
debugcflags = ['-W1', '-GX', '-EHsc', '-D_DEBUG', '/MDd', '/Zi'] #extra compile flags for debug
releasecflags = ['-O2', '-EHsc', '-DNDEBUG', '/MD'] #extra compile flags for release
if mode=="debug":
env=Environment(CPPPATH=listinc, LIBPATH='bin/windows/debug'
, CCFLAGS = debugcflags, CPPDEFINES=["WIN32", "_DEBUG", "_WINDOWS"], LINKFLAGS='/DEBUG'
)
else:
env=Environment(CPPPATH=listinc, LIBPATH='bin/windows/release'
, CCFLAGS = releasecflags, CPPDEFINES=["WIN32", "NDEBUG", "_WINDOWS"]
)
# check for required include files and libraries
conf = Configure(env)
if not conf.CheckCXXHeader('Ogre.h'):
print 'Ogre must be installed!'
Exit(1)
if not conf.CheckCXXHeader('OISPrereqs.h'):
print 'OIS must be installed!'
Exit(1)
if platform.name == "linux":
if not conf.CheckLib('OgreMain'):
print 'OgreMain library must be in path'
Exit(1)
if not conf.CheckLib('OIS'):
print 'OIS library must be in path'
Exit(1)
# list of files needed for compiling the program
main_program_list=Split("""
src/main.cpp
""")
if platform.name == "linux":
# list of libraries needed for linking
libs_list=Split("""
OgreMain
OIS
""")
else:
# list of libraries needed for release
libs_release_list=Split("""
c:/ogre-eihort/ogrenew/OgreMain/lib/Release/OgreMain
c:/ogre-eihort/ogrenew/Dependencies/lib/Release/OIS
kernel32.lib
user32.lib
gdi32.lib
winspool.lib
comdlg32.lib
advapi32.lib
shell32.lib
ole32.lib
oleaut32.lib
uuid.lib
odbc32.lib
odbccp32.lib
""")
# list of libraries needed for debug
libs_debug_list=Split("""
c:/ogre-eihort/ogrenew/OgreMain/lib/Debug/OgreMain_d
c:/ogre-eihort/ogrenew/Dependencies/lib/Debug/OIS_d
kernel32.lib
user32.lib
gdi32.lib
winspool.lib
comdlg32.lib
advapi32.lib
shell32.lib
ole32.lib
oleaut32.lib
uuid.lib
odbc32.lib
odbccp32.lib
""")
if platform.name == "linux":
if mode=="debug":
env.Program('bin/linux/debug/main_d', main_program_list, LIBS=libs_list, LIBPATH='.')
else:
env.Program('bin/linux/release/main', main_program_list, LIBS=libs_list, LIBPATH='.')
else:
if mode=="debug":
env.Program('bin/windows/debug/main_d', main_program_list, LIBS=libs_debug_list, LIBPATH='.')
else:
env.Program('bin/windows/release/main', main_program_list, LIBS=libs_release_list, LIBPATH='.')
You might need to change the file paths in the file though.
Use:
- Put your main.cpp file in directory src; create the following empty directories:
include bin/windows/debug bin/windows/release bin/linux/debug bin/linux/release
- In linux, write any of the following lines in the command line:
scons OS=linux mode=debug # build in debug mode scons OS=linux # build in release mode scons OS=linux mode=debug -c # clean in debug mode scons OS=linux -c # clean in release mode
In windows, use the following lines:
- For debug build:
scons MSTOOLKIT=yes OS=windows mode=debug
- For release build:
scons MSTOOLKIT=yes OS=windows
- For debug clean:
scons MSTOOLKIT=yes OS=windows mode=debug -c
- For release clean:
scons MSTOOLKIT=yes OS=windows -c
Alternate Scons File
Please actually read this file, it's easy to understand and will require some modification for use.
# A Linux Ogre/CEGUI/OIS/OgreOde build Script by Grey, with many thanks to keir from #scons on freenode. # Setup our Build Environment, Smarter scripts might be able to change this at the command line, env = Environment( CCFLAGS='-ggdb -pg -g3 -DEXT_HASH', LDFLAGS='-pg' ) # Our External Libs # Got some fugly stuff ( || true ) in there to supress unreadable crashes, it # ends up using the nicer formatted error messages below env.ParseConfig('pkg-config --silence-errors --libs --cflags OGRE || true') env.ParseConfig('pkg-config --silence-errors --libs --cflags OIS || true') env.ParseConfig('pkg-config --silence-errors --libs --cflags CEGUI-OGRE || true') env.ParseConfig('pkg-config --silence-errors --libs --cflags OgreOde_Core || true') # Get out current config so we can verify we have everything we need. # There is an autoadd method here, but then we'd have to specify full paths for # the libs and headers, which is lame. config = Configure(env); # Everyone needs OIS :) if not config.CheckLibWithHeader('OIS', 'OISPrereqs.h', 'C++'): print 'OIS must be installed!' Exit(1) # this should work to get Ogre included if not config.CheckLibWithHeader( 'OgreMain', 'Ogre.h', 'C++' ): print "Ogre Must be installed!" Exit(1) # Any other component libraries you use can be added and tested in this manner if not config.CheckLibWithHeader( 'OgreOde_Core', 'OgreOde_Core.h', 'C++'): print 'OgreOde must be installed!' Exit(1); # Substitute with your GUI of choice if not config.CheckLibWithHeader('CEGUIBase', 'CEGUI.h', 'C++'): print "You need CEGUI to compile this program" Exit(1); if not config.CheckLibWithHeader('CEGUIOgreRenderer', 'OgreCEGUIRenderer.h', 'C++'): print "You need OGRE-CEGUI to compile this program" Exit(1); # Validate the configuration and assign it to our env env = config.Finish(); # Build our main program env.Program( target = 'main', # Replace these with your source files of course source = [ 'main.cpp', 'GUISystem.cpp', 'EventQueue.cpp', 'InputManager.cpp', 'CommandListener.cpp', 'CameraController.cpp', 'KeyMap.cpp', 'OdeManager.cpp', 'PhysModel.cpp', 'ODECharacterController.cpp', 'RayCharacterController.cpp', 'ChaseCameraController.cpp'])
Eclipse
See the Eclipse setup pages for Ogre IDE Eclipse.
Anjuta IDE
A complete working project and sample can be found here: http://waxor.com/page/ogre-dome.tgz
- Create a new Generic/Terminal App project.
- Choose c++ and Executable target when prompted.
- Delete all of the included template code in main.cc. It will be replaced with the sample code below.
- Go to the Settings Menu and choose Compilier and Linker Settings.
- Open the following tabs and add each entry one line at a time: replace $OGRE_HOME/ with the full path to the ogrenew folder you compiled from. Also note you may have Ogre in /usr/local/include and /usr/local/lib depending on how you installed it.
- Include Paths:
$OGRE_HOME/Samples/Common/include
/usr/include/OGRE (Not necessary if you include like this '#include <OGRE/Ogre.h>')
/usr/include/CEGUI
/usr/include/OIS
- Library paths:
/usr/local/lib
- Libraries:
OgreMain
CEGUIBase
CEGUIOgreRenderer
OIS
- Options
check "Enable Debugging" (adds -g to the command line)
- Options | Compilier flags (CFLAGS):
-DEXT_HASH
KDevelop IDE
KDevelop is a nice flexible IDE for Linux, and it is easy to set up with OGRE.
- Create a new C++ "Simple Hello world program" project.
- Open "configure.in" and add the following line above the AC_OUTPUT statement.
PKG_CHECK_MODULES(OGRE, [OGRE >= 1.2.0])
- Open "src/Makefile.am" and add the following lines.
LDADD = @OGRE_LIBS@ AM_CXXFLAGS = @OGRE_CFLAGS@
- To avoid linker errors, add these lines to src/Makefile.am
sampleapp_LDADD = /usr/local/lib/libOIS.so \ /usr/local/lib/libOgreMain.so
- You may need to change /usr/local/lib/ to the location of your OGRE/OIS libs. Also, you if you get errors about missing libs later, you will have to link/copy them to /lib or /lib64.
- You may wish to have the program run in the sample apps directory when debuging so you do not have to copy all the runtime resource files over. To do this go into project options (top menu) then click run options, then select the custom directory option and put in "<insert ogre root dir here>/Samples/Common/bin/"
- If you choose to derive your application from the ExampleApplication class that comes with OGRE, open the automake manager, right click on your target, choose "Add Existing Files..." and add the files ExampleFrameListener.h and ExampleApplication.h from ogrenew/Samples/Common/include.
- You also need to tell KDevelop where to find the OGRE specific headers. Click on Project->Project Settings->Configure Options, click on the C++ tab and add the following line to "Compiler flags (CXXFLAGS)" :
-I <insert ogre root dir here>OgreMain/include -I <insert ogre root dir here>OgreMain/include/GLX
- Insert the SampleApp code below for the application, build and run the project.
- To use the debugger, open the projects options, choose the debugger tab and select "Enable separate terminal for application IO".
在Linux上跨平台编译Win32程序
如何在Linux上跨平台编译Win32程序: 文章.
你的第一个应用程序
现在我们将会建立一个OGRE应用程序所需的最基本代码。这个程序,和其他样例程序一样,使用了样例程序框架。
Copy the following code and include it as a new file in your project settings. Following our conventions, you'd put it in work_dir/src and name it SampleApp.cpp. Since this is dependent upon ExampleApplication.h and ExampleFrameListener.h make sure these files are accessible by your project. Our convention would have you put them in work_dir/include. You can copy them from the Samples directory.
#include "ExampleApplication.h"
// Declare a subclass of the ExampleFrameListener class
class MyListener : public ExampleFrameListener
{
public:
MyListener(RenderWindow* win, Camera* cam) : ExampleFrameListener(win, cam)
{
}
bool frameStarted(const FrameEvent& evt)
{
return ExampleFrameListener::frameStarted(evt);
}
bool frameEnded(const FrameEvent& evt)
{
return ExampleFrameListener::frameEnded(evt);
}
};
// Declare a subclass of the ExampleApplication class
class SampleApp : public ExampleApplication
{
public:
SampleApp()
{
}
protected:
// Define what is in the scene
void createScene(void)
{
// put your scene creation in here
}
// Create new frame listener
void createFrameListener(void)
{
mFrameListener = new MyListener(mWindow, mCamera);
mRoot->addFrameListener(mFrameListener);
}
};
#ifdef __cplusplus
extern "C" {
#endif
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
#define WIN32_LEAN_AND_MEAN
#include "windows.h"
INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT)
#else
int main(int argc, char **argv)
#endif
{
// Instantiate our subclass
SampleApp myApp;
try {
// ExampleApplication provides a go method, which starts the rendering.
myApp.go();
}
catch (Ogre::Exception& e) {
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
MessageBoxA(NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
#else
std::cerr << "Exception:\n";
std::cerr << e.getFullDescription().c_str() << "\n";
#endif
return 1;
}
return 0;
}
#ifdef __cplusplus
}
#endif
Compile this code now. However before running the program, make sure you have a plugins.cfg and a resources.cfg in the same directory as the executable. Review the Prerequisites section for the purpose of these files. Edit them and make sure the paths are correct. Otherwise your OGRE setup dialog box may not have any rendering libraries in it, or you may recieve an error on your screen or in Ogre.log that looks something like this:
Description: ../../Media/packs/OgreCore.zip - error whilst opening archive: Unable to read zip file
When the program starts it will display the OGRE setup dialog and start the application with a blank, black screen containing little more than the OGRE logo and an FPS (frame per second) display. We haven't added anything to this scene yet, as evidenced by the empty createScene method. Press ESC to quit the application.
If you didn't get this, something is not right in your setup. See the Prerequisites and the Getting Help sections to review your installation.
The ExampleApplication framework will boot the OGRE system, displaying a configuration dialog, create a window, setup a camera and respond to the standard mouselook & WSAD controls. All you have to do is to fill in the 'createScene' implementation. If you want to do more advanced things like adding extra controls, choosing a different scene manager, setting up different resource locations, etc, you will need to override more methods of ExampleApplication and maybe introduce a subclass of ExampleFrameListener.
As mentioned before, you don't have to use the ExampleApplication and ExampleFrameListener base classes. Use them to work through the tutorials and to test things out. For larger projects you'll want to write your own framework, or use one of the frameworks or engines available on Projects using OGRE.
- Note for American readers
Sinbad the lead developer and creator of OGRE, is British. Naturally he uses British spellings such as "Colour", "Initialise" and "Normalise". Watch out for these spellings in the API.
See below to learn about your resources for getting help. Then your next step is to work through the Ogre Tutorials.
Getting Help
Probably the top two problems people have with Ogre are not being able to compile or a missing dependency. For the first, you are going to need to learn how to use your compiler. If you barely know C++ then expect a challenge, but don't give up! Thousands of people have successfully gotten Ogre to work with both the GCC and MSVC compilers, so look in the wiki and forums for what they have done that you haven't. For missing dependencies, these are libraries that aren't installed, that aren't linked against your program, or that aren't in your runtime path. Other dependencies are incorrect rendering plugins in your plugins.cfg file or incorrect paths in your resources.cfg file, or missing one of the files all together.
If you have problems reread this page as well as Installing An SDK and Building From Source and look in the Ogre.log file. You may also find your problem answered in the Build FAQ. If you need further help, search the forums. It is likely your problem has happened to others many times. If this is a new issue, read the forum rules then ask away. Make sure to provide relevant details from your Ogre.log, exceptions, error messages, and/or debugger back traces. Be specific and people will be more able to help you.



