# Thread: Unique ID Colors for Primitives

1. ## 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!

2. ## Re: Unique ID Colors for Primitives

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

3. ## Re: Unique ID Colors for Primitives

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

How many possible combinations will it allow?

Thanx

4. ## Re: Unique ID Colors for Primitives

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

5. ## Re: Unique ID Colors for Primitives

Be aware of dithering... Disable it.

yooyo

6. ## Re: Unique ID Colors for Primitives

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

Thanx for you're help guys!

7. ## Re: Unique ID Colors for Primitives

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.

8. ## Re: Unique ID Colors for Primitives

Thanks for the correction Portal

N.

9. ## Re: Unique ID Colors for Primitives

-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.

10. ## Re: Unique ID Colors for Primitives

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

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•