Showing posts with label Ogre3D. Show all posts
Showing posts with label Ogre3D. Show all posts

Tuesday, August 21, 2007

Direct Draw video on Ogre Texture

Today I decided to try something different, following Hernan Moraldo's code snippet I made a video playing in the texture of an object:

fig.1 - movie playing in the texture of a knot mesh

I had to install the DirectX SDK to be able to use direct draw. The movie played synchronized and with sound.


Wednesday, August 15, 2007

Ogre: Object Picking

To create the effect that we are clicking on an object in Ogre we use a ray query. The ray is traced from the place were the mouse is and then goes perpendicularly to the screen. The query will result in a list of objects that were in the ray's way. An object is in the way, if its bounding box is in the way.

fig.1 - In this example if we click inside
the box that contains the Ogre's Head
it will play a sound

This is the code that I used, first we must declare a RaySceneQuery *mRaySceneQuery as a global, and then initialize it:

mRaySceneQuery = mSceneMgr->createRayQuery(Ray());
then in the mouse clicking event we make the following sorted query and try to see if the touched the Entity named "Head" :

bool GuiScene::mousePressed(const OIS::MouseEvent &arg, OIS::MouseButtonID id)
{
OgreApplication::mousePressed( arg, id );

mGUISystem->injectMouseButtonDown(convertOISMouseButtonToCegui(id));


CEGUI::Point mousePos = CEGUI::MouseCursor::getSingleton().getPosition();
Ray mouseRay = mCamera->getCameraToViewportRay(mousePos.d_x/float(mWindow->getWidth()), mousePos.d_y/float(mWindow->getHeight()));

mRaySceneQuery->setRay(mouseRay);
mRaySceneQuery->setSortByDistance(true);

// Execute query
RaySceneQueryResult &result = mRaySceneQuery->execute();
RaySceneQueryResult::iterator itr;

// Get results, create a node/entity on the position
for ( itr = result.begin(); itr != result.end(); itr++ )
{
if (itr->movable && itr->movable->getName() == "Head")
{
soundMgr->StopSound( &jaguarSoundChannel );
soundMgr->PlaySound( jaguarSound, headNode, &jaguarSoundChannel );
break;
} // if
}
return true;
}
Now if you touch the bounding box a jaguar sound will be heard.

Tuesday, August 14, 2007

Ogre: Multiple Viewports

fig.1 - Two viewports, one frontal fullscreen,
the other small left bottom with a sideways camera

To create multiple viewports in ogre3D it is necessary to add another viewport to the Rendering window. Viewports can use the same SceneManager with different cameras, or use completely different scene managers. Here's an example:

void GuiScene::createViewports()
{
// Create one viewport, entire window
Viewport* vp1 = mWindow->addViewport(mCamera);
vp->setBackgroundColour(ColourValue(0,0,0));

// Create one viewport, left bottom corner
Viewport* vp2 = mWindow->addViewport(mCamera,1,0,0.8,0.2,0.2);
vp2->setBackgroundColour(ColourValue(1,1,1));

// Alter the camera aspect ratio to match the viewport
mCamera->setAspectRatio(
Real(vp->getActualWidth()) / Real(vp->getActualHeight()));
}
Note that different viewports must have different Z values ( second argument in the addViewport function).

Sound with FMOD

There are many libraries that can be easily integrated with an Ogre3D project. We chose to use FMOD. This is a cool library that can play just about any sound, .wav, .mp3, .ogg, .whatever...

In addition to that I decided to use the Sound Manager available in the wiki. The good thing about it? You don't have to learn a thing about the FMOD library. It encapsulates all the major functions.

First we downloaded the FMOD Ex Programmers API and intalled it. Since we use VC++8 we linked with the fmodex_vc.lib library and added the include files.

After integrating the SoundManager in the project it is necessary to use the following code:

SoundManager *soundMgr = new SoundManager;
soundMgr->Initialize();

int jaguarSound = soundMgr->CreateSound(String("jaguar.wav"));
int music = soundMgr->CreateLoopedSound(String(music.mp3"));
int music2 = soundMgr->CreateStream(String(music.mp3")); //stream example

int jaguarSoundChannel = INVALID_SOUND_CHANNEL;
int musicSoundChannel = INVALID_SOUND_CHANNEL;

soundMgr->PlaySound( jaguarSound, mSceneNode, &jaguarSoundChannel );
soundMgr->PlaySound( music, mSceneNode, &musicSoundChannel );
First you create sounds and streams. Sounds are completely loaded to memory while streams are loaded in small chunks of data and are more suitable for large sounds.

When you play a sound the sound manager will create a sound channel for it. A sound can be played in multiple sound channels at the same time. Sound channels are useful if you want to stop the sound.

soundMgr->StopSound( &musicSoundChannel );

Each Sound Channel is associated with a SceneNode. This means that the farther the Node is from the origin the sound will be lower. The same applies to left and right movements. This is called 3D Sound and only is available to Sounds and not to Streams.



Friday, August 10, 2007

Cegui and Ogre

Now that the project is starting to become more complex, I felt that the use of a GUI would help. In Ogre's Wiki it is recommended to use CEGUI. CEGUI is a free library usefull for creating windows and widgets such as buttons, listboxes or textboxes.

Has a guide I used this tutorial available in the wiki here.

First of all, before going to the code, CEGUI has several configuration files, you can see an example in your [OGRE_HOME]/media/gui directory. This is what they do:

.tga - Image File with all the textures of all widgets

.looknfeel - XML file that describes the look of all widgets and their behaviour.

.schema - XML file where all the widgets are listed and all configurations are linked.

.layout - XML file describing the windows that you want to put in one sheet in the Scene.


For now we'll be using the default look and feel, so the only thing that we have to create is a .layout file. Here an example that creates a window with four buttons:

<?xml version="1.0" ?>
<GUILayout>
<Window Type="DefaultWindow" Name="DemoLayout">
<Window Type="TaharezLook/FrameWindow" Name="Main">
<Property Name="UnifiedSize" Value="{{0.35,0},{0.6,0}}" />
<Property Name="UnifiedPosition" Value="{{0.05,0},{0.05,0}}" />

<!-- <Property Name="RelativeMinSize" Value="w:0.2 h:0.2" />
<Property Name="RelativeMaxSize" Value="w:1.0 h:1.0" /> -->

<Property Name="Text" Value="Sound and Gui Test" />
<Property Name="CloseButtonEnabled" Value="False" />

<Window Type="TaharezLook/Button" Name="cmdQuit">
<Property Name="Text" Value="Quit" />
<Property Name="UnifiedPosition" Value="{{0.2,0},{0.8,0}}" />
<Property Name="UnifiedSize" Value="{{0.6,0},{0.07,0}}" />
</Window>
<Window Type="TaharezLook/Button" Name="cmdSound">
<Property Name="UnifiedPosition" Value="{{0.2,0},{0.6,0}}" />
<Property Name="UnifiedSize" Value="{{0.6,0},{0.07,0}}" />
<Property Name="Text" Value="Sound" />
</Window>
<Window Type="TaharezLook/Button" Name="cmdInstantAction">
<Property Name="UnifiedPosition" Value="{{0.2,0},{0.5,0}}" />
<Property Name="UnifiedSize" Value="{{0.6,0},{0.07,0}}" />
<Property Name="Text" Value="InstantAction" />
</Window>
<Window Type="TaharezLook/Button" Name="cmdMusic">
<Property Name="UnifiedPosition" Value="{{0.2,0},{0.7,0}}" />
<Property Name="UnifiedSize" Value="{{0.6,0},{0.07,0}}" />
<Property Name="Text" Value="Music" />
</Window>
</Window>
</Window>
</GUILayout>
Now we have to load this layout to our application:

Initialization :


mGUIRenderer = new CEGUI::OgreCEGUIRenderer(mWindow, Ogre::RENDER_QUEUE_OVERLAY, false, 3000, mSceneMgr);
mGUISystem = new CEGUI::System(mGUIRenderer);
CEGUI::Logger::getSingleton().setLoggingLevel(CEGUI::Informative);
CEGUI::SchemeManager::getSingleton().loadScheme((CEGUI::utf8*)"TaharezLookSkin.scheme");
mGUISystem->setDefaultMouseCursor((CEGUI::utf8*)"TaharezLook", (CEGUI::utf8*)"MouseArrow");

mGUISystem->setDefaultFont((CEGUI::utf8*)"BlueHighway-12");

CEGUI::Window* sheet = CEGUI::WindowManager::getSingleton().loadWindowLayout("myLayout.layout");
mGUISystem->setGUISheet( sheet );

// set the mouse cursor initially in the middle of the screen
mGUISystem->injectMousePosition((float)mWindow->getWidth() / 2.0f, (float)mWindow->getHeight() / 2.0f);

Creating Handler's for the buttons:


CEGUI::Window *win;

CEGUI::Window *m_win = CEGUI::WindowManager::getSingleton().getWindow( "Main" );
win = m_win->getChild("cmdQuit");
win->subscribeEvent( CEGUI::PushButton::EventClicked, CEGUI::Event::Subscriber( &GuiScene::handleQuit, this ));
bool GuiScene::handleQuit(const CEGUI::EventArgs& e)
{
this->stop();

return true;
}
Taking care of the IO:

In MouseListener class:

bool GuiScene::mouseMoved(const OIS::MouseEvent &mouseEventRef)
{
mGUISystem->injectMouseMove(mouseEventRef.state.X.rel, mouseEventRef.state.Y.rel);

return true;
}

bool GuiScene::mousePressed(const OIS::MouseEvent &arg, OIS::MouseButtonID id)
{
mGUISystem->injectMouseButtonDown(convertOISMouseButtonToCegui(id));

return true;
}

And that is it, this example is for Ogre 1.4.x and CEGUI 0.5.x for further explanations please look at the practical example. This is the final result:

fig.1 - CEGUI menu

Wednesday, July 18, 2007

Portuguese Characters

In the last thread I mencioned that we introduced MovableText. The text was fine, but it didn't suport special portuguese characters like L'ç' L'é' or L'À' . Here's somethings I learn today:

In C++ normal char's take 8 bit's while wide chars take 16 bits.

char:

char c = 'a';

wide char:

wchar_t wc = L'ç';

you have to put an L before the char literal to say that it is a wide char.
Theres also std::wstring for strings of wide chars.

In the MovableText class they use the Ogre::Font class. This class loads a true type font into the ogre system. It supports all types of UTF formats, but by default, it only loads to the memory the characters with unicode between 33 and 166. That's all fine if you're writing English, but if you're writing a language with accents like I am the first 166 characteres are just not enough.

I changed the range to [33, 255] before the Font's loading.

I added this code to MovableText in the setFontName function:
...
mpFont->addCodePointRange( Font::CodePointRange( 33, 255 ));
mpFont->load();
...

and there it is "ÇçáàéèíÍ..."

Tuesday, July 17, 2007

Mesh Exporting, Billboards, Movable Text

After importing my model with the help of an exporter, I started to animate the scene.
The wheels roll nicely and the car bounces a little bit.


fig.1 - Lexi Exporter


There are two more innovations in this scene.
I introduced the billboards which are textures that are always facing the camera.

BillboardSet* bbset = mSceneMgr->createBillboardSet("BBSet",1); bbset->setMaterialName("Examples/Flare"); Billboard* bb = bbset->createBillboard( Vector3( 0,0,0 ) ); FloatRect coords( 0.0, 0.0, 1.0, 1.0 ); bb->setTexcoordRect(coords); billboardNode->attachObject( bbset );

In fig.2 we can see a flare made with a billboard.

fig.2- Animated Car Scene with Flare and Label

We also added the MovableText class to put text on top of objects.

MovableText * msg = new MovableText("fireCarLabel", "Carro dos
bombeiros","Console",12,ColourValue(0.5f,1.0,0.5,1.0f));

Friday, July 13, 2007

Ogre Visual Studio 2005 port from 2003

In the weekend I felt that my computer was a complete mess. Nothing worked! I want a Mac! sniff Haaaaa! After 2 months of experiments I decided that a Windows reinstall was in order!
To all linux fans ( and I know lot's off them) I only use windows because after 4 days of configurations, I gave up installing Hardware 3D acceleration for OpenGL in my Notebook
(Ati Radeon 9700 Mobile with some weird taiwan tweeks). So...hell... I will be using Windows XP.

I started using Ogre with Visual Studio 2003 (7.1), this time I installed VS 2005 (8.0).
In the beginning nothing worked. To use Ogre3D and VS2005 it is very important for you to install service pack 1 of VS2005.

To make an Ogre Project download the SDK in website here. Download also the AppWizard for Visual Studio 8 here, this will allow you to easily create projects on VS. Finally follow the instruction of the wiki for install SDK and setting up. If you use the wizard you don't have to do most of the things.
fig.1 - Ogre Application Wizard

In this project I usually change some things in the project properties. In the debugging section I change the command and working Directory to the project's bin/Debug directory. The wizard usualy uses the SDK's bin directory.
In the linker Input section I usually add the ws2_32.lib because I need winsockets and winthreads.

Very important, don't forget to copy all the dll's and cfg's in the SDK bin directory to your project bin directory.

First Post: Visualization Component

Hi, after a weird first post months ago(deleted), I decided to follow the steps of André and start writing here on the forum.

I'm Rui Nóbrega and I am the person responsable for the visualization component of the project LifeSaver.

For that means I will be using an open source library called Ogre3D, this is a 3D engine used mainly in computer games. For now the objective is to render a 3D terrain of Guadiana river valley, and present some information on it.

To display the terrain we will be using an Ogre plugin, the Paging Landscape Scene Manager 2 or PLSM2 for short. Theres no official site, but this is a good starting point.

fig.1 - PLSM2 example with Guadiana river valley

For debug purposes I adapted Ogre Console, made some changes for it to work on Ogre 1.4 and to load C++ style class function instead of C function pointers.

There is also a Network component wich will connect to the simulation component made in Repast. This is still in open discussion. The main problem is that the simulation is being done in Java and I'm doing the visualization in C++. For now we are using TCP/IP socket's and threads.
Andre's using the java sockets I'm using winsocket's in C++.

I finally finished reading the Pro Ogre 3D Programming book. It's a nice book, I would recommend for you to learn Ogre from the wiki, feel confortable with it and then read the book to consolidate your knowledge.

Meanwhile the last days were spent learning Adobe Flash CS3 and explaining the portuguese culture to our american teacher Geoff Marslett who came from the University of Texas at Austin.

From now on I will try to make smaller posts.