Selection buffer, pixel perfect picking, cg output color wro

Problems building or running the engine, queries about how to use features etc.
Post Reply
jonim8or
Goblin
Posts: 287
Joined: Mon Dec 08, 2008 4:49 pm
x 10

Selection buffer, pixel perfect picking, cg output color wro

Post by jonim8or »

I'm using selectionbuffer-picking, meaning I make an offscreen render in which every selectable object has a different color, and non-selectable objects are not rendered. My solution is based on this example from the pyogre wiki, but then in C++.

In my implementation I use cg shaders and the customparameter option to achieve this. This way I can also support features like transparency in my picking. for every selectable object that I create, I generate a distinct random Vec4, which is passed to the cg script. All the cg script does, is return the color.

However, my problem is the accuracy. The color I get back from picking, is close, but not precisely the original color. I tried fixing this with a bias factor of 0.05 for each of the values, but this factor is relatively big, and still sometimes the selected object is still not recognized. My guess is that things go wrong in the conversion from color to vector, or from selected colour to colour. Here are my conversions:
Creating a random color vector

Code: Select all

		Real r = Math::RangeRandom(0, 1);
		Real g = Math::RangeRandom(0, 1);
		Real b = Math::RangeRandom(0, 1);
		ColourValue possible_color(r,g,b);
		color = Vector4(possible_color.r,possible_color.g,possible_color.b,1);
Converting the picked value to a vector

Code: Select all

		Image::Box reading_box(x, y, x+1, y+1);
		ARGB data;
		PixelBox read_data(1, 1, 0, picking_texture->getFormat(), &data);
		HardwarePixelBufferSharedPtr buffer = picking_texture->getBuffer(0, 0);
		buffer->blitToMemory(reading_box, read_data);
		ColourValue found_colour;
		found_colour.setAsARGB(data);
		Vector4 color(found_colour.r, found_colour.g, found_colour.b, found_colour.a);
These are the results:
Selected color: Vector4(0.0705882, 0.94902, 0.52549, 1)
found color:Vector4(0.0697553, 0.949327, 0.525995, 1)

Selected color: Vector4(0.639216, 0.352941, 0.686275, 1)
found color:Vector4(0.639979, 0.354049, 0.687861, 1)
Is there a way to get the colour more accurately?
User avatar
enkuklios
Gnoblar
Posts: 23
Joined: Fri Nov 04, 2005 5:33 am

Re: Selection buffer, pixel perfect picking, cg output color

Post by enkuklios »

Maybe use a packed int as RGBA instead of 4 floats? This would still support well more objects then you will ever have on screen and would always be accurate.
Nodrev
Gremlin
Posts: 193
Joined: Fri Jan 25, 2008 6:55 pm
Location: Nantes / France
x 17

Re: Selection buffer, pixel perfect picking, cg output color

Post by Nodrev »

For those whose got the same problem, it may due to the fog activated on the scene (I encounter this problem when I implemented selection buffer), it slightly modify the final color.
So, deactivate fog on the scene before rendering to the texture, and reactivate it after.
Post Reply