Ogre3D如何正确渲染实体?

时间:2018-10-26 22:05:46

标签: c++ ogre

我从Ogre3D引擎开始,我一直在关注他们的教程,但是它有些过时并且使用了不推荐使用的东西,所以我尝试自己修复它们。我相信一切都可以正确编译和运行,网格模型可以正确加载,但是我所看到的只是一个黑屏。我不知道为什么并且无法弄清楚,我的代码:

#include <iostream>
#include <OgreConfigFile.h>
#include <OgreShaderGenerator.h>
#include <OgreSceneNode.h>
#include <OgreException.h>
#include <OgreRenderSystem.h>
#include <OgreTextureManager.h>
#include <OgreViewport.h>
#include <OgreEntity.h>
#include <OgreWindowEventUtilities.h>
#include "Window.h"

Window::Window() : mRoot(nullptr), mResourcesConfig(Ogre::BLANKSTRING), mPluginsConfig(Ogre::BLANKSTRING) {
    std::cout << "Creating window" << std::endl;
}

Window::~Window() {
}

bool Window::go() {
    #ifdef _DEBUG
        mResourcesConfig = "/home/wojciech/code/game/configs/resources_d.cfg";
        mPluginsConfig = "/home/wojciech/code/game/configs/plugins_d.cfg";
    #else
        mResourcesConfig = "/home/wojciech/code/game/configs/resources.cfg";
        mPluginsConfig = "/home/wojciech/code/game/configs/plugins.cfg";
    #endif

    mRoot = std::make_unique<Ogre::Root>(mPluginsConfig);
    Ogre::ConfigFile config;
    config.load(mResourcesConfig);

    Ogre::String name, locationType;

    Ogre::ConfigFile::SettingsBySection_ settingsBySection = config.getSettingsBySection();

    for (const auto &p : settingsBySection) {
        for (const auto &r : p.second) {
            locationType = r.first;
            name = r.second;
            Ogre::ResourceGroupManager::getSingleton().addResourceLocation(name, locationType);
        }
    }

    if(!(mRoot->restoreConfig() || mRoot->showConfigDialog(nullptr)))
        return false;

    mWindow = mRoot->initialise(true, "Test Game");

    Ogre::TextureManager::getSingleton().setDefaultNumMipmaps(5);
    Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();

    mSceneManager = mRoot->createSceneManager();
    mCamera = mSceneManager->createCamera("Main Camera");

    Ogre::Light* light = mSceneManager->createLight("Main Light");

    Ogre::Entity* ogreEntity = mSceneManager->createEntity("ogrehead.mesh");

    Ogre::SceneNode* ogreNode = mSceneManager->getRootSceneNode()->createChildSceneNode();
    ogreNode->attachObject(ogreEntity);
    ogreNode->setPosition(0, 0, 0);

    Ogre::SceneNode* cameraNode = mSceneManager->getRootSceneNode()->createChildSceneNode();

    cameraNode->attachObject(mCamera);
    light->setVisible(true);
    cameraNode->attachObject(light);
    cameraNode->setPosition(100, 100, 100);
    cameraNode->lookAt(Ogre::Vector3(0, 0, 0), Ogre::Node::TS_LOCAL);

    mSceneManager->setAmbientLight(Ogre::ColourValue(0.5, 0.5, 0.5));

    Ogre::Viewport* viewport = mWindow->addViewport(mCamera);
    viewport->setBackgroundColour(Ogre::ColourValue(0, 0, 0));

    mCamera->setNearClipDistance(1);
    mCamera->setFarClipDistance(10000);
    mCamera->setAspectRatio(Ogre::Real(viewport->getActualWidth()) / Ogre::Real(viewport->getActualHeight()));

    while(true) {
        Ogre::WindowEventUtilities::messagePump();

        if (mWindow->isClosed()) return false;

        if (!mRoot->renderOneFrame()) return false;
    }
}

0 个答案:

没有答案