Allegro.cc - Online Community

Allegro.cc Forums » Allegro Development » I did it! ex_ogre3d is now working on Windows!!

This thread is locked; no one can reply to it. rss feed Print
I did it! ex_ogre3d is now working on Windows!!
AMCerasoli
Member #11,955
May 2010
avatar

I did it!!!

I thought I was not going to be able to do it... Today is the day! AMCerasoli is not a noob anymore!.

Yes I know they're are just a couple of lines, but I know what it's happening, that's what it matters!.

Video or didn't happen!

Another thing is that I could swear that I saw that ninja with some shadows but now he doesn't have it...

The code example

#SelectExpand
1/* 2 * Example program for the Allegro library, by Peter Wang. 3 * Now working on Windows too. 4 * 5 * This is a test program to see if Allegro can be used alongside the OGRE 6 * graphics library. It currently only works on Linux/GLX. To run, you 7 * will need to have OGRE plugins.cfg and resources.cfg files in the 8 * current directory. 9 * 10 * Inputs: W A S D, mouse 11 * 12 * This code is based on the samples in the OGRE distribution. 13 */ 14 15#include <Ogre.h> 16#include <allegro5/allegro.h> 17#include <allegro5/allegro_windows.h> 18#include <allegro5/allegro_opengl.h> 19 20/* 21 * Ogre 1.7 (and, optionally, earlier versions) uses the FreeImage library to 22 * handle image loading. FreeImage bundles its own copies of common libraries 23 * like libjpeg and libpng, which can conflict with the system copies of those 24 * libraries that allegro_image uses. That means we can't use allegro_image 25 * safely, nor any of the addons which depend on it. 26 * 27 * One solution would be to write a codec for Ogre that avoids FreeImage, or 28 * write allegro_image handlers using FreeImage. The latter would probably be 29 * easier and useful for other reasons. 30 */ 31 32using namespace Ogre; 33 34const int WIDTH = 640; 35const int HEIGHT = 480; 36const float MOVE_SPEED = 1500.0; 37 38class Application 39{ 40protected: 41 Root *mRoot; 42 RenderWindow *mWindow; 43 SceneManager *mSceneMgr; 44 Camera *mCamera; 45 46public: 47 void setup(int w, int h) 48 { 49 createRoot(); 50 defineResources(); 51 setupRenderSystem(); 52 createRenderWindow(w, h); 53 initializeResourceGroups(); 54 chooseSceneManager(); 55 createCamera(); 56 createViewports(); 57 createScene(); 58 } 59 60 ~Application() 61 { 62 delete mRoot; 63 } 64 65private: 66 void createRoot() 67 { 68 mRoot = new Root(); 69 } 70 71 void defineResources() 72 { 73 String secName, typeName, archName; 74 ConfigFile cf; 75 cf.load("resources.cfg"); 76 ConfigFile::SectionIterator seci = cf.getSectionIterator(); 77 while (seci.hasMoreElements()) { 78 secName = seci.peekNextKey(); 79 ConfigFile::SettingsMultiMap *settings = seci.getNext(); 80 ConfigFile::SettingsMultiMap::iterator i; 81 for (i = settings->begin(); i != settings->end(); ++i) { 82 typeName = i->first; 83 archName = i->second; 84 ResourceGroupManager::getSingleton().addResourceLocation(archName, 85 typeName, secName); 86 } 87 } 88 } 89 90 void setupRenderSystem() 91 { 92 if (!mRoot->restoreConfig() && !mRoot->showConfigDialog()) { 93 throw Exception(52, "User canceled the config dialog!", 94 "Application::setupRenderSystem()"); 95 } 96 } 97 98 void createRenderWindow(int w, int h) 99 { 100 mRoot->initialise(false); 101 102 Ogre::NameValuePairList misc; 103 104 unsigned long winHandle = reinterpret_cast<size_t>(al_get_win_window_handle(al_get_current_display())); 105 unsigned long winGlContext = reinterpret_cast<size_t>(wglGetCurrentContext()); 106 107 misc["externalWindowHandle"] = StringConverter::toString(winHandle); 108 misc["externalGLContext"] = StringConverter::toString(winGlContext); 109 misc["externalGLControl"] = String("True"); 110 111 mWindow = mRoot->createRenderWindow("MainRenderWindow", w, h, false, 112 &misc); 113 //renderWindow->setVisible(false); 114 115 } 116 117 void initializeResourceGroups() 118 { 119 TextureManager::getSingleton().setDefaultNumMipmaps(5); 120 ResourceGroupManager::getSingleton().initialiseAllResourceGroups(); 121 } 122 123 virtual void chooseSceneManager() 124 { 125 mSceneMgr = mRoot->createSceneManager(ST_GENERIC, "Default SceneManager"); 126 } 127 128 virtual void createCamera() 129 { 130 mCamera = mSceneMgr->createCamera("PlayerCam"); 131 mCamera->setPosition(Vector3(-300, 300, -300)); 132 mCamera->lookAt(Vector3(0, 0, 0)); 133 mCamera->setNearClipDistance(5); 134 } 135 136 virtual void createViewports() 137 { 138 // Create one viewport, entire window. 139 Viewport *vp = mWindow->addViewport(mCamera); 140 vp->setBackgroundColour(ColourValue(0, 0.25, 0.5)); 141 142 // Alter the camera aspect ratio to match the viewport. 143 mCamera->setAspectRatio( 144 Real(vp->getActualWidth()) / Real(vp->getActualHeight())); 145 } 146 147 virtual void createScene() = 0; 148 149public: 150 151 void render() 152 { 153 const bool swap_buffers = false; 154 mWindow->update(swap_buffers); 155 mRoot->renderOneFrame(); 156 al_flip_display(); 157 } 158}; 159 160class Example : public Application 161{ 162private: 163 ALLEGRO_DISPLAY *display; 164 ALLEGRO_EVENT_QUEUE *queue; 165 ALLEGRO_TIMER *timer; 166 167 double startTime; 168 double lastRenderTime; 169 double lastMoveTime; 170 bool lmb; 171 bool rmb; 172 bool forward; 173 bool back; 174 bool left; 175 bool right; 176 float current_speed; 177 Vector3 last_translate; 178 179public: 180 Example(ALLEGRO_DISPLAY *display); 181 ~Example(); 182 void setup(); 183 void mainLoop(); 184 185private: 186 void createScene(); 187 void moveCamera(double timestamp, Radian rot_x, Radian rot_y, 188 Vector3 & translate); 189 void animate(double now); 190 void nextFrame(); 191}; 192 193Example::Example(ALLEGRO_DISPLAY *display) : 194 display(display), 195 queue(NULL), 196 timer(NULL), 197 startTime(0.0), 198 lastRenderTime(0.0), 199 lastMoveTime(0.0), 200 lmb(false), 201 rmb(false), 202 forward(false), 203 back(false), 204 left(false), 205 right(false), 206 current_speed(0), 207 last_translate(Vector3::ZERO) 208{ 209} 210 211Example::~Example() 212{ 213 al_destroy_timer(timer); 214 al_destroy_event_queue(queue); 215} 216 217void Example::createScene() 218{ 219 // Enable shadows. 220 mSceneMgr->setAmbientLight(ColourValue(0.5, 0.25, 0.0)); 221 //mSceneMgr->setShadowTechnique(SHADOWTYPE_STENCIL_ADDITIVE); // slower 222 mSceneMgr->setShadowTechnique(SHADOWTYPE_STENCIL_MODULATIVE); // faster 223 224 // Create the character. 225 Entity *ent1 = mSceneMgr->createEntity("Ninja", "ninja.mesh"); 226 ent1->setCastShadows(true); 227 mSceneMgr->getRootSceneNode()->createChildSceneNode()->attachObject(ent1); 228 229 AnimationState *anim1 = ent1->getAnimationState("Walk"); 230 anim1->setLoop(true); 231 anim1->setEnabled(true); 232 233 // Create the ground. 234 Plane plane(Vector3::UNIT_Y, 0); 235 MeshManager::getSingleton().createPlane("ground", 236 ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, plane, 237 1500, 1500, 20, 20, true, 1, 5, 5, Vector3::UNIT_Z); 238 239 Entity *ent2 = mSceneMgr->createEntity("GroundEntity", "ground"); 240 ent2->setMaterialName("Examples/Rockwall"); 241 ent2->setCastShadows(false); 242 mSceneMgr->getRootSceneNode()->createChildSceneNode()->attachObject(ent2); 243 244 // Create a light. 245 Light *light = mSceneMgr->createLight("Light1"); 246 light->setType(Light::LT_POINT); 247 light->setPosition(Vector3(0, 150, 250)); 248 light->setDiffuseColour(1.0, 1.0, 1.0); 249 light->setSpecularColour(1.0, 0.0, 0.0); 250} 251 252void Example::setup() 253{ 254 int w = al_get_display_width(display); 255 int h = al_get_display_height(display); 256 Application::setup(w, h); 257 258 const double BPS = 60.0; 259 timer = al_create_timer(1.0 / BPS); 260 261 queue = al_create_event_queue(); 262 al_register_event_source(queue, al_get_keyboard_event_source()); 263 al_register_event_source(queue, al_get_mouse_event_source()); 264 al_register_event_source(queue, al_get_timer_event_source(timer)); 265 al_register_event_source(queue, al_get_display_event_source(display)); 266} 267 268void Example::mainLoop() 269{ 270 bool redraw = true; 271 272 startTime = lastMoveTime = al_get_time(); 273 al_start_timer(timer); 274 275 for (;;) { 276 ALLEGRO_EVENT event; 277 278 if (al_is_event_queue_empty(queue) && redraw) { 279 nextFrame(); 280 redraw = false; 281 } 282 283 al_wait_for_event(queue, &event); 284 if (event.type == ALLEGRO_EVENT_KEY_DOWN && 285 event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) { 286 break; 287 } 288 if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { 289 break; 290 } 291 292 Radian rot_x(0); 293 Radian rot_y(0); 294 Vector3 translate(Vector3::ZERO); 295 296 switch (event.type) { 297 case ALLEGRO_EVENT_TIMER: 298 redraw = true; 299 break; 300 301 case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN: 302 if (event.mouse.button == 1) 303 lmb = true; 304 if (event.mouse.button == 2) 305 rmb = true; 306 break; 307 308 case ALLEGRO_EVENT_MOUSE_BUTTON_UP: 309 if (event.mouse.button == 1) 310 lmb = false; 311 if (event.mouse.button == 2) 312 rmb = false; 313 if (!lmb && !rmb) 314 al_show_mouse_cursor(display); 315 break; 316 317 case ALLEGRO_EVENT_MOUSE_AXES: 318 if (lmb) { 319 rot_x = Degree(-event.mouse.dx * 0.13); 320 rot_y = Degree(-event.mouse.dy * 0.13); 321 } 322 if (rmb) { 323 translate.x = event.mouse.dx * 0.5; 324 translate.y = event.mouse.dy * -0.5; 325 } 326 if (lmb || rmb) { 327 al_hide_mouse_cursor(display); 328 al_set_mouse_xy(display, 329 al_get_display_width(display)/2, 330 al_get_display_height(display)/2); 331 } 332 break; 333 334 case ALLEGRO_EVENT_KEY_DOWN: 335 case ALLEGRO_EVENT_KEY_UP: { 336 const bool is_down = (event.type == ALLEGRO_EVENT_KEY_DOWN); 337 if (event.keyboard.keycode == ALLEGRO_KEY_W) 338 forward = is_down; 339 if (event.keyboard.keycode == ALLEGRO_KEY_S) 340 back = is_down; 341 if (event.keyboard.keycode == ALLEGRO_KEY_A) 342 left = is_down; 343 if (event.keyboard.keycode == ALLEGRO_KEY_D) 344 right = is_down; 345 break; 346 } 347 348 case ALLEGRO_EVENT_DISPLAY_RESIZE: { 349 al_acknowledge_resize(event.display.source); 350 int w = al_get_display_width(display); 351 int h = al_get_display_height(display); 352 mWindow->resize(w, h); 353 mCamera->setAspectRatio(Real(w) / Real(h)); 354 redraw = true; 355 break; 356 } 357 } 358 359 moveCamera(event.any.timestamp, rot_x, rot_y, translate); 360 } 361} 362 363void Example::moveCamera(double timestamp, Radian rot_x, Radian rot_y, 364 Vector3 & translate) 365{ 366 const double time_since_move = timestamp - lastMoveTime; 367 const float move_scale = MOVE_SPEED * time_since_move; 368 369 if (forward) { 370 translate.z = -move_scale; 371 } 372 if (back) { 373 translate.z = move_scale; 374 } 375 if (left) { 376 translate.x = -move_scale; 377 } 378 if (right) { 379 translate.x = move_scale; 380 } 381 382 if (translate == Vector3::ZERO) { 383 // Continue previous motion but dampen. 384 translate = last_translate; 385 current_speed -= time_since_move * 0.3; 386 } 387 else { 388 // Ramp up. 389 current_speed += time_since_move; 390 } 391 if (current_speed > 1.0) 392 current_speed = 1.0; 393 if (current_speed < 0.0) 394 current_speed = 0.0; 395 396 translate *= current_speed; 397 398 mCamera->yaw(rot_x); 399 mCamera->pitch(rot_y); 400 mCamera->moveRelative(translate); 401 402 last_translate = translate; 403 lastMoveTime = timestamp; 404} 405 406void Example::animate(double now) 407{ 408 const double dt0 = now - startTime; 409 const double dt = now - lastRenderTime; 410 411 // Animate the character. 412 Entity *ent = mSceneMgr->getEntity("Ninja"); 413 AnimationState *anim = ent->getAnimationState("Walk"); 414 anim->addTime(dt); 415 416 // Move the light around. 417 Light *light = mSceneMgr->getLight("Light1"); 418 light->setPosition(Vector3(300 * cos(dt0), 300, 300 * sin(dt0))); 419} 420 421void Example::nextFrame() 422{ 423 const double now = al_get_time(); 424 animate(now); 425 render(); 426 lastRenderTime = now; 427} 428 429int main(int argc, char *argv[]) 430{ 431 (void)argc; 432 (void)argv; 433 ALLEGRO_DISPLAY *display; 434 435 if (!al_init()) { 436 return 1; 437 } 438 al_install_keyboard(); 439 al_install_mouse(); 440 441 al_set_new_display_flags(ALLEGRO_OPENGL | ALLEGRO_RESIZABLE); 442 display = al_create_display(WIDTH, HEIGHT); 443 if (!display) { 444 return 1; 445 } 446 al_set_window_title(display, "My window"); 447 448 { 449 Example app(display); 450 app.setup(); 451 app.mainLoop(); 452 } 453 454 al_uninstall_system(); 455 456 return 0; 457}

Edited:

Damn with the title!! >:(, I screw it again... I meant "working with Allegro 5 on windows"

Dario ff
Member #10,065
August 2008
avatar

The ninja is supposed to have shadows in that example. :o

ent1->setCastShadows(true);

So you ain't done yet. 8-)

EDIT: What did you add to stop the white screen from appearing tho?

TranslatorHack 2010, a human translation chain in a.cc.
My games: [GiftCraft] - [Blocky Rhythm[SH2011]] - [Elven Revolution] - [Dune Smasher!]

AMCerasoli
Member #11,955
May 2010
avatar

Nothing... I just kick the case a couple of time and that did it... ;D

Well actually I did something that my brain is still deciphering, practically I just read 1.000 websites and start extracting what I thought it could make the difference.

In one of those website I found this, all what I found was related to SDL so I had to make some modifications:

   unsigned long winHandle      = reinterpret_cast<size_t>(al_get_win_window_handle(al_get_current_display()));
   unsigned long winGlContext   = reinterpret_cast<size_t>(wglGetCurrentContext());

So here I'm kind of "serializing" the al_get_win_window_handle() value. Why? I don't know.

The same with the second function...

But what I think is the most important part is this one:

   misc["externalWindowHandle"] = StringConverter::toString(winHandle);
   misc["externalGLContext"]    = StringConverter::toString(winGlContext);
   misc["externalGLControl"]    = String("True");

Where after telling to Ogre to don't initialize (would create another display) with mRoot->initialise(false);, I'm then tell it to use this values instead.

As you can see it's not very clear, but boy I can already see a new tutorial on the wiki ;).

tobing
Member #5,213
November 2004
avatar

That's cool. Really cool.

Elias
Member #358
May 2000

Indeed. I'll download Ogre later and see if the example still works in Linux (if I can get it to work at all) - then commit your version if it does.

--
"Either help out or stop whining" - Evert

AMCerasoli
Member #11,955
May 2010
avatar

WTH is carring your avatar?...

Anyway...

Dario ff said:

So you ain't done yet.

Well in my defense, I have ran the example in my laptop, and works perfectly. I'm getting a:

WARNING: Stencil shadows were requested, but this device does not have a hardwar
e stencil. Shadows disabled.

Why? I dunno.

A question.. ¿?¿?¿:

If I set the GL context to Ogre, then I'm not going to be able to use normal drawing functions from Allegro?.

I ask this because before initializing Ogre, I'm able to draw with allegro, but once I init ogre, the function don't crash or anything but I can't see anything, just the 3d example.

Edited: Oh, By the way, thank you guys! :)

Elias
Member #358
May 2000

WTH is carring your avatar?...

A christmas tree (notice the green?) and a yellow box. It's not finished but I won't have time to finish most likely so replaced it anyway :P

Quote:

Stencil shadows were requested, but this device does not have a hardwar
e stencil. Shadows disabled.

What if you use al_set_display_option(ALLEGRO_STENCIL_SIZE, 8, ALLEGRO_SUGGEST)?

Quote:

If I set the GL context to Ogre, then I'm not going to be able to use normal drawing functions from Allegro?

Yes, unless you are willing to reset the complete OpenGL state to something Allegro expects (and then back to something Ogre expects).

--
"Either help out or stop whining" - Evert

AMCerasoli
Member #11,955
May 2010
avatar

Elias said:

A christmas tree

It looks more like a cucumber ;D.

Quote:

What if you use al_set_display_option(ALLEGRO_STENCIL_SIZE, 8, ALLEGRO_SUGGEST)?

That fixed it!, well I used al_set_new_display_option() instead.

Take that Dario!!. :D

Dario ff
Member #10,065
August 2008
avatar

Take that Dario!

>:(

Anyway, it's so nice to see this working so easily, I've been longing for using both A5 and Ogre for a long time. I've just recently began using Ogre as well, so this thread's timing is ideal. ;D

Do you dare to try using the Direct3D driver now? The OpenGL Ogre one is a bit glitchy with some addons. :P

TranslatorHack 2010, a human translation chain in a.cc.
My games: [GiftCraft] - [Blocky Rhythm[SH2011]] - [Elven Revolution] - [Dune Smasher!]

Felix-The-Ghost
Member #9,729
April 2008
avatar

Kinda related:
What's Ogre?

Not related:
A5 scares me :o
I need to learn it from A4 :(

==========================
<--- The ghost with the most!
---------------------------
[Website] [Youtube]

AMCerasoli
Member #11,955
May 2010
avatar

Dario ff said:

Do you dare to try using the Direct3D driver now? The OpenGL Ogre one is a bit glitchy with some addons.

Actually once when it appeared the platform screen, that which ask you if you want to run it as DirectX or OpenGL, I pressed DirectX :P, with all configured to work with OpenGL :o and it could create the display, import everything and start even rendering, the problem was that it as an horrendous flicker, that sometimes let me see only half of the screen. But I think it should be possible. Would need to read about how to get the directx context? :-/

What's Ogre?

Ogre is the equivalent of Allegro in the world of 3D. Actually should be OGRE since it is is an acronym: Object-Oriented Graphics Rendering Engine, but it happens the same than with Allegro which supposedly it's also an acronym, but you're not going around putting ALLEGRO 5 ALLEGRO 5 because it looks grotesque. Well not quite the equivalent of Allegro since it only allows you to draw 3D meshes on the screen, there is no events, keyboards, mouse, threads, UTF-8, file IO... It's just an OGRE.

But since we all know how difficult is to be able to create a mesh in Blender for example, and then import it to Allegro and draw it with raw OpenGL, Ogre is a wonderful tool to keep you away from that. That's only a tiny reason of course, if you get into 3D you'll know what I'm talking about.

OGRE among other things, is cross-platform (Windows, Linux, Mac and I thin even iPhone), it has a resource manager included, which allow you to load files from Zip files too (as Allegro 5), support shaders and a lot more.

And now combined with Allegro 5, you have really BIG and powerful tool on your hands, configurable within minutes.

Quote:

A5 scares me

What do you mean with that?, don't you like it?

Do you know what scares me? the size of the main .dll of Ogre... 11MB in relase mode... and... 310MB in debug mode :o, MY GOOOOD!!. And you also need to link to a lot of plugins!.

Dario ff
Member #10,065
August 2008
avatar

Only thing that annoys me about Ogre so far is that the only interface is C++. There was a project to write a C interface for it I think, but I think it was never finished.

Heck, I think it was even gonna use Allegro. :o
Low level C Ogre3D Interface(llcoi)

TranslatorHack 2010, a human translation chain in a.cc.
My games: [GiftCraft] - [Blocky Rhythm[SH2011]] - [Elven Revolution] - [Dune Smasher!]

Go to: