PDA

View Full Version : Unique ID Colors for Primitives

_new_horizon
05-04-2004, 06:12 AM
I'm using a selection mechanism which assigns an ID color to every primitive in the backbuffer so that it can be identified (picked) with the mouse. But my code is a fudge and only generates a limited number of colors. Can anyone provide code for generating *unique* colors for each object (10000 or more). 32 bit color is assumed.

I find some code over at http://users.frii.com/martz/oglfaq/#indx0110 (look under color) but is is incomplete. Any help appreciated!

-NiCo-
05-04-2004, 07:11 AM
If you have an unsigned integer value for representing the primitive IDs, you can try

GLuint id;

//assign id

red = id & 255;
green = (id>>8) & 255;
blue = (id>>12) & 255;
alpha = (id>>16) & 255;

>> is the right bit-shift operator and &255 is a masking of the 8 least significant bits.

This results in a binary like representation of the id, but with basis 256 instead of 2. red represents the least significant byte and alpha the most significant byte.

I hope this is what you're looking for.

Nico

_new_horizon
05-05-2004, 03:06 AM
So how do you use that in OpenGL when with glColor...()?

How many possible combinations will it allow?

Thanx

-NiCo-
05-05-2004, 03:30 AM
Use glColor4ub(red,green,blue,alpha) and replace the arguments with the values from my previous post. You can represent all possible unique 32 bit values.

Nico

yooyo
05-05-2004, 05:05 AM
Be aware of dithering... Disable it.

yooyo

_new_horizon
05-05-2004, 03:22 PM
Sorry to be a complete mong, but you could show me an OpenGL code snippit using this code?

Thanx for you're help guys! :)

plasmonster
05-05-2004, 03:51 PM
A minor correction to the code above:

red = id & 255;
green = (id>>8) & 255;
blue = (id>>16) & 255; // was 12
alpha = (id>>24) & 255; // was 16

What -NiCo- said should just work, just draw your object in this color:

glColor4ub(red,green,blue,alpha);

But, as yooyo pointed out, beware of dithering, blending, anti-aliasing, anything that will affect the final color on the screen.

-NiCo-
05-05-2004, 04:15 PM
Right, my bad :(

Thanks for the correction Portal

N.

plasmonster
05-05-2004, 04:27 PM
-NiCo-, I know you'd do the same for me. :)

Topic:

I guess gl picking should at least be mentioned here as an alternative. In my experience, a good picking setup simply can't be beaten. While somewhat more difficult to implement, it avoids some of the pitfalls of the color ID method.

_new_horizon
05-06-2004, 05:44 AM
Thanks for you're help guys! And do check out my other post, 'Picking Techniques'