第四章学习之前,先了解:
模板函数 template <class T> int fun(T a, T b);形式
模板类 template <class Type1, class Type2……….. >
Class name{private: Type1 fun(); }等
关键字class 也可以是 typename 。
Template <class type> class queitem;
Template <class T> class que
{private: queitem<type> *instant;}
实事上,que<int> a;que的int 传给了T, queitem<type>中type为T又传给了queitem的item
Vcproj:意思是C++工程文件扩展名VisualStudioProject,是一个XML文件,属性ProjectType意思是工程类型,默认是visual c++,Version 版本默认是7.1, Name表示工程名称等属性。
__declspec:是微软对CC++语言的扩展,__declspec(extended-attribute) extended-attribute若为dllexport,就是在函数或类定义前加 __declspec(dllexport) 就是指类或函数从dll中导出,以其它程序使用。
第四章 开始使用OGRE
OGRE最吸引人之一就是它的适用性很高,你可以在写自己的OGRE时多或者少的控制程序进行。就好象他提供自动行为,也允许你自己控制。
OGRE初始化:快速启动
创建任何的OGRE应用程序的第一件事就是创建Root实例。必须在调用任何OGRE操作之前。有以下几咱构造形式:
Root *root = new Root();
Root *root = new Root(“plugins.cfg”);
Root *root = new Root(“plugins.cfg”,”ogre.cfg”);
Root *root = new Root(“”, “”);
Root 类的构造函数是
Root(const String& pluginFileName = "plugins.cfg", const String& configFileName = "ogre.cfg", const String& logFileName = "Ogre.log");
Plugins.cfg的内容如下:
# Defines plugins to load
# Define plugin folder
PluginFolder=.
# Define plugins
Plugin=RenderSystem_Direct3D9
Plugin=RenderSystem_GL
Plugin=Plugin_ParticleFX
Plugin=Plugin_BSPSceneManager
Plugin=Plugin_OctreeSceneManager
Plugin=Plugin_CgProgramManager
(so是linux下的动态连接库)
# Defines plugins to load
# Define plugin folder
PluginFolder=/usr/local/lib/OGRE
# Define D3D rendering implementation plugin
Plugin=RenderSystem_GL.so
Plugin=Plugin_ParticleFX.so
Plugin=Plugin_BSPSceneManager.so
Plugin=Plugin_OctreeSceneManager.so
Plugin=Plugin_CgProgramManager.so
Ogre.cfg的内容如下:
Render System=OpenGL Rendering Subsystem
[Direct3D9 Rendering Subsystem]
Allow NVPerfHUD=No
Anti aliasing=None
Floating-point mode=Fastest
Full Screen=No
Rendering Device=NVIDIA GeForce Go 7600
VSync=No
Video Mode=800 x 600 @ 32-bit colour
[OpenGL Rendering Subsystem]
Colour Depth=32
Display Frequency=N/A
FSAA=0
Full Screen=No
RTT Preferred Mode=FBO
VSync=No
Video Mode=1024 x 768
Root *root = new Root(“”, “”);;的意思是:前两个参数没有,最后一个参数默认。最后一个参数日志文件是必不可少的。
Plugin.cfg是程序运行前装载入程序的.lib和.dll等插件的路径和文件名称。本地在ogrenew/Samples/Common/bin/Debug目录下。也不必在root构造的时候加入plugin.cfg文件,可以在程序任何时候加入。Root *root = new Root(“ ”,” ”);就是不加入plugin.cfg的意思。
PluginFolder=.意思是在plugin.cfg的当前路径下去找各插件。
如果只建立简单的场景,只须要OctreeSceneManager插件就可以了。
Ogre.cfg中的内容就是程序exe运行时,弹出要求选择哪种渲染系统,或进行全屏与否等选择的配置框。
因为Root *root = new Root();
Bool rtn = root->showConfigDialog();就会出现配置框。也可以自己写配置框简化。
ExampleApplication->go->setup->config中调用root 的showconfigdialog()
配置框先择OK则函数返回真。
装载已存在的orge.cfg语句是if ( !root->restoreConfig() ) root->showConfigDialog();saveconfig保存设置。
在root的构造函数中构造日志
mLogManager = new LogManager();mLogManager->createLog(logFileName, true, true);
可以用Ogre LogManager注册一个Log Listener, 以任何方式重定向log data。可以用这种方式来屏蔽任何日志信息。然后还一个更简单的方法达到上述目的:在实例化Root之前,当实例化一个LogManager后,不调用createLog()方法。
以下是实现日志信息截流的代码片断:
class MyLogListener : public LogListener
{
public:
void write (const String& name, const String& message,
LogMessageLevel level, bool maskDebug)
{
// redirect log output here as needed
};
MyLogListener *myListener = new MyLogListener;
// this is the same as calling LogManager::getSingletonPtr() after the
// LogManager has first been instanced; the same pointer value is returned
LogManager *logMgr = new LogManager;
LogMgr->addListener(myListener);
logMgr->createLog("mylog.log", true, false, true);
logMgr->setLogDetail(LL_NORMAL);
Root *root = new Root("", "", "mylog.log");
渲染系统选择后,则调用Root的初始化函数
Root->initialise(true, “my render window”);
RenderWindow *window = root->getAutoCreateWindow();
OGRE至少需要一个camera,和一个或多个viewport。使用SceneManager来创建Camera
Camera* SceneManager::createCamera(const String& name)
{
// Check name not used
if (mCameras.find(name) != mCameras.end())
{
OGRE_EXCEPT(
Exception::ERR_DUPLICATE_ITEM,
"A camera with the name " + name + " already exists",
"SceneManager::createCamera" );
}
Camera *c = new Camera(name, this);
mCameras.insert(CameraList::value_type(name, c));
return c;
}
由Root创建RenderWindow,再由RenderWindow创建ViewPort,如:
Viewport *vp = window->addViewport(camera);
Vp->setbackgroundColour(ColorValue(0,0,0));
RenderWindow类本身没有addViewport成员,因为RenderWindow是继承RenderTarget类,RenderTarget是有addViewport这个对象的。
RenderWindow* Root::createRenderWindow(const String &name, unsigned int width, unsigned int height,
bool fullScreen, const NameValuePairList *miscParams)
{
if (!mActiveRenderer)
{
OGRE_EXCEPT(Exception::ERR_NO_RENDERSYSTEM_SELECTED,
"Cannot create window - no render "
"system has been selected.", "Root::createRenderWindow");
}
RenderWindow* ret;
ret = mActiveRenderer->createRenderWindow(name, width, height, fullScreen, miscParams);
// Initialisation for classes dependent on first window created
if(!mFirstTimePostWindowInit)
{
oneTimePostWindowInit();
ret->_setPrimary();
}
return ret;
}