Example #1
0
        public Mesh (MemoryBlock vertices, VertexLayout decl, ushort[] indices) {
            var group = new MeshGroup();
            group.VertexBuffer = new VertexBuffer(vertices, decl);
            group.IndexBuffer = new IndexBuffer(MemoryBlock.FromArray(indices));

            vertexDecl = decl;
            groups = new List<MeshGroup> { group };
        }
Example #2
0
        /// <summary>
        /// Welds vertices that are close together.
        /// </summary>
        /// <param name="layout">The layout of the vertex stream.</param>
        /// <param name="data">A pointer to the vertex data stream.</param>
        /// <param name="count">The number of vertices in the stream.</param>
        /// <param name="remappingTable">An output remapping table from the original vertices to the welded ones.</param>
        /// <param name="epsilon">The tolerance for welding vertex positions.</param>
        /// <returns>
        /// The number of unique vertices after welding.
        /// </returns>
        public static int WeldVertices(VertexLayout layout, IntPtr data, int count, out int[] remappingTable, float epsilon = 0.001f)
        {
            var output = stackalloc ushort[count];
            var result = NativeMethods.bgfx_weld_vertices(output, ref layout.data, data, (ushort)count, epsilon);

            remappingTable = new int[count];
            for (int i = 0; i < count; i++)
            {
                remappingTable[i] = output[i];
            }

            return(result);
        }
Example #3
0
        private void Init()
        {
            _imguiContext = ImGui.CreateContext();
            var io = ImGui.GetIO();

            // load the shaders for imgui
            _imguiProgram   = ResourceLoader.LoadProgram("vs_ocornut_imgui", "fs_ocornut_imgui");
            _textureProgram = ResourceLoader.LoadProgram("vs_imgui_image", "fs_imgui_image");

            _vertexLayout = new VertexLayout();
            _vertexLayout.Begin()
            .Add(VertexAttributeUsage.Position, 2, VertexAttributeType.Float)
            .Add(VertexAttributeUsage.TexCoord0, 2, VertexAttributeType.Float)
            .Add(VertexAttributeUsage.Color0, 4, VertexAttributeType.UInt8, true)
            .End();

            _texUniform = new Uniform("s_tex", UniformType.Sampler);

            _fonts.Add("default", io.Fonts.AddFontDefault());

            // Fonts: All fonts need to be known here, because we compile the fontatlas only once
            // Example code:

            /*foreach (var font in fonts)
             * {
             *      _fonts.Add(font.name, io.Fonts.AddFontFromFileTTF(font.path, font.size));
             * }*/

            // Build and save the fontatlas
            unsafe
            {
                io.Fonts.GetTexDataAsRGBA32(out var data, out var width, out var height, out var bytesPerPixel);

                FontAtlas = Texture.Create2D(width, height, false, 1, TextureFormat.BGRA8, 0,
                                             new MemoryBlock((IntPtr)data, width * height * bytesPerPixel));

                _textures.Add((IntPtr)FontAtlas.GetHashCode(), FontAtlas);
            }

            io.Fonts.SetTexID((IntPtr)FontAtlas.GetHashCode());

            // ToDo: Set KeyMappings
            // -> Engine specific
        }
Example #4
0
 public static extern ushort bgfx_create_dynamic_vertex_buffer(int vertexCount, ref VertexLayout.Data decl, BufferFlags flags);
Example #5
0
 public static extern void bgfx_vertex_unpack(float* output, VertexAttributeUsage attribute, ref VertexLayout.Data decl, IntPtr data, int index);
Example #6
0
 public static extern bool bgfx_check_avail_transient_buffers(int numVertices, ref VertexLayout.Data decl, int numIndices);
Example #7
0
 public static extern void bgfx_vertex_decl_begin(ref VertexLayout.Data decl, RendererBackend backend);
Example #8
0
 public static extern void bgfx_vertex_decl_skip(ref VertexLayout.Data decl, byte count);
Example #9
0
 public static extern bool bgfx_alloc_transient_buffers(out TransientVertexBuffer tvb, ref VertexLayout.Data decl, ushort numVertices, out TransientIndexBuffer tib, ushort numIndices);
 /// <summary>
 /// Gets the available space in the global transient vertex buffer.
 /// </summary>
 /// <param name="count">The number of vertices required.</param>
 /// <param name="layout">The layout of each vertex.</param>
 /// <returns>The number of available vertices.</returns>
 public static int GetAvailableSpace(int count, VertexLayout layout)
 {
     return(NativeMethods.bgfx_get_avail_transient_vertex_buffer(count, ref layout.data));
 }
Example #11
0
 /// <summary>
 /// Packs a vector into vertex stream format.
 /// </summary>
 /// <param name="input">The four element vector to pack.</param>
 /// <param name="inputNormalized"><c>true</c> if the input vector is normalized.</param>
 /// <param name="attribute">The attribute usage of the vector data.</param>
 /// <param name="layout">The layout of the vertex stream.</param>
 /// <param name="data">The pointer to the vertex data stream.</param>
 /// <param name="index">The index of the vertex within the stream.</param>
 public static void VertexPack(float* input, bool inputNormalized, VertexAttributeUsage attribute, VertexLayout layout, IntPtr data, int index = 0)
 {
     NativeMethods.bgfx_vertex_pack(input, inputNormalized, attribute, ref layout.data, data, index);
 }
Example #12
0
 /// <summary>
 /// Unpack a vector from a vertex stream.
 /// </summary>
 /// <param name="output">A pointer to four floats that will receive the unpacked vector.</param>
 /// <param name="attribute">The usage of the vertex attribute.</param>
 /// <param name="layout">The layout of the vertex stream.</param>
 /// <param name="data">A pointer to the vertex data stream.</param>
 /// <param name="index">The index of the vertex within the stream.</param>
 public static void VertexUnpack(float* output, VertexAttributeUsage attribute, VertexLayout layout, IntPtr data, int index = 0)
 {
     NativeMethods.bgfx_vertex_unpack(output, attribute, ref layout.data, data, index);
 }
Example #13
0
 /// <summary>
 /// Converts a stream of vertex data from one format to another.
 /// </summary>
 /// <param name="destinationLayout">The destination format.</param>
 /// <param name="destinationData">A pointer to the output location.</param>
 /// <param name="sourceLayout">The source format.</param>
 /// <param name="sourceData">A pointer to the source vertex data to convert.</param>
 /// <param name="count">The number of vertices to convert.</param>
 public static void VertexConvert(VertexLayout destinationLayout, IntPtr destinationData, VertexLayout sourceLayout, IntPtr sourceData, int count = 1)
 {
     NativeMethods.bgfx_vertex_convert(ref destinationLayout.data, destinationData, ref sourceLayout.data, sourceData, count);
 }
Example #14
0
 /// <summary>
 /// Checks for available space to allocate transient index and vertex buffers.
 /// </summary>
 /// <param name="vertexCount">The number of vertices to allocate.</param>
 /// <param name="layout">The layout of each vertex.</param>
 /// <param name="indexCount">The number of indices to allocate.</param>
 /// <returns><c>true</c> if there is sufficient space for both vertex and index buffers.</returns>
 public static bool CheckAvailableTransientBufferSpace(int vertexCount, VertexLayout layout, int indexCount)
 {
     return NativeMethods.bgfx_check_avail_transient_buffers(vertexCount, ref layout.data, indexCount);
 }
Example #15
0
 /// <summary>
 /// Attempts to allocate both a transient vertex buffer and index buffer.
 /// </summary>
 /// <param name="vertexCount">The number of vertices to allocate.</param>
 /// <param name="layout">The layout of each vertex.</param>
 /// <param name="indexCount">The number of indices to allocate.</param>
 /// <param name="vertexBuffer">Returns the allocated transient vertex buffer.</param>
 /// <param name="indexBuffer">Returns the allocated transient index buffer.</param>
 /// <returns><c>true</c> if both space requirements are satisfied and the buffers were allocated.</returns>
 public static bool AllocateTransientBuffers(int vertexCount, VertexLayout layout, int indexCount, out TransientVertexBuffer vertexBuffer, out TransientIndexBuffer indexBuffer)
 {
     return NativeMethods.bgfx_alloc_transient_buffers(out vertexBuffer, ref layout.data, (ushort)vertexCount, out indexBuffer, (ushort)indexCount);
 }
Example #16
0
 /// <summary>
 /// Converts a stream of vertex data from one format to another.
 /// </summary>
 /// <param name="destinationLayout">The destination format.</param>
 /// <param name="destinationData">A pointer to the output location.</param>
 /// <param name="sourceLayout">The source format.</param>
 /// <param name="sourceData">A pointer to the source vertex data to convert.</param>
 /// <param name="count">The number of vertices to convert.</param>
 public static void VertexConvert(VertexLayout destinationLayout, IntPtr destinationData, VertexLayout sourceLayout, IntPtr sourceData, int count = 1)
 {
     NativeMethods.bgfx_vertex_convert(ref destinationLayout.data, destinationData, ref sourceLayout.data, sourceData, count);
 }
Example #17
0
 public static extern void bgfx_vertex_convert(ref VertexLayout.Data destDecl, IntPtr destData, ref VertexLayout.Data srcDecl, IntPtr srcData, int num);
Example #18
0
        /// <summary>
        /// Welds vertices that are close together.
        /// </summary>
        /// <param name="layout">The layout of the vertex stream.</param>
        /// <param name="data">A pointer to the vertex data stream.</param>
        /// <param name="count">The number of vertices in the stream.</param>
        /// <param name="remappingTable">An output remapping table from the original vertices to the welded ones.</param>
        /// <param name="epsilon">The tolerance for welding vertex positions.</param>
        /// <returns>
        /// The number of unique vertices after welding.
        /// </returns>
        public static int WeldVertices(VertexLayout layout, IntPtr data, int count, out int[] remappingTable, float epsilon = 0.001f)
        {
            var output = stackalloc ushort[count];
            var result = NativeMethods.bgfx_weld_vertices(output, ref layout.data, data, (ushort)count, epsilon);

            remappingTable = new int[count];
            for (int i = 0; i < count; i++)
                remappingTable[i] = output[i];

            return result;
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="TransientVertexBuffer"/> struct.
 /// </summary>
 /// <param name="vertexCount">The number of vertices that fit in the buffer.</param>
 /// <param name="layout">The layout of the vertex data.</param>
 public TransientVertexBuffer(int vertexCount, VertexLayout layout)
 {
     NativeMethods.bgfx_alloc_transient_vertex_buffer(out this, vertexCount, ref layout.data);
 }
Example #20
0
 public static extern void bgfx_alloc_transient_vertex_buffer(out TransientVertexBuffer tvb, int num, ref VertexLayout.Data decl);
Example #21
0
 /// <summary>
 /// Checks for available space to allocate transient index and vertex buffers.
 /// </summary>
 /// <param name="vertexCount">The number of vertices to allocate.</param>
 /// <param name="layout">The layout of each vertex.</param>
 /// <param name="indexCount">The number of indices to allocate.</param>
 /// <returns><c>true</c> if there is sufficient space for both vertex and index buffers.</returns>
 public static bool CheckAvailableTransientBufferSpace(int vertexCount, VertexLayout layout, int indexCount)
 {
     return(NativeMethods.bgfx_check_avail_transient_buffers(vertexCount, ref layout.data, indexCount));
 }
Example #22
0
 public static extern void bgfx_vertex_decl_add(ref VertexLayout.Data decl, VertexAttributeUsage attribute, byte count, VertexAttributeType type, [MarshalAs(UnmanagedType.U1)] bool normalized, [MarshalAs(UnmanagedType.U1)] bool asInt);
Example #23
0
 /// <summary>
 /// Initializes a new instance of the <see cref="VertexBuffer"/> struct.
 /// </summary>
 /// <param name="memory">The vertex data with which to populate the buffer.</param>
 /// <param name="layout">The layout of the vertex data.</param>
 /// <param name="flags">Flags used to control buffer behavior.</param>
 public VertexBuffer(MemoryBlock memory, VertexLayout layout, BufferFlags flags = BufferFlags.None)
 {
     handle = NativeMethods.bgfx_create_vertex_buffer(memory.ptr, ref layout.data, flags);
 }
Example #24
0
 public static extern void bgfx_vertex_decl_end(ref VertexLayout.Data decl);
Example #25
0
 internal Mesh (VertexLayout decl, List<MeshGroup> groups) {
     vertexDecl = decl;
     this.groups = groups;
 }
Example #26
0
 public static extern void bgfx_vertex_pack(float* input, [MarshalAs(UnmanagedType.U1)] bool inputNormalized, VertexAttributeUsage attribute, ref VertexLayout.Data decl, IntPtr data, int index);
Example #27
0
 /// <summary>
 /// Attempts to allocate both a transient vertex buffer and index buffer.
 /// </summary>
 /// <param name="vertexCount">The number of vertices to allocate.</param>
 /// <param name="layout">The layout of each vertex.</param>
 /// <param name="indexCount">The number of indices to allocate.</param>
 /// <param name="vertexBuffer">Returns the allocated transient vertex buffer.</param>
 /// <param name="indexBuffer">Returns the allocated transient index buffer.</param>
 /// <returns><c>true</c> if both space requirements are satisfied and the buffers were allocated.</returns>
 public static bool AllocateTransientBuffers(int vertexCount, VertexLayout layout, int indexCount, out TransientVertexBuffer vertexBuffer, out TransientIndexBuffer indexBuffer)
 {
     return(NativeMethods.bgfx_alloc_transient_buffers(out vertexBuffer, ref layout.data, (ushort)vertexCount, out indexBuffer, (ushort)indexCount));
 }
Example #28
0
 public static extern ushort bgfx_weld_vertices(ushort* output, ref VertexLayout.Data decl, IntPtr data, ushort num, float epsilon);
Example #29
0
 /// <summary>
 /// Packs a vector into vertex stream format.
 /// </summary>
 /// <param name="input">The four element vector to pack.</param>
 /// <param name="inputNormalized"><c>true</c> if the input vector is normalized.</param>
 /// <param name="attribute">The attribute usage of the vector data.</param>
 /// <param name="layout">The layout of the vertex stream.</param>
 /// <param name="data">The pointer to the vertex data stream.</param>
 /// <param name="index">The index of the vertex within the stream.</param>
 public static void VertexPack(float *input, bool inputNormalized, VertexAttributeUsage attribute, VertexLayout layout, IntPtr data, int index = 0)
 {
     NativeMethods.bgfx_vertex_pack(input, inputNormalized, attribute, ref layout.data, data, index);
 }
Example #30
0
 public static extern bool bgfx_check_avail_transient_vertex_buffer(int num, ref VertexLayout.Data decl);
Example #31
0
 /// <summary>
 /// Unpack a vector from a vertex stream.
 /// </summary>
 /// <param name="output">A pointer to four floats that will receive the unpacked vector.</param>
 /// <param name="attribute">The usage of the vertex attribute.</param>
 /// <param name="layout">The layout of the vertex stream.</param>
 /// <param name="data">A pointer to the vertex data stream.</param>
 /// <param name="index">The index of the vertex within the stream.</param>
 public static void VertexUnpack(float *output, VertexAttributeUsage attribute, VertexLayout layout, IntPtr data, int index = 0)
 {
     NativeMethods.bgfx_vertex_unpack(output, attribute, ref layout.data, data, index);
 }
Example #32
0
 public static extern ushort bgfx_create_dynamic_vertex_buffer_mem(MemoryBlock.DataPtr* memory, ref VertexLayout.Data decl, BufferFlags flags);
Example #33
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DynamicVertexBuffer"/> struct.
 /// </summary>
 /// <param name="vertexCount">The number of vertices that fit in the buffer.</param>
 /// <param name="layout">The layout of the vertex data.</param>
 /// <param name="flags">Flags used to control buffer behavior.</param>
 public DynamicVertexBuffer(int vertexCount, VertexLayout layout, BufferFlags flags = BufferFlags.None)
 {
     handle = NativeMethods.bgfx_create_dynamic_vertex_buffer(vertexCount, ref layout.data, flags);
 }