コード例 #1
0
ファイル: VBObject.cs プロジェクト: windygu/CSharpGL
        public bool LoadFromVBM(string filename, int vertexIndex, int normalIndex, int texCoord0Index)
        {
            FileStream   f  = new FileStream(filename, FileMode.Open, FileAccess.Read);
            BinaryReader br = new BinaryReader(f);

            long filesize = f.Length;

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

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

            VBM_HEADER        header        = br.ReadStruct <VBM_HEADER>();
            VBM_ATTRIB_HEADER attrib_header = br.ReadStruct <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;

            this.m_header = 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>();
            }

            {
                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);

            raw_data.Dispose();

            return(true);
        }
コード例 #2
0
ファイル: VBObject.cs プロジェクト: jiailiuyan/CSharpGL
        public bool LoadFromVBM(string filename, int vertexIndex, int normalIndex, int texCoord0Index)
        {
            FileStream f = new FileStream(filename, FileMode.Open, FileAccess.Read);
            BinaryReader br = new BinaryReader(f);

            long filesize = f.Length;

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

            VBM_HEADER header = br.ReadStruct<VBM_HEADER>();
            VBM_ATTRIB_HEADER attrib_header = br.ReadStruct<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;

            this.m_header = 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>();
            }

            {
                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);

            raw_data.Dispose();

            return true;
        }