Unique ID Colors for Primitives

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!

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

So how do you use that in OpenGL when with glColor…()?

How many possible combinations will it allow?

Thanx

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

Be aware of dithering… Disable it.

yooyo

Sorry to be a complete mong, but you could show me an OpenGL code snippit using this code?

Thanx for you’re help guys! :slight_smile:

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.

Right, my bad :frowning:

Thanks for the correction Portal

N.

-NiCo-, I know you’d do the same for me. :slight_smile:

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.

Thanks for you’re help guys! And do check out my other post, ‘Picking Techniques’