Exemple #1
0
		public static VertexBuffer Create<T>(ReadOnlySpan<T> data, ReadOnlySpan<VertexBufferElement> layout) where T : unmanaged
		{
			uint vertexBufferId;
		
			OpenGl3Native.GenerateBuffers(1, &vertexBufferId);
			
			var vertexBuffer = new VertexBuffer(vertexBufferId);

			vertexBuffer.Bind();

			fixed (T* dataPointer = data)
				OpenGl3Native.BufferData(glArrayBuffer, data.Length * sizeof(T), dataPointer, glStaticDraw);

			nuint offset = 0;
				
			for (uint i = 0; i < layout.Length; i++)
			{
				var element = layout[(int)i];
				var typeSize = element.Type switch
				{
					OpenGlType.Float => sizeof(float),
					_ => throw new ArgumentOutOfRangeException()
				};
				var size = element.Count * typeSize;
					
				OpenGl3Native.VertexAttributePointer(i, element.Count, (int)element.Type, glFalse, size, offset);
				OpenGl3Native.EnableVertexAttributeArray(i);

				offset += (nuint)size;
			}

			vertexBuffer.Unbind();
			
			return vertexBuffer;
		}
Exemple #2
0
        public static VertexArray Create()
        {
            uint vertexArrayId;

            OpenGl3Native.GenerateVertexArrays(1, &vertexArrayId);

            return(new VertexArray(vertexArrayId));
        }
Exemple #3
0
        public static unsafe void Main()
        {
            using (new ExitHandler.LifetimeHandle())
            {
                const int glfwTrue  = 1;
                const int glfwFalse = 0;
                const int glfwContextVersionMajor = 0x00022002;
                const int glfwContextVersionMinor = 0x00022003;
                const int glfwOpenGlProfile       = 0x00022008;
                const int glfwOpenGlCoreProfile   = 0x00032001;
                const int glfwOpenGlForwardCompat = 0x00022006;
                const int glfwResizable           = 0x00020003;

                const int glColorBufferBit = 0x00004000;
                const int glTriangles      = 0x0004;

                GlfwNative.Init();
                GlfwNative.WindowHint(glfwContextVersionMajor, 3);
                GlfwNative.WindowHint(glfwContextVersionMinor, 3);
                GlfwNative.WindowHint(glfwOpenGlProfile, glfwOpenGlCoreProfile);
                GlfwNative.WindowHint(glfwOpenGlForwardCompat, glfwTrue);
                GlfwNative.WindowHint(glfwResizable, glfwFalse);

                using (var window = new Window("Poltergeist Editor"))
                {
                    using (var vertexArray = VertexArray.Create())
                    {
                        vertexArray.Bind();

                        Span <float> vertices = stackalloc float[]
                        {
                            -0.5f, -0.5f, 0.0f,
                            0.5f, -0.5f, 0.0f,
                            0.0f, 0.5f, 0.0f
                        };

                        Span <VertexBufferElement> layout = stackalloc VertexBufferElement[]
                        {
                            new VertexBufferElement(OpenGlType.Float, 3)
                        };

                        using (VertexBuffer.Create <float>(vertices, layout)) { }

                        while (window.IsOpen)
                        {
                            window.PollEvents();

                            OpenGl3Native.ClearColor(0.3f, 0.3f, 0.3f, 1.0f);
                            OpenGl3Native.Clear(glColorBufferBit);
                            OpenGl3Native.DrawArrays(glTriangles, 0, 3);

                            window.SwapBuffers();
                        }

                        vertexArray.Unbind();
                    }
                }

                GlfwNative.Terminate();
            }
        }
    }
}
Exemple #4
0
		public void Dispose()
		{
			fixed (uint* vertexBufferIdPointer = &_vertexBufferId)
				OpenGl3Native.DeleteBuffers(1, vertexBufferIdPointer);
		}
Exemple #5
0
		public void Unbind()
		{
			OpenGl3Native.BindBuffer(glArrayBuffer, 0);
		}
Exemple #6
0
		public void Bind()
		{
			OpenGl3Native.BindBuffer(glArrayBuffer, _vertexBufferId);
		}
Exemple #7
0
 public void Dispose()
 {
     fixed(uint *vertexArrayIdPointer = &_vertexArrayId)
     OpenGl3Native.DeleteVertexArrays(1, vertexArrayIdPointer);
 }
Exemple #8
0
 public void Unbind()
 {
     OpenGl3Native.BindVertexArray(0);
 }
Exemple #9
0
 public void Bind()
 {
     OpenGl3Native.BindVertexArray(_vertexArrayId);
 }