本文翻译自OGRE 1.12.0
notes:
有关如何构建OGRE本身的说明,请参阅GORE构建指南
CMake Configure(配置)
Ogre使用CMake作为其构建系统。建议你也在项目中使用它。然后你只需要在你的项目中添加以下行
# specify which version and components you need()
find_package(OGRE 1.11 REQUIRED COMPONENTS Bites RTShaderSystem)
# copy resource.cfg next to our binaries where OGRE looks for it
file(COPY ${OGRE_CONFIG_DIR}/resources.cfg DESTINATION ${CMAKE_BINARY_DIR})
# add the source files as usual
add_executable(0_Bootstrap Bootstrap.cpp)
# this also sets the includes and pulls third party dependencies
target_link_libraries(0_Bootstrap OgreBites OgreRTShaderSystem)
这些设置已包含组件所依赖的任何第三方库(例如SDL) - 无需再做其他事情。或者,使用$ {OGRE_LIBRARIES}链接所有可用的OGRE组件。
如果您在非标准路径中安装了OGRE,则必须将OGRE_DIR设置为OGREConfig.cmake的位置,以便find_package可以找出其余部分。
为了检查检测到的OGRE安装,可以使用以下CMake变量
- OGRE_TSATIC : 是否将ogre构建为静态库
- OGRE_${COMPONENT}_FOUND : ${COMPONENT}可用
- OGRE_PLUGIN_DIR : OGRE插件所在的目录
- OGRE_MEDIA_DIR : OGRE样本媒体所在的目录
- OGRE_CONFIG_DIR : OGRE配置文件所在的目录
Application skeleton
最简单的入门方法是OgreBites组件。它处理Ogre启动/拆除(包括Ogre :: Overlay,RTSS),使用SDL2输入,甚至包括一个简单的GUI系统。
如果你只想获得一个启动并运行FPS计数器的场景(快速原型设计),这将非常有用。如果可用,它还使用SDL2进行输入 - 你现在只需要实现回调。
要使用它,只需从OgreBites :: ApplicationContext派生,如果你想从OgreBites :: InputListener获取输入事件
class MyTestApp : public OgreBites::ApplicationContext, public OgreBites::InputListener
{
...
}
在构造函数中,我们设置了应用程序名称。ogre配置文件将存储在特定于我们的应用程序的系统相关位置。
MyTestApp::MyTestApp() : OgreBites::ApplicationContext("OgreTutorialApp")
{
}
为了处理输入事件,我们覆盖相应的方法
bool MyTestApp::keyPressed(const OgreBites::KeyboardEvent& evt)
{
if (evt.keysym.sym == OgreBites::SDLK_ESCAPE)
{
getRoot()->queueEndRendering();
}
return true;
}
然而,有趣的部分是setup函数
void MyTestApp::setup(void)
{
// do not forget to call the base first
OgreBites::ApplicationContext::setup();
// register for input events
addInputListener(this);
// get a pointer to the already created root
Ogre::Root* root = getRoot();
Ogre::SceneManager* scnMgr = root->createSceneManager();
// register our scene with the RTSS
Ogre::RTShader::ShaderGenerator* shadergen = Ogre::RTShader::ShaderGenerator::getSingletonPtr();
shadergen->addSceneManager(scnMgr);
// without light we would just get a black screen
Ogre::Light* light = scnMgr->createLight("MainLight");
Ogre::SceneNode* lightNode = scnMgr->getRootSceneNode()->createChildSceneNode();
lightNode->setPosition(0, 10, 15);
lightNode->attachObject(light);
// also need to tell where we are
Ogre::SceneNode* camNode = scnMgr->getRootSceneNode()->createChildSceneNode();
camNode->setPosition(0, 0, 15);
camNode->lookAt(Ogre::Vector3(0, 0, -1), Ogre::Node::TS_PARENT);
// create the camera
Ogre::Camera* cam = scnMgr->createCamera("myCam");
cam->setNearClipDistance(5); // specific to this sample
cam->setAutoAspectRatio(true);
camNode->attachObject(cam);
// and tell it to render into the main window
getRenderWindow()->addViewport(cam);
// finally something to render
Ogre::Entity* ent = scnMgr->createEntity("Sinbad.mesh");
Ogre::SceneNode* node = scnMgr->getRootSceneNode()->createChildSceneNode();
node->attachObject(ent);
}
Note:
上面的代码将在下一节详细说明。
最后我们在main()函数中
int main(int argc, char *argv[])
{
MyTestApp app;
app.initApp();
app.getRoot()->startRendering();
app.closeApp();
return 0;
}
Note:
您可以在以下位置找到上述示例的完整代码
Samples/Tutorials/Bootstrap.cpp
for C++Samples/Python/bites_sample.py
for PythonSamples/AndroidJNI/MainActivity.java
for Java (Android)
如果您需要对Camera或Window创建进行更多控制,OgreBites本身也是一个很好的起点。例如,渲染到现有的Qt窗口。
see also
Ogre::FileSystemLayer::getConfigFilePath
Ogre::RenderSystem::_createRenderWindow
Ogre::RenderSystem::preExtraThreadsStarted
Running your App
在Linux上,您通常会将OGRE安装到/ usr / local /中,并由链接器自动搜索,因此无需再做其他事情。但是,在Windows上,您必须将sdk / bin文件夹添加到PATH或将可执行文件复制到sdk / bin。
附上我自己的代码
#include <OgreInput.h>
#include<OgreApplicationContext.h>
#include<OgreRoot.h>
#include<OgreSceneNode.h>
#include<OgreCamera.h>
#include<OgreRenderWindow.h>
#include<OgreEntity.h>
#include<OgreMesh.h>
#include<OgreMeshManager.h>
#include<OgrePlane.h>
#include<OgreVector.h>
#include<OgreViewport.h>
using namespace Ogre;
class MyTestApp :public OgreBites::ApplicationContext, public OgreBites::InputListener
{
public:
MyTestApp();
~MyTestApp();
bool keyPressed(const OgreBites::KeyboardEvent& evt);
void setup(void);
};
MyTestApp::MyTestApp() :OgreBites::ApplicationContext("OgreTurialApp")
{
}
MyTestApp::~MyTestApp()
{
}
bool MyTestApp::keyPressed(const OgreBites::KeyboardEvent& evt)
{
if (evt.keysym.sym == OgreBites::SDLK_ESCAPE)
{
getRoot()->queueEndRendering();
}
return true;
}
void MyTestApp::setup(void)
{
// do not forget to call the base first
OgreBites::ApplicationContext::setup();
// register for input events
addInputListener(this);
// get a pointer to the already created root
Ogre::Root* root = getRoot();
Ogre::SceneManager* scnMgr = root->createSceneManager();
// register our scene with the RTSS
Ogre::RTShader::ShaderGenerator* shadergen = Ogre::RTShader::ShaderGenerator::getSingletonPtr();
shadergen->addSceneManager(scnMgr);
// without light we would just get a black screen
scnMgr->setAmbientLight(ColourValue(0.5, 0.5, 0.5));
Ogre::Light* light = scnMgr->createLight("MainLight");
Ogre::SceneNode* lightNode = scnMgr->getRootSceneNode()->createChildSceneNode();
lightNode->setPosition(20, 80, 222);
lightNode->attachObject(light);
// also need to tell where we are
Ogre::SceneNode* camNode = scnMgr->getRootSceneNode()->createChildSceneNode();
camNode->setPosition(0, 10, 100);
camNode->lookAt(Ogre::Vector3(0, 0, -1), Ogre::Node::TS_PARENT);
// create the camera
Ogre::Camera* cam = scnMgr->createCamera("myCam");
cam->setNearClipDistance(5); // specific to this sample
cam->setAutoAspectRatio(true);
camNode->attachObject(cam);
// and tell it to render into the main window
Viewport* vp = getRenderWindow()->addViewport(cam);
vp->setBackgroundColour(ColourValue(0, 0, 0));
cam->setAspectRatio(Real(vp->getActualWidth()) / Real(vp->getActualHeight()));
// finally something to render
Ogre::Entity* ent = scnMgr->createEntity("Sinbad.mesh");
ent->setCastShadows(true);
Ogre::SceneNode* node = scnMgr->getRootSceneNode()->createChildSceneNode(Vector3(0, 0, 40));
node->attachObject(ent);
}
int main(int argc, char *argv[])
{
MyTestApp app;
app.initApp();
app.getRoot()->startRendering();
app.closeApp();
return 0;
}