SceneComponent.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using fun.Communication;
  2. using ObjLoader.Loader.Loaders;
  3. using OpenTK;
  4. using OpenTK.Graphics.OpenGL;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Drawing;
  8. using System.IO;
  9. using System.Linq;
  10. using Environment = fun.Core.Environment;
  11. namespace fun.Client.Components
  12. {
  13. internal sealed class SceneComponent : GameComponent
  14. {
  15. private SimulationComponent simulation;
  16. private CameraComponent camera;
  17. private Dictionary<string, Mesh> meshes;
  18. public SceneComponent(GameWindow game, SimulationComponent simulaiton, CameraComponent camera)
  19. : base(game)
  20. {
  21. this.simulation = simulaiton;
  22. this.camera = camera;
  23. meshes = new Dictionary<string, Mesh>();
  24. }
  25. public override void Initialize()
  26. {
  27. foreach (var perceived in simulation.Perceiveder)
  28. {
  29. Directory.SetCurrentDirectory("assets\\models");
  30. if (!File.Exists(perceived.Name))
  31. continue;
  32. if (meshes.Keys.Contains(perceived.Name))
  33. continue;
  34. var objFactory = new ObjLoaderFactory();
  35. var objloader = objFactory.Create();
  36. var result = objloader.Load(new FileStream(perceived.Name, FileMode.Open, FileAccess.Read));
  37. Directory.SetCurrentDirectory("..\\..");
  38. var vertices = result.Vertices.Select(v => new Vector3(new Vector3(v.X, v.Y, v.Z))).ToArray();
  39. var indiceslist = new List<uint>();
  40. foreach (var group in result.Groups)
  41. foreach (var face in group.Faces)
  42. for (int i = 0; i < face.Count; i++)
  43. indiceslist.Add((uint)face[i].VertexIndex);
  44. var indices = indiceslist.Select(i => i - 1).ToArray();
  45. meshes.Add(perceived.Name, new Mesh(vertices, indices));
  46. }
  47. }
  48. public override void Draw(FrameEventArgs e)
  49. {
  50. GL.Clear(ClearBufferMask.ColorBufferBit);
  51. GL.ClearColor(Color.Honeydew);
  52. GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Line);
  53. foreach (var entity in camera.Seen)
  54. {
  55. var name = (entity.GetElement<IPerceived>() as IPerceived).Name;
  56. if (!meshes.Keys.Contains(name))
  57. continue;
  58. var mesh = meshes[name];
  59. var transform = entity.GetElement<ITransform>() as ITransform;
  60. var world = Matrix4.CreateScale(transform.Scale) *
  61. Matrix4.CreateRotationX(transform.Rotation.X) *
  62. Matrix4.CreateRotationY(transform.Rotation.Y) *
  63. Matrix4.CreateRotationZ(transform.Rotation.Z) *
  64. Matrix4.CreateTranslation(transform.Position);
  65. mesh.Draw(world, camera.View, camera.Projection);
  66. }
  67. GL.Flush();
  68. }
  69. private class Mesh
  70. {
  71. public int VBO;
  72. public int IBO;
  73. public int IndicesLength { get; private set; }
  74. public int VerticesLength { get; private set; }
  75. public Mesh(Vector3[] vertices, uint[] indices)
  76. {
  77. //defining VertexBufferObject
  78. VBO = GL.GenBuffer();
  79. GL.BindBuffer(BufferTarget.ArrayBuffer, VBO);
  80. GL.BufferData<Vector3>(BufferTarget.ArrayBuffer, (Vector3.SizeInBytes * vertices.Length), vertices, BufferUsageHint.StaticDraw);
  81. GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
  82. //defining IndexBufferObject
  83. IBO = GL.GenBuffer();
  84. GL.BindBuffer(BufferTarget.ElementArrayBuffer, IBO);
  85. GL.BufferData<uint>(BufferTarget.ElementArrayBuffer, (sizeof(uint) * indices.Length), indices, BufferUsageHint.StaticDraw);
  86. GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
  87. IndicesLength = indices.Length;
  88. VerticesLength = vertices.Length;
  89. }
  90. public void Draw(Matrix4 world, Matrix4 view, Matrix4 projection)
  91. {
  92. GL.MatrixMode(MatrixMode.Projection);
  93. var mat = world * view * projection;
  94. GL.LoadMatrix(ref mat);
  95. GL.EnableClientState(ArrayCap.VertexArray);
  96. GL.EnableClientState(ArrayCap.IndexArray);
  97. GL.BindBuffer(BufferTarget.ArrayBuffer, VBO);
  98. GL.VertexPointer(3, VertexPointerType.Float, Vector3.SizeInBytes, 0);
  99. GL.BindBuffer(BufferTarget.ElementArrayBuffer, IBO);
  100. GL.Color3(Color.Black);
  101. GL.DrawElements(PrimitiveType.Triangles, IndicesLength, DrawElementsType.UnsignedInt, 0);
  102. GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
  103. GL.LoadIdentity();
  104. }
  105. }
  106. }
  107. }