   Ray picking and selection
  I've been having trouble with selection by ray-plane intersection. I broke the ray calculation up from the plane intersection testing. I want the selection to be used for moving to a point on the terrain/map. The problem I am having is that instead of rendering a point (like I want) it cuts out half of my terrain. I can provide pictures if it's necessary to see what I mean. If I comment out the function that I use to draw the point (commented) then the terrain doesn't dissapear. If I click again the terrain will come back (sometimes). I followed the ray-plane equations from this site.If anyone has an idea as to why it would cut out my terrain, and not draw a point like I want it would be greatly appreciated.//handling message for clicking the left mouse button   case WM_LBUTTONDOWN:   {  LPPOINT pt = new POINT;  LPRECT rect = new RECT;  GetCursorPos(pt);  ScreenToClient(hwnd,pt);  GetWindowRect(hwnd,rect);  pt->y = (rect->bottom - pt->y);  if(!CalcMouseRay(pt, Ray, Po)) MessageBox(hwnd,"Error calculating mouse cursor ray!","test",MB_OK);   }   break;//function used in the above message for calculating the ray from 2d screen coordsbool CalcMouseRay(LPPOINT cCoords, vec3 Ray, vec3 Po){    bool bSuccess = true;    glvec3 p1, p2;    double viewMatrix[16];    double projMatrix[16];    int viewport[4];    glGetDoublev(GL_MODELVIEW_MATRIX,viewMatrix);    glGetDoublev(GL_PROJECTION_MATRIX,projMatrix);    glGetIntegerv(GL_VIEWPORT,viewport);    if(!gluUnProject(cCoords->x,cCoords->y,0,viewMatrix,projMatrix,viewport,&p1[0],&p1[1],&p1[2])) bSuccess = false;    if(!gluUnProject(cCoords->x,cCoords->y,1,viewMatrix,projMatrix,viewport,&p2[0],&p2[1],&p2[2])) bSuccess = false;    Po[0] = p1[0];    Po[1] = p1[1];    Po[2] = p1[2];    Ray[0] = p2[0] - p1[0];    Ray[1] = p2[1] - p1[1];    Ray[2] = p2[2] - p1[2];    return bSuccess;}//and here's how I test the ray-plane intersection point, the function//DrawIntersectPoint() is the one that is messing up my terrain    if(type == MDL_COLLIDE_TERRAIN){  vec3 normal, tdRay;  float NPod, RayN, d;  GetUnitNormal(v3one,v3two,v3three,normal);  d = CalcD(v3one,normal);  NPod = v3DotProduct(normal,v3one) + d;  RayN = v3DotProduct(Ray,normal);  tdRay[0] = Ray[0]*NPod/RayN;  tdRay[1] = Ray[1]*NPod/RayN;  tdRay[2] = Ray[2]*NPod/RayN;  Point[0] = Po[0]-tdRay[0];  Point[1] = Po[1]-tdRay[1];  Point[2] = Po[2]-tdRay[2];  if(WithinPolygon(Point, v_point1, v_point2, v_point3)){ DrawIntersectPoint(Point); //THIS FUNCTION CAUSES PROBLEMS  }   }//the function used to draw the pointbool DrawIntersectPoint(vec3 P){    glPointSize(5);    glBegin(GL_POINT);    glVertex3fv(P);    glEnd();    glPointSize(1);    return true;}
  Just a blind guess, try:bool DrawIntersectPoint(vec3 P){    glPushMatrix();    glLoadIdentity();    glPointSize(5);    glBegin(GL_POINT);    glVertex3fv(P);    glEnd();    glPointSize(1);    glPopMatrix();    return true;};  I tried that, didn't work. I found out why my terrain was dissapearing. The glBegin(GL_POINT) was interfering with the glBegin(GL_TRIANGLES) from the previous loops. That is now taken care of so my terrain does not vanish. However, I'm having a problem with being able to render the ray as an actual line. Whenever I click the ray does not appear and I have no idea why. I changed the code to the following. Does the ray have to be repositioned to the camera's coordinates? I figured not since the camera does not move and the world moves around the camera. This should be fairly simple but it's driving me nuts.bool DrawIntersectPoint(vec3 Ray){    glBegin(GL_LINE);    glPointSize(10.0);    glColor3f(1.0f,1.0f,1.0f);    glVertex3fv(Ray);    Ray[0]*=20;    Ray[1]*=20;    Ray[2]*=20;    glVertex3fv(Ray);    glPointSize(1.0);    glEnd();    return true;}//and correcting this so the y-coordinate is inverted so opengl uses the correct y value   case WM_LBUTTONDOWN:   {  LPPOINT pt = new POINT;  LPRECT rect = new RECT;  GetCursorPos(pt);  ScreenToClient(hwnd,pt);  GetWindowRect(hwnd,rect);  pt->y = (rect->bottom - pt->y);  if(!CalcMouseRay(pt, Ray, Po)) MessageBox(hwnd,"Error calculating mouse cursor ray!","test",MB_OK);   }   break;//changed this around a little but it's still virtually the same in functionalitybool CalcMouseRay(LPPOINT cCoords, vec3 Ray, vec3 Po){    bool bSuccess = true;    vec3 p2;    double dx, dy, dz;    double viewMatrix[16];    double projMatrix[16];    int viewport[4];    glGetDoublev(GL_MODELVIEW_MATRIX,viewMatrix);    glGetDoublev(GL_PROJECTION_MATRIX,projMatrix);    glGetIntegerv(GL_VIEWPORT,viewport);    if(!gluUnProject(cCoords->x,cCoords->y,0,viewMatrix,projMatrix,viewport,&dx,&dy,&dz)) bSuccess = false;    Po[0] = dx;    Po[1] = dy;    Po[2] = dz;    if(!gluUnProject(cCoords->x,cCoords->y,1,viewMatrix,projMatrix,viewport,&dx,&dy,&dz)) bSuccess = false;    p2[0] = dx;    p2[1] = dy;    p2[2] = dz;    Po[0] = xpos;    Po[1] = ypos;    Po[2] = zpos;    Ray[0] = Po[0] - p2[0];    Ray[1] = Po[1] - p2[1];    Ray[2] = Po[2] - p2[2];    return bSuccess;};  Ok I think I have it set up correctly now. I can draw a line that represents the picking ray. The problem I was having before is I was passing a pointer, and was changing that pointer in the function so essentially it moved out to infinity faster than I could see it. Next step is to rotate the ray so it represent my true picking ray from any orientation (and not just looking along z-axis). If anyone has an idea on this it'd be helpful, otherwise I have plenty to still work on :)   