[C#, OpenGL] 3D coordinates to 2D coordinates


This code will convert 3D coordinates to 2D coordinates. It is taken from around the web and put together to work with C# and OpenGL (OpenTK actually). If you use something else you will probably have to change the code somewhat, but that should not be a huge problem I think.
private Point WorldToScreen(Vector3 p)
    {
      Matrix4 model, proj;
      int[] view = new int[4];

      GL.GetFloat(GetPName.ModelviewMatrix, out model);
      GL.GetFloat(GetPName.ProjectionMatrix, out proj);
      GL.GetInteger(GetPName.Viewport, view);

      double wx = 0, wy = 0, wz = 0;

      int d = Glu.gluProject
                      (
                        p.X, 
                        p.Y, 
                        p.Z, 
                        model, 
                        proj, 
                        view, 
                        ref wx, 
                        ref wy, 
                        ref wz
                      );

      return new Point((int)wx, (int)wy);
    }
int gluProject
  (
   float objx, 
   float objy, 
   float objz, 
   Matrix4 modelMatrix, 
   Matrix4 projMatrix, 
   int[] viewport, 
   ref double winx, 
   ref double winy, 
   ref double winz
  )
  {
      Vector4 _in;
      Vector4 _out;

      _in.X = objx;
      _in.Y = objy;
      _in.Z = objz;
      _in.W = 1.0f;
      //__gluMultMatrixVecd(modelMatrix, in, out); // Commented out by code author
      //__gluMultMatrixVecd(projMatrix, out, in);  // Commented out by code author
      //TODO: check if multiplication is in right order
      _out = Vector4.Transform(_in, modelMatrix);
      _in = Vector4.Transform(_out, projMatrix);

      if (_in.W == 0.0)
        return (0);
      _in.X /= _in.W;
      _in.Y /= _in.W;
      _in.Z /= _in.W;
      /* Map x, y and z to range 0-1 */
      _in.X = _in.X * 0.5f + 0.5f;
      _in.Y = _in.Y * 0.5f + 0.5f;
      _in.Z = _in.Z * 0.5f + 0.5f;

      /* Map x,y to viewport */
      _in.X = _in.X * viewport[2] + viewport[0];
      _in.Y = _in.Y * viewport[3] + viewport[1];

      winx = _in.X;
      winy = _in.Y;
      winz = _in.Z;
      return (1);
  }