Beispiel #1
0
 public OpenGLIndexBuffer(object indices, uint count)
 {
     this.count = count;
     bufferID   = Gl.CreateBuffer();
     Gl.BindBuffer(BufferTarget.ElementArrayBuffer, bufferID);
     Gl.BufferData(BufferTarget.ElementArrayBuffer, count * sizeof(uint), indices, BufferUsage.StaticDraw);
 }
Beispiel #2
0
        protected void UpdateBuffer()
        {
            if (firstRender)
            {
                firstRender = false;

                VAO = Gl.CreateVertexArray();
                Gl.BindVertexArray(VAO);

                Gl.BindBuffer(BufferTarget.ArrayBuffer, Texture.baseVBO);
                Gl.VertexAttribPointer(0, 2, VertexAttribType.Float, false, 4 * sizeof(float), (IntPtr)0);
                Gl.VertexAttribPointer(1, 2, VertexAttribType.Float, false, 4 * sizeof(float), (IntPtr)(2 * sizeof(float)));
                Gl.EnableVertexAttribArray(0);
                Gl.EnableVertexAttribArray(1);

                ibo = Gl.CreateBuffer();
                Gl.BindBuffer(BufferTarget.ArrayBuffer, ibo);
                Gl.VertexAttribPointer(2, 2, VertexAttribType.Float, false, 4 * sizeof(float), (IntPtr)0);
                Gl.VertexAttribPointer(3, 2, VertexAttribType.Float, false, 4 * sizeof(float), (IntPtr)(2 * sizeof(float)));
                Gl.EnableVertexAttribArray(2);
                Gl.EnableVertexAttribArray(3);
                Gl.VertexAttribDivisor(2, 1);
                Gl.VertexAttribDivisor(3, 1);
            }

            Gl.BindBuffer(BufferTarget.ArrayBuffer, ibo);
            Gl.BufferData(BufferTarget.ArrayBuffer, (uint)bufferData.Length * sizeof(float), bufferData, BufferUsage.DynamicDraw);
            updateBuffer = false;
        }
Beispiel #3
0
        public override void SetBuffers()
        {
            vAO = Gl.GenVertexArray();
            vBO = Gl.GenBuffer();
            eBO = Gl.GenBuffer();

            Gl.BindVertexArray(VAO);

            Gl.BindBuffer(BufferTarget.ArrayBuffer, VBO);
            Gl.BufferData(BufferTarget.ArrayBuffer, ((uint)Vertices.Length) * sizeof(float), Vertices, BufferUsage.StaticDraw);

            Gl.BindBuffer(BufferTarget.ElementArrayBuffer, EBO);
            Gl.BufferData(BufferTarget.ElementArrayBuffer, ((uint)Indices.Length) * sizeof(uint), Indices, BufferUsage.StaticDraw);

            Gl.VertexAttribPointer(0, 3, VertexAttribType.Float, false, (int)VertexStride * sizeof(float), IntPtr.Zero);
            Gl.EnableVertexAttribArray(0);

            Gl.VertexAttribPointer(1, 3, VertexAttribType.Float, false, (int)VertexStride * sizeof(float), IntPtr.Zero + 3 * sizeof(float));
            Gl.EnableVertexAttribArray(1);

            Gl.VertexAttribPointer(2, 3, VertexAttribType.Float, false, (int)VertexStride * sizeof(float), IntPtr.Zero + 6 * sizeof(float));
            Gl.EnableVertexAttribArray(2);

            Gl.VertexAttribPointer(3, 2, VertexAttribType.Float, false, (int)VertexStride * sizeof(float), IntPtr.Zero + 9 * sizeof(float));
            Gl.EnableVertexAttribArray(3);

            Gl.BindBuffer(BufferTarget.ArrayBuffer, 0);
            Gl.BindVertexArray(0);
        }
Beispiel #4
0
        public static Geometry GenerateGeometry(GeometryData geometryData, float[] vertices, float[] normals, float[] texCoords, PrimitiveType primitiveType = PrimitiveType.Triangles)
        {
            if (vertices.Length % 3 != 0 || normals.Length % 3 != 0 || texCoords.Length % 2 != 0)
            {
                throw new ArgumentException($"Invalid count of input variables");
            }
            var geometry = new Geometry(geometryData, primitiveType);

            geometry.m_VerticesCount = vertices.Length / 3;

            geometry.VAO = Gl.GenVertexArray();
            Gl.BindVertexArray(geometry.VAO);

            geometry.m_VertexBuffer = Gl.GenBuffer();
            Gl.BindBuffer(BufferTarget.ArrayBuffer, geometry.m_VertexBuffer);
            Gl.BufferData(BufferTarget.ArrayBuffer, (uint)(sizeof(float) * vertices.Length), vertices, BufferUsage.StaticDraw);
            Gl.VertexAttribPointer(POSITION_LOCATION, 3, VertexAttribType.Float, false, 0, IntPtr.Zero);
            Gl.EnableVertexAttribArray(POSITION_LOCATION);

            geometry.m_NormalBuffer = Gl.GenBuffer();
            Gl.BindBuffer(BufferTarget.ArrayBuffer, geometry.m_NormalBuffer);
            Gl.BufferData(BufferTarget.ArrayBuffer, (uint)(sizeof(float) * normals.Length), normals, BufferUsage.StaticDraw);
            Gl.VertexAttribPointer(NORMAL_LOCATION, 3, VertexAttribType.Float, false, 0, IntPtr.Zero);
            Gl.EnableVertexAttribArray(NORMAL_LOCATION);

            geometry.m_TexCoordBuffer = Gl.GenBuffer();
            Gl.BindBuffer(BufferTarget.ArrayBuffer, geometry.m_TexCoordBuffer);
            Gl.BufferData(BufferTarget.ArrayBuffer, (uint)(sizeof(float) * texCoords.Length), texCoords, BufferUsage.StaticDraw);
            Gl.VertexAttribPointer(TEX_COORD_LOCATION, 2, VertexAttribType.Float, false, 0, IntPtr.Zero);
            Gl.EnableVertexAttribArray(TEX_COORD_LOCATION);

            Gl.BindBuffer(BufferTarget.ArrayBuffer, 0);
            return(geometry);
        }
Beispiel #5
0
            private static UInt32 CreateBuffer(uint size, float[] data)
            {
                UInt32 buffer = 0;

                try {
                    buffer = Gl.GenBuffer();

                    Gl.BindBuffer(BufferTargetARB.ArrayBuffer, buffer);

                    if (data != null)
                    {
                        GCHandle dataHandle = GCHandle.Alloc(data, GCHandleType.Pinned);
                        try {
                            Gl.BufferData(BufferTargetARB.ArrayBuffer, size, dataHandle.AddrOfPinnedObject(), BufferUsageARB.StaticDraw);
                        } finally {
                            dataHandle.Free();
                        }
                    }
                    else
                    {
                        Gl.BufferData(BufferTargetARB.ArrayBuffer, size, IntPtr.Zero, BufferUsageARB.StaticDraw);
                    }

                    return(buffer);
                } catch {
                    if (buffer != 0)
                    {
                        Gl.DeleteBuffers(buffer);
                    }
                    throw;
                }
            }
Beispiel #6
0
        public void update(Chunk chunk)
        {
            if (vaoID_ == 0)
            {
                vaoID_ = Gl.GenVertexArray();
                Gl.GenBuffers(vboID_);
            }

            Gl.ShadeModel(ShadingModel.Smooth);

            Gl.BindVertexArray(vaoID_);

            if (chunk.vertices.Count() > 0)
            {
                Gl.BindBuffer(BufferTarget.ArrayBuffer, vboID_[0]);
                Gl.BufferData(BufferTarget.ArrayBuffer, (uint)chunk.vertices.Count() * sizeof(float) * 3, chunk.vertices.ToArray(), BufferUsage.DynamicDraw);
                Gl.VertexAttribPointer(Shader.ATTRIB_VERTICES_POS, 3, VertexAttribType.Float, false, 0, IntPtr.Zero);
                Gl.EnableVertexAttribArray(Shader.ATTRIB_VERTICES_POS);
            }
            if (chunk.triangles.Count() > 0)
            {
                Gl.BindBuffer(BufferTarget.ElementArrayBuffer, vboID_[1]);
                Gl.BufferData(BufferTarget.ElementArrayBuffer, (uint)chunk.triangles.Count() * sizeof(int), chunk.triangles.ToArray(), BufferUsage.DynamicDraw);
                current_fc = chunk.triangles.Count();
            }

            Gl.BindVertexArray(0);
            Gl.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
            Gl.BindBuffer(BufferTarget.ArrayBuffer, 0);
        }
Beispiel #7
0
        private void UpdateBuffer(Texture texture)
        {
            if (texture is null)
            {
                return;
            }
            if (firstRender)
            {
                firstRender = false;

                vao = Gl.CreateVertexArray();
                Gl.BindVertexArray(vao);

                Gl.BindBuffer(BufferTarget.ArrayBuffer, texture.baseVBO);
                Gl.VertexAttribPointer(0, 2, VertexAttribType.Float, false, 4 * sizeof(float), (IntPtr)0);
                Gl.VertexAttribPointer(1, 2, VertexAttribType.Float, false, 4 * sizeof(float), (IntPtr)(2 * sizeof(float)));
                Gl.EnableVertexAttribArray(0);
                Gl.EnableVertexAttribArray(1);

                ibo = Gl.CreateBuffer();
                Gl.BindBuffer(BufferTarget.ArrayBuffer, ibo);
                Gl.VertexAttribPointer(2, 2, VertexAttribType.Float, false, 4 * sizeof(float), (IntPtr)0);
                Gl.VertexAttribPointer(3, 2, VertexAttribType.Float, false, 4 * sizeof(float), (IntPtr)(2 * sizeof(float)));
                Gl.EnableVertexAttribArray(2);
                Gl.EnableVertexAttribArray(3);
                Gl.VertexAttribDivisor(2, 1);
                Gl.VertexAttribDivisor(3, 1);
            }

            Gl.BindBuffer(BufferTarget.ArrayBuffer, ibo);
            Gl.BufferData(BufferTarget.ArrayBuffer, (uint)tilesBuffer.Length * sizeof(float), tilesBuffer, BufferUsage.DynamicDraw);
            updateBuffer = false;
        }
Beispiel #8
0
        public override void Resize(uint size)
        {
            this.size = size;

            Gl.BindBuffer(BufferTarget.ArrayBuffer, handle[0]);
            Gl.BufferData(BufferTarget.ArrayBuffer, new IntPtr(size), IntPtr.Zero, SPBufferUsageToOpenGL(usage));
        }
Beispiel #9
0
 /// <summary>
 /// Reset the allocated GPU buffer for this BufferObject.
 /// </summary>
 /// <param name="size">
 /// A <see cref="UInt32"/> that determine the size of the buffer object GPU buffer, in bytes.
 /// </param>
 protected void AllocateGpuBuffer(uint size, object data)
 {
     // Define buffer object (type, size and hints)
     Gl.BufferData((int)BufferType, size, data, (int)Hint);
     // Store GPU buffer size
     _GpuBufferSize = size;
 }
Beispiel #10
0
        private void DrawBatch(Batch batch)
        {
            Gl.BindVertexArray(VAO);

            Gl.BindBuffer(BufferTargetARB.ArrayBuffer, VBO);
            Gl.BufferData(BufferTargetARB.ArrayBuffer, batch.VertexCount * (uint)VertexStride, batch.Vertices, BufferUsageARB.StreamDraw);

            Gl.BindBuffer(BufferTargetARB.ElementArrayBuffer, EBO);
            Gl.BufferData(BufferTargetARB.ElementArrayBuffer, batch.VertexCount * sizeof(int), batch.Indices, BufferUsageARB.StreamDraw);

            // Vertex position
            Gl.EnableVertexAttribArray(0);
            Gl.VertexAttribPointer(0, 3, (int)VertexPointerType.Float, false, VertexStride, 0);
            // Color
            Gl.EnableVertexAttribArray(1);
            Gl.VertexAttribPointer(1, 4, Gl.UNSIGNED_BYTE, true, VertexStride, Vector3.SizeInBytes);
            // UV
            Gl.EnableVertexAttribArray(2);
            Gl.VertexAttribPointer(2, 2, (int)VertexPointerType.Float, false, VertexStride, Vector3.SizeInBytes + Color.SizeInBytes);

            shader.Use();

            Gl.BindTexture(batch.Texture.TextureTarget, batch.Texture.TextureID);
            Gl.DrawArrays(PrimitiveType.Triangles, 0, (int)(batch.VertexCount / 3));

            Gl.BindBuffer(BufferTargetARB.ArrayBuffer, 0);
            Gl.BindBuffer(BufferTargetARB.ElementArrayBuffer, 0);
            Gl.DisableVertexAttribArray(0);
            Gl.DisableVertexAttribArray(1);
            Gl.DisableVertexAttribArray(2);
            Gl.BindVertexArray(0);
        }
Beispiel #11
0
    public void BufferData()
    {
        Gl.BindVertexArray(arrayHandle);

        if (used > 0 && dirty)
        {
            unsafe
            {
                fixed(int *p = data)
                {
                    Gl.BindBuffer(BufferTarget.ArrayBuffer, bufferHandle);

                    // BufferSubData if we can
                    if (used <= lastUsed < used)
                    {
                        Gl.BufferSubData(BufferTarget.ArrayBuffer, (IntPtr)0, (uint)(used * vertexSize), (IntPtr)p);
                    }
                    else
                    {
                        Gl.BufferData(BufferTarget.ArrayBuffer, (uint)(used * vertexSize), (IntPtr)p, BufferUsage.StaticDraw);
                        lastUsed = used;
                    }

                    Gl.BindBuffer(BufferTarget.ArrayBuffer, 0);
                }
            }

            dirty = false;

            // Clear the data from memory as it is now stored on the GPU
            data = null;
        }

        Gl.BindVertexArray(0);
    }
Beispiel #12
0
        public void update(Vector4[] points)
        {
            if (vaoID_ == 0)
            {
                vaoID_ = Gl.GenVertexArray();
                Gl.GenBuffers(vboID_);
            }

            Gl.ShadeModel(ShadingModel.Smooth);
            index.Clear();
            for (int c = 0; c < points.Count(); c++)
            {
                index.Add(c);
            }

            Gl.BindVertexArray(vaoID_);

            if (points.Count() > 0)
            {
                Gl.BindBuffer(BufferTarget.ArrayBuffer, vboID_[0]);
                Gl.BufferData(BufferTarget.ArrayBuffer, (uint)points.Count() * sizeof(float) * 4, points, BufferUsage.DynamicDraw);
                Gl.VertexAttribPointer(Shader.ATTRIB_VERTICES_POS, 4, VertexAttribType.Float, false, 0, IntPtr.Zero);
                Gl.EnableVertexAttribArray(Shader.ATTRIB_VERTICES_POS);
            }

            Gl.BindBuffer(BufferTarget.ElementArrayBuffer, vboID_[1]);
            Gl.BufferData(BufferTarget.ElementArrayBuffer, (uint)index.Count() * sizeof(int), index.ToArray(), BufferUsage.DynamicDraw);
            current_fc = index.Count();

            Gl.BindVertexArray(0);
            Gl.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
            Gl.BindBuffer(BufferTarget.ArrayBuffer, 0);
        }
Beispiel #13
0
        // Initialize Opengl buffers
        public void initialize()
        {
            shaderImage.it = new Shader(Shader.IMAGE_VERTEX_SHADER, Shader.IMAGE_FRAGMENT_SHADER);
            texID          = Gl.GetUniformLocation(shaderImage.it.getProgramId(), "texImage");

            float[] g_quad_vertex_buffer_data = new float[18] {
                -1.0f, -1.0f, 0.0f,
                1.0f, -1.0f, 0.0f,
                -1.0f, 1.0f, 0.0f,
                -1.0f, 1.0f, 0.0f,
                1.0f, -1.0f, 0.0f,
                1.0f, 1, 0.0f
            };

            quad_vb = Gl.GenBuffer();
            Gl.BindBuffer(BufferTarget.ArrayBuffer, quad_vb);
            Gl.BufferData(BufferTarget.ArrayBuffer, (uint)(sizeof(float) * g_quad_vertex_buffer_data.Length), g_quad_vertex_buffer_data, BufferUsage.StaticDraw);
            Gl.BindBuffer(BufferTarget.ArrayBuffer, 0);

            Gl.Enable(EnableCap.Texture2d);
            imageTex = Gl.GenTexture();
            Gl.BindTexture(TextureTarget.Texture2d, imageTex);
            Gl.TexParameter(TextureTarget.Texture2d, TextureParameterName.TextureWrapS, Gl.CLAMP_TO_BORDER);
            Gl.TexParameter(TextureTarget.Texture2d, TextureParameterName.TextureWrapT, Gl.CLAMP_TO_BORDER);
            Gl.TexParameter(TextureTarget.Texture2d, TextureParameterName.TextureWrapR, Gl.CLAMP_TO_BORDER);
            Gl.TexParameter(TextureTarget.Texture2d, TextureParameterName.TextureMinFilter, Gl.LINEAR);
            Gl.TexParameter(TextureTarget.Texture2d, TextureParameterName.TextureMagFilter, Gl.LINEAR);
            Gl.TexImage2D(TextureTarget.Texture2d, 0, InternalFormat.Rgba, (int)resolution.width, (int)resolution.height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, null);
            Gl.BindTexture(TextureTarget.Texture2d, 0);
        }
Beispiel #14
0
        public void SetupMesh()
        {
            var vertexStructSize = 14 * sizeof(float);

            vao = Gl.GenVertexArray();
            Gl.BindVertexArray(vao);

            vbo = Gl.GenBuffer();
            ebo = Gl.GenBuffer();

            var glVertices = new List <float>();

            foreach (var vertex in Vertices)
            {
                glVertices.AddRange(new[] {
                    vertex.Position.x,
                    vertex.Position.y,
                    vertex.Position.z,

                    vertex.Normal.x,
                    vertex.Normal.y,
                    vertex.Normal.z,

                    vertex.Tangent.x,
                    vertex.Tangent.y,
                    vertex.Tangent.z,

                    vertex.BiTangent.x,
                    vertex.BiTangent.y,
                    vertex.BiTangent.z,

                    vertex.TexCoords.x,
                    vertex.TexCoords.y
                });
            }

            Gl.BindBuffer(BufferTarget.ArrayBuffer, vbo);
            Gl.BufferData(BufferTarget.ArrayBuffer, (uint)glVertices.Count * sizeof(float), glVertices.ToArray(), BufferUsage.StaticDraw);

            Gl.BindBuffer(BufferTarget.ElementArrayBuffer, ebo);
            Gl.BufferData(BufferTarget.ElementArrayBuffer, (uint)Indices.Length * sizeof(uint), Indices, BufferUsage.StaticDraw);

            Gl.EnableVertexAttribArray(0);
            Gl.VertexAttribPointer(0, 3, VertexAttribType.Float, false, vertexStructSize, (IntPtr)0);

            Gl.EnableVertexAttribArray(1);
            Gl.VertexAttribPointer(1, 3, VertexAttribType.Float, false, vertexStructSize, (IntPtr)(3 * sizeof(float)));

            Gl.EnableVertexAttribArray(2);
            Gl.VertexAttribPointer(2, 3, VertexAttribType.Float, false, vertexStructSize, (IntPtr)(6 * sizeof(float)));

            Gl.EnableVertexAttribArray(3);
            Gl.VertexAttribPointer(3, 3, VertexAttribType.Float, false, vertexStructSize, (IntPtr)(9 * sizeof(float)));

            Gl.EnableVertexAttribArray(4);
            Gl.VertexAttribPointer(4, 2, VertexAttribType.Float, false, vertexStructSize, (IntPtr)(12 * sizeof(float)));

            Gl.BindVertexArray(0);
        }
 public void CopyData(T[] data)
 {
     if (data == null)
     {
         throw new ArgumentNullException(nameof(data));
     }
     Gl.BufferData(BufferTarget.ArrayBuffer, (this.vertexSize * (uint)data.Length), data, usage);
 }
Beispiel #16
0
 public void BufferData(long size, IntPtr data, Usage usage)
 {
     unsafe
     {
         Gl.BufferData(Target, (void *)size, data.ToPointer(), (uint)usage);
         GlHelper.GetError();
     }
 }
        private void bindindicesBuffer(uint[] indices)
        {
            uint vboId = Gl.GenBuffer();

            VBOS.Add(vboId);
            Gl.BindBuffer(BufferTarget.ElementArrayBuffer, vboId);
            Gl.BufferData(BufferTarget.ElementArrayBuffer, (uint)(indices.Length * sizeof(uint)), indices, BufferUsage.StaticDraw);
        }
Beispiel #18
0
 public void CopyRawData(byte[] data)
 {
     if (data == null)
     {
         throw new ArgumentNullException(nameof(data));
     }
     Gl.BufferData(BufferTarget.ElementArrayBuffer, (uint)data.Length, data, usage);
 }
Beispiel #19
0
        public void TestBufferData()
        {
            if (!HasVersion(1, 5) && !IsGlExtensionSupported("GL_ARB_vertex_buffer_object") && !HasEsVersion(1, 0))
            {
                Assert.Inconclusive("OpenGL 1.5 or GL_ARB_vertex_buffer_object not supported or OpenGL ES 1.0");
            }

            int arrayBufferGet;

            uint arrayBuffer = Gl.GenBuffer();

            try {
                Assert.AreNotEqual(0, arrayBuffer, "Gl.GenBuffer failure");
                Assert.IsFalse(Gl.IsBuffer(arrayBuffer));

                Gl.BindBuffer(BufferTarget.ArrayBuffer, arrayBuffer);
                Assert.IsTrue(Gl.IsBuffer(arrayBuffer));

                Gl.Get(Gl.ARRAY_BUFFER_BINDING, out arrayBufferGet);
                Assert.AreEqual((int)arrayBuffer, arrayBufferGet);

                Random random          = new Random();
                byte[] arrayBufferData = new byte[64], arrayBufferDataGet = new byte[64];
                for (int i = 0; i < arrayBufferData.Length; i++)
                {
                    arrayBufferData[i] = (Byte)random.Next(Byte.MaxValue);
                }

                int arrayBufferDataParam;

                Gl.BufferData(BufferTarget.ArrayBuffer, (uint)arrayBufferData.Length, arrayBufferData, BufferUsage.StaticDraw);

                Gl.GetBufferParameter(BufferTarget.ArrayBuffer, Gl.BUFFER_SIZE, out arrayBufferDataParam);
                Assert.AreEqual(arrayBufferData.Length, arrayBufferDataParam);

                Gl.GetBufferParameter(BufferTarget.ArrayBuffer, Gl.BUFFER_USAGE, out arrayBufferDataParam);
                Assert.AreEqual((int)BufferUsage.StaticDraw, arrayBufferDataParam);

                if (HasVersion(1, 5) || IsGlExtensionSupported("GL_ARB_vertex_buffer_object"))
                {
                    Gl.GetBufferSubData(BufferTarget.ArrayBuffer, IntPtr.Zero, (uint)arrayBufferData.Length, arrayBufferDataGet);
                    for (int i = 0; i < arrayBufferDataGet.Length; i++)
                    {
                        Assert.AreEqual(arrayBufferData[i], arrayBufferDataGet[i]);
                    }
                }

                Gl.BindBuffer(BufferTarget.ArrayBuffer, 0);
                Gl.Get(Gl.ARRAY_BUFFER_BINDING, out arrayBufferGet);
                Assert.AreEqual(0, arrayBufferGet);
            } finally {
                if (arrayBuffer != 0)
                {
                    Gl.DeleteBuffers(new UInt32[] { arrayBuffer });
                    Assert.IsFalse(Gl.IsBuffer(arrayBuffer));
                }
            }
        }
Beispiel #20
0
        public void initialize(uint maxNumVertices, uint[] texChannels, ushort[] indices, uint maxNumRenderCalls)
        {
            {
                uint positionsDataSize = (uint)(maxNumVertices * Marshal.SizeOf(typeof(Neutrino._math.vec3)));
                positionsId_ = Gl.GenBuffer();
                positions_   = new Neutrino._math.vec3[maxNumVertices];

                Gl.BindBuffer(BufferTarget.ArrayBuffer, positionsId_);
                Gl.BufferData(BufferTarget.ArrayBuffer, (IntPtr)positionsDataSize, IntPtr.Zero, BufferUsageHint.DynamicDraw);
            }

            {
                uint colorsDataSize = (uint)(maxNumVertices * Marshal.SizeOf(typeof(uint)));
                colorsId_ = Gl.GenBuffer();
                colors_   = new uint[maxNumVertices];

                Gl.BindBuffer(BufferTarget.ArrayBuffer, colorsId_);
                Gl.BufferData(BufferTarget.ArrayBuffer, (IntPtr)colorsDataSize, IntPtr.Zero, BufferUsageHint.DynamicDraw);
            }

            texChannels_ = new TexChannel[texChannels.Length];

            for (uint texIndex = 0; texIndex < texChannels.Length; ++texIndex)
            {
                TexChannel texChannel  = new TexChannel();
                uint       texDataSize = (uint)(maxNumVertices * texChannels[texIndex] * Marshal.SizeOf(typeof(float)));
                texChannel.dimensions_ = texChannels[texIndex];
                texChannel.id_         = Gl.GenBuffer();
                texChannel.data_       = new float[maxNumVertices * texChannels[texIndex]];

                Gl.BindBuffer(BufferTarget.ArrayBuffer, texChannel.id_);
                Gl.BufferData(BufferTarget.ArrayBuffer, (IntPtr)texDataSize, IntPtr.Zero, BufferUsageHint.DynamicDraw);

                texChannels_[texIndex] = texChannel;
            }

            {
                uint indicesDataSize = (uint)(indices.Length * Marshal.SizeOf(typeof(ushort)));
                indicesId_   = Gl.GenBuffer();
                indicesData_ = Marshal.AllocHGlobal((int)indicesDataSize);

                {
                    IntPtr ptr = indicesData_;
                    for (uint i = 0; i < indices.Length; ptr = IntPtr.Add(ptr, Marshal.SizeOf(typeof(ushort))), ++i)
                    {
                        Marshal.WriteInt16(ptr, (short)indices[i]);
                    }
                }

                Gl.BindBuffer(BufferTarget.ElementArrayBuffer, indicesId_);
                Gl.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)indicesDataSize, indicesData_, BufferUsageHint.StaticDraw);
            }

            numVertices_ = 0;

            renderCalls_    = new Neutrino.RenderCall[maxNumRenderCalls];
            numRenderCalls_ = 0;
        }
Beispiel #21
0
        static void Main(string[] args)
        {
            Glfw.Init();

            Glfw.WindowHint(Hint.ContextVersionMajor, 4);
            Glfw.WindowHint(Hint.ContextVersionMinor, 6);
            Glfw.WindowHint(Hint.OpenglProfile, Profile.Compatibility);
            Window window = Glfw.CreateWindow(1080, 720, "Yeet", Monitor.None, Window.None);

            // `Gl.Initialize()` has to be don before `Glfw.MakeContextCurrent(window)`
            // [How Do I Initialize OpenGL.NET with GLFW.Net?](https://stackoverflow.com/questions/61318104/how-do-i-initialize-opengl-net-with-glfw-net/61319044?noredirect=1#comment108476826_61319044)
            Gl.Initialize();
            Glfw.MakeContextCurrent(window);

            var v = Gl.GetString(StringName.Version);

            Console.WriteLine(v);

            uint vao = Gl.CreateVertexArray();

            Gl.BindVertexArray(vao);

            uint vbo = Gl.GenBuffer();

            Gl.BindBuffer(BufferTarget.ArrayBuffer, vbo);

            var vertices = new float[] { -0.5f, -0.5f, 0.5f, -0.5f, 0.0f, 0.5f };

            Gl.BufferData(BufferTarget.ArrayBuffer, (uint)(4 * vertices.Length), null, BufferUsage.StaticDraw);

            IntPtr unmanagedPointer = Marshal.AllocHGlobal(4 * vertices.Length);

            Marshal.Copy(vertices, 0, unmanagedPointer, vertices.Length);
            Gl.BufferSubData(BufferTarget.ArrayBuffer, new IntPtr(0), (uint)(4 * vertices.Length), unmanagedPointer);
            Marshal.FreeHGlobal(unmanagedPointer);

            //Gl.BufferSubData(BufferTarget.ArrayBuffer, new IntPtr(0), (uint)(4 * vertices.Length), vertices);

            Gl.VertexAttribPointer(0, 2, VertexAttribType.Float, false, 0, null);

            while (!Glfw.WindowShouldClose(window))
            {
                Glfw.PollEvents();

                Gl.ClearColor(0.0f, 1.0f, 1.0f, 1.0f);
                Gl.Clear(ClearBufferMask.ColorBufferBit);
                Gl.BindVertexArray(vao);
                Gl.EnableVertexAttribArray(0);
                Gl.DrawArrays(PrimitiveType.Triangles, 0, 3);
                Gl.DisableVertexAttribArray(0);
                Gl.BindVertexArray(0);

                Glfw.SwapBuffers(window);
            }

            Glfw.DestroyWindow(window);
            Glfw.Terminate();
        }
Beispiel #22
0
        void bindIndicesBuffer(int[] indices)
        {
            uint vboID = Gl.GenBuffer();

            _vbos.Add(vboID);
            Gl.BindBuffer(BufferTarget.ElementArrayBuffer, vboID);

            Gl.BufferData(BufferTarget.ElementArrayBuffer, (uint)(indices.Length * sizeof(int)), indices, BufferUsage.StaticDraw);
        }
Beispiel #23
0
        public void BufferData(int newLength)
        {
            currentLength = newLength;

            // Bind, buffer and unbind
            Gl.BindBuffer(BufferTarget.ArrayBuffer, bufferHandle);
            Gl.BufferData(BufferTarget.ArrayBuffer, (uint)(currentLength * vertexSize), IntPtr.Zero, BufferUsage.DynamicDraw);
            Gl.BindBuffer(BufferTarget.ArrayBuffer, 0);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="OpenGLVertexBuffer"/> class.
        /// </summary>
        /// <param name="vertices">The vertices.</param>
        /// <exception cref="ArgumentNullException"></exception>
        public OpenGLVertexBuffer(float[] vertices)
        {
            _vertices = vertices ?? throw new ArgumentNullException();
            var size = (uint)(vertices.Length * sizeof(float));

            _id = Gl.GenBuffer();
            Gl.BindBuffer(BufferTarget.ArrayBuffer, _id);
            Gl.BufferData(BufferTarget.ArrayBuffer, size, vertices, BufferUsage.StaticDraw);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="OpenGLIndexBuffer"/> class.
        /// </summary>
        /// <param name="indices">The indices.</param>
        /// <exception cref="ArgumentNullException"></exception>
        public OpenGLIndexBuffer(int[] indices)
        {
            _indices = indices ?? throw new ArgumentNullException();
            var size = (uint)(indices.Length * sizeof(int));

            _id = Gl.GenBuffer();
            Gl.BindBuffer(BufferTarget.ElementArrayBuffer, _id);
            Gl.BufferData(BufferTarget.ElementArrayBuffer, size, indices, BufferUsage.StaticDraw);
        }
Beispiel #26
0
            /// <summary>
            /// Update the reference Buffer, using this technique.
            /// </summary>
            /// <param name="ctx">
            /// A <see cref="GraphicsContext"/> used for allocating resources.
            /// </param>
            public override void Create(GraphicsContext ctx)
            {
                if (ctx.Extensions.DirectStateAccess_ARB || ctx.Version.IsCompatible(Gl.Version_450))
                {
                    if (!ctx.Extensions.BufferStorage_ARB || !Buffer.Immutable)
                    {
                        // Emulates glBufferStorage error checking (only for size)
                        if (Buffer.Immutable && Buffer.Size != 0)
                        {
                            throw new GlException(ErrorCode.InvalidOperation);
                        }

                        Gl.NamedBufferData(Buffer.ObjectName, _Size, _Array, Buffer.Hint);
                    }
                    else
                    {
                        if (Buffer.Immutable)
                        {
                            Gl.NamedBufferStorage(Buffer.ObjectName, _Size, _Array, Buffer.UsageMask);
                        }
                        else
                        {
                            Gl.NamedBufferData(Buffer.ObjectName, _Size, _Array, Buffer.Hint);
                        }
                    }
                }
                else
                {
                    if (!ctx.Extensions.BufferStorage_ARB || !Buffer.Immutable)
                    {
                        // Emulates glBufferStorage error checking (only for size)
                        if (Buffer.Immutable && Buffer.Size != 0)
                        {
                            throw new GlException(ErrorCode.InvalidOperation);
                        }

                        Gl.BufferData(Buffer.Target, _Size, _Array, Buffer.Hint);
                    }
                    else
                    {
                        if (Buffer.Immutable)
                        {
                            Gl.BufferStorage(Buffer.Target, _Size, _Array, Buffer.UsageMask);
                        }
                        else
                        {
                            Gl.BufferData(Buffer.Target, _Size, _Array, Buffer.Hint);
                        }
                    }
                }

                // Explictly check for errors
                Gl.CheckErrors();

                Buffer.Size = _Size;
            }
Beispiel #27
0
        private void AddDataToBuffer(uint index, double[] data)
        {
            uint vboId = Gl.GenBuffer();

            vbos.Add(vboId);
            Gl.BindBuffer(BufferTarget.ArrayBuffer, vboId);
            Gl.BufferData(BufferTarget.ArrayBuffer, (uint)(8 * data.Length), data, BufferUsage.StaticDraw);
            Gl.VertexAttribPointer(index, 2, VertexAttribType.Double, false, 0, IntPtr.Zero);
            Gl.BindBuffer(BufferTarget.ArrayBuffer, 0);
        }
        private void StoreDataInAttributeList(uint attribute_number, int coordinateSize, float[] data)
        {
            uint vboID = Gl.GenBuffer();

            VBOS.Add(vboID);
            Gl.BindBuffer(BufferTarget.ArrayBuffer, vboID);
            Gl.BufferData(BufferTarget.ArrayBuffer, (uint)(data.Length * sizeof(float)), data, BufferUsage.StaticDraw);
            Gl.VertexAttribPointer(attribute_number, coordinateSize, VertexAttribType.Float, false, 0, IntPtr.Zero);
            Gl.BindBuffer(BufferTarget.ArrayBuffer, 0);
        }
Beispiel #29
0
        public GpuSpace(DiscreteBounds bounds)
        {
            _buffer  = Gl.GenBuffer();
            _texture = Gl.GenTexture();

            Bounds = bounds;

            Gl.BindBuffer(BufferTarget.TextureBuffer, _buffer);
            Gl.BufferData(BufferTarget.TextureBuffer, (uint)Marshal.SizeOf <VoxelFace>() * (uint)bounds.Length, null, BufferUsage.DynamicDraw);
        }
Beispiel #30
0
        private uint CreateAndInitializeVertexAttribArray <T>(AttributeType location, T[] array)
        {
            uint buffer = Gl.GenBuffer();

            Gl.EnableVertexAttribArray((uint)location);
            Gl.BindBuffer(BufferTarget.ArrayBuffer, buffer);
            Gl.BufferData(BufferTarget.ArrayBuffer, (uint)(Marshal.SizeOf(typeof(T)) * array.Length), array, BufferUsage.StaticDraw);
            Gl.VertexAttribPointer((uint)location, Marshal.SizeOf(typeof(T)) / Marshal.SizeOf(typeof(float)), VertexAttribType.Float, false, 0, IntPtr.Zero);
            return(buffer);
        }