chalamov

03-30-2008, 08:05 PM

http://www.opengl.org/resources/faq/technical/selection.htm

What I try to do it's to generate an event when I click on object in my 3D univers

Someone can give me an example using this approach with opengl and win 32 api?

I will really appreciate!

/////////////////////////////////////////////////////////////////////

Another method is to generate the ray in eye coordinates, and transform it by the inverse of the ModelView matrix. In eye coordinates, the pick ray origin is simply (0, 0, 0). You can build the pick ray vector from the perspective projection parameters, for example, by setting up your perspective projection this way

aspect = double(window_width)/double(window_height); glMatrixMode( GL_PROJECTION ); glLoadIdentity(); glFrustum(-near_height * aspect, near_height * aspect, -near_height, near_height, zNear, zFar );

you can build your pick ray vector like this:

int window_y = (window_height - mouse_y) - window_height/2; double norm_y = double(window_y)/double(window_height/2); int window_x = mouse_x - window_width/2; double norm_x = double(window_x)/double(window_width/2);

(Note that most window systems place the mouse coordinate origin in the upper left of the window instead of the lower left. That's why window_y is calculated the way it is in the above code. When using a glViewport() that doesn't match the window height, the viewport height and viewport Y are used to determine the values for window_y and norm_y.)

The variables norm_x and norm_y are scaled between -1.0 and 1.0. Use them to find the mouse location on your zNear clipping plane like so:

float y = near_height * norm_y; float x = near_height * aspect * norm_x;

Now your pick ray vector is (x, y, -zNear).

To transform this eye coordinate pick ray into object coordinates, multiply it by the inverse of the ModelView matrix in use when the scene was rendered. When performing this multiplication, remember that the pick ray is made up of a vector and a point, and that vectors and points transform differently. You can translate and rotate points, but vectors only rotate. The way to guarantee that this is working correctly is to define your point and vector as four-element arrays, as the following pseudo-code shows:

float ray_pnt[4] = {0.f, 0.f, 0.f, 1.f}; float ray_vec[4] = {x, y, -near_distance, 0.f};

The one and zero in the last element determines whether an array transforms as a point or a vector when multiplied by the inverse of the ModelView matrix.

////////////////////////////////////////////

What I try to do it's to generate an event when I click on object in my 3D univers

Someone can give me an example using this approach with opengl and win 32 api?

I will really appreciate!

/////////////////////////////////////////////////////////////////////

Another method is to generate the ray in eye coordinates, and transform it by the inverse of the ModelView matrix. In eye coordinates, the pick ray origin is simply (0, 0, 0). You can build the pick ray vector from the perspective projection parameters, for example, by setting up your perspective projection this way

aspect = double(window_width)/double(window_height); glMatrixMode( GL_PROJECTION ); glLoadIdentity(); glFrustum(-near_height * aspect, near_height * aspect, -near_height, near_height, zNear, zFar );

you can build your pick ray vector like this:

int window_y = (window_height - mouse_y) - window_height/2; double norm_y = double(window_y)/double(window_height/2); int window_x = mouse_x - window_width/2; double norm_x = double(window_x)/double(window_width/2);

(Note that most window systems place the mouse coordinate origin in the upper left of the window instead of the lower left. That's why window_y is calculated the way it is in the above code. When using a glViewport() that doesn't match the window height, the viewport height and viewport Y are used to determine the values for window_y and norm_y.)

The variables norm_x and norm_y are scaled between -1.0 and 1.0. Use them to find the mouse location on your zNear clipping plane like so:

float y = near_height * norm_y; float x = near_height * aspect * norm_x;

Now your pick ray vector is (x, y, -zNear).

To transform this eye coordinate pick ray into object coordinates, multiply it by the inverse of the ModelView matrix in use when the scene was rendered. When performing this multiplication, remember that the pick ray is made up of a vector and a point, and that vectors and points transform differently. You can translate and rotate points, but vectors only rotate. The way to guarantee that this is working correctly is to define your point and vector as four-element arrays, as the following pseudo-code shows:

float ray_pnt[4] = {0.f, 0.f, 0.f, 1.f}; float ray_vec[4] = {x, y, -near_distance, 0.f};

The one and zero in the last element determines whether an array transforms as a point or a vector when multiplied by the inverse of the ModelView matrix.

////////////////////////////////////////////