Opengl picking problem (zip file) - Printable Version +- iDevGames Forums (http://www.idevgames.com/forums) +-- Forum: Development Zone (/forum-3.html) +--- Forum: Graphics & Audio Programming (/forum-9.html) +--- Thread: Opengl picking problem (zip file) (/thread-1961.html) Opengl picking problem (zip file) - papillon68 - Jan 4, 2009 02:40 AM I'm trying to get done with picking objects, but it doesn't work (I'm getting always a hit=TRUE value). I'm using the projected ray+sphere intersection tecnique but there is something wrong in the code. I am providing the complete, commented project zip file (it's very simple and "clean" since it contains just the test code): http://rapidshare.com/files/179621768/experimenting.zip.html I'd really appreciate if someone might help. I'm basically calling two function, one to generate the ray: // this is the function that creates the ray from x and y mouse position void wuRayFromMousePos( int x, int y ) { float value_fov = 0.7853; //45 degrees converted to radians float value_aspect = 320.0/480.0; //the aspect ratio for a 320 x 480 window, in my case float half_window_width = 160.0; float half_window_height = 240.0; float modifier_x; //mathematical handling of the difference between float modifier_y; //mouse position and the 'center' of the window float point[3]; //the untransformed ray float point_dist = 10.0; //it'll be put this far on the Z plane float camera_origin[3]; //this is where the camera sits, in 3dspace float point_xformed[3]; //this is the transformed point GLfloat pulledMatrix[16]; //pulledMatrix is the OpenGL matrix. float final_point[3]; //This is where the mouse position is turned into a mathematical //'relative' of 3d space. The conversion to an actual point modifier_x = tan( value_fov * 0.5 ) * (( 1.0 - x / half_window_width ) * ( value_aspect ) ); modifier_y = tan( value_fov * 0.5 ) * -( 1.0 - y / half_window_height ); //These 3 take our modifier_x/y values and our 'casting' distance //to throw out a point in space that lies on the point_dist plane. point[0] = modifier_x * point_dist; point[1] = modifier_y * point_dist; point[2] = point_dist; //openGL call to grab our MODELVIEW_MATRIX - glGetFloatv(GL_MODELVIEW_MATRIX, pulledMatrix); //Invert matrix results. //First, to get the camera_origin, we transform the 12, 13, 14 //slots of our pulledMatrix - this gets us the actual viewing //position we are 'sitting' at when the function is called camera_origin[0] = -(pulledMatrix[0] * pulledMatrix[12] + pulledMatrix[1] * pulledMatrix[13] + pulledMatrix[2] * pulledMatrix[14]); camera_origin[1] = -(pulledMatrix[4] * pulledMatrix[12] + pulledMatrix[5] * pulledMatrix[13] + pulledMatrix[6] * pulledMatrix[14]); camera_origin[2] = -(pulledMatrix[8] * pulledMatrix[12] + pulledMatrix[9] * pulledMatrix[13] + pulledMatrix[10] * pulledMatrix[14]); //Second, we transform the position we generated earlier - the '3d' //mouse position - by our viewing matrix. point_xformed[0] = -(pulledMatrix[0] * point[0] + pulledMatrix[1] * point[1] + pulledMatrix[2] * point[2]); point_xformed[1] = -( pulledMatrix[4] * point[0] + pulledMatrix[5] * point[1] + pulledMatrix[6] * point[2]); point_xformed[2] = -(pulledMatrix[8] * point[0] + pulledMatrix[9] * point[1] + pulledMatrix[10] * point[2]); final_point[0] = point_xformed[0] + camera_origin[0]; final_point[1] = point_xformed[1] + camera_origin[1]; final_point[2] = point_xformed[2] + camera_origin[2]; vector3_t ray_dir; ray_dir.x = final_point[0]; ray_dir.y = final_point[1]; ray_dir.z = final_point[2]; vector3_t ray_orig; ray_orig.x = camera_origin[0]; ray_orig.y = camera_origin[1]; ray_orig.z = camera_origin[2]; float SphereRadius = 0.5; GLboolean isHit = intersect_ray_sphere(ray_orig, ray_dir, SphereRadius); } The I'm doing sphere intersection: GLboolean intersect_ray_sphere( vector3_t ray_orig, vector3_t ray_dir, GLfloat sphere_radius ) { vector3_t sphereCenter; sphereCenter.x=sphereCenter.y=sphereCenter.z = -7; vector3_t v; v = ray_orig - sphereCenter; GLfloat b = 2.0f * scalar_product( ray_dir, v ); GLfloat c = scalar_product( v, v ) - sphere_radius*sphere_radius; GLfloat d = b * b - 4.0f * c; if ( d < 0.0f ) return GL_FALSE; d = sqrtf(d); GLfloat s0 = (d-b)*0.5f; GLfloat s1 = (-d-b)*0.5f; if ( s0 >=0.0f || s1 >= 0.0f ) return GL_TRUE; return GL_FALSE; } Finally, this is the code in the Draw routine: void Draw() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); renderSimpleTestQuad(); // this is drawn at 0,0,-5 wuRayFromMousePos(mouseX, mouseY); } Opengl picking problem (zip file) - chronus - Mar 1, 2009 08:49 PM hey, did you ever figure out a solution to your opengl picking problem?