//------------------------------------------------------------------------------------- public static int FromBytes(byte[] bytes, int offset, out ChunkVertexAttrib obj) { obj = new ChunkVertexAttrib(); int blockSize = TypeUtils.FromBytes <MarshalChunkVertexAttrib>(bytes, offset, out obj._data); obj.AttribData = new VertexAttribDecl[obj.AttribCount]; for (int i = 0; i < obj.AttribCount; i++) { blockSize += TypeUtils.FromBytes <VertexAttribDecl>(bytes, offset + blockSize, out obj.AttribData[i]); } return(blockSize); }
//------------------------------------------------------------------------------------- public static int ToBytes(ChunkVertexAttrib obj, out byte[] bytes) { bytes = new byte[obj.Header.Size]; byte[] block; int size = TypeUtils.ToBytes <MarshalChunkVertexAttrib>(obj._data, out block); Array.Copy(block, bytes, size); int blockOffset = size; for (int i = 0; i < obj.AttribCount; i++) { size = TypeUtils.ToBytes <VertexAttribDecl>(obj.AttribData[i], out block); Array.Copy(block, 0, bytes, blockOffset, size); blockOffset += size; } return(blockOffset); }
//----------------------------------------------------------------------------------------- public void Load(string filename) { BinaryReader file = null; try { file = new BinaryReader(File.Open(filename, FileMode.Open)); byte[] data = file.ReadBytes((int)file.BaseStream.Length); SBM6Header hdr; int dataOffset = SBM6Header.FromBytes(data, 0, out hdr); ChunkVertexAttrib vertexAttribChunk = null; ChunkVertexData vertexDataChunk = null; ChunkIndexData indexDataChunk = null; ChunkSubObjectList subObjectChunk = null; for (int i = 0; i < hdr.NumChunks; i++) { ChunkHeader chunkHeader = new ChunkHeader(); TypeUtils.FromBytes <ChunkHeader>(data, dataOffset, out chunkHeader); switch (chunkHeader.Type) { case ChunkType_e.VertexAttribs: dataOffset += ChunkVertexAttrib.FromBytes(data, dataOffset, out vertexAttribChunk); break; case ChunkType_e.VertexData: dataOffset += ChunkVertexData.FromBytes(data, dataOffset, out vertexDataChunk); break; case ChunkType_e.IndexData: dataOffset += ChunkIndexData.FromBytes(data, dataOffset, out indexDataChunk); break; case ChunkType_e.SubObjectList: dataOffset += ChunkSubObjectList.FromBytes(data, dataOffset, out subObjectChunk); break; case ChunkType_e.Comment: dataOffset += (int)chunkHeader.Size; break; default: throw new SBM6ModelException("Unknown SBM6 chunk encountered."); } } if (subObjectChunk != null) { if (subObjectChunk.Count > _maxSubObjects) { subObjectChunk.Count = _maxSubObjects; } _subObject = new SubObjectDecl[subObjectChunk.Count]; for (int i = 0; i < subObjectChunk.Count; i++) { _subObject[i] = subObjectChunk.SubObject[i]; } _numSubObjects = subObjectChunk.Count; } else { _subObject = new SubObjectDecl[1]; _subObject[0] = new SubObjectDecl(); _subObject[0].First = 0; _subObject[0].Count = vertexDataChunk.TotalVertices; _numSubObjects = 1; } GL.GenBuffers(1, out _vertexBuffer); GL.BindBuffer(BufferTarget.ArrayBuffer, _vertexBuffer); GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)vertexDataChunk.DataSize, ref data[vertexDataChunk.DataOffset], BufferUsageHint.StaticDraw); GL.GenVertexArrays(1, out _vao); GL.BindVertexArray(_vao); for (int i = 0; i < vertexAttribChunk.AttribCount; i++) { VertexAttribDecl attribDecl = vertexAttribChunk.AttribData[i]; bool normalized = true; if ((attribDecl.Flags & (uint)VertexAttribFlag_e.Normalized) == 0) { normalized = false; } GL.VertexAttribPointer(i, (int)attribDecl.Size, (VertexAttribPointerType)attribDecl.Type, normalized, (int)attribDecl.Stride, (IntPtr)attribDecl.DataOffset); GL.EnableVertexAttribArray(i); } if (indexDataChunk != null) { GL.GenBuffers(1, out _indexBuffer); GL.BindBuffer(BufferTarget.ElementArrayBuffer, _indexBuffer); int indexSize = (indexDataChunk.IndexType == (uint)VertexAttribType.UnsignedShort) ? sizeof(ushort) : sizeof(byte); GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(indexDataChunk.IndexCount * indexSize), (IntPtr)data[indexDataChunk.DataOffset], BufferUsageHint.StaticDraw); _numIndices = indexDataChunk.IndexCount; _indexType = indexDataChunk.IndexType; } else { _numIndices = vertexDataChunk.TotalVertices; } GL.BindVertexArray(0); GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0); } catch (IOException e) { Console.WriteLine("Framwork.SBM6 IO Exception: " + e.Message); } catch (SBM6ModelException e) { Console.WriteLine("Framwork.SBM6 Exception: " + e.Message); } if (file != null) { file.Close(); } }