Ogre Control
Ogre Control
| ABlueEyedMonkey |
Aug 23 2008, 04:38 PM
Post
#1
|
|
Advanced Member ![]() ![]() ![]() Group: Members Posts: 31 Joined: 13-August 08 Member No.: 1176 |
Hi guys - im trying to create a control which uses the ogre engine. Im posting this control on here and in the ogre community because im not sure where the problem lies. Im hoping someone can help me and by the end have something we can all use
The header file: CODE #ifndef PANEL_OGRE #define PANEL_OGRE #include "Ogre.h" #include "OgreStringConverter.h" #include "OgreException.h" #include "vcf/ApplicationKit/ApplicationKit.h" #include "vcf/ApplicationKit/ControlsKit.h" #include <string> using namespace VCF; using namespace Ogre; using namespace std; class OgreControl : public CustomControl { protected: //Each of the member variables for easy reference. TimerComponent* m_FrameTimer; Ogre::Root* m_Root; Ogre::RenderSystem* m_RenderSystem; Ogre::RenderWindow* m_RenderWindow; Ogre::SceneManager* m_SceneManager; Ogre::Camera* m_Camera; Ogre::Viewport* m_ViewPort; public: //Public functions OgreControl(); virtual ~OgreControl(); //This will initialize the ogre control virtual bool Initialize(); protected: //Protected functions virtual bool InitializeRendersystem(); virtual void InitializeTimer(); virtual void LoadResources(); virtual void ChooseSceneManager(); virtual void CreateCamera(); virtual void CreateViewport(); virtual void CreateLights(); virtual void InitializeScene(); virtual void DoFrame(); private: //Event handlers virtual void paint( GraphicsContext* gc ); virtual void onTimer( Event* e ) { DoFrame(); } //Overriden functions virtual void OnResized( ControlEvent*e ); }; #endif The source file: CODE #include "OgreControl.h"; #include <direct.h> //========================================================================== //Constructors //========================================================================== OgreControl::OgreControl() : CustomControl() { //MANUAL METHOD // create a new Root without config files m_Root = new Root("", "resources.cfg"); m_RenderWindow = NULL; // Setup event delegates ControlSized += new ClassProcedure1<ControlEvent*,OgreControl>(this, &OgreControl::OnResized, "OgreControl::onResized" ); } //========================================================================== //Destructors //========================================================================== OgreControl::~OgreControl() { delete m_Root; } //========================================================================== //Initializes the Ogre render control. This will return false if it fails. //========================================================================== bool OgreControl::Initialize() { bool initialized = InitializeRendersystem(); if (initialized == false) return false; ChooseSceneManager(); CreateCamera(); CreateViewport(); LoadResources(); CreateLights(); //activate the window m_RenderWindow->setActive(true); InitializeScene(); InitializeTimer(); return true; } //========================================================================== //Initializes the Ogre render system. This will return false if it fails. //========================================================================== bool OgreControl::InitializeRendersystem() { // load the render system plug-ins #if defined(_DEBUG) m_Root->loadPlugin("RenderSystem_Direct3D9_d"); m_Root->loadPlugin("RenderSystem_GL_d"); #else root->loadPlugin("RenderSystem_Direct3D9"); root->loadPlugin("RenderSystem_GL"); #endif // pretend the user used some other mechanism to select the // OpenGL renderer //String rName("OpenGL Rendering Subsystem"); Ogre::String rName("Direct3D9 Rendering Subsystem"); RenderSystemList *rList = m_Root->getAvailableRenderers(); RenderSystemList::iterator it = rList->begin(); RenderSystem *rSys = 0; while (it != rList->end()) { rSys = *(it++); if ( rSys->getName() == rName) { // set this renderer and break out m_Root->setRenderSystem(rSys); break; } } // end gracelessly if the preferred renderer is not available if (m_Root->getRenderSystem() == NULL) { delete m_Root; return false; } m_RenderSystem = m_Root->getRenderSystem(); HWND hWnd = (HWND)getPeer()->getHandleID(); NameValuePairList params; params["left"] = "0"; params["top"] = "0"; params["title"] = "Alternate Window Title"; params["externalWindowHandle"] = StringConverter::toString((size_t)hWnd); // init the window m_RenderWindow = m_Root->initialise(false); // set up the render window with all default params m_RenderWindow = m_RenderSystem->createRenderWindow( "Manual Ogre Window", // window name 800, // window width, in pixels 600, // window height, in pixels false, // fullscreen or not ¶ms); // use '0' for defaults // from here you can set up your camera and viewports as normal return true; } //========================================================================== //Initializes the timer which will call the render function //========================================================================== void OgreControl::InitializeTimer() { CallBack* ev = new ClassProcedure1<Event*, OgreControl>( this, &OgreControl::onTimer, "OgreControl::onTimer" ); TimerComponent* timer = new TimerComponent(); addComponent( timer ); timer->TimerPulse.add(ev); timer->setTimeoutInterval( 33 ); //around 30 FPS timer->setActivated( true ); } //========================================================================== //This will redraw the scene when a paint command is heard //========================================================================== void OgreControl::paint( GraphicsContext* gc ) { DoFrame(); } //========================================================================== //Resizes the render window //========================================================================== void OgreControl::OnResized( ControlEvent*e ) { if (!m_RenderWindow) return; m_RenderWindow->resize(getWidth(), getHeight()); } //========================================================================== //Calls the update function on the renderer //========================================================================== void OgreControl::DoFrame() { if (!m_RenderWindow) return; m_RenderWindow->update(); } //========================================================================== //Initializes each of the resource groups we will be using. //========================================================================== void OgreControl::LoadResources() { char *path = NULL; path = getcwd(NULL, 0); // or _getcwd string mainDir = path; //Firat we need to register a resource group. ResourceGroupManager *rgm = ResourceGroupManager::getSingletonPtr(); rgm->addResourceLocation(mainDir + "\\media\\models", "FileSystem", "Models"); rgm->addResourceLocation(mainDir + "\\media\\materials\\scripts", "FileSystem", "Scripts"); //rgm->addResourceLocation("../../media/packs/OgreCore.zip", "Zip", "Bootstrap"); //rgm->addResourceLocation("../../media", "FileSystem", "General"); // initialize all of the previously defined resource groups rgm->initialiseAllResourceGroups(); // or, alternately, initialize the defined resource groups one at a time //ResourceGroupManager::getSingleton().initialiseResourceGroup("Models"); } //========================================================================== //Creates the scene manager //========================================================================== void OgreControl::ChooseSceneManager() { /* ST_GENERIC: Minimal scene manager implementation, not optimized for any particular scene content or structure. Most useful for minimally complex scenes (such as the GUI phases of an application). • ST_INTERIOR: Scene manager implementation optimized for rendering interior, close-quarter, potentially high-density scenes. • ST_EXTERIOR_CLOSE: Scene manager implementation optimized for rendering outdoor scenes with near-to-medium visibility, such as those based on tiled single-page terrain mesh or heightfield. • ST_EXTERIOR_FAR: Anachronism in Ogre, typically no longer used. Use ST_EXTERIOR_CLOSE or ST_EXTERIOR_REAL_FAR instead. • ST_EXTERIOR_REAL_FAR: Scene manager implementation typically suited for paged landscape or paged scene construction. Paged landscapes often are huge, possibly entire planets.*/ m_SceneManager = m_Root->createSceneManager(ST_GENERIC, "MySceneManager"); } //========================================================================== //Creates the camera which we will be using in the editor //========================================================================== void OgreControl::CreateCamera() { m_Camera = m_SceneManager->createCamera("MainCamera"); m_Camera->setNearClipDistance(0.1f); m_Camera->setFarClipDistance(1000); // normally you would calculate this from the size of the viewport m_Camera->setAspectRatio(1.33333f); //Other values include PM_WIREFRAME and PM_POINTS //You can use PolygonMode mode = camera->getPolygonMode() to get the mode m_Camera->setPolygonMode(PM_SOLID); //Position coming 10 units out the screem m_Camera->setPosition(0, 10, 50); m_Camera->lookAt(0,0,0); //m_SceneManager->getRootSceneNode()->createChildSceneNode()->attachObject(m_Camera); } //========================================================================== //Adds the default viewport to the scene //========================================================================== void OgreControl::CreateViewport() { m_ViewPort = m_RenderWindow->addViewport(m_Camera, 0, 0, 0, 1, 1); m_ViewPort->setBackgroundColour(ColourValue(0.3f, 0.3f, 0.3f)); } //========================================================================== //Creates the lights used in the scene //========================================================================== void OgreControl::CreateLights() { // Set ambient light m_SceneManager->setAmbientLight(ColourValue(0.5, 0.5, 0.5)); // Create a point light Light* light = m_SceneManager->createLight("EditorLight"); // Accept default settings: point light, white diffuse, just set position // NB You could attach the light to a SceneNode if you wanted it to move // automatically with other objects light->setPosition(20,80,50); } //========================================================================== //Use this to create objects on start up //========================================================================== void OgreControl::InitializeScene() { Entity *ent = m_SceneManager->createEntity("head", "ogrehead.mesh"); // Set material loaded from Example.material //ent->setMaterialName("..//..//media//materials//scripts//Example"); // Add entity to the root scene node m_SceneManager->getRootSceneNode()->createChildSceneNode()->attachObject(ent); //The default translation space is TS_PARENT, so you do not need to specify parent space //when you perform a translation: //mSceneNode->translate(100.0, 10.0, 0.0); //If you need to perform a translation in world space, you must inform Ogre of that: //mSceneNode->translate(100.0, 10.0, 0.0, TS_WORLD); //Likewise for local space. The following will move a node “forward” 100 units in the direction //it is facing: //mSceneNode->translate(0.0, 0.0, 100.0, TS_LOCAL); //For rotations, the default is TS_LOCAL; if you need a different rotation space, you must tell Ogre: // rotate around object's own Y axis by 1 radian, about 57 degrees //mSceneNode->yaw(Ogre::Radian(1.0)); } The problem is that im seeing a strange white box instead of the ogre model which im loading. Its not the ogre engine i dont think because if i tell it to create its own window it works fine. If you guys could tell me anything im doing wrong or forgot? Thanks guys Attached thumbnail(s) |
![]() ![]() ![]() |
| ABlueEyedMonkey |
Aug 24 2008, 10:51 AM
Post
#2
|
|
Advanced Member ![]() ![]() ![]() Group: Members Posts: 31 Joined: 13-August 08 Member No.: 1176 |
Hmmm this is very odd - if i create a window and add the ogre control as a child to it -it works fine. So it seems the panel being the parent is whats causing trouble. I'll post a screenie to show what i mean...
This is what i changed: Old: CODE Panel *renderPanel = new Panel; OgreControl *ogreControl = new OgreControl(); renderPanel->add(ogreControl, AlignmentType::AlignClient); That doesn't want to work - but this does: New: CODE OgreControl *ogreControl = new OgreControl(); Window *t = new Window(this); //this is the main window... t->add(ogreControl, AlignmentType::AlignClient); t->show(); Any ideas guys? Ive been rattling my brains over this for quite a while now :S Attached thumbnail(s) |
ABlueEyedMonkey Ogre Control Aug 23 2008, 04:38 PM
ABlueEyedMonkey Does no one have an idea :( Aug 24 2008, 01:38 PM
ABlueEyedMonkey Im thinking it might have something to do with the... Aug 25 2008, 12:54 PM
ABlueEyedMonkey GOT IT!
This was driving me insane, but i fig... Aug 26 2008, 10:20 PM
ddiego Hi, glad to see you solved it. Sorry about not res... Aug 27 2008, 07:57 PM
ddiego By the way, don't forget that for laying out y... Aug 27 2008, 08:00 PM
ABlueEyedMonkey Thats great ddiego - I will have a look into that ... Aug 31 2008, 09:04 PM
ABlueEyedMonkey Ogre just released a new version - to get this wor... Sep 2 2008, 09:28 PM![]() ![]() ![]() |
| Lo-fi Version | : 19th May 2013 - 09:06 PM |