Tutorial:How does mRenderSystem->createBody know about Ogre?

kergoth

03-10-2009 13:41:33

I'm a bit puzzled in the 2nd tutorial example. How does the mRenderSystem->createBody method used to instantiate a cube knows about Ogre's SceneManager? mRenderSystem is a OGRE3DRenderSystem that receives mScene through the constructor, and mScene is built by mWorld who is also apparently not aware of Ogre: mWorld = NxOgre::World::createWorld();

Actually, I've reimplemented this example without the help of the ExampleApplication class, and the cubes do not appear anymore. Is there something I'm missing?

Thanks a lot,
KG

Druha

03-10-2009 14:16:38

Constructor of OGRE3DRenderSystem looks like:

OGRE3DRenderSystem(NxOgre::Scene*, Ogre::SceneManager* = ::Ogre::Root::getSingletonPtr()->getSceneManagerIterator().getNext());

This means if you don't pass second parameter to constructor, it'll take first available SceneManager if any

kergoth

03-10-2009 17:35:36

Thanks, I get it now.
But still, in the example below, my code shows the plane ground, but no cube. Do you see what's wrong with it?


#include <Ogre.h>
#include <OIS/OIS.h>
#include <CEGUI/CEGUI.h>
#include <OgreCEGUIRenderer.h>
#include <NxOgre.h>
#include <NxOgreOGRE3D.h>

OGRE3DRenderSystem *mRenderSystem;


class DualListener : public Ogre::FrameListener
{
public:
DualListener(OIS::Keyboard *keyboard)
: mKeyboard(keyboard)
{
mTimeController = NxOgre::TimeController::getSingleton();
}

bool frameStarted(const Ogre::FrameEvent& evt)
{
mKeyboard->capture();
mTimeController->advance(evt.timeSinceLastFrame);
return !mKeyboard->isKeyDown(OIS::KC_ESCAPE);
}

protected:
NxOgre::TimeController* mTimeController;

private:
OIS::Keyboard *mKeyboard;
};



class Application
{
public:
void go()
{
createRoot();
defineResources();
setupRenderSystem();
createRenderWindow();
initializeResourceGroups();
setupScene();
setupInputSystem();
setupCEGUI();
createFrameListener();
startRenderLoop();
}

~Application()
{
mInputManager->destroyInputObject(mKeyboard);
OIS::InputManager::destroyInputSystem(mInputManager);

delete mRenderer;
delete mSystem;

delete mListener;
delete mRoot;
}

protected:
NxOgre::World* mWorld;
NxOgre::Scene* mScene;

OGRE3DBody* mCube;
OGRE3DBody* mCubeTwo;
OGRE3DBody* mCubeCaca;

private:
Ogre::Root *mRoot;
OIS::Keyboard *mKeyboard;
OIS::InputManager *mInputManager;
CEGUI::OgreCEGUIRenderer *mRenderer;
CEGUI::System *mSystem;
DualListener *mListener;


void createRoot()
{
mRoot = new Ogre::Root();
}

void defineResources()
{
Ogre::String secName, typeName, archName;
Ogre::ConfigFile cf;
cf.load("resources.cfg");

Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();
while (seci.hasMoreElements())
{
secName = seci.peekNextKey();
Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
Ogre::ConfigFile::SettingsMultiMap::iterator i;
for (i = settings->begin(); i != settings->end(); ++i)
{
typeName = i->first;
archName = i->second;
Ogre::ResourceGroupManager::getSingleton().addResourceLocation(archName, typeName, secName);
}
}
}

void setupRenderSystem()
{
if (!mRoot->restoreConfig() && !mRoot->showConfigDialog())
throw Ogre::Exception(52, "User canceled the config dialog!", "Application::setupRenderSystem()");

//// Do not add this to the application
//RenderSystem *rs = mRoot->getRenderSystemByName("Direct3D9 Rendering Subsystem");
// // or use "OpenGL Rendering Subsystem"
//mRoot->setRenderSystem(rs);
//rs->setConfigOption("Full Screen", "No");
//rs->setConfigOption("Video Mode", "800 x 600 @ 32-bit colour");
}

void createRenderWindow()
{
mRoot->initialise(true, "Render Window");

//// Do not add this to the application
//mRoot->initialise(false);
//HWND hWnd = 0; // Get the hWnd of the application!
//NameValuePairList misc;
//misc["externalWindowHandle"] = StringConverter::toString((int)hWnd);
//RenderWindow *win = mRoot->createRenderWindow("Main RenderWindow", 800, 600, false, &misc);
}

void initializeResourceGroups()
{
Ogre::TextureManager::getSingleton().setDefaultNumMipmaps(5);
Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
}

void setupScene()
{
Ogre::SceneManager *mgr = mRoot->createSceneManager(Ogre::ST_GENERIC, "Default SceneManager");
Ogre::Camera *cam = mgr->createCamera("Camera");
Ogre::Viewport *vp = mRoot->getAutoCreatedWindow()->addViewport(cam);

// Set ambient light
mgr->setAmbientLight(Ogre::ColourValue(0.5f, 0.5f, 0.5f));

// Create a light
Ogre::Light* l = mgr->createLight("MainLight");
l->setPosition(20, 80, 50);

// Position the camera
cam->setPosition(0, 50, 80);
cam->lookAt(0, 20, 0);

// Create the world
mWorld = NxOgre::World::createWorld();

// Create scene description
NxOgre::SceneDescription sceneDesc;
sceneDesc.mGravity = NxOgre::Vec3(0, -9.8f, 0);
sceneDesc.mName = "DemoScene";

// Create scene
mScene = mWorld->createScene(sceneDesc);

// Set some physical scene values
mScene->getMaterial(0)->setStaticFriction(0.5);
mScene->getMaterial(0)->setDynamicFriction(0.5);
mScene->getMaterial(0)->setRestitution(0.1);

// Create render system
mRenderSystem = new OGRE3DRenderSystem(mScene,mgr);

// Add objects
std::cout << "caca";
mCube = mRenderSystem->createBody(new NxOgre::Box(1, 1, 1), NxOgre::Vec3(0, 40, 0), "cube.1m.mesh");
mCubeTwo = mRenderSystem->createBody(new NxOgre::Box(1, 1, 1), NxOgre::Vec3(20, 45, 0), "cube.1m.mesh");
mCubeTwo->addForce(NxOgre::Vec3(-800, -200, 0), NxOgre::Enums::ForceMode_Force);
mCubeCaca = mRenderSystem->createBody(new NxOgre::Box(1, 1, 1), NxOgre::Vec3(-20, 45, 0), "cube.1m.mesh");
mCubeCaca->addForce(NxOgre::Vec3(800, -200, 0), NxOgre::Enums::ForceMode_Force);

// Create floor plane (BloodyMess)
mScene->createSceneGeometry(new NxOgre::PlaneGeometry(0, NxOgre::Vec3(0, 1, 0)), Matrix44_Identity);

// Create floor plane (Ogre)
Ogre::MovablePlane *plane = new Ogre::MovablePlane("Plane");
plane->d = 0;
plane->normal = Ogre::Vector3::UNIT_Y;
Ogre::MeshManager::getSingleton().createPlane("PlaneMesh",
Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
*plane, 120, 120, 1, 1, true, 1, 3, 3, Ogre::Vector3::UNIT_Z);
Ogre::Entity *planeEnt = mgr->createEntity("PlaneEntity", "PlaneMesh");
planeEnt->setMaterialName("Examples/GrassFloor");

Ogre::SceneNode* mPlaneNode = mgr->getRootSceneNode()->createChildSceneNode();
mPlaneNode->attachObject(planeEnt);
}

void setupInputSystem()
{
size_t windowHnd = 0;
std::ostringstream windowHndStr;
OIS::ParamList pl;
Ogre::RenderWindow *win = mRoot->getAutoCreatedWindow();

win->getCustomAttribute("WINDOW", &windowHnd);
windowHndStr << windowHnd;
pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
mInputManager = OIS::InputManager::createInputSystem(pl);

try
{
mKeyboard = static_cast<OIS::Keyboard*>(mInputManager->createInputObject(OIS::OISKeyboard, false));
//mMouse = static_cast<OIS::Mouse*>(mInputManager->createInputObject(OIS::OISMouse, false));
//mJoy = static_cast<OIS::JoyStick*>(mInputManager->createInputObject(OIS::OISJoyStick, false));
}
catch (const OIS::Exception &e)
{
throw new Ogre::Exception(42, e.eText, "Application::setupInputSystem");
}
}

void setupCEGUI()
{
Ogre::SceneManager *mgr = mRoot->getSceneManager("Default SceneManager");
Ogre::RenderWindow *win = mRoot->getAutoCreatedWindow();

// CEGUI setup
mRenderer = new CEGUI::OgreCEGUIRenderer(win, Ogre::RENDER_QUEUE_OVERLAY, false, 3000, mgr);
mSystem = new CEGUI::System(mRenderer);

// Other CEGUI setup here.
}

void createFrameListener()
{
mListener = new DualListener(mKeyboard);
mRoot->addFrameListener(mListener);
}

void startRenderLoop()
{
mRoot->startRendering();

//// Do not add this to the application
//while (mRoot->renderOneFrame())
//{
// // Do some things here, like sleep for x milliseconds or perform other actions.
//}
}
};

#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
#define WIN32_LEAN_AND_MEAN
#include "windows.h"
#endif

#ifdef __cplusplus
extern "C" {
#endif

#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT)
#else
int main(int argc, char **argv)
#endif
{
// Create application object
Application app;

try {
app.go();
} catch(Ogre::Exception& e) {
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
MessageBoxA(NULL, e.getFullDescription().c_str(),
"An exception has occurred!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
#else
std::cerr << "An exception has occurred: " << e.getFullDescription();
#endif
}

return 0;
}

#ifdef __cplusplus
}
#endif

kergoth

05-10-2009 19:21:49

Problem solved: the ExampleApplication code I used as a basis was using the fact that the mCamera had been initialized with a NearClipDistance of 5. I've added this to my code and now the cubes appear as expected. They were simply clipped out.

Cheers,
KG

betajaen

05-10-2009 19:34:56

Occam's Razor.