Advertisement

how to implement picking for irregular texture, just need scheme.

Started by January 11, 2018 06:56 AM
11 comments, last by dream rz 7 years ago

 

In your first post you have this function:


/*picking function*/
bool picking(int x,int y)
{
    int mouse_to_image_x, mouse_to_image_y;//get mouse position in the image
    mouse_to_image_x = 30 - x;
    mouse_to_image_y = 30 - y;
    if((mouse_to_image_x < 0 && mouse_to_image_x > 7) && (mouse_to_image_y < 0 && mouse_to_image_y > 7))return false;//mouse is not in the image
    for(int i = 0; i < 7 * 7; i++)
    {
        if(pixel_alpha[mouse_to_image_x][mouse_to_image_y] == 255)return true;//in the image and alpha channel is 255
    }
    return false;
}

 

and this data structure


BYTE pixel_alpha[7][7];//save alpha channel matrix

 

If you have gone to the trouble to store hittable bits in your own data structure wouldn't you be better of just looking up the alpha value at a given location instead of iterating in a for loop searching for it 


// something like this?
lookup_x = mouse_click_x - location_of_object_x;
lookup_y = mouse_click_y - location_of_object_y;

if(   (lookup_x < 0 || lookup_x > 7) 
   || (lookup_y < 0 || lookup_y > 7))
{
  // WAS: OOPS had && but can't be less than 0 and greater than 7 
  // NOW: changed to ||
  return 0;//mouse is not in the image
}

hit = pixel_alpha[lookup_x][lookup_y];

return hit;

 

Say mouse_x is at 355 and object corner is at 351 you need to lookup 355-351 = 4 and a similar thing for y.

Also you are storing value 255 in a BYTE, I presume a BYTE is 8 bits, so it'll cost 8 bits for each texture bit. Wouldn't you be better off using 1 bit per bit (hope that makes sense).

Sorry I can't be more specific since I'm not sure what you're trying to do, these days I would use modern OpenGL and vector graphics and I'd render to an off-screen frame-buffer, each object in its own color with unhittable objects left as black then I'd use glGetPixels to decide what was hit. So called "Color-Based Mouse Picking" ... It requires a lot more code than this - but it gets to run on a "hardware" accelerated board so probably worth it?

Cheers.

So, it is really a dilemma.
For the last few years, I was always “shape pickup”.
To tell the truth, just about enough of this.

This topic is closed to new replies.

Advertisement