OGRE Basic tutorial
Pada Saat setting applikasi kita masih menggunakan class (tutorial application) yang inherit dari class OGRE example Application. Sekarang kita akan mencoba buat class applikasi OGRE sendiri. Ubah Main.cpp seperti dibawah ini :
#include
#include
using namespace Ogre ;
class TutorialApplication
{
public:
TutorialApplication()
{
}
~TutorialApplication()
{
}
void go()
{
}
};
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
#define WIN32_LEAN_AND_MEAN
#include "windows.h"
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
#else
int main(int argc, char **argv)
#endif
{
// Create application object
TutorialApplication app;
try {
app.go();
} catch( Exception& e ) {
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
MessageBoxA( NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
#else
fprintf(stderr, "An exception has occured: %s\n",
e.getFullDescription().c_str());
#endif
}
return 0;
}
Pada kode diatas kita membaut class TutorialApplication dengan sebuah fungsi void go(), yang nantinya akan dipanggil di fungsi main. Jika kita membuka ExampleApplication.h maka akan banyak kita temui fungsi – fungsi seperti setup(), createCamera,createViewport,dll.
Pada latihan kali ini kita hanya membuat 1 fungsi go() saja dan semua code untuk class TutorialApplication akan kita letakkan disana. Ok, kita mulai saja letakkan code berikut pada Main.cpp class TutorialApplication, fugnsi Void go() :
1. Hal yang pertama akan kita buat adalah objek Ogre::Root, yang merupakan inti dari Ogre
// Create OGRE Root
Ogre::Root *mRoot = new Root();
2. Kita beri tahu Ogre dimana letak Resource ( scripts, texture, model, particle, dll ). Path untuk Resource ada di file resources.cfg, file tersebut bisa kita temukan di cobaOgre/bin/debug. Kita akan meLoad resources.cfg dengan menggunakan Ogre::ConfigFile dan memparsing isinya (path dari resources) kedalam ResourceGroupManager
Ogre::ConfigFile cf;
cf.load("resources.cfg");
ConfigFile::SectionIterator seci = cf.getSectionIterator();
while (seci.hasMoreElements())
{
String secName = seci.peekNextKey();
ConfigFile::SettingsMultiMap *settings = seci.getNext();
ConfigFile::SettingsMultiMap::iterator i;
for (i = settings->begin(); i != settings->end(); ++i)
{
String typeName = i->first;
String archName = i->second;
ResourceGroupManager::getSingleton().addResourceLocation(archName, typeName, secName);
}
}
3. Memunculkan Ogre config Dialog ( Gambar 1.2 ) agar user bisa mengeset Rendering Subsystem (DirectX / OpenGL ) dan Rendering System Options (FullScreen, video Mode, dll). Apabila user membatalkan config Dialog maka Ogre::Root akan dihapus.
// Show OGRE config Dialog
if(!mRoot->showConfigDialog())
{
delete mRoot;
throw Exception(52, "User canceled the config dialog!","Ogre Config Dialog");
}
4.Membuat Ogre::RenderWindow, Window untuk tempat kita merender Ogre
Ogre::RenderWindow* window = mRoot->initialise(true, "Ogre Head Application");
5.Membuat Scene , menambah Camera pade scene dan menambah Viewport pada Camera
//create Scene
Ogre::SceneManager* sceneMgr = mRoot->createSceneManager(ST_EXTERIOR_CLOSE);
//createCamera
Ogre::Camera* camera = sceneMgr->createCamera("SimpleCamera");
camera->setPosition(Vector3(100,100,200));
camera->lookAt(Vector3(0,0,0));
camera->setNearClipDistance(4);
// Create one viewport
Ogre::Viewport* viewPort = window->addViewport(camera);
6.initialisasi Resource Group
// init Resource Group
ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
7.Menambah Sesuatu ( OgreHead.mesh dan SkyDome ) pada Scene
// insert something
sceneMgr->setAmbientLight( ColourValue( 1, 1, 1 ) );
Ogre::Entity *ent1 = sceneMgr->createEntity( "OgreHead", "ogrehead.mesh" );
Ogre::SceneNode *node1 = sceneMgr->getRootSceneNode()->createChildSceneNode( "OgreHeadNode" );
node1->attachObject( ent1 );
node1->setPosition(Vector3(0,0,0));
sceneMgr->setSkyDome(true, "Examples/CloudySky", 5, 8);
8.Setup OIS ( Object Oriented Input System ) dengan Ogre
// Setup Input System OIS with OGRE
size_t windowHnd = 0;
std::ostringstream windowHndStr;
OIS::ParamList pl;
Ogre::RenderWindow *win = mRoot->getAutoCreatedWindow();
win->getCustomAttribute("WINDOW", &windowHnd);
windowHndStr << minputmanager =" OIS::InputManager::createInputSystem(pl);" mkeyboard =" static_cast(mInputManager-">createInputObject(OIS::OISKeyboard, false));
9. Membuat class Exit Listener, sekarang kita keluar dari class TutorialApplication, kita buat class ExitListener taruh saja diatas classtutorialApplication. kegunaan dari ExitListener adalah untuk mengCapture keyboard dan mengembalikan nilai false pada FrameStarted apabila tombol Esc ditekan. Sehingga Render Loop akan berhenti.
class ExitListener : public FrameListener
{
private:
OIS::Keyboard *mKeyboard;
public:
ExitListener(OIS::Keyboard *keyboard)
: mKeyboard(keyboard)
{
}
bool frameStarted(const FrameEvent& evt)
{
mKeyboard->capture();
return !mKeyboard->isKeyDown(OIS::KC_ESCAPE);
}
};
10. Menambahkan Exit Listener pada aplikasi, kembali lagi pada class TutorialApplication, fungsi void go() setelah class ExitListener selesai dibuat tambahkan pada applikasi
// add EXIT Listener
ExitListener* mListener = new ExitListener(mKeyboard);
mRoot->addFrameListener(mListener);
11. Waktunya Render!!!
//start rendering
mRoot->startRendering();
12. bersih bersih
//Cleaning
mInputManager->destroyInputObject(mKeyboard);
OIS::InputManager::destroyInputSystem(mInputManager);
delete mListener;
delete mRoot;
13. Contoh Hasil Akhir, yang sudah complete :
#include
#include
using namespace Ogre ;
class ExitListener : public FrameListener
{
private:
OIS::Keyboard *mKeyboard;
public:
ExitListener(OIS::Keyboard *keyboard)
: mKeyboard(keyboard)
{
}
bool frameStarted(const FrameEvent& evt)
{
mKeyboard->capture();
return !mKeyboard->isKeyDown(OIS::KC_ESCAPE);
}
};
class TutorialApplication
{
public:
TutorialApplication()
{
}
~TutorialApplication()
{
}
void go()
{
// Create OGRE Root
Ogre::Root *mRoot = new Root();
// Load n parsing Resource.cfg
Ogre::ConfigFile cf;
cf.load("resources.cfg");
ConfigFile::SectionIterator seci = cf.getSectionIterator();
while (seci.hasMoreElements())
{
String secName = seci.peekNextKey();
ConfigFile::SettingsMultiMap *settings = seci.getNext();
ConfigFile::SettingsMultiMap::iterator i;
for (i = settings->begin(); i != settings->end(); ++i)
{
String typeName = i->first;
String archName = i->second;
ResourceGroupManager::getSingleton().addResourceLocation(archName, typeName, secName);
}
}
// Show OGRE config Dialog
if(!mRoot->showConfigDialog())
{
delete mRoot;
throw Exception(52, "User canceled the config dialog!","Ogre Config Dialog");
//return false; // Exit the application on cancel
}
// Create Render Window
Ogre::RenderWindow* window = mRoot->initialise(true, "Ogre Head Application");
//create Scene
Ogre::SceneManager* sceneMgr = mRoot->createSceneManager(ST_EXTERIOR_CLOSE);
//createCamera
Ogre::Camera* camera = sceneMgr->createCamera("SimpleCamera");
camera->setPosition(Vector3(100,100,200));
camera->lookAt(Vector3(0,0,0));
camera->setNearClipDistance(4);
// Create one viewport
Ogre::Viewport* viewPort = window->addViewport(camera);
// add Loading bar
ExampleLoadingBar mLoadingBar;
mLoadingBar.start(window, 1, 1, 0.7f);
sceneMgr->clearSpecialCaseRenderQueues();
sceneMgr->addSpecialCaseRenderQueue(RENDER_QUEUE_OVERLAY);
sceneMgr->setSpecialCaseRenderQueueMode(SceneManager::SCRQM_INCLUDE);
// init Resource Group
ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
//ResourceGroupManager::getSingleton().loadResourceGroup(
//ResourceGroupManager::getSingleton().getWorldResourceGroupName(),
//false, true);
// Back to full rendering
sceneMgr->clearSpecialCaseRenderQueues();
sceneMgr->setSpecialCaseRenderQueueMode(SceneManager::SCRQM_EXCLUDE);
mLoadingBar.finish();
// insert something
sceneMgr->setAmbientLight( ColourValue( 1, 1, 1 ) );
Ogre::Entity *ent1 = sceneMgr->createEntity( "OgreHead", "ogrehead.mesh" );
Ogre::SceneNode *node1 = sceneMgr->getRootSceneNode()->createChildSceneNode( "OgreHeadNode" );
node1->attachObject( ent1 );
node1->setPosition(Vector3(0,0,0));
sceneMgr->setSkyDome(true, "Examples/CloudySky", 5, 8);
// Setup Input System OIS with OGRE
size_t windowHnd = 0;
std::ostringstream windowHndStr;
OIS::ParamList pl;
Ogre::RenderWindow *win = mRoot->getAutoCreatedWindow();
win->getCustomAttribute("WINDOW", &windowHnd);
windowHndStr << minputmanager =" OIS::InputManager::createInputSystem(pl);" mkeyboard =" static_cast(mInputManager-">createInputObject(OIS::OISKeyboard, false));
// add EXIT Listener
ExitListener* mListener = new ExitListener(mKeyboard);
mRoot->addFrameListener(mListener);
//start rendering
mRoot->startRendering();
//Cleaning
mInputManager->destroyInputObject(mKeyboard);
OIS::InputManager::destroyInputSystem(mInputManager);
delete mListener;
delete mRoot;
}
};
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
#define WIN32_LEAN_AND_MEAN
#include "windows.h"
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
#else
int main(int argc, char **argv)
#endif
{
// Create application object
TutorialApplication app;
try {
app.go();
} catch( Exception& e ) {
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
MessageBoxA( NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
#else
fprintf(stderr, "An exception has occured: %s\n",
e.getFullDescription().c_str());
#endif
}
return 0;
}
14. Silahkan di Build & Run
referensi:
http://www.ogre3d.org/wiki/index.php/Basic_Tutorial_6
Tidak ada komentar:
Posting Komentar