I've been evaluating Ogre 2.0 for a project, in the processes of which I made a minimal example of Ogre 2.0 code. I was going to contribute this to the wiki, since there didn't seem to be something working like that for 2.0 yet, but first I was going to open it up to comment here.
The project is a rewrite of visualisation tool, originally written with ogre 1.7.
About the example:
Are there any simple examples that can be done on D3D11 without using the RTShader system? Otherwise ShaderGeneratorTechniqueResolverListener seems like a necessity.
Also, removing the camera from the root scene node to begin with seems to be what you would always want to do with a new camera, right?
I'm also not sure that everything is necessarily in the most 'canonical' order, esp. the initialisation of the compositor.
The whole code can be found here
Code: Select all
int main()
{
// Start up ogre
Ogre::Root root("", "", "OgreLog.txt");
loadPlugins({
"RenderSystem_Direct3D11",
"Plugin_ParticleFX",
"Plugin_CgProgramManager"
});
// Use the first available renderer
Ogre::RenderSystem* renderSys = root.getAvailableRenderers()[0];
root.setRenderSystem(renderSys);
// Set video mode
renderSys->setConfigOption("Full Screen", "No");
renderSys->setConfigOption("Video Mode", "800 x 600 @ 32-bit colour");
// Make the window
Ogre::RenderWindow* pRenderWin = root.initialise(true);
// Start compositor
root.initialiseCompositor();
// Resources necessary for the real-time shader.
loadResources("Essential", {
R"(media\RTShaderLib)",
R"(media\RTShaderLib\materials)",
R"(media\RTShaderLib\hlsl)",
R"(media\RTShaderLib\cg)"
});
bool ok = Ogre::RTShader::ShaderGenerator::initialize();
auto &shaderGenerator = Ogre::RTShader::ShaderGenerator::getSingleton();
// Necessary for D3D11. It has no fixed function pipeline.
ShaderGeneratorTechniqueResolverListener sgtrl(&shaderGenerator);
Ogre::MaterialManager::getSingleton().addListener(&sgtrl);
// Set up scene manager and one camera
Ogre::SceneManager* pSceneMgr = root.createSceneManager(
Ogre::ST_GENERIC,
1,
Ogre::INSTANCING_CULLING_SINGLETHREAD
);
// Add scene manager to shader generator
shaderGenerator.addSceneManager(pSceneMgr);
loadResources("General", {
R"(media\models)",
R"(media\materials\scripts)",
R"(media\materials\textures)",
R"(media\materials\programs)",
R"(media\materials\programs\cg)",
R"(media\materials\programs\hlsl)",
R"(media\packs\cubemapsJS.zip)"
});
// Done with the machinery, we can actually set up the scene now
// Make camera
Ogre::Camera* pCamera = pSceneMgr->createCamera("Camera");
// Set up workspace
Ogre::CompositorManager2* pCompositorManager = root.getCompositorManager2();
const Ogre::String workspaceName = "scene workspace";
pCompositorManager->createBasicWorkspaceDef(workspaceName, Ogre::ColourValue::Black);
pCompositorManager->addWorkspace(pSceneMgr, pRenderWin, pCamera, workspaceName, true);
// Make a simple scene. Mostly following the camera track example.
pSceneMgr->setSkyBox(true, "Examples/MorningSkyBox");
// setup some basic lighting for our scene
pSceneMgr->setAmbientLight(Ogre::ColourValue(0.3, 0.3, 0.3));
Ogre::SceneNode *lightNode = pSceneMgr->getRootSceneNode()->createChildSceneNode();
lightNode->setPosition(20, 80, 50);
lightNode->attachObject(pSceneMgr->createLight());
// create an ogre head entity and attach it to a node
Ogre::Entity* head = pSceneMgr->createEntity("ogrehead.mesh");
head->setName("Head");
Ogre::SceneNode* headNode = pSceneMgr->getRootSceneNode()->createChildSceneNode();
headNode->attachObject(head);
pCamera->setAutoTracking(true, headNode); // make the camera face the head
// create a camera node and attach camera to it
Ogre::SceneNode* camNode = pSceneMgr->getRootSceneNode()->createChildSceneNode();
pCamera->detachFromParent();
camNode->attachObject(pCamera);
// set up a 10 second animation for our camera, using spline interpolation for nice curves
Ogre::Animation* anim = pSceneMgr->createAnimation("CameraTrack", 10);
anim->setInterpolationMode(Ogre::Animation::IM_SPLINE);
// create a track to animate the camera's node
Ogre::NodeAnimationTrack* track = anim->createNodeTrack(camNode);
// create keyframes for our track
track->createNodeKeyFrame(0)->setTranslate(Ogre::Vector3(200, 0, 0));
track->createNodeKeyFrame(2.5)->setTranslate(Ogre::Vector3(0, -50, 100));
track->createNodeKeyFrame(5)->setTranslate(Ogre::Vector3(-500, 100, 0));
track->createNodeKeyFrame(7.5)->setTranslate(Ogre::Vector3(0, 200, -300));
track->createNodeKeyFrame(10)->setTranslate(Ogre::Vector3(200, 0, 0));
// create a new animation state to track this
Ogre::AnimationState* pAnimState = pSceneMgr->createAnimationState("CameraTrack");
pAnimState->setEnabled(true);
// Listens for window close events and frame events.
Listener listener(pRenderWin);
listener.doEachFrame([=] (const Ogre::FrameEvent& evt) {
pAnimState->addTime(evt.timeSinceLastFrame);
});
root.startRendering();
return 0;
}