/// <summary> /// Create the block vertex buffer. /// </summary> void CreateBlockVertexBuffer(GraphicsBuffer buffer, int offset) { Vector4b[] vertices = new Vector4b[BlockVertexCount]; for (int z = 0, i = 0; z < BlockVertexRow; z++) { int pz = Math.Max(0, Math.Min(BlockEdgeCount - 1, z - 1)); for (int x = 0; x < BlockVertexRow; x++) { int px = Math.Max(0, Math.Min(BlockEdgeCount - 1, x - 1)); int py = (x == 0 || z == 0 || x == BlockVertexRow - 1 || z == BlockVertexRow - 1) ? 0 : 1; vertices[i++] = new Vector4b((byte)px, (byte)py, (byte)pz, 0); } } buffer.Write(offset, vertices); }
internal int FlushInstanceArray() { int count = InstanceArray.Count; if (count > 0) { if (InstanceVertexBuffer == null || InstanceVertexBuffer.Size < InstanceArray.Count * InstanceTypeSize) { if (InstanceVertexBuffer != null) { InstanceVertexBuffer.Dispose(); } InstanceVertexBuffer = new GraphicsBuffer(count * InstanceTypeSize * 3 / 2, BufferUsage.DynamicDraw); } InstanceVertexBuffer.Write(0, InstanceArray.Array, 0, InstanceArray.Count); ClearInstanceArray(); } return(count / InstanceVertexElementCount); }
/// <summary> /// Renders the terrain. /// </summary> /// <param name="camera"></param> public void Render(Camera camera) { int offset = 0; int oldOffset; TerrainQuad quad; GameEngine.Device.RenderState.CullMode = Cull.Clockwise; GameEngine.Device.VertexFormat = PositionNormalTextured.Format; GameEngine.Device.Material = GraphicsUtility.InitMaterial(Color.White); GameEngine.Device.Transform.World = Matrix.Identity; GameEngine.Device.Transform.View = camera.ViewMatrix; List <TerrainQuad> drawnQuads = new List <TerrainQuad>(); GraphicsBuffer <PositionNormalTextured> buffer = this.vertexBuffer.Lock <PositionNormalTextured>(0, 0, LockFlags.None); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { oldOffset = offset; quad = this.terrainQuads[x, y]; offset = this.terrainQuads[x, y].AppendVertexData(offset, this.vertices); if (offset != oldOffset) { drawnQuads.Add(quad); } } } buffer.Write(this.vertices); this.vertexBuffer.Unlock(); offset = 0; foreach (TerrainQuad nextQuad in drawnQuads) { GameEngine.Device.VertexFormat = PositionNormalTextured.Format; GameEngine.Device.SetTexture(0, nextQuad.Texture); GameEngine.Device.SetStreamSource(0, this.vertexBuffer, offset); GameEngine.Device.DrawPrimitives(PrimitiveType.TriangleList, 0, 6); offset += 6; } }
/// <summary>Create the block index buffer.</summary> void CreateBlockIndexBuffer(GraphicsBuffer buffer, int offset) { ushort[] indices = new ushort[BlockIndexCount]; for (int z = 0, i = 0, l = 0; z < BlockVertexRow - 1; z++) { for (int x = 0; x < BlockVertexRow; x++) { l = indices[i++] = (ushort)(x + (z + 1) * BlockVertexRow); if (x == 0 && z > 0) { indices[i++] = (ushort)l; } l = indices[i++] = (ushort)(x + z * BlockVertexRow); } if (z < BlockVertexRow - 2) { indices[i++] = (ushort)l; } } buffer.Write(offset, indices); }
internal Model(AssetLoader loader) : base(loader) { Asset context = loader.Context; string name = loader.Name; BinaryReader reader = loader.Reader; FolderAsset textureArchive = null; FolderAsset bonesFolder = new FolderAsset(this, "Bones"); FolderAsset materialsFolder = new FolderAsset(this, "Materials"); FolderAsset meshesFolder = new FolderAsset(this, "Meshes"); FolderAsset vertexDeclarationsFolder = new FolderAsset(this, "Vertex declarations"); ArrayBackedList <byte> bufferData = new ArrayBackedList <byte>(); if (context != null && context.Parent is FolderAsset) { // @"/map/m##_##_##_##/m*.flver.dcx" for DS1; textures are in the @"/map/m##/" folder. if (name.StartsWith(@"map/m")) { string folderName = name.Substring(4, 3); FolderAsset maps = (FolderAsset)context.Parent.Parent; foreach (FolderAsset child in maps.Children) { if (child.Name == folderName) { textureArchive = child; break; } } } else { textureArchive = (FolderAsset)context.Parent; } } #if Marking MarkingStream markingStream = loader.StartMarking(out reader); #endif loader.ExpectMagic(Magic); char endian = (char)reader.ReadByte(); switch (endian) { case 'L': ByteOrder = ByteOrder.LittleEndian; break; case 'B': reader = loader.MakeBigEndian(); ByteOrder = ByteOrder.BigEndian; break; default: throw new Exception(); } using (reader) { // Read header. loader.Expect((byte)0); Version = (ModelVersion)reader.ReadInt32(); if (Version != ModelVersion.DarkSouls && Version != ModelVersion.DarkSouls2) { loader.AddError(loader.Position - 4, "Unknown model version " + VersionString + "; will try to load it anyway."); } int dataOffset = reader.ReadInt32(); int dataSize = reader.ReadInt32(); if (((dataOffset + dataSize + 31) & ~31) != reader.BaseStream.Length) { loader.AddError(loader.Position - 4, "Data size and offset aren't correct."); } int boneUnknownCount = reader.ReadInt32(); int materialCount = reader.ReadInt32(); int boneCount = reader.ReadInt32(); int meshCount = reader.ReadInt32(); int meshCount2 = reader.ReadInt32(); if (meshCount != meshCount2) { loader.AddError(loader.Position - 4, "Mesh count 1 and 2 aren't the same."); } Bounds = new Box3f(reader.ReadVector3f(), reader.ReadVector3f()); Unknowns.ReadInt32s(reader, 1); // Possible the non-degenerate triangle count. Seems related. int triangleCount = reader.ReadInt32(); loader.Expect(IsDS1 ? 272 : 0x10010100); loader.Expect(IsDS1 ? 0 : 0xFFFF); int partCount = reader.ReadInt32(); int vertexDeclarationCount = reader.ReadInt32(); int materialParameterCount = reader.ReadInt32(); loader.Expect(IsDS1 ? 0 : 0x1000000); loader.ExpectZeroes(4, 8); // Calculate offsets. long boneUnknownsOffset = HeaderSize; long materialsOffset = boneUnknownsOffset + boneUnknownCount * ModelBoneUnknown.DataSize; long bonesOffset = materialsOffset + materialCount * ModelMaterial.DataSize; long meshesOffset = bonesOffset + boneCount * ModelBone.DataSize; long detailLevelsOffset = meshesOffset + meshCount * ModelMesh.DataSize; long meshVerticesOffset = detailLevelsOffset + partCount * ModelDetailLevel.DataSize; long vertexDeclarationsOffset = meshVerticesOffset + meshCount * ModelMesh.DataSizeVertexHeader; long materialParametersOffset = vertexDeclarationsOffset + vertexDeclarationCount * ModelVertexDeclaration.DataSize; long postHeaderOffset = materialParametersOffset + materialParameterCount * ModelMaterialParameter.DataSize; // BoneUnknowns ExpectedOffset(loader, boneUnknownsOffset, typeof(ModelBoneUnknown).Name); for (int index = 0; index < boneUnknownCount; index++) { boneUnknowns.Add(new ModelBoneUnknown(bonesFolder, index, loader)); } // Materials ExpectedOffset(loader, materialsOffset, typeof(ModelMaterial).Name); for (int index = 0; index < materialCount; index++) { materials.Add(new ModelMaterial(materialsFolder, index, loader)); } int expectedMaterialParameterCount = materialCount > 0 ? materials[materialCount - 1].ParameterEndIndex : 0; if (expectedMaterialParameterCount != materialParameterCount) { loader.AddError(null, "Expected material parameter count {0} doesn't match actual count {1}.", expectedMaterialParameterCount, materialParameterCount); } // Bones ExpectedOffset(loader, bonesOffset, typeof(ModelBone).Name); for (int index = 0; index < boneCount; index++) { bones.Add(new ModelBone(bonesFolder, index, loader)); } // Meshes ExpectedOffset(loader, meshesOffset, typeof(ModelMesh).Name); for (int index = 0; index < meshCount; index++) { meshes.Add(new ModelMesh(meshesFolder, index, loader)); } int expectedPartCount = meshCount > 0 ? meshes[meshCount - 1].PartEndIndex : 0; if (expectedPartCount != partCount) { throw new InvalidDataException("Expected part count doesn't match actual count."); } // Detail levels ExpectedOffset(loader, detailLevelsOffset, typeof(ModelDetailLevel).Name); foreach (ModelMesh mesh in meshes) { mesh.ReadDetailLevels(loader, dataOffset, bufferData); detailLevels.AddRange(mesh.DetailLevels); } // Mesh vertices ExpectedOffset(loader, meshVerticesOffset, typeof(ModelMesh).Name + " vertex header"); foreach (ModelMesh mesh in meshes) { mesh.ReadVertexHeaders(reader, dataOffset, bufferData); } // Vertex declarations ExpectedOffset(loader, vertexDeclarationsOffset, typeof(ModelVertexDeclaration).Name); for (int index = 0; index < vertexDeclarationCount; index++) { vertexDeclarations.Add(new ModelVertexDeclaration(vertexDeclarationsFolder, index, loader)); } // Material parameters ExpectedOffset(loader, materialParametersOffset, typeof(ModelMaterialParameter).Name); foreach (ModelMaterial material in materials) { material.ReadParameters(loader, textureArchive); materialParameters.AddRange(material.Parameters); } ExpectedOffset(loader, postHeaderOffset, "Post-header"); #if Marking if (markingStream != null) { markingStream.Report(loader); } #endif // Marking #if SkippedChecks /*int vertexDataSize = 0, indexCount = 0, indexSize = 0, vertexCount = 0, expectedTriangleCount = 0, nondegenerateTriangleCount = 0; * foreach (var mesh in Meshes) { * vertexDataSize += mesh.VertexCount * mesh.VertexSize; * vertexCount += mesh.VertexCount; * foreach (var part in mesh.Parts) { * indexCount += part.Indices.Length; * indexSize += part.Indices.Length * 2; * expectedTriangleCount += part.Indices.Length - 2; * for (int index = 0; index < part.Indices.Length - 2; index++) { * if (part.Indices[index] != part.Indices[index + 1] && part.Indices[index + 1] != part.Indices[index + 2] && part.Indices[index] != part.Indices[index + 2]) * nondegenerateTriangleCount++; * } * } * } * if (Math.Abs(expectedTriangleCount - triangleCount) > partCount) * throw new InvalidDataException("Expected triangle count doesn't match the read value.");*/ #endif } Buffer = new GraphicsBuffer(bufferData.Count == 0 ? 1 : bufferData.Count); Buffer.Write(0, bufferData.Array, 0, bufferData.Count); }
internal Model(AssetLoader loader) : base(loader) { Asset context = loader.Context; string name = loader.Name; BinaryReader reader = loader.Reader; FolderAsset textureArchive = null; FolderAsset bonesFolder = new FolderAsset(this, "Bones"); FolderAsset materialsFolder = new FolderAsset(this, "Materials"); FolderAsset meshesFolder = new FolderAsset(this, "Meshes"); FolderAsset vertexDeclarationsFolder = new FolderAsset(this, "Vertex declarations"); ArrayBackedList<byte> bufferData = new ArrayBackedList<byte>(); if (context != null && context.Parent is FolderAsset) { // @"/map/m##_##_##_##/m*.flver.dcx" for DS1; textures are in the @"/map/m##/" folder. if (name.StartsWith(@"map/m")) { string folderName = name.Substring(4, 3); FolderAsset maps = (FolderAsset)context.Parent.Parent; foreach (FolderAsset child in maps.Children) { if (child.Name == folderName) { textureArchive = child; break; } } } else textureArchive = (FolderAsset)context.Parent; } #if Marking MarkingStream markingStream = loader.StartMarking(out reader); #endif loader.ExpectMagic(Magic); char endian = (char)reader.ReadByte(); switch (endian) { case 'L': ByteOrder = ByteOrder.LittleEndian; break; case 'B': reader = loader.MakeBigEndian(); ByteOrder = ByteOrder.BigEndian; break; default: throw new Exception(); } using (reader) { // Read header. loader.Expect((byte)0); Version = (ModelVersion)reader.ReadInt32(); if (Version != ModelVersion.DarkSouls && Version != ModelVersion.DarkSouls2) loader.AddError(loader.Position - 4, "Unknown model version " + VersionString + "; will try to load it anyway."); int dataOffset = reader.ReadInt32(); int dataSize = reader.ReadInt32(); if (((dataOffset + dataSize + 31) & ~31) != reader.BaseStream.Length) loader.AddError(loader.Position - 4, "Data size and offset aren't correct."); int boneUnknownCount = reader.ReadInt32(); int materialCount = reader.ReadInt32(); int boneCount = reader.ReadInt32(); int meshCount = reader.ReadInt32(); int meshCount2 = reader.ReadInt32(); if (meshCount != meshCount2) loader.AddError(loader.Position - 4, "Mesh count 1 and 2 aren't the same."); Bounds = new Box3f(reader.ReadVector3f(), reader.ReadVector3f()); Unknowns.ReadInt32s(reader, 1); // Possible the non-degenerate triangle count. Seems related. int triangleCount = reader.ReadInt32(); loader.Expect(IsDS1 ? 272 : 0x10010100); loader.Expect(IsDS1 ? 0 : 0xFFFF); int partCount = reader.ReadInt32(); int vertexDeclarationCount = reader.ReadInt32(); int materialParameterCount = reader.ReadInt32(); loader.Expect(IsDS1 ? 0 : 0x1000000); loader.ExpectZeroes(4, 8); // Calculate offsets. long boneUnknownsOffset = HeaderSize; long materialsOffset = boneUnknownsOffset + boneUnknownCount * ModelBoneUnknown.DataSize; long bonesOffset = materialsOffset + materialCount * ModelMaterial.DataSize; long meshesOffset = bonesOffset + boneCount * ModelBone.DataSize; long detailLevelsOffset = meshesOffset + meshCount * ModelMesh.DataSize; long meshVerticesOffset = detailLevelsOffset + partCount * ModelDetailLevel.DataSize; long vertexDeclarationsOffset = meshVerticesOffset + meshCount * ModelMesh.DataSizeVertexHeader; long materialParametersOffset = vertexDeclarationsOffset + vertexDeclarationCount * ModelVertexDeclaration.DataSize; long postHeaderOffset = materialParametersOffset + materialParameterCount * ModelMaterialParameter.DataSize; // BoneUnknowns ExpectedOffset(loader, boneUnknownsOffset, typeof(ModelBoneUnknown).Name); for (int index = 0; index < boneUnknownCount; index++) boneUnknowns.Add(new ModelBoneUnknown(bonesFolder, index, loader)); // Materials ExpectedOffset(loader, materialsOffset, typeof(ModelMaterial).Name); for (int index = 0; index < materialCount; index++) materials.Add(new ModelMaterial(materialsFolder, index, loader)); int expectedMaterialParameterCount = materialCount > 0 ? materials[materialCount - 1].ParameterEndIndex : 0; if (expectedMaterialParameterCount != materialParameterCount) loader.AddError(null, "Expected material parameter count {0} doesn't match actual count {1}.", expectedMaterialParameterCount, materialParameterCount); // Bones ExpectedOffset(loader, bonesOffset, typeof(ModelBone).Name); for (int index = 0; index < boneCount; index++) bones.Add(new ModelBone(bonesFolder, index, loader)); // Meshes ExpectedOffset(loader, meshesOffset, typeof(ModelMesh).Name); for (int index = 0; index < meshCount; index++) meshes.Add(new ModelMesh(meshesFolder, index, loader)); int expectedPartCount = meshCount > 0 ? meshes[meshCount - 1].PartEndIndex : 0; if (expectedPartCount != partCount) throw new InvalidDataException("Expected part count doesn't match actual count."); // Detail levels ExpectedOffset(loader, detailLevelsOffset, typeof(ModelDetailLevel).Name); foreach (ModelMesh mesh in meshes) { mesh.ReadDetailLevels(loader, dataOffset, bufferData); detailLevels.AddRange(mesh.DetailLevels); } // Mesh vertices ExpectedOffset(loader, meshVerticesOffset, typeof(ModelMesh).Name + " vertex header"); foreach (ModelMesh mesh in meshes) mesh.ReadVertexHeaders(reader, dataOffset, bufferData); // Vertex declarations ExpectedOffset(loader, vertexDeclarationsOffset, typeof(ModelVertexDeclaration).Name); for (int index = 0; index < vertexDeclarationCount; index++) vertexDeclarations.Add(new ModelVertexDeclaration(vertexDeclarationsFolder, index, loader)); // Material parameters ExpectedOffset(loader, materialParametersOffset, typeof(ModelMaterialParameter).Name); foreach (ModelMaterial material in materials) { material.ReadParameters(loader, textureArchive); materialParameters.AddRange(material.Parameters); } ExpectedOffset(loader, postHeaderOffset, "Post-header"); #if Marking if (markingStream != null) { markingStream.Report(loader); } #endif // Marking #if SkippedChecks /*int vertexDataSize = 0, indexCount = 0, indexSize = 0, vertexCount = 0, expectedTriangleCount = 0, nondegenerateTriangleCount = 0; foreach (var mesh in Meshes) { vertexDataSize += mesh.VertexCount * mesh.VertexSize; vertexCount += mesh.VertexCount; foreach (var part in mesh.Parts) { indexCount += part.Indices.Length; indexSize += part.Indices.Length * 2; expectedTriangleCount += part.Indices.Length - 2; for (int index = 0; index < part.Indices.Length - 2; index++) { if (part.Indices[index] != part.Indices[index + 1] && part.Indices[index + 1] != part.Indices[index + 2] && part.Indices[index] != part.Indices[index + 2]) nondegenerateTriangleCount++; } } } if (Math.Abs(expectedTriangleCount - triangleCount) > partCount) throw new InvalidDataException("Expected triangle count doesn't match the read value.");*/ #endif } Buffer = new GraphicsBuffer(bufferData.Count == 0 ? 1 : bufferData.Count); Buffer.Write(0, bufferData.Array, 0, bufferData.Count); }