ogre 学习笔记 - Day 2

这篇博客详细介绍了Ogre3D引擎的BasicTutorial1源码,包括如何初始化应用、创建场景管理器、设置环境光、创建灯光和相机,以及加载实体。通过代码分析,揭示了Ogre3D的基本操作和架构,适合初学者入门。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ogre 学习笔记 - Day 2


打开项目工程,竟然没有HelloWorld,差评!
运行SampleBrowser,看看代码,发现很复杂,不适合入门。

看看在线文档,https://ogrecave.github.io/ogre/api/latest/tutorials.html

[Your First Scene]

在该文中提到了 : Samples/Tutorials/BasicTutorial1.cpp
为什么没有加入工程?
暂时忽略这个问题。直接看代码


BasicTutorial1.cpp

  int main(int argc, char **argv)
  {
      BasicTutorial1 app;
      app.initApp();
      app.getRoot()->startRendering();
      app.closeApp();

      return 0;
  }

从BasicTutorial1的代码里发现,initApp(), closeApp() 都没有进行重载。


BasicTutorial1 重载了 ApplicationContext::setup()

  void BasicTutorial1::setup()
  {
      // do not forget to call the base first
      ApplicationContext::setup();
      addInputListener(this);
      // ...
  }

在该函数里调用了 ApplicationContext::setup(),并创建了一些节点/对象。


Root* root = getRoot();
SceneManager* scnMgr = root->createSceneManager();

获取根节点,并创建一个场景管理器。
创建场景管理器?可以创建多个?有什么区别?


  RTShader::ShaderGenerator* shadergen = RTShader::ShaderGenerator::getSingletonPtr();
  shadergen->addSceneManager(scnMgr);

获取shader生成器,然后,shader生成器持有场景管理器?


  scnMgr->setAmbientLight(ColourValue(0.5, 0.5, 0.5));

设置环境光


  Light* light = scnMgr->createLight("MainLight");
  SceneNode* lightNode = scnMgr->getRootSceneNode()->createChildSceneNode();
  lightNode->attachObject(light);
  lightNode->setPosition(20, 80, 50);

创建灯光,创建灯光节点,并把灯光依附在灯光节点上。
从这个操作来看,采用了组合代替继承


SceneNode* camNode = scnMgr->getRootSceneNode()->createChildSceneNode();

// create the camera
Camera* cam = scnMgr->createCamera("myCam");
cam->setNearClipDistance(5); // specific to this sample
cam->setAutoAspectRatio(true);
camNode->attachObject(cam);
camNode->setPosition(0, 0, 140);

创建/设置相机。


getRenderWindow()->addViewport(cam);

获取渲染窗口,并设置视口


Entity* ogreEntity = scnMgr->createEntity("ogrehead.mesh");
SceneNode* ogreNode = scnMgr->getRootSceneNode()->createChildSceneNode();
ogreNode->attachObject(ogreEntity);

创建实体(Mesh),并附加到一个节点上。


BasicTutorial1 重载了 InputListener::keyPressed

bool BasicTutorial1::keyPressed(const KeyboardEvent& evt)

解决上面产生的疑问

粗略看了一下 SceneManager 的函数,发现这个类应该是"Scene",而不是"Scene Manager"

struct InputListener(它是个结构体?) 输入监听器,监听按键/鼠标等的输入事件。这里采用的是继承的方式。

ApplicationContext 继承自 class FrameListener,帧事件监听器。这个Listener又是个class。

class _OgreExport FrameListener
{
   virtual bool frameStarted(const FrameEvent& evt);
   virtual bool frameRenderingQueued(const FrameEvent& evt);
   virtual bool frameEnded(const FrameEvent& evt);
}

看看其他的Listener,发现只有 InputListener, ImGuiInputListener 是结构体。?

每个Listener都有对特定的事件进行响应。


void ApplicationContextBase::initApp()
{
    createRoot();
    if (!oneTimeConfig()) return;

#if OGRE_PLATFORM != OGRE_PLATFORM_ANDROID
    // if the context was reconfigured, set requested renderer
    if (!mFirstRun) 
        mRoot->setRenderSystem(mRoot->getRenderSystemByName(mNextRenderer));
#endif

    setup();

#if OGRE_PLATFORM == OGRE_PLATFORM_APPLE_IOS
    // ...
#endif
}

createRoot 根据文档介绍

The Ogre::Root object is the entry point to the OGRE system. This object MUST be the first one to be created, and the last one to be destroyed.

Root对象是Ogre的入口.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值