public SceneManager(GameStatus theGameStatus, UserInputPlayer theUserInputPlayer, KeyBindings theKeyBindings, ResourceManager theResourceManager, RenderStatus theRenderStatus) { GameCore.TheGameCore.TheGameEventHandler += TheGameCore_TheGameEventHandler; this.width = theRenderStatus.Width; this.height = theRenderStatus.Height; this.theGameStatus = theGameStatus; this.theUserInputPlayer = theUserInputPlayer; this.theKeyBindings = theKeyBindings; this.theResourceManager = theResourceManager; this.TheRenderStatus = theRenderStatus; ReInitialize(); }
/// <summary> /// This one is nearly working perfectly. There is a small error which I think can be corrected using something like /// this /// mouse.Y = y + (ClientRectangle.Height - glview.Size.Height); /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <param name="modelViewMatrix"></param> /// <param name="projectionMatrix"></param> /// <param name="cameraPosition"></param> /// <returns></returns> public static Vector3 ConvertScreenToWorldCoords(int x, int y, Matrix4 modelViewMatrix, Matrix4 projectionMatrix, Vector3 cameraPosition, RenderStatus renderStatus) { int[] viewport = new int[4]; Gl.GetIntegerv(GetPName.Viewport, viewport); int[] zInt = new int[1]; Gl.ReadPixels(x, viewport[3] - y, 1, 1, PixelFormat.DepthComponent, PixelType.Float, zInt); byte[] bytes = BitConverter.GetBytes(zInt[0]); float z = BitConverter.ToSingle(bytes, 0); // http://www.songho.ca/opengl/gl_projectionmatrix.html // http://web.archive.org/web/20130416194336/http://olivers.posterous.com/linear-depth-in-glsl-for-real // The depth stored in the buffer [0 1]. float z_b = z; // The depth in the normalized device coordinates [-1 1]. float z_n = 2.0f * z_b - 1.0f; // The distance to the camera plane in grid units. float z_e = 2.0f * renderStatus.ZFar * renderStatus.ZNear / (renderStatus.ZFar + renderStatus.ZNear - (renderStatus.ZFar - renderStatus.ZNear) * (2.0f * z_b - 1.0f)); Vector3 mouse; // mouse.y = viewport[3] - y; // mouse.y =-( viewport[3] - y ); // mouse.Y = y + (ClientRectangle.Height - glview.Size.Height); mouse.x = x; mouse.y = y; //B mouse.z = z_n; //C Vector4 vector = UnProject(projectionMatrix, modelViewMatrix, new Size(viewport[2], viewport[3]), mouse); Vector3 distanceVec = -cameraPosition + vector.Xyz; if (distanceVec.Length > renderStatus.ZFar) { Vector3 distNormVec = distanceVec.Normalize(); vector.Xyz = cameraPosition + (distNormVec * renderStatus.ZFar * 0.99f); } Vector3 coords = new Vector3(vector.x, vector.y, vector.z); return(coords); }