Archived

This topic is now archived and is closed to further replies.

Object Picking + Floating Point Error

This topic is 5594 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I''ve got a problem with object picking: I''m rendering each object with an unique color like this: for (int t=0; t<60; t++) {glColor3f((float)t/60,0,0); RenderObject(t);} ... Get the pixel color at the cursor pos ... So I want to select object 2 and clicked on it. The r value in float is 0.031373, if I''m converting it to an integer (to get the object nr), I get 1 (final float after * 60 was 1,88238). So how can I round the floating point number to the nearest integer or is there anything else wrong?

Share this post


Link to post
Share on other sites
Casting will truncate the float into an int; from there it fairly easy to round.

int roundf(float f) {
int a = (int) f;
if (f < a+0.5f) return a;
return a+1;
}

Share this post


Link to post
Share on other sites
why not the following:

your color is really only 8bit anyway, so think about object IDs 0-255.
Now, the color = (float)ID/255.0.
When you read the color back, the ID = (int)(color * 255.0).

It''s basically the same as what you have, but is a little more explicit about how the number is quantized.

Share this post


Link to post
Share on other sites