PageRenderTime 54ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/SolarSystem.Wpf/SystemController.cs

https://bitbucket.org/efouts/solarsystem
C# | 67 lines | 58 code | 9 blank | 0 comment | 2 complexity | dee0188a11093b934f0a614541916bfa MD5 | raw file
Possible License(s): BSD-3-Clause
  1. using SharpGL;
  2. using SharpGL.SceneGraph.Assets;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. namespace SolarSystem.Wpf
  9. {
  10. public class SystemController
  11. {
  12. private System system;
  13. private Normalizer normalizer;
  14. private Dictionary<Body, Texture> bodyTextures;
  15. public SystemController(System system, Normalizer normalizer)
  16. {
  17. this.system = system;
  18. this.normalizer = normalizer;
  19. bodyTextures = new Dictionary<Body, Texture>();
  20. }
  21. public void Render(OpenGL gl)
  22. {
  23. foreach (var body in system.Bodies)
  24. DrawBody(gl, body);
  25. }
  26. private void DrawBody(OpenGL gl, Body body)
  27. {
  28. gl.PushMatrix();
  29. gl.Translate(normalizer.NormalizePosition(body.Position[0]), 0, normalizer.NormalizePosition(body.Position[1]));
  30. var quad = gl.NewQuadric();
  31. if (bodyTextures.ContainsKey(body))
  32. {
  33. var texture = bodyTextures[body];
  34. texture.Bind(gl);
  35. }
  36. gl.Color(1f, 1f, 1f);
  37. gl.Rotate(90, 1.0f, 0.0f, 0.0f);
  38. gl.Rotate(body.Rotation, 0.0f, 0.0f, 1.0f);
  39. gl.QuadricTexture(quad, 1);
  40. gl.Sphere(quad, normalizer.NormalizeRadius(body.Radius), 20, 20);
  41. gl.PopMatrix();
  42. gl.BindTexture(OpenGL.GL_TEXTURE_2D, 0);
  43. }
  44. public void LoadBodyTextures(OpenGL gl)
  45. {
  46. foreach (var body in system.Bodies)
  47. {
  48. var textureFileName = String.Format(@"Textures\{0}_map.jpg", body.Name);
  49. if (File.Exists(textureFileName))
  50. {
  51. var texture = new Texture();
  52. texture.Create(gl, textureFileName);
  53. bodyTextures.Add(body, texture);
  54. }
  55. }
  56. }
  57. }
  58. }