启动第一个OGRE项目

转自OGRE官方手册:https://ogrecave.github.io/ogre/api/latest/setup.html

Setting up an OGRE project

Note

see Guide to building OGRE for instructions how to build OGRE itself

CMake Configuration

Ogre uses CMake as its build system. It is recommended that you use it in your project as well. Then all you need is to add the following lines to your project

# 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})

These settings include all available components and third party libraries OGRE depends on (e.g. boost) - nothing more to do.

If you installed OGRE in a non-standard path, you will have to set OGRE_DIR to the location of OGREConfig.cmake so find_package can figure out the rest.

For inspecting the detected OGRE installation, the following CMake variables are available

  • OGRE_STATIC - whether ogre was build as static lib
  • OGRE_${COMPONENT}_FOUND - ${COMPONENT} is available
  • OGRE_PLUGIN_DIR - The directory where the OGRE plugins are located
  • OGRE_MEDIA_DIR - The directory where the OGRE sample media is located
  • OGRE_CONFIG_DIR - The directory where the OGRE config files are located

Application skeleton

The easiest way to get started is the OgreBites Component. It handles Ogre startup/ tear down (including Ogre::OverlayRTSS), input using SDL2 and even includes a Simple GUI System.

This is useful if all you want is to get a Scene with a FPS counter up and running (rapid prototyping). If available it also uses SDL2 for input - you now just have to implement the callbacks.

To use it, simply derive from OgreBites::ApplicationContext and if you want to get input events from OgreBites::InputListener

class MyTestApp : public OgreBites::ApplicationContext, public OgreBites::InputListener

{

...

}

in the constructor we set our application name. The ogre configuration files will be stored in a system dependant location specific to our app.

MyTestApp::MyTestApp() : OgreBites::ApplicationContext("OgreTutorialApp")

{

}

to handle input events, we then override the according method

bool MyTestApp::keyPressed(const OgreBites::KeyboardEvent& evt)

{

if (evt.keysym.sym == OgreBites::SDLK_ESCAPE)

{

getRoot()->queueEndRendering();

}

return true;

}

the interesting part however is the setup method

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

The above code is explained in detail in Your First Scene.

finally we start everything as

int main(int argc, char *argv[])

{

MyTestApp app;

app.initApp();

app.getRoot()->startRendering();

app.closeApp();

return 0;

}

Note

You can find the full code of the above example at

  • Samples/Tutorials/Bootstrap.cpp for C++
  • Samples/Python/bites_sample.py for Python
  • Samples/AndroidJNI/MainActivity.java for Java (Android)

OgreBites itself is also a good starting point if you need more control over the Camera or the Window creation. For instance to render into an existing Qt Window.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值