ogre_v13.6.1的Tutorial例子

这里使用的是Ogre官网提供的预编译的ogre-sdk-v13.6.1-msvc141-x64,假设解压在E:\ogre-sdk-v13.6.1-msvc141-x64, 环境变量设置

OGRE_HOME e:\ogre-sdk-v13.6.1-msvc141-x64

OGRE_CONFIG_DIR e:\ogre-sdk-v13.6.1-msvc141-x64\bin

PATH 添加: %OGRE_HOME%\bin

 

VC2019 C++ 控制台程序, toolset设置 VC142

属性页下面的配置: Release

调试->环境:  PATH=$(OGRE_HOME)\bin;$(PATH)

C/C++->常规->附加包含目录:  $(OGRE_HOME)\include;$(OGRE_HOME)\include\OGRE;$(OGRE_HOME)\include\OGRE\Bites;$(OGRE_HOME)\include\OGRE\RTShaderSystem;%(AdditionalIncludeDirectories)

C/C++->代码生成->运行库: 多线程 DLL (/MD)

连接器->常规-> 附加库目录:      $(OGRE_HOME)\lib

连接器->输入-> 附加依赖项:OgreBites.lib;OgreMain.lib;OgreRTShaderSystem.lib;%(AdditionalDependencies)

将下面的 txt内容拷贝为ogre.cfg 保存在 E:/ogrehome/

Render System=OpenGL 3+ Rendering Subsystem

[OpenGL Rendering Subsystem]
Colour Depth=
Display Frequency=N/A
FSAA=0
Fixed Pipeline Enabled=Yes
Full Screen=No
RTT Preferred Mode=FBO
VSync=Yes
VSync Interval=1
Video Mode= 800 x  600
sRGB Gamma Conversion=No

[OpenGL 3+ Rendering Subsystem]
Colour Depth=
Debug Layer=Off
Display Frequency=N/A
FSAA=0
Full Screen=No
Reversed Z-Buffer=No
Separate Shader Objects=Yes
VSync=Yes
VSync Interval=1
Video Mode= 800 x  600
sRGB Gamma Conversion=No

[Tiny Rendering Subsystem]
Full Screen=No
VSync=Yes
VSync Interval=1
Video Mode=800 x 600
sRGB Gamma Conversion=No

[Vulkan Rendering Subsystem]
Device=NVIDIA GeForce RTX 3060
FSAA=1
Full Screen=No
Reversed Z-Buffer=No
VSync=Yes
VSync Interval=1
Video Mode=800 x 600
sRGB Gamma Conversion=No

运行结果为:

 

main.cpp 内容

//! [starter]

#include <exception>
#include <iostream>

#include "Ogre.h"
#include "OgreApplicationContext.h"
#include "OgreInput.h"
#include "OgreRTShaderSystem.h"
#include "OgreCameraMan.h"


using namespace Ogre;
using namespace OgreBites;

class TutorialApplication
        : public ApplicationContext
        , public InputListener
{
public:
    TutorialApplication();
    virtual ~TutorialApplication();
    
    virtual void setup();
    
    bool keyPressed(const KeyboardEvent& evt);

    virtual bool mouseMoved(const MouseMotionEvent& evt)
    {
        return mCamMan->mouseMoved(evt);
    }

    virtual bool mouseWheelRolled(const MouseWheelEvent& evt)
    {
        return mCamMan->mouseWheelRolled(evt);
    }

    virtual bool mousePressed(const MouseButtonEvent& evt)
    {
        return mCamMan->mousePressed(evt);
    }

    virtual bool mouseReleased(const MouseButtonEvent& evt)
    {

        return mCamMan->mouseReleased(evt);
    }

    virtual void frameRendered(const Ogre::FrameEvent& evt)
    {
        mCamMan->frameRendered(evt);
    }

    virtual void windowResized(Ogre::RenderWindow* rw)
    {
        Viewport* vp = rw->getViewport(0);
        Ogre::Camera* cam = dynamic_cast<Ogre::Camera*>(mCamMan->getCamera()->getAttachedObject(0));
        cam->setAspectRatio(Real(vp->getActualWidth()) / Real(vp->getActualHeight()));
    }

    std::unique_ptr<CameraMan> mCamMan;
};


TutorialApplication::TutorialApplication()
    : ApplicationContext("Ogre13-Test00")
{
}


TutorialApplication::~TutorialApplication()
{
    mCamMan.reset();
}

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


    // get a pointer to the already created root
    Root* root = getRoot();
    SceneManager* scnMgr = root->createSceneManager();

    // register our scene with the RTSS
    RTShader::ShaderGenerator* shadergen = RTShader::ShaderGenerator::getSingletonPtr();
    shadergen->addSceneManager(scnMgr);

    // -- tutorial section start --
    //! [cameracreate]
    SceneNode* camNode = scnMgr->getRootSceneNode()->createChildSceneNode();
    Camera* cam = scnMgr->createCamera("myCam");
    //! [cameracreate]
    //! 
    //const auto& resloc = Ogre::ResourceGroupManager::getSingleton().listResourceLocations("Material");
    Ogre::ResourceManager::ResourceMapIterator res_i = Ogre::MaterialManager::getSingleton().getResourceIterator();

    Ogre::StringVector matnames;
    while (res_i.hasMoreElements())
    {
        
        auto sname = res_i.current()->second->getName();
        matnames.push_back(sname);

        res_i.moveNext();
    }

    //! [cameraposition]
    camNode->setPosition(0, 0, 1000);
    
    //camNode->setFixedYawAxis(true, Vector3::UNIT_Z);
    //camNode->lookAt(Vector3(0, 0, 0), Node::TransformSpace::TS_WORLD);

    Ogre::Quaternion rotq;
    rotq.FromAxes(Ogre::Vector3(1, 0, 0), Ogre::Vector3(0, 0, 1), Ogre::Vector3(0, -1, 0));
    camNode->setOrientation(rotq);

    // camNode->setFixedYawAxis(false, Vector3::UNIT_Z);

    //camNode->setFixedYawAxis(false);
    //camNode->setPosition(0, -500, 0);
    
    //camNode->setDirection(Ogre::Vector3(0, 1, 0));

    //! [cameraposition]

    //! [cameralaststep]
    cam->setNearClipDistance(5);
    camNode->attachObject(cam);

    auto rup = cam->getRealUp();
    auto rrt = cam->getRealRight();
    auto rto = cam->getRealDirection();
    auto pos = cam->getRealPosition();

    mCamMan.reset(new CameraMan(camNode));
    mCamMan->setFixedYaw(false);
    mCamMan->setStyle(CS_ORBIT);
    //! [cameralaststep]
    //! 
    //

    //camNode->lookAt(Vector3(0, 0, 0), Node::TransformSpace::TS_WORLD);
    //Ogre::Quaternion rotq;
    //rotq.FromAxes(Ogre::Vector3(1, 0, 0), Ogre::Vector3(0, 0, 1), Ogre::Vector3(0, 1, 0));
    //camNode->setOrientation(rotq);
    

    rup = cam->getRealUp();
    rrt = cam->getRealRight();
    rto = cam->getRealDirection();
    pos = cam->getRealPosition();

    //! [addviewport]
    Viewport* vp = getRenderWindow()->addViewport(cam);
    //! [addviewport]

    //! [viewportback]
    vp->setBackgroundColour(ColourValue(0.2, 0.2, 0.4));

    vp->setMaterialScheme(MSN_SHADERGEN);
    //! [viewportback]

    //! [cameraratio]
    cam->setAspectRatio(Real(vp->getActualWidth()) / Real(vp->getActualHeight()));
    //! [cameraratio]

    //! [lightingsset]
    scnMgr->setAmbientLight(ColourValue(0.65, 0.65, 0.85));
    scnMgr->setShadowTechnique(SHADOWTYPE_NONE/*SHADOWTYPE_STENCIL_ADDITIVE*/);

    // create a floor plane

    Plane plane(Vector3::UNIT_Z, 0);
    MeshManager::getSingleton().createPlane(
        "ground", RGN_DEFAULT,
        plane,
        1000, 1000, 50, 50,
        true,
        1, 1, 1,
        Vector3::UNIT_Y);
    Entity* groundEntity = scnMgr->createEntity("ground");
    scnMgr->getRootSceneNode()->createChildSceneNode()->attachObject(groundEntity);

    groundEntity->setCastShadows(false);
    groundEntity->setVisible(true);

    //
    {
        unsigned short src, dst;
        if (!groundEntity->getMesh()->suggestTangentVectorBuildParams(VES_TANGENT, src, dst))
        {
            // enforce that we have tangent vectors
            groundEntity->getMesh()->buildTangentVectors(VES_TANGENT, src, dst);
        }
    }
    groundEntity->setMaterialName("DamagedHelmet"/*"RustyBarrel"*/);
    groundEntity->getSubEntity(0)->getMaterial()->getTechnique(0)->getPass(0)->setCullingMode(CULL_NONE);

    Entity* ent1 = scnMgr->createEntity("uv_sphere.mesh"/*"knot.mesh"*/);
    SceneNode* node1 = scnMgr->getRootSceneNode()->createChildSceneNode();
    node1->attachObject(ent1);
    node1->setPosition(Ogre::Vector3(0, 0, 100));
    {    
        unsigned short src, dst;
        if (!ent1->getMesh()->suggestTangentVectorBuildParams(VES_TANGENT, src, dst))
        {
            // enforce that we have tangent vectors
            ent1->getMesh()->buildTangentVectors(VES_TANGENT, src, dst);
        }
    }
    ent1->setMaterialName("DamagedHelmet_RTSS");
}


bool TutorialApplication::keyPressed(const KeyboardEvent& evt)
{
    if (evt.keysym.sym == SDLK_ESCAPE)
    {
        getRoot()->queueEndRendering();
    }
    return true;
}


int main(int argc, char **argv)
{
    try
    {
        TutorialApplication app;
        app.getFSLayer().setHomePath("E:/ogrehome/");
        app.initApp();
        app.getRoot()->startRendering();
        app.closeApp();
    }
    catch (const std::exception& e)
    {
        std::cerr << "Error occurred during execution: " << e.what() << '\n';
        return 1;
    }

    return 0;
}

//! [starter]

 

posted @ 2023-11-19 17:57  abcstar  阅读(19)  评论(0编辑  收藏  举报