private void Form1_MouseDoubleClick(object sender, MouseEventArgs e) { Vector2 cursorpos = new Vector2((float)e.X, (float)e.Y); Vector3 pickingPoint = CRay.GetPickingPoint(this.device, this.myTerrain.v3vertices, cursorpos, this.worldMatrix, this.viewMatrix, this.projectionMatrix); if (pickingPoint != Vector3.Empty) { this.CenterX = pickingPoint.X / (float)this.myTerrain.WIDTH; this.CenterY = pickingPoint.Y / (float)this.myTerrain.HEIGHT; } }
private void timer1_Tick(object sender, EventArgs e) { Vector2 cursorpos = new Vector2((float)this.LastMouseX2, (float)this.LastMouseY2); Vector3 pickingPoint = CRay.GetPickingPoint(this.device, this.myTerrain.v3vertices, cursorpos, this.worldMatrix, this.viewMatrix, this.projectionMatrix); if (pickingPoint != Vector3.Empty) { this.DoCao = -pickingPoint.Z * this.myTerrain.SCALE_FACTOR; this.OnPaint(); } else { this.DoCao = 0f; this.OnPaint(); } this.DoCao = 0f; this.timer1.Enabled = false; }
public static Vector3 GetPickingPoint(Device device, Vector3[] v3vertices, Vector2 cursorpos, Matrix worldMatrix, Matrix viewMatrix, Matrix projectionMatrix) { Vector3 result = Vector3.Empty; Ray ray = CRay.CalculateCursorRay(device, cursorpos, projectionMatrix, viewMatrix, worldMatrix); bool flag; Vector3 item; Vector3 item2; Vector3 item3; float num; float num2; if (CRay.RayIntersectsTerrain(ray, v3vertices, worldMatrix, out flag, out item, out item2, out item3, out num, out num2).HasValue) { List <Vector3> list = new List <Vector3>(); list.Add(item); list.Add(item2); list.Add(item3); result = (1f - num - num2) * list[0] + num * list[1] + num2 * list[2]; } return(result); }
private static float?RayIntersectsTerrain(Ray ray, Vector3[] vertices, Matrix modelTransform, out bool insideBoundingSphere, out Vector3 vertex1, out Vector3 vertex2, out Vector3 vertex3, out float ptriangleU, out float ptriangleV) { vertex1 = (vertex2 = (vertex3 = new Vector3(0f, 0f, 0f))); insideBoundingSphere = true; float?num = null; ptriangleU = 0f; ptriangleV = 0f; for (int i = 0; i < vertices.Length; i += 3) { float num2 = 0f; float num3 = 0f; float?num4; CRay.RayIntersectsTriangle(ref ray, ref vertices[i], ref vertices[i + 1], ref vertices[i + 2], out num4, out num2, out num3); if (num4.HasValue) { if (num.HasValue) { float?num5 = num4; float?num6 = num; if (num5.GetValueOrDefault() >= num6.GetValueOrDefault() || !(num5.HasValue & num6.HasValue)) { goto IL_10E; } } num = num4; vertex1 = vertices[i]; vertex2 = vertices[i + 1]; vertex3 = vertices[i + 2]; ptriangleU = num2; ptriangleV = num3; } IL_10E :; } return(num); }