Skip to main content

History: Selection Buffer

Preview of version: 23

From the Famfamfam Silk icon collection, a white page with black text. Description

The following classes are a C++ implementation of a pixel perfect object selection mechanism.

It works by rendering a single frame to a texture with custom substituted materials that colorize each entity with a unique color (up to 224 colors/objects). The MaterialSwitcher class saves which color belongs to which entity. The SelectionBuffer class then retrieves the correct Entity from the SceneManager. It does not work on StaticGeometry or any other geometry that isn't independent entities, in Ogre terms.

These classes are a C++ port of the Python version found here. The behavior of the C++ version differs from the original Python version in one notable way: render colors are assigned sequentially, rather than randomly. This makes interpreting the debug overlay more visually difficult, but simplifies the code somewhat.


%ogre17%%ogre16%

From the Famfamfam Silk icon collection, a green arrow pointing down. Download

Downloadable 7-Zip archive of all eight source files:
From the Famfamfam Silk icon collection, a white page with the yellow compressed icon reminiscent of the Winzip icon. SelectionBufferSource.7z

From the Famfamfam Silk icon collection, a white page with blue 'C++' text. Usage

The following fragments won't work as-is. You'll need to put them in appropriate places to integrate them with your code.

First the necessary include:

Copy to clipboard
#include "SelectionBuffer.h"

Then initialization:

Copy to clipboard
// Hang on to this pointer somewhere (e.g. a member of a user interface manager class) SelectionBuffer *mSelectionBuffer; // SceneManager *sceneManager; (previously created) // RenderWindow *renderWindow; (which can come from calling Ogre::Root::initialise(), among other places) mSelectionBuffer = new SelectionBuffer(sceneManager, renderWindow);

You'll also need to make sure the material scripts are loaded using the Ogre resource system. (noteSample code not shown.)

Then process every frame:

Copy to clipboard
// showSelectionDebugOverlay is a boolean whose value is determined by, for instance, a key press if(mSelectionBuffer) mSelectionBuffer->ShowOverlay(showSelectionDebugOverlay); Entity *selectedEntity; // rightMouseClicked is a boolean whose value came from, for instance, // an OIS::MouseListener mouseReleased(const OIS::MouseEvent &arg, OIS::MouseButtonID id) method if(rightMouseClicked) { if(mSelectionBuffer) { // mouseAbsX and mouseAbsY are the absolute coordinates of the click from, for instance, an OIS::MouseState selectedEntity = mSelectionBuffer->OnSelectionClick(mouseAbsX, mouseAbsY); if(selectedEntity != 0) { LogManager::getSingleton().logMessage("Selected " + selectedEntity->getName()); // Do something useful to the selected entity here } else { LogManager::getSingleton().logMessage("Entity not found"); } } }

[+] From the Famfamfam Silk icon collection, a white page with blue 'C++' text. Source

The Ogre eye 16x16 icon. Compatibility

This code is known to work in Ogre 1.6 and Ogre 1.7.

History

Information Version
Wed 21 of Mar, 2012 21:00 GMT-0000 brunobg Added line mRenderTexture->getViewport(0)->setShadowsEnabled(false); so code works with shadows 25
Tue 05 of Apr, 2011 18:52 GMT-0000 DragonM Add background color specification to floated table of contents div, which makes it opaque. Removed clear dynamic variable since the opaque div covers section header horizontal rules. 24
Tue 05 of Apr, 2011 18:11 GMT-0000 DragonM Add description section title. 23
Tue 05 of Apr, 2011 01:38 GMT-0000 DragonM Fix section indents, add icons, add download link to source archive, reorder sections, float table of contents and collapse source sections since they're less important with a download. 22
Fri 30 of Apr, 2010 18:50 GMT-0000 jacmoe 21
Fri 30 of Apr, 2010 18:44 GMT-0000 jacmoe 20
Fri 30 of Apr, 2010 18:43 GMT-0000 jacmoe 19
Fri 30 of Apr, 2010 18:42 GMT-0000 jacmoe 18
Fri 30 of Apr, 2010 18:42 GMT-0000 jacmoe 17
Fri 30 of Apr, 2010 18:41 GMT-0000 jacmoe 16
Fri 30 of Apr, 2010 18:41 GMT-0000 jacmoe 15
Fri 30 of Apr, 2010 18:40 GMT-0000 jacmoe 14
Fri 30 of Apr, 2010 18:35 GMT-0000 jacmoe 13
Fri 30 of Apr, 2010 18:33 GMT-0000 jacmoe 12
Fri 30 of Apr, 2010 18:32 GMT-0000 jacmoe 11
Fri 30 of Apr, 2010 18:27 GMT-0000 jacmoe 10
Fri 30 of Apr, 2010 18:26 GMT-0000 jacmoe 9
Fri 30 of Apr, 2010 18:26 GMT-0000 jacmoe 8
Fri 30 of Apr, 2010 18:25 GMT-0000 jacmoe 7
Fri 30 of Apr, 2010 18:24 GMT-0000 jacmoe 6
Fri 30 of Apr, 2010 18:23 GMT-0000 jacmoe 5
Fri 30 of Apr, 2010 18:21 GMT-0000 jacmoe 4
Fri 30 of Apr, 2010 18:19 GMT-0000 jacmoe 3
Fri 30 of Apr, 2010 18:18 GMT-0000 jacmoe 2
Fri 30 of Apr, 2010 18:15 GMT-0000 jacmoe 1