Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #region Using
- using System;
- using System.Runtime.InteropServices;
- using Emotion.Graphics;
- using Emotion.Primitives;
- using OpenTK;
- using OpenTK.Graphics.ES30;
- using Soul;
- using Matrix4 = OpenTK.Matrix4;
- using Vector2 = Emotion.Primitives.Vector2;
- using Vector3 = Emotion.Primitives.Vector3;
- using Vector4 = Emotion.Primitives.Vector4;
- #endregion
- namespace Test
- {
- public unsafe class TestMain
- {
- public static readonly int MaxRenderable = 10000;
- public static readonly int MaxRenderableSize = VertexData.SizeInBytes * 4;
- public static readonly int MaxBufferSize = MaxRenderable * MaxRenderableSize;
- public static readonly int MaxIndicesSize = MaxRenderable * 6;
- public static int Vbo;
- public static int Vao;
- public static int Ibo;
- public static void Main()
- {
- WindowTest windowTest = new WindowTest();
- string defaultVertex = Utilities.ReadEmbeddedResource("Emotion.Embedded.Shaders.DefaultVertex.glsl");
- string defaultFrag = Utilities.ReadEmbeddedResource("Emotion.Embedded.Shaders.DefaultFrag.glsl");
- // Create and compile the shaders.
- int vertPointer = GL.CreateShader(ShaderType.VertexShader);
- GL.ShaderSource(vertPointer, defaultVertex);
- GL.CompileShader(vertPointer);
- int fragPointer = GL.CreateShader(ShaderType.FragmentShader);
- GL.ShaderSource(fragPointer, defaultFrag);
- GL.CompileShader(fragPointer);
- // Create program.
- int programPointer = GL.CreateProgram();
- GL.AttachShader(programPointer, vertPointer);
- GL.AttachShader(programPointer, fragPointer);
- GL.LinkProgram(programPointer);
- GL.UseProgram(programPointer);
- GL.DetachShader(programPointer, vertPointer);
- GL.DetachShader(programPointer, fragPointer);
- // Upload projection.
- Matrix4 projectionMatrix = Matrix4.CreateOrthographicOffCenter(0, 960, 540, 0, -1, 1);
- float* matrixPtr = &projectionMatrix.Row0.X;
- int loc = GL.GetUniformLocation(programPointer, "projectionMatrix");
- GL.UniformMatrix4(loc, 1, false, matrixPtr);
- // VBO
- Vbo = GL.GenBuffer();
- GL.BindBuffer(BufferTarget.ArrayBuffer, Vbo);
- GL.BufferData(BufferTarget.ArrayBuffer, MaxBufferSize, IntPtr.Zero, BufferUsageHint.DynamicDraw);
- GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
- // VAO
- Vao = GL.GenVertexArray();
- GL.BindVertexArray(Vao);
- GL.BindBuffer(BufferTarget.ArrayBuffer, Vbo);
- GL.EnableVertexAttribArray(0);
- GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, VertexData.SizeInBytes, 0);
- GL.EnableVertexAttribArray(1);
- GL.VertexAttribPointer(1, 4, VertexAttribPointerType.Float, false, VertexData.SizeInBytes, (byte) Marshal.OffsetOf(typeof(VertexData), "Color"));
- GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
- GL.BindVertexArray(0);
- // IBO
- ushort[] indices = new ushort[MaxIndicesSize];
- ushort offset = 0;
- for (int i = 0; i < MaxIndicesSize; i += 6)
- {
- indices[i] = (ushort) (offset + 0);
- indices[i + 1] = (ushort) (offset + 1);
- indices[i + 2] = (ushort) (offset + 2);
- indices[i + 3] = (ushort) (offset + 2);
- indices[i + 4] = (ushort) (offset + 3);
- indices[i + 5] = (ushort) (offset + 0);
- offset += 4;
- }
- Ibo = GL.GenBuffer();
- GL.BindBuffer(BufferTarget.ElementArrayBuffer, Ibo);
- GL.BufferData(BufferTarget.ElementArrayBuffer, indices.Length * sizeof(ushort), indices, BufferUsageHint.StaticDraw);
- GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
- // Other
- GL.ClearColor(1f, 0, 0, 1f);
- windowTest.Run();
- }
- }
- public class WindowTest : GameWindow
- {
- private unsafe VertexData* _dataPointer;
- private int _indicesCount;
- protected override unsafe void OnRenderFrame(FrameEventArgs e)
- {
- // L185
- GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
- GL.BindBuffer(BufferTarget.ArrayBuffer, TestMain.Vbo);
- _dataPointer = (VertexData*) GL.MapBufferRange(BufferTarget.ArrayBuffer, IntPtr.Zero, VertexData.SizeInBytes, BufferAccessMask.MapWriteBit);
- // L224
- Render(new Vector3(100, 100, 0), Color.Blue, new Vector2(100, 100));
- // L247
- GL.BindVertexArray(TestMain.Vao);
- GL.BindBuffer(BufferTarget.ElementArrayBuffer, TestMain.Ibo);
- GL.DrawElements(PrimitiveType.Triangles, _indicesCount, DrawElementsType.UnsignedShort, IntPtr.Zero);
- GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
- GL.BindVertexArray(0);
- _indicesCount = 0;
- // L195
- GL.UnmapBuffer(BufferTarget.ArrayBuffer);
- GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
- SwapBuffers();
- }
- public unsafe void Render(Vector3 pos, Color col, Vector2 size)
- {
- Vector4 color = new Vector4(col.R / 255f, col.G / 255f, col.B / 255f, col.A / 255f);
- _dataPointer->Vertex = pos;
- _dataPointer->Color = color;
- _dataPointer++;
- _dataPointer->Vertex = new Vector3(pos.X + size.X, pos.Y, pos.Z);
- _dataPointer->Color = color;
- _dataPointer++;
- _dataPointer->Vertex = new Vector3(pos.X + size.X, pos.Y + size.Y, pos.Z);
- _dataPointer->Color = color;
- _dataPointer++;
- _dataPointer->Vertex = new Vector3(pos.X, pos.Y + size.Y, pos.Z);
- _dataPointer->Color = color;
- _dataPointer++;
- _indicesCount += 6;
- }
- }
- }
Add Comment
Please, Sign In to add comment