private void GetSelection3D(MouseEventArgs e) { device.Transform.View = Matrix.LookAtLH(CamEye, CamEye + CamDir, new Vector3(0.0f, 0.0f, 1.0f)); device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, 1.0f, 1.0f, 1000000.0f); Vector3 near = Vector3.Unproject(new Vector3(e.X, e.Y, 0), device.Viewport, device.Transform.Projection, device.Transform.View, Matrix.Identity); Vector3 far = Vector3.Unproject(new Vector3(e.X, e.Y, 1), device.Viewport, device.Transform.Projection, device.Transform.View, Matrix.Identity); Vector3 dir = far - near; dir.Normalize(); List <RayCastSelection> selection = new List <RayCastSelection>(); for (int i = 0; i < Level.UStat.Count; i++) { if (Level.UStat[i].DirectX.visible) { ULevel.DirectXObject d = Level.UStat[i].DirectX; Vector2 result = RaySphereIntersect(CamEye - d.tfpos, dir, d.tfr); if (result != new Vector2(-1, -1)) { RayCastSelection r = new RayCastSelection(); r.dist = result.Y; r.entry = i; if (!float.IsNaN(r.dist)) { selection.Add(r); } } } } bool run = true; while (run) { run = false; for (int i = 0; i < selection.Count - 1; i++) { if (selection[i].dist > selection[i + 1].dist) { run = true; RayCastSelection r = selection[i]; selection[i] = selection[i + 1]; selection[i + 1] = r; } } } int found = -1; for (int i = 0; i < selection.Count - 1; i++) { if (selection[i].entry == SelectStat) { found = i; } } if (found != -1) { if (found < Level.UStat.Count - 1) { found++; } else { found = 0; } SelectStat = found; CheckSelect(); TreeNode t = treeView1.Nodes[0]; treeView1.SelectedNode = t.Nodes[selection[found].entry]; } else { SelectStat = selection[0].entry; CheckSelect(); TreeNode t = treeView1.Nodes[0]; treeView1.SelectedNode = t.Nodes[selection[0].entry]; } }
public void Render() { if (device == null) { return; } try { device.Transform.View = Matrix.LookAtLH(CamEye, CamEye + CamDir, new Vector3(0.0f, 0.0f, 1.0f)); device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, 1.0f, 1.0f, 1000000.0f); device.SetRenderState(RenderStates.ShadeMode, 1); device.RenderState.Lighting = DrawLit; device.RenderState.Ambient = Color.Gray; device.Material = material; device.SamplerState[0].MinFilter = TextureFilter.Linear; device.SamplerState[0].MagFilter = TextureFilter.Linear; device.Lights[0].Type = LightType.Directional; device.Lights[0].Diffuse = Color.White; device.Lights[0].Range = 100000; device.Lights[0].Direction = -CamDir; //new Vector3(0.8f, 0.4f, 1); device.Lights[0].Enabled = true; device.RenderState.CullMode = Cull.None; device.SetRenderState(RenderStates.ZEnable, true); if (DrawWireFrame) { device.RenderState.FillMode = FillMode.WireFrame; } else { device.RenderState.FillMode = FillMode.Solid; } device.VertexShader = null; device.Clear(ClearFlags.Target, System.Drawing.Color.Blue, 1.0f, 0); device.Clear(ClearFlags.ZBuffer, System.Drawing.Color.Black, 1.0f, 0); device.BeginScene(); device.RenderState.AlphaBlendEnable = false; for (int i = 0; i < Level.UStat.Count(); i++) { if (Level.UStat[i].DirectX.visible & CheckVisible(Level.UStat[i].DirectX.tfpos, Level.UStat[i].DirectX.tfr)) { if (DrawTexture) { if (Level.UStat[i].LP.textureindex == -1) { device.SetTexture(0, defaulttex); } else { device.SetTexture(0, Level.Tex[Level.UStat[i].LP.textureindex].tex); } } else { device.SetTexture(0, null); } ULevel.DirectXObject t = Level.UStat[i].DirectX; device.Transform.World = t.m; device.VertexFormat = CustomVertex.PositionNormalTextured.Format; device.DrawUserPrimitives(PrimitiveType.TriangleList, t.verts.Length / 3, t.verts); } } if (DrawSphere && selbounding != null) { device.RenderState.SourceBlend = Blend.One; device.RenderState.DestinationBlend = Blend.One; device.RenderState.AlphaBlendEnable = true; device.RenderState.Lighting = false; ULevel.DirectXObject t = Level.UStat[SelectStat].DirectX; device.Transform.World = Matrix.Translation(t.tfpos); device.SetTexture(0, seltex); selbounding.DrawSubset(0); device.SetTexture(0, null); } device.EndScene(); device.Present(); } catch (DirectXException) { return; } }