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的入口.