Exemplo n.º 1
0
        private static WadMesh LoadMesh(ChunkReader chunkIO, long chunkSize, Dictionary <long, WadTexture> textures)
        {
            var  mesh          = new WadMesh();
            long obsoleteIndex = 0;

            chunkIO.ReadChunks((id2, chunkSize2) =>
            {
                if (id2 == Wad2Chunks.MeshIndex)
                {
                    obsoleteIndex = chunkIO.ReadChunkLong(chunkSize2);
                }
                else if (id2 == Wad2Chunks.MeshName)
                {
                    mesh.Name = chunkIO.ReadChunkString(chunkSize2);
                }
                else if (id2 == Wad2Chunks.MeshSphere)
                {
                    // Read bounding sphere
                    float radius   = 0;
                    Vector3 center = Vector3.Zero;
                    chunkIO.ReadChunks((id3, chunkSize3) =>
                    {
                        if (id3 == Wad2Chunks.MeshSphereCenter)
                        {
                            center = chunkIO.ReadChunkVector3(chunkSize3);
                        }
                        else if (id3 == Wad2Chunks.MeshSphereRadius)
                        {
                            radius = chunkIO.ReadChunkFloat(chunkSize3);
                        }
                        else
                        {
                            return(false);
                        }
                        return(true);
                    });
                    mesh.BoundingSphere = new BoundingSphere(center, radius);
                }
                else if (id2 == Wad2Chunks.MeshBoundingBox)
                {
                    // Read bounding box
                    Vector3 min = Vector3.Zero;
                    Vector3 max = Vector3.Zero;
                    chunkIO.ReadChunks((id3, chunkSize3) =>
                    {
                        if (id3 == Wad2Chunks.MeshBoundingBoxMin)
                        {
                            min = chunkIO.ReadChunkVector3(chunkSize3);
                        }
                        else if (id3 == Wad2Chunks.MeshBoundingBoxMax)
                        {
                            max = chunkIO.ReadChunkVector3(chunkSize3);
                        }
                        else
                        {
                            return(false);
                        }
                        return(true);
                    });
                    mesh.BoundingBox = new BoundingBox(min, max);
                }
                else if (id2 == Wad2Chunks.MeshVertexPositions)
                {
                    chunkIO.ReadChunks((id3, chunkSize3) =>
                    {
                        if (id3 == Wad2Chunks.MeshVertexPosition)
                        {
                            mesh.VerticesPositions.Add(chunkIO.ReadChunkVector3(chunkSize3));
                        }
                        else
                        {
                            return(false);
                        }
                        return(true);
                    });
                }
                else if (id2 == Wad2Chunks.MeshVertexNormals)
                {
                    chunkIO.ReadChunks((id3, chunkSize3) =>
                    {
                        if (id3 == Wad2Chunks.MeshVertexNormal)
                        {
                            mesh.VerticesNormals.Add(chunkIO.ReadChunkVector3(chunkSize3));
                        }
                        else
                        {
                            return(false);
                        }
                        return(true);
                    });
                }
                else if (id2 == Wad2Chunks.MeshVertexShades)
                {
                    chunkIO.ReadChunks((id3, chunkSize3) =>
                    {
                        if (id3 == Wad2Chunks.MeshVertexShade)
                        {
                            mesh.VerticesShades.Add(chunkIO.ReadChunkShort(chunkSize3));
                        }
                        else
                        {
                            return(false);
                        }
                        return(true);
                    });
                }
                else if (id2 == Wad2Chunks.MeshPolygons)
                {
                    chunkIO.ReadChunks((id3, chunkSize3) =>
                    {
                        if (id3 == Wad2Chunks.MeshQuad ||
                            id3 == Wad2Chunks.MeshTriangle)
                        {
                            var polygon    = new WadPolygon();
                            polygon.Shape  = id3 == Wad2Chunks.MeshQuad ? WadPolygonShape.Quad : WadPolygonShape.Triangle;
                            polygon.Index0 = LEB128.ReadInt(chunkIO.Raw);
                            polygon.Index1 = LEB128.ReadInt(chunkIO.Raw);
                            polygon.Index2 = LEB128.ReadInt(chunkIO.Raw);
                            if (id3 == Wad2Chunks.MeshQuad)
                            {
                                polygon.Index3 = LEB128.ReadInt(chunkIO.Raw);
                            }
                            polygon.ShineStrength = LEB128.ReadByte(chunkIO.Raw);

                            TextureArea textureArea = new TextureArea();
                            textureArea.Texture     = textures[LEB128.ReadInt(chunkIO.Raw)];
                            textureArea.TexCoord0   = chunkIO.Raw.ReadVector2();
                            textureArea.TexCoord1   = chunkIO.Raw.ReadVector2();
                            textureArea.TexCoord2   = chunkIO.Raw.ReadVector2();
                            if (id3 == Wad2Chunks.MeshQuad)
                            {
                                textureArea.TexCoord3 = chunkIO.Raw.ReadVector2();
                            }
                            else
                            {
                                textureArea.TexCoord3 = textureArea.TexCoord2;
                            }
                            textureArea.BlendMode   = (BlendMode)LEB128.ReadLong(chunkIO.Raw);
                            textureArea.DoubleSided = chunkIO.Raw.ReadBoolean();
                            polygon.Texture         = textureArea;

                            chunkIO.ReadChunks((id4, chunkSize4) =>
                            {
                                return(false);
                            });

                            mesh.Polys.Add(polygon);
                        }
                        else
                        {
                            return(false);
                        }
                        return(true);
                    });
                }
                else
                {
                    return(false);
                }

                return(true);
            });

            return(mesh);
        }
Exemplo n.º 2
0
        private static bool LoadStatics(ChunkReader chunkIO, ChunkId idOuter, Wad2 wad, /*Dictionary<long, WadMesh> meshes*/
                                        Dictionary <long, WadTexture> textures)
        {
            if (idOuter != Wad2Chunks.Statics)
            {
                return(false);
            }

            chunkIO.ReadChunks((id, chunkSize) =>
            {
                if (id != Wad2Chunks.Static)
                {
                    return(false);
                }

                var s = new WadStatic(new WadStaticId(LEB128.ReadUInt(chunkIO.Raw)));
                //s.Mesh = meshes[LEB128.ReadInt(chunkIO.Raw)];
                s.Flags        = LEB128.ReadShort(chunkIO.Raw);
                s.LightingType = (WadMeshLightingType)LEB128.ReadShort(chunkIO.Raw);

                chunkIO.ReadChunks((id2, chunkSize2) =>
                {
                    if (id2 == Wad2Chunks.StaticVisibilityBox)
                    {
                        var min = Vector3.Zero;
                        var max = Vector3.Zero;
                        chunkIO.ReadChunks((id3, chunkSize3) =>
                        {
                            if (id3 == Wad2Chunks.MeshBoundingBoxMin)
                            {
                                min = chunkIO.ReadChunkVector3(chunkSize3);
                            }
                            else if (id3 == Wad2Chunks.MeshBoundingBoxMax)
                            {
                                max = chunkIO.ReadChunkVector3(chunkSize3);
                            }
                            else
                            {
                                return(false);
                            }
                            return(true);
                        });
                        s.VisibilityBox = new BoundingBox(min, max);
                    }
                    else if (id2 == Wad2Chunks.StaticCollisionBox)
                    {
                        var min = Vector3.Zero;
                        var max = Vector3.Zero;
                        chunkIO.ReadChunks((id3, chunkSize3) =>
                        {
                            if (id3 == Wad2Chunks.MeshBoundingBoxMin)
                            {
                                min = chunkIO.ReadChunkVector3(chunkSize3);
                            }
                            else if (id3 == Wad2Chunks.MeshBoundingBoxMax)
                            {
                                max = chunkIO.ReadChunkVector3(chunkSize3);
                            }
                            else
                            {
                                return(false);
                            }
                            return(true);
                        });
                        s.CollisionBox = new BoundingBox(min, max);
                    }
                    else if (id2 == Wad2Chunks.Mesh)
                    {
                        s.Mesh = LoadMesh(chunkIO, chunkSize2, textures);
                    }
                    else if (id2 == Wad2Chunks.StaticAmbientLight)
                    {
                        s.AmbientLight = chunkIO.ReadChunkShort(chunkSize2);
                    }
                    else if (id2 == Wad2Chunks.StaticLight)
                    {
                        var light = new WadLight();
                        chunkIO.ReadChunks((id3, chunkSize3) =>
                        {
                            if (id3 == Wad2Chunks.StaticLightPosition)
                            {
                                light.Position = chunkIO.ReadChunkVector3(chunkSize3);
                            }
                            else if (id3 == Wad2Chunks.StaticLightRadius)
                            {
                                light.Radius = chunkIO.ReadChunkFloat(chunkSize3);
                            }
                            else if (id3 == Wad2Chunks.StaticLightIntensity)
                            {
                                light.Intensity = chunkIO.ReadChunkFloat(chunkSize3);
                            }
                            else
                            {
                                return(false);
                            }
                            return(true);
                        });
                        s.Lights.Add(light);
                    }
                    else
                    {
                        return(false);
                    }
                    return(true);
                });

                wad.Statics.Add(s.Id, s);
                return(true);
            });

            return(true);
        }