public RenderWareTextureDictionary(ExtendedSection clump)
        {
            foreach (Section section in clump.GetChildren(SectionType.RwTextureNative))
            {
                var textureInfo = (TextureNativeDataSection)((ExtendedSection)section).GetChild(0);
                var brush       = new ImageBrush()
                {
                    ViewportUnits = BrushMappingMode.Absolute,
                };

                // set the brush to image data after decoding the format
                if (textureInfo.D3DTextureFormat == TextureFormats.DXT1)
                {
                    // decode dxt1
                    byte[] decompressedImage = Squish.DecompressImage(
                        textureInfo.Data.ToArray(),
                        (int)textureInfo.Width,
                        (int)textureInfo.Height,
                        SquishFlags.Dxt1
                        );

                    PixelFormat pixelFormat = PixelFormats.Bgr565;
                    int         width       = (int)textureInfo.Width;
                    int         height      = (int)textureInfo.Height;
                    int         rawStride   = (width * pixelFormat.BitsPerPixel + 7) / 8;

                    if (rawStride * height != textureInfo.DataSize)
                    {
                        //Debug.WriteLine("Data Length Error. {0} != {1}",
                        //    rawStride * height,
                        //    textureInfo.DataSize);
                        return;
                    }

                    BitmapSource bmpSrc = BitmapSource.Create(
                        width,
                        height,
                        96,
                        96,
                        pixelFormat,
                        null,
                        decompressedImage,
                        rawStride
                        );

                    brush.ImageSource = bmpSrc;
                }
                else if (textureInfo.D3DTextureFormat == TextureFormats.DXT3)
                {
                    // decode dxt3
                    byte[] decompressedImage = Squish.DecompressImage(
                        textureInfo.Data.ToArray(),
                        (int)textureInfo.Width,
                        (int)textureInfo.Height,
                        SquishFlags.Dxt3
                        );
                }
                else
                {
                    if (textureInfo.BitDepth == 8)
                    {
                        PixelFormat pixelFormat = PixelFormats.Indexed8;
                        int         width       = (int)textureInfo.Width;
                        int         height      = (int)textureInfo.Height;
                        int         rawStride   = (width * pixelFormat.BitsPerPixel + 7) / 8;

                        if (rawStride * height != textureInfo.DataSize)
                        {
                            //Debug.WriteLine("Data Length Error. {0} != {1}",
                            //    rawStride * height,
                            //    textureInfo.DataSize);
                            return;
                        }

                        List <Color> paletteColors = new List <Color>();
                        for (int j = 0; j < textureInfo.Palette.Count; j += 4)
                        {
                            Color col = new Color
                            {
                                B = textureInfo.Palette[j],
                                G = textureInfo.Palette[j + 1],
                                R = textureInfo.Palette[j + 2],
                                A = textureInfo.Palette[j + 3]
                            };
                            paletteColors.Add(col);
                        }
                        var palette = new BitmapPalette(paletteColors);

                        BitmapSource bmpSrc = BitmapSource.Create(
                            width,
                            height,
                            96,
                            96,
                            pixelFormat,
                            palette,
                            textureInfo.Data.ToArray(),
                            rawStride
                            );

                        brush.ImageSource = bmpSrc;
                    }
                    else if (textureInfo.BitDepth == 16)
                    {
                        PixelFormat pixelFormat = PixelFormats.Bgr565;
                        int         width       = (int)textureInfo.Width;
                        int         height      = (int)textureInfo.Height;
                        int         rawStride   = (width * pixelFormat.BitsPerPixel + 7) / 8;

                        if (rawStride * height != textureInfo.DataSize)
                        {
                            //Debug.WriteLine("Data Length Error. {0} != {1}",
                            //    rawStride * height,
                            //    textureInfo.DataSize);
                            return;
                        }

                        BitmapSource bmpSrc = BitmapSource.Create(
                            width,
                            height,
                            96,
                            96,
                            pixelFormat,
                            null,
                            textureInfo.Data.ToArray(),
                            rawStride
                            );

                        brush.ImageSource = bmpSrc;
                    }
                    else if (textureInfo.BitDepth == 32)
                    {
                        PixelFormat pixelFormat = PixelFormats.Bgra32;
                        int         width       = (int)textureInfo.Width;
                        int         height      = (int)textureInfo.Height;
                        int         rawStride   = (width * pixelFormat.BitsPerPixel + 7) / 8;

                        if (rawStride * height != textureInfo.DataSize)
                        {
                            //Debug.WriteLine("Data Length Error. {0} != {1}",
                            //    rawStride * height,
                            //    textureInfo.DataSize);
                            return;
                        }

                        BitmapSource bmpSrc = BitmapSource.Create(
                            width,
                            height,
                            96,
                            96,
                            pixelFormat,
                            null,
                            textureInfo.Data.ToArray(),
                            rawStride
                            );

                        brush.ImageSource = bmpSrc;
                    }
                    else
                    {
                        brush = null;
                    }
                }
                if (brush != null)
                {
                    MaterialDictionary.Add(textureInfo.TextureName, new DiffuseMaterial(brush));
                }
                else
                {
                    MaterialDictionary.Add(textureInfo.TextureName, new DiffuseMaterial(Brushes.White));
                }
            }
        }
Example #2
0
        public RenderWareModel(ExtendedSection clump) : base()
        {
            // populate
            var model3dGroup = new Model3DGroup();

            Content = model3dGroup;
            MaterialGroupDictionary.Clear();
            //

            foreach (Section geometryList in clump.GetChildren(SectionType.RwGeometryList))
            {
                foreach (Section geometry in ((ExtendedSection)geometryList).GetChildren(SectionType.RwGeometry))
                {
                    var geometryInfo   = (GeometryDataSection)((ExtendedSection)geometry).GetChild(0);
                    var model3d        = new GeometryModel3D();
                    var meshGeometry3d = new MeshGeometry3D();
                    model3d.Geometry = meshGeometry3d;
                    model3dGroup.Children.Add(model3d);

                    //
                    foreach (var vertex in geometryInfo.Vertices)
                    {
                        meshGeometry3d.Positions.Add(new Point3D(vertex.X, vertex.Z, vertex.Y));
                    }
                    foreach (var normal in geometryInfo.Normals)
                    {
                        meshGeometry3d.Normals.Add(new Vector3D(normal.X, normal.Z, normal.Y));
                    }
                    foreach (var uvcoord in geometryInfo.VertexUVs)
                    {
                        meshGeometry3d.TextureCoordinates.Add(new Point(uvcoord.U, uvcoord.V));
                    }
                    foreach (var triangle in geometryInfo.Triangles)
                    {
                        meshGeometry3d.TriangleIndices.Add(triangle.Vertex2);
                        meshGeometry3d.TriangleIndices.Add(triangle.Vertex1);
                        meshGeometry3d.TriangleIndices.Add(triangle.Vertex3);
                    }

                    var matGroup = new MaterialGroup();
                    model3d.Material = matGroup;
                    matGroup.Children.Add(new SpecularMaterial(Brushes.White, 3));

                    foreach (Section materialList in ((ExtendedSection)geometry).GetChildren(SectionType.RwMaterialList))
                    {
                        foreach (Section material in ((ExtendedSection)materialList).GetChildren(SectionType.RwMaterial))
                        {
                            foreach (Section texture in ((ExtendedSection)material).GetChildren(SectionType.RwTexture))
                            {
                                var textureInfo = (StringDataSection)((ExtendedSection)texture).GetChild(1);

                                if (!MaterialGroupDictionary.ContainsKey(textureInfo.String))
                                {
                                    var list = new List <MaterialInfo>
                                    {
                                        new MaterialInfo()
                                        {
                                            MaterialGroup = matGroup
                                        }
                                    };
                                    MaterialGroupDictionary.Add(textureInfo.String, list);
                                }
                                else
                                {
                                    var list = MaterialGroupDictionary[textureInfo.String];
                                    list.Add(new MaterialInfo()
                                    {
                                        MaterialGroup = matGroup
                                    });
                                }
                            }
                        }
                    }
                }
            }
            model3dGroup.Children.Add(new AmbientLight());
        }