Monday, December 3, 2007

Repast - Wave 2

This is a more complex situation for the wave.

Here we have the agents (green) and a lot of other feature layers.

The agents travel from their home locations (pink) to the safe points (red) ... not always through the safest path.


Saturday, November 3, 2007

Repast - Wave

Here goes a glimpse of the wave we're going to use with the simulation.

It uses geographical information to calculate it's boundaries, and a set of marks collected from the output of the scenario fluid simulation.

The progression of the wave follows a cellular automaton model to simulate the progression of a fluid between the marks.

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.



Monday, August 13, 2007

Repast Space: Windows Dynamics

So we have a window that can float over the Object2DGrid and display the objects inside its area.

But this only makes sense if there's a possibility of moving the window over the grid. We'll do that using events - from the keyboard to start with.

Events comprehend a programming paradigm commonly used in interactive programs. Repast provides ways to use event handlers through the registration of adapters, in this case, keyboard adapters.

As an example, we show how to handle events from the directional keys in order to navigate through the space with a display window:

ModelBuilder.java


// The default move values.
int MOVE_Y = 10, MOVE_X = 10;

...

private void moveViewWindow() {
display.setViewWindow(viewX, viewY, width, height);
DisplaySurface surf = model.getDisplaySurface();
surf.updateDisplayDirect();
}

public void buildDisplays() {

DisplaySurface surf = model.getDisplaySurface();

surf.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent evt) {
switch (evt.getKeyCode()) {
case KeyEvent.VK_DOWN:
if (viewY < space.getY() - MOVE_Y)
viewY += MOVE_Y;
break;

case KeyEvent.VK_UP:
if (viewY > MOVE_Y - 1)
viewY -= MOVE_Y;
break;

case KeyEvent.VK_LEFT:
if (viewX > MOVE_X - 1)
viewX -= MOVE_X;
break;

case KeyEvent.VK_RIGHT:
if (viewX < space.getX() - MOVE_X)
viewX += MOVE_X;
break;

default:

}
moveViewWindow();
}
});

display =
(ShapefileSpaceDisplay)space.getWindowDisplay();
width = display.getWidth();
height = display.getHeight();

surf.addDisplayableProbeable(display, "Window");
surf.display();
}

This will handle the keyboard events, triggered by the directional arrows, thus moving the display window over the space.

Every time the user moves the window, the area covered by it will change and so will the objects to be drawn.