| bool InitializeGraphics() |
| { |
| try |
| { |
| DisplayMode disp = Manager.Adapters[Manager.Adapters.Default.Adapter].CurrentDisplayMode; |
| presentParams.Windowed = true; |
| presentParams.SwapEffect = SwapEffect.Discard; |
| presentParams.MultiSample = MultiSampleType.None; |
| presentParams.EnableAutoDepthStencil = true; |
| presentParams.AutoDepthStencilFormat = DepthFormat.D16; |
| |
| bufwid = presentParams.BackBufferWidth; |
| buflen = presentParams.BackBufferHeight; |
| |
| // Create the D3DDevice |
| device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams); |
| |
| device.DeviceReset += new System.EventHandler(this.OnResetDevice); |
| this.OnResetDevice(device, null); |
| pause = false; |
| SetupMatrices(); |
| setMeshVerts(); |
| //TextureMap(); |
| } |
| catch (DirectXException) |
| { |
| return false; |
| } |
| return true; |
| } |
| |
| void SetupMatrices() |
| { |
| device.RenderState.CullMode = Cull.None; |
| Viewport frontView = new Viewport(); |
| frontView.X = 0; |
| frontView.Y = 0; |
| frontView.Width = this.Width;//2; |
| frontView.Height = this.Height;//2; |
| frontView.MinZ = 0; |
| |
| frontView.MaxZ = 1; |
| |
| device.Transform.World = worldFix * Matrix.Translation(xMovement, modMovement, 0) * Matrix.RotationY(rotYAng) * Matrix.RotationX(rotXAng); |
| |
| device.Transform.View = Matrix.LookAtLH(new Vector3(angle, 0, -550000 + zoom), |
| new Vector3(0.0f, 0.0f, 0.0f), |
| new Vector3(0.0f, 1.0f, 0.0f)); |
| |
| |
| if (viewFound == 0) |
| { |
| view = device.Transform.View; |
| } |
| |
| device.Transform.Projection = Matrix.PerspectiveFovLH((float)(Math.PI / 7), 239.0f / 294.0f, 100.0f, 1000000.0f); |
| |
| device.RenderState.ZBufferEnable = true; |
| |
| if (!texturise) |
| { |
| device.RenderState.Lighting = true; |
| |
| device.Lights[0].Diffuse = Color.Beige; |
| device.Lights[0].Type = LightType.Directional; |
| device.Lights[0].Direction = new Vector3(0, -1, -1); |
| device.Lights[0].Enabled = true; |
| |
| device.Lights[1].Diffuse = Color.Beige; |
| device.Lights[1].Type = LightType.Directional; |
| //not working!! |
| device.Lights[1].Direction = new Vector3(0, 1, 1); |
| device.Lights[1].Enabled = true; |
| } |
| else |
| { |
| device.RenderState.Lighting = false; |
| } |
| |
| if (texturise) |
| { |
| device.SamplerState[0].MinFilter = TextureFilter.Anisotropic; |
| device.SamplerState[0].MagFilter = TextureFilter.Anisotropic; |
| |
| device.SamplerState[0].AddressU = TextureAddress.Mirror; |
| device.SamplerState[0].AddressV = TextureAddress.Mirror; |
| |
| } |
| } |
| |
| public void Render() |
| { |
| if (device == null) |
| return; |
| |
| if (pause) |
| return; |
| |
| |
| try |
| { |
| device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, System.Drawing.Color.White, 1.0f, 0); |
| |
| device.BeginScene(); |
| |
| SetupMatrices(); |
| |
| if (texturise) |
| { |
| device.VertexFormat = CustomVertex.PositionNormalTextured.Format; |
| |
| device.SetTexture(0, txt); |
| } |
| else |
| { |
| device.VertexFormat = CustomVertex.PositionNormalColored.Format; |
| mesh.ComputeNormals(); |
| device.RenderState.ShadeMode = ShadeMode.Gouraud; |
| } |
| |
| if (meshRen == 0) |
| { |
| setFeat(meshFeatIndices); |
| if (checkFeatPts()) |
| { |
| meshRen = 1; |
| } |
| } |
| |
| device.RenderState.FillMode = FillMode.Solid; |
| |
| for (int i = 0; i < meshMaterials.Length; i++) |
| { |
| device.Material = meshMaterials[i]; |
| mesh.DrawSubset(i); |
| } |
| |
| if (meshFeaturePoints.Count > 0) //spheres to show feature points |
| { |
| Direct3D.Mesh sphere = Mesh.Sphere(device, 5000f, 50, 50); |
| sphere.ComputeNormals(); |
| |
| Material SphereCol = new Material(); |
| SphereCol.Ambient = Color.DarkRed; |
| SphereCol.Diffuse = Color.Red; |
| SphereCol.Specular = Color.White; |
| |
| Direct3D.Mesh sphere2 = Mesh.Sphere(device, 5000f, 50, 50); |
| sphere2.ComputeNormals(); |
| |
| Material SphereCol2 = new Material(); |
| SphereCol2.Ambient = Color.Yellow; |
| SphereCol2.Diffuse = Color.Yellow; |
| SphereCol2.Specular = Color.White; |
| |
| |
| Matrix world2 = new Matrix(); |
| Matrix headMat = device.Transform.World; |
| |
| for (int i = 0; i < meshFeaturePoints.Count; i++) |
| { |
| CustomVertex.PositionNormalTextured pnt = (CustomVertex.PositionNormalTextured)meshFeaturePoints[i]; |
| if (i != colouredDiff) |
| { |
| if (pnt.X != -1 && pnt.Y != -1 && pnt.Z != -1) |
| { |
| world2 = Matrix.Translation(pnt.X, pnt.Y, pnt.Z); |
| |
| device.Transform.World = world2 * headMat; |
| device.Material = SphereCol; |
| sphere.DrawSubset(0); |
| } |
| } |
| else |
| { |
| if (pnt.X != -1 && pnt.Y != -1 && pnt.Z != -1) |
| { |
| world2 = Matrix.Translation(pnt.X, pnt.Y, pnt.Z); |
| |
| device.Transform.World = world2 * headMat; |
| device.Material = SphereCol2; |
| sphere2.DrawSubset(0); |
| } |
| } |
| } |
| } |
| } |
| |
| device.EndScene(); |
| device.Present(); |
| } |
| catch (Direct3DXException e) |
| { |
| |
| } |
| |
| } |
| |
| |