   Detecting when you click on a object
  I have a program I am writing that I am having issues detecting mouse clicks. I am using OpenGL and MFC. I've gotten an unproject function that looks like: CVector3 GetOGLPos(int x, int y) {GLint viewport[4];GLdouble modelview[16],projection[16];GLfloat winX,winY,winZ;GLdouble posX,posY,posZ;glGetDoublev(GL_MODELVIEW_MATRIX,modelview);glGetDoublev(GL_PROJECTION_MATRIX,projection);glGetIntegerv(GL_VIEWPORT,viewport);winX=(GLfloat)x;winY=(GLfloat)viewport[3]-(GLfloat)y;glReadPixels((GLint)winX,(GLint)winY,1,1,GL_DEPTH_COMPONENT,GL_FLOAT,&winZ);gluUnProject(winX,winY,winZ,modelview,projection,viewport,&posX,&posY,&posZ);return CVector3(posX,posY,posZ);}Now, I send all of my objects in as 3 points to a triangle to see if at any point my mouse click is in that triangle. This code, amongst others I have tried have failed at telling me if I have clicked on it. bool CTriangle::Intersects(const CRay &ray) {CVector3 e1,e2;e1=points[1]-points[0];e2=points[2]-points[0];CVector3 p=ray.GetDirection().Cross(e2);GLdouble a=e1.Dot(p);if(a==0)return false;GLdouble f=1.0/a;CVector3 s=ray.GetOrigin() - points[0];GLdouble u=f*s.Dot(p);if(u < 0.0 || u > 1.0)return false;CVector3 q=s.Cross(e1);GLdouble v = f*ray.GetDirection().Dot(q);if(v < 0.0 || v > 1.0)return false;GLdouble t=f*e2.Dot(q);return (t >= 0.0);}I would appreciate any help, even if it's just a point in the right direction. I can post more of the code as needed as well.I've tried a couple of different methods of detecting the mouse click. Also I've tried modifying the gluPerspective in the hopes it would do something. I know my coordinates as I move the mouse with a caption I have in my program. But I am still very confused on why it doesn't work. Thanks in advanced.   
  You ought to be able to determine which of those two functions isn't working with your debugger.Try to click on an object, and then step through the intersects algorithm for that object. Look at the ray you're passing and your "points" and check if the ray really goes through that triangle.   ;  Output of the debugger when i click in the middle.-this0x0031f1bc {points=0x0031f1bc }CTriangle * const-points0x0031f1bc {x=-9.8964846849788000 y=6.7216719661841333 z=-20.000009059909800 }CVector3 [3]+[0]{x=-9.8964846849788000 y=6.7216719661841333 z=-20.000009059909800 }CVector3+[1]{x=-5.9279695230825391 y=6.7216719661841333 z=-20.000009059909800 }CVector3+[2]{x=-9.8964846849788000 y=2.9267796753126483 z=-20.000009059909800 }CVector3-ray{origin={...} direction={...} }const CRay &+origin{x=-9.4500267292654705 y=5.0598563878286447 z=0.00000000000000000 }CVector3+direction{x=-9.4500267292654705 y=5.0598563878286447 z=-20.000009059909800 }CVector3t-9.2559631349317831e+061double-e1{x=3.9685151618962609 y=0.00000000000000000 z=0.00000000000000000 }CVector3x3.9685151618962609doubley0.00000000000000000doublez0.00000000000000000double-q{x=-9.2559631349317831e+061 y=-9.2559631349317831e+061 z=-9.2559631349317831e+061 }CVector3x-9.2559631349317831e+061doubley-9.2559631349317831e+061doublez-9.2559631349317831e+061doublef-0.0033200323064434250double-s{x=0.44645795571332947 y=-1.6618155783554887 z=20.000009059909800 }CVector3x0.44645795571332947doubley-1.6618155783554887doublez20.000009059909800doubleu-2.2687499999999998double-e2{x=0.00000000000000000 y=-3.7948922908714851 z=0.00000000000000000 }CVector3x0.00000000000000000doubley-3.7948922908714851doublez0.00000000000000000doublea-301.20188832476970doublev-9.2559631349317831e+061double-p{x=-75.897880198811563 y=0.00000000000000000 z=35.861833583419006 }CVector3x-75.897880198811563doubley0.00000000000000000doublez35.861833583419006double