Guest User

Untitled

a guest
Jul 16th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.93 KB | None | 0 0
  1. #region Using
  2.  
  3. using System;
  4. using System.Runtime.InteropServices;
  5. using Emotion.Graphics;
  6. using Emotion.Primitives;
  7. using OpenTK;
  8. using OpenTK.Graphics.ES30;
  9. using Soul;
  10. using Matrix4 = OpenTK.Matrix4;
  11. using Vector2 = Emotion.Primitives.Vector2;
  12. using Vector3 = Emotion.Primitives.Vector3;
  13. using Vector4 = Emotion.Primitives.Vector4;
  14.  
  15. #endregion
  16.  
  17. namespace Test
  18. {
  19. public unsafe class TestMain
  20. {
  21. public static readonly int MaxRenderable = 10000;
  22. public static readonly int MaxRenderableSize = VertexData.SizeInBytes * 4;
  23. public static readonly int MaxBufferSize = MaxRenderable * MaxRenderableSize;
  24. public static readonly int MaxIndicesSize = MaxRenderable * 6;
  25.  
  26. public static int Vbo;
  27. public static int Vao;
  28. public static int Ibo;
  29.  
  30. public static void Main()
  31. {
  32. WindowTest windowTest = new WindowTest();
  33.  
  34. string defaultVertex = Utilities.ReadEmbeddedResource("Emotion.Embedded.Shaders.DefaultVertex.glsl");
  35. string defaultFrag = Utilities.ReadEmbeddedResource("Emotion.Embedded.Shaders.DefaultFrag.glsl");
  36.  
  37. // Create and compile the shaders.
  38. int vertPointer = GL.CreateShader(ShaderType.VertexShader);
  39. GL.ShaderSource(vertPointer, defaultVertex);
  40. GL.CompileShader(vertPointer);
  41. int fragPointer = GL.CreateShader(ShaderType.FragmentShader);
  42. GL.ShaderSource(fragPointer, defaultFrag);
  43. GL.CompileShader(fragPointer);
  44.  
  45. // Create program.
  46. int programPointer = GL.CreateProgram();
  47. GL.AttachShader(programPointer, vertPointer);
  48. GL.AttachShader(programPointer, fragPointer);
  49. GL.LinkProgram(programPointer);
  50. GL.UseProgram(programPointer);
  51. GL.DetachShader(programPointer, vertPointer);
  52. GL.DetachShader(programPointer, fragPointer);
  53.  
  54. // Upload projection.
  55. Matrix4 projectionMatrix = Matrix4.CreateOrthographicOffCenter(0, 960, 540, 0, -1, 1);
  56. float* matrixPtr = &projectionMatrix.Row0.X;
  57. int loc = GL.GetUniformLocation(programPointer, "projectionMatrix");
  58. GL.UniformMatrix4(loc, 1, false, matrixPtr);
  59.  
  60. // VBO
  61. Vbo = GL.GenBuffer();
  62. GL.BindBuffer(BufferTarget.ArrayBuffer, Vbo);
  63. GL.BufferData(BufferTarget.ArrayBuffer, MaxBufferSize, IntPtr.Zero, BufferUsageHint.DynamicDraw);
  64. GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
  65.  
  66. // VAO
  67. Vao = GL.GenVertexArray();
  68. GL.BindVertexArray(Vao);
  69. GL.BindBuffer(BufferTarget.ArrayBuffer, Vbo);
  70. GL.EnableVertexAttribArray(0);
  71. GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, VertexData.SizeInBytes, 0);
  72. GL.EnableVertexAttribArray(1);
  73. GL.VertexAttribPointer(1, 4, VertexAttribPointerType.Float, false, VertexData.SizeInBytes, (byte) Marshal.OffsetOf(typeof(VertexData), "Color"));
  74. GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
  75. GL.BindVertexArray(0);
  76.  
  77. // IBO
  78. ushort[] indices = new ushort[MaxIndicesSize];
  79. ushort offset = 0;
  80. for (int i = 0; i < MaxIndicesSize; i += 6)
  81. {
  82. indices[i] = (ushort) (offset + 0);
  83. indices[i + 1] = (ushort) (offset + 1);
  84. indices[i + 2] = (ushort) (offset + 2);
  85. indices[i + 3] = (ushort) (offset + 2);
  86. indices[i + 4] = (ushort) (offset + 3);
  87. indices[i + 5] = (ushort) (offset + 0);
  88.  
  89. offset += 4;
  90. }
  91.  
  92. Ibo = GL.GenBuffer();
  93. GL.BindBuffer(BufferTarget.ElementArrayBuffer, Ibo);
  94. GL.BufferData(BufferTarget.ElementArrayBuffer, indices.Length * sizeof(ushort), indices, BufferUsageHint.StaticDraw);
  95. GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
  96.  
  97. // Other
  98. GL.ClearColor(1f, 0, 0, 1f);
  99.  
  100. windowTest.Run();
  101. }
  102. }
  103.  
  104. public class WindowTest : GameWindow
  105. {
  106. private unsafe VertexData* _dataPointer;
  107. private int _indicesCount;
  108.  
  109. protected override unsafe void OnRenderFrame(FrameEventArgs e)
  110. {
  111. // L185
  112. GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
  113.  
  114. GL.BindBuffer(BufferTarget.ArrayBuffer, TestMain.Vbo);
  115. _dataPointer = (VertexData*) GL.MapBufferRange(BufferTarget.ArrayBuffer, IntPtr.Zero, VertexData.SizeInBytes, BufferAccessMask.MapWriteBit);
  116.  
  117. // L224
  118. Render(new Vector3(100, 100, 0), Color.Blue, new Vector2(100, 100));
  119.  
  120. // L247
  121. GL.BindVertexArray(TestMain.Vao);
  122. GL.BindBuffer(BufferTarget.ElementArrayBuffer, TestMain.Ibo);
  123. GL.DrawElements(PrimitiveType.Triangles, _indicesCount, DrawElementsType.UnsignedShort, IntPtr.Zero);
  124. GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
  125. GL.BindVertexArray(0);
  126. _indicesCount = 0;
  127.  
  128. // L195
  129. GL.UnmapBuffer(BufferTarget.ArrayBuffer);
  130. GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
  131.  
  132. SwapBuffers();
  133. }
  134.  
  135. public unsafe void Render(Vector3 pos, Color col, Vector2 size)
  136. {
  137. Vector4 color = new Vector4(col.R / 255f, col.G / 255f, col.B / 255f, col.A / 255f);
  138.  
  139. _dataPointer->Vertex = pos;
  140. _dataPointer->Color = color;
  141. _dataPointer++;
  142.  
  143. _dataPointer->Vertex = new Vector3(pos.X + size.X, pos.Y, pos.Z);
  144. _dataPointer->Color = color;
  145. _dataPointer++;
  146.  
  147. _dataPointer->Vertex = new Vector3(pos.X + size.X, pos.Y + size.Y, pos.Z);
  148. _dataPointer->Color = color;
  149. _dataPointer++;
  150.  
  151. _dataPointer->Vertex = new Vector3(pos.X, pos.Y + size.Y, pos.Z);
  152. _dataPointer->Color = color;
  153. _dataPointer++;
  154.  
  155. _indicesCount += 6;
  156. }
  157. }
  158. }
Add Comment
Please, Sign In to add comment