Example #1
0
        public VertexBuffer(int size)
        {
            OpenGL.glGenBuffers(1, out buffer);
            OpenGL.CheckGLError();
            Bind();

            // Generates a buffer with uninitialized memory.
            OpenGL.glBufferData(OpenGL.GL_ARRAY_BUFFER,
                                new IntPtr(VertexSize * size),
                                IntPtr.Zero,
                                OpenGL.GL_DYNAMIC_DRAW);
            OpenGL.CheckGLError();

            // We need to zero all the memory. Let's generate a smallish array and copy that over the whole buffer.
            var zeroedArrayElementSize = Math.Min(size, 2048);
            var ptr = GCHandle.Alloc(new T[zeroedArrayElementSize], GCHandleType.Pinned);

            try
            {
                for (var offset = 0; offset < size; offset += zeroedArrayElementSize)
                {
                    var length = Math.Min(zeroedArrayElementSize, size - offset);
                    OpenGL.glBufferSubData(OpenGL.GL_ARRAY_BUFFER,
                                           new IntPtr(VertexSize * offset),
                                           new IntPtr(VertexSize * length),
                                           ptr.AddrOfPinnedObject());
                    OpenGL.CheckGLError();
                }
            }
            finally
            {
                ptr.Free();
            }
        }
Example #2
0
        public VertexBuffer(int size, string ownername)
        {
                        #if DEBUG_VERTEX
            Console.WriteLine("VB created owner: " + ownername);
#endif
            this.ownername = ownername;
            GraphicsContext.VAOReserveStack += 1;             //помечаем, что резерв уменьшился.
            LocalVertexArrayIndex            = GraphicsContext.VAOReserveStack;


            BindOnceOpen();

            // Generates a buffer with uninitialized memory.
            OpenGL.glBufferData(OpenGL.GL_ARRAY_BUFFER,
                                new IntPtr(VertexSize * size),
                                IntPtr.Zero,
                                OpenGL.GL_DYNAMIC_DRAW);
            OpenGL.CheckGLError();

            // We need to zero all the memory. Let's generate a smallish array and copy that over the whole buffer.
            var zeroedArrayElementSize = Math.Min(size, 2048);
            var ptr = GCHandle.Alloc(new T[zeroedArrayElementSize], GCHandleType.Pinned);
            try
            {
                for (var offset = 0; offset < size; offset += zeroedArrayElementSize)
                {
                    var length = Math.Min(zeroedArrayElementSize, size - offset);
                    OpenGL.glBufferSubData(OpenGL.GL_ARRAY_BUFFER,
                                           new IntPtr(VertexSize * offset),
                                           new IntPtr(VertexSize * length),
                                           ptr.AddrOfPinnedObject());
                    OpenGL.CheckGLError();
                }
            }
            finally
            {
                ptr.Free();
            }
            BindOnceClose();
        }
Example #3
0
        public VertexBuffer(int size)
        {
            OpenGL.glGenBuffers(1, out buffer);
            OpenGL.CheckGLError();
            Bind();

            var ptr = GCHandle.Alloc(new T[size], GCHandleType.Pinned);

            try
            {
                OpenGL.glBufferData(OpenGL.GL_ARRAY_BUFFER,
                                    new IntPtr(VertexSize * size),
                                    ptr.AddrOfPinnedObject(),
                                    OpenGL.GL_DYNAMIC_DRAW);
            }
            finally
            {
                ptr.Free();
            }

            OpenGL.CheckGLError();
        }