Пример #1
0
        public bool LoadFromVBM(string filename, int vertexIndex, int normalIndex, int texCoord0Index)
        {
            //FILE * f = NULL;
            FileStream   f  = new FileStream(filename, FileMode.Open, FileAccess.Read);
            BinaryReader br = new BinaryReader(f);

            //f = fopen(filename, "rb");
            //if(f == NULL)
            //return false;

            //fseek(f, 0, SEEK_END);
            //size_t filesize = ftell(f);
            //fseek(f, 0, SEEK_SET);
            long filesize = f.Length;

            //f.Seek(0, SeekOrigin.End);
            //f.Seek(0, SeekOrigin.Begin);

            byte[] data = new byte[filesize];
            UnmanagedArray <byte> raw_data;

            f.Read(data, 0, (int)filesize);
            //f.Close();
            f.Seek(0, SeekOrigin.Begin);

            //VBM_HEADER * header = (VBM_HEADER *)data;
            VBM_HEADER header = br.ReadStruct <VBM_HEADER>();
            //raw_data = data + header.size + header->num_attribs * sizeof(VBM_ATTRIB_HEADER) + header->num_frames * sizeof(VBM_FRAME_HEADER);
            //{
            //    long offset = header.size + header.num_attribs * Marshal.SizeOf(typeof(VBM_ATTRIB_HEADER)) + header.num_frames * Marshal.SizeOf(typeof(VBM_FRAME_HEADER));
            //    raw_data = new UnmanagedArray<byte>((int)(data.Length - offset));
            //    for (int i = 0; i < raw_data.Length; i++)
            //    {
            //        raw_data[i] = data[offset+i];
            //    }
            //}
            //VBM_ATTRIB_HEADER * attrib_header = (VBM_ATTRIB_HEADER *)(data + header.size);
            VBM_ATTRIB_HEADER attrib_header = br.ReadStruct <VBM_ATTRIB_HEADER>();
            //VBM_FRAME_HEADER * frame_header = (VBM_FRAME_HEADER *)(data + header.size + header.num_attribs * sizeof(VBM_ATTRIB_HEADER));
            {
                long offset = header.size + header.num_attribs * Marshal.SizeOf(typeof(VBM_ATTRIB_HEADER));
                f.Seek(offset, SeekOrigin.Begin);
            }
            VBM_FRAME_HEADER frame_header = br.ReadStruct <VBM_FRAME_HEADER>();

            uint total_data_size = 0;

            //memcpy(&m_header, header, header.size < Marshal.SizeOf(typeof(VBM_HEADER)) ? header.size : Marshal.SizeOf(typeof(VBM_HEADER)));
            this.m_header = header;

            //memcpy(m_attrib, attrib_header, header.num_attribs * Marshal.SizeOf(typeof(VBM_ATTRIB_HEADER)));
            {
                long offset = header.size;// +header.num_attribs * Marshal.SizeOf(typeof(VBM_ATTRIB_HEADER));
                f.Seek(offset, SeekOrigin.Begin);
            }
            this.m_attrib = new VBM_ATTRIB_HEADER[header.num_attribs];
            for (int i = 0; i < header.num_attribs; i++)
            {
                this.m_attrib[i] = br.ReadStruct <VBM_ATTRIB_HEADER>();
            }

            //memcpy(m_frame, frame_header, header.num_frames * Marshal.SizeOf(typeof(VBM_FRAME_HEADER)));
            {
                long offset = header.size + header.num_attribs * Marshal.SizeOf(typeof(VBM_ATTRIB_HEADER));// +header.num_frames * Marshal.SizeOf(typeof(VBM_FRAME_HEADER));
                f.Seek(offset, SeekOrigin.Begin);
            }
            this.m_frame = new VBM_FRAME_HEADER[header.num_frames];
            for (int i = 0; i < header.num_frames; i++)
            {
                this.m_frame[i] = br.ReadStruct <VBM_FRAME_HEADER>();
            }

            GL.GenVertexArrays(1, m_vao);
            GL.BindVertexArray(m_vao[0]);
            GL.GenBuffers(1, m_attribute_buffer);
            GL.BindBuffer(BufferTarget.ArrayBuffer, m_attribute_buffer[0]);

            //uint i;

            for (uint i = 0; i < header.num_attribs; i++)
            {
                total_data_size += m_attrib[i].components * sizeof(float) * header.num_vertices;
            }
            {
                long offset = header.size + header.num_attribs * Marshal.SizeOf(typeof(VBM_ATTRIB_HEADER)) + header.num_frames * Marshal.SizeOf(typeof(VBM_FRAME_HEADER));
                raw_data = new UnmanagedArray <byte>((int)total_data_size);
                for (int i = 0; i < raw_data.Length; i++)
                {
                    raw_data[i] = data[offset + i];
                }
            }
            //GL.BufferData(GL_ARRAY_BUFFER, total_data_size, raw_data, GL_STATIC_DRAW);
            GL.BufferData(BufferTarget.ArrayBuffer, raw_data, BufferUsage.StaticDraw);

            total_data_size = 0;

            for (uint i = 0; i < header.num_attribs; i++)
            {
                uint attribIndex = i;

                if (attribIndex == 0)
                {
                    attribIndex = (uint)vertexIndex;
                }
                else if (attribIndex == 1)
                {
                    attribIndex = (uint)normalIndex;
                }
                else if (attribIndex == 2)
                {
                    attribIndex = (uint)texCoord0Index;
                }

                GL.VertexAttribPointer(attribIndex, (int)m_attrib[i].components, m_attrib[i].type, false, 0, new IntPtr(total_data_size));
                GL.EnableVertexAttribArray(attribIndex);
                total_data_size += m_attrib[i].components * sizeof(float) * header.num_vertices;
            }

            if (header.num_indices > 0)
            {
                GL.GenBuffers(1, m_index_buffer);
                GL.BindBuffer(BufferTarget.ElementArrayBuffer, m_index_buffer[0]);
                uint element_size;
                if (header.indexype == 0x1403)
                {
                    element_size = sizeof(ushort);
                }
                else
                {
                    element_size = sizeof(uint);
                }
                //GL.BufferData(GL_ELEMENT_ARRAY_BUFFER, header.num_indices * element_size, raw_data + total_data_size, GL_STATIC_DRAW);
                UnmanagedArray <byte> tmp = new UnmanagedArray <byte>((int)(header.num_indices * element_size));
                for (int t = 0; t < tmp.Length; t++)
                {
                    tmp[t] = raw_data[(int)(t + total_data_size)];
                }
                GL.BufferData(BufferTarget.ElementArrayBuffer, tmp, BufferUsage.StaticDraw);
                tmp.Dispose();
            }

            GL.BindVertexArray(0);

            if (m_header.num_materials != 0)
            {
                m_material = new VBM_MATERIAL[m_header.num_materials];
                //memcpy(m_material, raw_data + total_data_size, m_header.num_materials * sizeof(VBM_MATERIAL));
                {
                    var offset = header.size + total_data_size;
                    f.Seek(offset, SeekOrigin.Begin);
                }
                for (int t = 0; t < m_header.num_materials; t++)
                {
                    m_material[t] = br.ReadStruct <VBM_MATERIAL>();
                }
                total_data_size    += (uint)(m_header.num_materials * Marshal.SizeOf(typeof(VBM_MATERIAL)));
                m_material_textures = new material_texture[m_header.num_materials];
                //memset(m_material_textures, 0, m_header.num_materials * sizeof(*m_material_textures));
                {
                    var offset = 0;
                    f.Seek(0, SeekOrigin.Begin);
                }
                for (int t = 0; t < m_header.num_materials; t++)
                {
                    m_material_textures[t] = br.ReadStruct <material_texture>();
                }
            }

            if (m_header.num_chunks != 0)
            {
                m_chunks = new VBM_RENDER_CHUNK[m_header.num_chunks];
                //memcpy(m_chunks, raw_data + total_data_size, m_header.num_chunks * sizeof(VBM_RENDER_CHUNK));
                {
                    var offset = m_header.size + total_data_size;
                    f.Seek(offset, SeekOrigin.Begin);
                }
                for (int t = 0; t < m_header.num_chunks; t++)
                {
                    m_chunks[t] = br.ReadStruct <VBM_RENDER_CHUNK>();
                }
                //total_data_size += m_header.num_chunks * sizeof(VBM_RENDER_CHUNK);
            }

            raw_data.Dispose();

            return(true);
        }
Пример #2
0
        public bool LoadFromVBM(string filename, int vertexIndex, int normalIndex, int texCoord0Index)
        {
            //FILE * f = NULL;
            FileStream f = new FileStream(filename, FileMode.Open, FileAccess.Read);
            BinaryReader br = new BinaryReader(f);

            //f = fopen(filename, "rb");
            //if(f == NULL)
            //return false;

            //fseek(f, 0, SEEK_END);
            //size_t filesize = ftell(f);
            //fseek(f, 0, SEEK_SET);
            long filesize = f.Length;
            //f.Seek(0, SeekOrigin.End);
            //f.Seek(0, SeekOrigin.Begin);

            byte[] data = new byte[filesize];
            UnmanagedArray<byte> raw_data;
            f.Read(data, 0, (int)filesize);
            //f.Close();
            f.Seek(0, SeekOrigin.Begin);

            //VBM_HEADER * header = (VBM_HEADER *)data;
            VBM_HEADER header = br.ReadStruct<VBM_HEADER>();
            //raw_data = data + header.size + header->num_attribs * sizeof(VBM_ATTRIB_HEADER) + header->num_frames * sizeof(VBM_FRAME_HEADER);
            //{
            //    long offset = header.size + header.num_attribs * Marshal.SizeOf(typeof(VBM_ATTRIB_HEADER)) + header.num_frames * Marshal.SizeOf(typeof(VBM_FRAME_HEADER)); 
            //    raw_data = new UnmanagedArray<byte>((int)(data.Length - offset));
            //    for (int i = 0; i < raw_data.Length; i++)
            //    {
            //        raw_data[i] = data[offset+i];
            //    }
            //}
            //VBM_ATTRIB_HEADER * attrib_header = (VBM_ATTRIB_HEADER *)(data + header.size);
            VBM_ATTRIB_HEADER attrib_header = br.ReadStruct<VBM_ATTRIB_HEADER>();
            //VBM_FRAME_HEADER * frame_header = (VBM_FRAME_HEADER *)(data + header.size + header.num_attribs * sizeof(VBM_ATTRIB_HEADER));
            {
                long offset = header.size + header.num_attribs * Marshal.SizeOf(typeof(VBM_ATTRIB_HEADER));
                f.Seek(offset, SeekOrigin.Begin);
            }
            VBM_FRAME_HEADER frame_header = br.ReadStruct<VBM_FRAME_HEADER>();

            uint total_data_size = 0;

            //memcpy(&m_header, header, header.size < Marshal.SizeOf(typeof(VBM_HEADER)) ? header.size : Marshal.SizeOf(typeof(VBM_HEADER)));
            this.m_header = header;

            //memcpy(m_attrib, attrib_header, header.num_attribs * Marshal.SizeOf(typeof(VBM_ATTRIB_HEADER)));
            {
                long offset = header.size;// +header.num_attribs * Marshal.SizeOf(typeof(VBM_ATTRIB_HEADER));
                f.Seek(offset, SeekOrigin.Begin);
            }
            this.m_attrib = new VBM_ATTRIB_HEADER[header.num_attribs];
            for (int i = 0; i < header.num_attribs; i++)
            {
                this.m_attrib[i] = br.ReadStruct<VBM_ATTRIB_HEADER>();
            }

            //memcpy(m_frame, frame_header, header.num_frames * Marshal.SizeOf(typeof(VBM_FRAME_HEADER)));
            {
                long offset = header.size + header.num_attribs * Marshal.SizeOf(typeof(VBM_ATTRIB_HEADER));// +header.num_frames * Marshal.SizeOf(typeof(VBM_FRAME_HEADER));
                f.Seek(offset, SeekOrigin.Begin);
            }
            this.m_frame = new VBM_FRAME_HEADER[header.num_frames];
            for (int i = 0; i < header.num_frames; i++)
            {
                this.m_frame[i] = br.ReadStruct<VBM_FRAME_HEADER>();
            }

            GL.GenVertexArrays(1, m_vao);
            GL.BindVertexArray(m_vao[0]);
            GL.GenBuffers(1, m_attribute_buffer);
            GL.BindBuffer(BufferTarget.ArrayBuffer, m_attribute_buffer[0]);

            //uint i;

            for (uint i = 0; i < header.num_attribs; i++)
            {
                total_data_size += m_attrib[i].components * sizeof(float) * header.num_vertices;
            }
            {
                long offset = header.size + header.num_attribs * Marshal.SizeOf(typeof(VBM_ATTRIB_HEADER)) + header.num_frames * Marshal.SizeOf(typeof(VBM_FRAME_HEADER));
                raw_data = new UnmanagedArray<byte>((int)total_data_size);
                for (int i = 0; i < raw_data.Length; i++)
                {
                    raw_data[i] = data[offset + i];
                }
            }
            //GL.BufferData(GL_ARRAY_BUFFER, total_data_size, raw_data, GL_STATIC_DRAW);
            GL.BufferData(BufferTarget.ArrayBuffer, raw_data, BufferUsage.StaticDraw);

            total_data_size = 0;

            for (uint i = 0; i < header.num_attribs; i++)
            {
                uint attribIndex = i;

                if (attribIndex == 0)
                    attribIndex = (uint)vertexIndex;
                else if (attribIndex == 1)
                    attribIndex = (uint)normalIndex;
                else if (attribIndex == 2)
                    attribIndex = (uint)texCoord0Index;

                GL.VertexAttribPointer(attribIndex, (int)m_attrib[i].components, m_attrib[i].type, false, 0, new IntPtr(total_data_size));
                GL.EnableVertexAttribArray(attribIndex);
                total_data_size += m_attrib[i].components * sizeof(float) * header.num_vertices;
            }

            if (header.num_indices > 0)
            {
                GL.GenBuffers(1, m_index_buffer);
                GL.BindBuffer(BufferTarget.ElementArrayBuffer, m_index_buffer[0]);
                uint element_size;
                if (header.indexype == 0x1403)
                {
                    element_size = sizeof(ushort);
                }
                else
                {
                    element_size = sizeof(uint);
                }
                //GL.BufferData(GL_ELEMENT_ARRAY_BUFFER, header.num_indices * element_size, raw_data + total_data_size, GL_STATIC_DRAW);
                UnmanagedArray<byte> tmp = new UnmanagedArray<byte>((int)(header.num_indices * element_size));
                for (int t = 0; t < tmp.Length; t++)
                {
                    tmp[t] = raw_data[(int)(t + total_data_size)];
                }
                GL.BufferData(BufferTarget.ElementArrayBuffer, tmp, BufferUsage.StaticDraw);
                tmp.Dispose();
            }

            GL.BindVertexArray(0);

            if (m_header.num_materials != 0)
            {
                m_material = new VBM_MATERIAL[m_header.num_materials];
                //memcpy(m_material, raw_data + total_data_size, m_header.num_materials * sizeof(VBM_MATERIAL));
                {
                    var offset = header.size + total_data_size;
                    f.Seek(offset, SeekOrigin.Begin);
                }
                for (int t = 0; t < m_header.num_materials; t++)
                {
                    m_material[t] = br.ReadStruct<VBM_MATERIAL>();
                }
                total_data_size += (uint)(m_header.num_materials * Marshal.SizeOf(typeof(VBM_MATERIAL)));
                m_material_textures = new material_texture[m_header.num_materials];
                //memset(m_material_textures, 0, m_header.num_materials * sizeof(*m_material_textures));
                {
                    var offset = 0;
                    f.Seek(0, SeekOrigin.Begin);
                }
                for (int t = 0; t < m_header.num_materials; t++)
                {
                    m_material_textures[t] = br.ReadStruct<material_texture>();
                }
            }

            if (m_header.num_chunks != 0)
            {
                m_chunks = new VBM_RENDER_CHUNK[m_header.num_chunks];
                //memcpy(m_chunks, raw_data + total_data_size, m_header.num_chunks * sizeof(VBM_RENDER_CHUNK));
                {
                    var offset = m_header.size + total_data_size;
                    f.Seek(offset, SeekOrigin.Begin);
                }
                for (int t = 0; t < m_header.num_chunks; t++)
                {
                    m_chunks[t] = br.ReadStruct<VBM_RENDER_CHUNK>();
                }
                //total_data_size += m_header.num_chunks * sizeof(VBM_RENDER_CHUNK);
            }

            raw_data.Dispose();

            return true;
        }