ogre3D学习基础17 --- 如何手动创建ogre程序
建立自己的Ogre程序
一直以来都是使用ExampleApplication.h来写程序,现在来看看它到底有什么神奇的地方。
首先,我们新建一个win32空项目
然后配置环境
最后新建define.cpp、define.h、main.cpp
具体过程如下
第一,在define.h中包含头文件,这里需要Orge.h文件,然后添加如下代码
#include "Ogre.h"
class MyApplication
{
public:
MyApplication();
~MyApplication();
void go();
protected:
bool setup();//设置环境
void setupResources();//设置资源
bool configure();//配置窗口
void chooseSceneManager();//选择场景
void createCamera();//创建摄像机
void createViewports();//创建视口
void loadResources();//加载资源
void createScene();//创建场景
void renderOneFrame();
private:
Ogre::Root*mRoot;//根节点
Ogre::RenderWindow*mWindow;//渲染窗口
Ogre::SceneManager*mSceneMgr;//场景管理器指针
Ogre::Camera*mCamera;//摄像机
bool mShutDowm;
};
第二,在define.cpp文件中定义函数,代码如下
#include "define.h" MyApplication::MyApplication()//构造函数
{
mRoot = NULL;
mWindow = NULL;
mSceneMgr = NULL;
mCamera = NULL;
mWindow = NULL;
mShutDowm = false;
}
MyApplication::~MyApplication()//析构函数
{
if (mRoot)
{
delete mRoot;
}
}
bool MyApplication::setup()
{
mRoot = new Ogre::Root("plugins_d.cfg");
setupResources();
if (!configure())
{
return false;
}
chooseSceneManager();
createCamera();
createViewports();
loadResources();
createScene();
return true;
}
void MyApplication::setupResources()
{
// 加载资源,ConfigFile类是Ogre中用来读取和解析脚本使用的格式的
Ogre::ConfigFile cf;
cf.load("resources_d.cfg");
//循环遍历资源
Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();// 获取迭代器
Ogre::String secName, typeName,archName;
while (seci.hasMoreElements())//组名
{
secName = seci.peekNextKey();
Ogre::ConfigFile::SettingsMultiMap *settings =seci.getNext();
Ogre::ConfigFile::SettingsMultiMap::iterator i;
for (i =settings->begin();i != settings->end(); ++i)
{
typeName = i->first;//键名
archName = i->second;//键值
Ogre::ResourceGroupManager::getSingleton().addResourceLocation(archName,typeName, secName);
}
}
}
bool MyApplication::configure()
{
if (!mRoot->showConfigDialog())//是否显示配置窗口
{
return false;
}
mWindow = mRoot->initialise(true,"IMedia Project");
return true;
}
void MyApplication::chooseSceneManager()
{
mSceneMgr = mRoot->createSceneManager(Ogre::ST_GENERIC);
}
void MyApplication::createCamera()
{
mCamera = mSceneMgr->createCamera("MyCamera");
mCamera->setPosition(Ogre::Vector3(,,));
mCamera->lookAt(Ogre::Vector3(,,-));
mCamera->setNearClipDistance();
}
void MyApplication::createViewports()
{
Ogre::Viewport*vp = mWindow->addViewport(mCamera);
vp->setBackgroundColour(Ogre::ColourValue(,,));
mCamera->setAspectRatio(Ogre::Real(vp->getActualWidth())/Ogre::Real(vp->getActualHeight()));
}
void MyApplication::loadResources()
{
Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
}
void MyApplication::createScene()
{
Ogre::Entity*ent = mSceneMgr->createEntity("Sinbad.mesh");
mSceneMgr->getRootSceneNode()->attachObject(ent);
}
void MyApplication::go()
{
if (!setup())
return;
mRoot->startRendering();
}
在setup函数中完成了一系列的初始化操作,其中的setupResource()函数解释一下,ConfigFile类是Ogre中用来读取和解析脚本使用的格式的,我们用它来加载资源脚本,load函数定义如下:
void load(const String& filename, const String& separators = "\t:=", bool trimWhitespace = true);//使用文件名加载文件
接下来的while循环是循环,while循环负责组名的遍历,内部的for循环负责键名与键值的读取。

第三,我们在main.cpp文件中添加WinMain函数
#include <windows.h>
#include "define.h" INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
{
MyApplication app;
app.go();
return ;
}
这里是我们所需要的最少的代码。如此就可以保证程序跑起来,但还需要继续改进。运行就可以看到结果。
现在我们试试把鼠标和键盘监听添加进去
第一添加新的父类,包含头文件OIS.h
class MyApplication:public Ogre::FrameListener,public OIS::KeyListener,public OIS::MouseListener
{
.......................
}
第二,添加成员函数以及成员变量
Public: void createFrameListener(void);//创建帧监听器 virtual bool frameRenderingQueued(const Ogre::FrameEvent& evt);//OGRE virtual bool keyPressed(const OIS::KeyEvent &arg);//ois key virtual bool keyReleased(const OIS::KeyEvent &arg);//key virtual bool mouseMoved( const OIS::MouseEvent &arg );//mouse virtual bool mousePressed( const OIS::MouseEvent &arg, OIS::MouseButtonID id ); virtual bool mouseReleased( const OIS::MouseEvent &arg, OIS::MouseButtonID id ); Private: OIS::InputManager *minputManager;//输入管理器 OIS::Mouse *mMouse;//鼠标 OIS::Keyboard *mKeyboard;//键盘
第三,函数定义,虚函数并没有添加其他代码,你可以直接添加需要的动作
void MyApplication::createFrameListener()
{
OIS::ParamList pl;
size_t windowHnd = ;
std::ostringstream windowHndStr;
mWindow->getCustomAttribute("WINDOW", &windowHnd);//获取自己定义的属性
windowHndStr << windowHnd;
pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
minputManager = OIS::InputManager::createInputSystem( pl );//依照系统环境创建一个合适的输入系统
mKeyboard=static_cast<OIS::Keyboard*>(minputManager->createInputObject( OIS::OISKeyboard, true ));//创建输入对象
mMouse = static_cast<OIS::Mouse*>(minputManager->createInputObject( OIS::OISMouse, true )); mMouse->setEventCallback(this);
mKeyboard->setEventCallback(this);
mRoot->addFrameListener(this); }
bool MyApplication::frameRenderingQueued(const Ogre::FrameEvent& evt)
{
if (mWindow->isClosed())
{
return false;
}
if (mShutDowm)
{
return false;
}
mKeyboard->capture();
mMouse->capture();
return true;
}
bool MyApplication::keyPressed(const OIS::KeyEvent &arg)
{
if (arg.key == OIS::KC_ESCAPE)
{
mShutDowm = true;
}
return true;
}
bool MyApplication::keyReleased(const OIS::KeyEvent &arg)
{
return true;
}
bool MyApplication::mouseReleased( const OIS::MouseEvent &arg, OIS::MouseButtonID id )
{
return true;
}
bool MyApplication::mousePressed( const OIS::MouseEvent &arg, OIS::MouseButtonID id )
{
return true;
}
bool MyApplication::mouseMoved( const OIS::MouseEvent &arg )
{ return true;
}
代码解释:
这里看一下createFrameListener()函数,其中 ParamList是一个map类型,定义如下:
typedef std::multimap<std::string, std::string> ParamList;
然后是创建输入对象,第一个参数指出了对象的类型
Object* createInputObject( Type iType, bool bufferMode, const std::string &vendor = "");
第四,记得在析构函数中清空对象
if (minputManager)
{
minputManager->destroyInputObject(mKeyboard);
minputManager->destroyInputObject(mMouse);
OIS::InputManager::destroyInputSystem(minputManager);
minputManager = ;
}
并在setup()函数中调用createFrameListener()函数
bool MyApplication::setup()
{
.........
createFrameListener();//
return true;
}
现在可以实现我们以前用ExampleApplication.h才能实现的功能了,ogre程序的运行过程就是这样。
ogre3D学习基础17 --- 如何手动创建ogre程序的更多相关文章
- ogre3D学习基础16 -- 手动创建实体(ManualObject)
这一节练习一下手动创建实体,用到了对象(ManualObject) 第一,依然是模板 #include "ExampleApplication.h" class Example1 ...
- ogre3D学习基础18 -- 材质的使用与脚本的简单书写
这一节以基础16为基础,练习材质的使用. 第一,看看框架 //material #include "ExampleApplication.h" class TutorialAppl ...
- ogre3D学习基础6---场景管理器的使用
场景管理器的使用 最常使用的坐标系统空间(同时也是Ogre程序所能提供的)即是世界空间(World).父节点空间(Parent)以及本地空间(Local). 1.世界空间 就是物体所存在的地方,当我们 ...
- maven学习(二)——手动创建maven项目
一.Maven的简单使用 1.Maven项目的目录约定 MavenProjectRoot(项目根目录) |----src | |----main | | | ...
- ogre3D学习基础15 -- 创建BSP Scene Manager
BSP(binary-space partitioning) Scene Manager(二叉空间分割)场景管理器比较适合用于室内场景. 第一,添加框架代码如下 #include "Exam ...
- ogre3D学习基础5 -- 阴影与动画
五.阴影 阴影是渲染一个真实场景的重要组成部分,它可以给场景中的物体提供更加真实的感觉,同时还可以帮助用户更好的了解对象间的空间关系. 启用阴影: 缺省情况下,阴影是关闭的,开启方式如下: 1.建立场 ...
- ogre3D学习基础1 -- 核心对象与脚本技术
一.核心对象介绍1.命名空间 Ogre3d使用了C++的特性--命名空间,可以防止命名混淆.使用方法也简单,using namespace Ogre;或者直接在使用时加上“Ogre::”的前缀,如Og ...
- ogre3D学习基础13 -- 键盘控制网格动画mesh
以上一节为蓝本,这里增加一点难度,添加了四个节点,增加键盘控制移动速度,使用bool变量控制是否移动. 第一,要增加键盘控制,那就使用OIS::KeyListener,在监听器里添加一个父类KeyLi ...
- ogre3D学习基础3 -- 粒子与表层脚本
9.粒子脚本 粒子脚本允许你实例化地在你的脚本代码中定义粒子系统,而不必在源代码中进行设置,使得你做任何修改都能得到快速回应.脚本里定义的粒子系统被用作模板,并且多个实际的系统可以在运行时从这里被创建 ...
随机推荐
- ArcGIS API for JavaScript开发初探——HelloMap
1.前言 在开始ArcGIS API for JavaScript开发之前我们需要了解一些基本的知识: 1.开发工具选什么? 前端技术的开发工具选择是一个仁者见仁智者见智的问题,有人喜欢Hbuilde ...
- 【迷你微信】基于MINA、Hibernate、Spring、Protobuf的即时聊天系统:5.技术简介之Hibernate
目录 序言 配置 hibernate.cfg.xml配置文件 加载hibernate.cfg.html配置文件并获取Session 对象的注解配置 增删改查 具体的增删改查代码 数据库操作的封装 连接 ...
- 绿盟堡垒机云服务(vSAS-H)
绿盟堡垒机云服务(vSAS-H) 平台: linux 类型: 虚拟机镜像 软件包: basic software devops nsfocus security 堡垒机 服务优惠价: 按服务商许可协议 ...
- C# linq根据自定义筛选条件和所对应的数值进行筛选
在软件应用中有时候会出现这样的界面:上面是利用多选框和下拉框组合的筛选条件.下面表格展示筛选后的数据.如下图 上面是筛选条件,表格是根据筛选条件筛选的结果. 如果表格不支持筛选功能.可以利用Linq对 ...
- LeetCode Longest Substring Without Repeating Characters 最长不重复子串
题意:给一字符串,求一个子串的长度,该子串满足所有字符都不重复.字符可能包含标点之类的,不仅仅是字母.按ASCII码算,就有2^8=128个. 思路:从左到右扫每个字符,判断该字符距离上一次出现的距离 ...
- SSH框架快速搭建(Maven)
1.新建Maven项目ssh 2.pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=& ...
- Mybatis generator(复制粘贴完成)
命令行模式 1.java -jar mybatis-generator-core-x.x.x.jar -configfile generatorConfig.xml 2.Maven plugin(my ...
- Rhadoop安装
1.ubuntu,hadoop,R,jdk安装好 2.下载Rhadoop项目的的三个包,rmr,hdfs,rHBase存放到Downloads/R. 3.切换到root 4.安装依赖的库 ~R CMD ...
- React后台管理系统-rich-editor组件
1.Simditor组件的github地址:https://github.com/mycolorway/simditor 网址:http://simditor.tower.im/ 2.在util里边新 ...
- [牛客OI测试赛2]F假的数学游戏(斯特灵公式)
题意 输入一个整数X,求一个整数N,使得N!恰好大于$X^X$. Sol 考试的时候只会$O(n)$求$N!$的前缀和啊. 不过最后的结论挺好玩的 $n! \approx \sqrt{2 \pi n} ...