Exemple #1
0
        public CarMesh(MeshChunk meshChunk, BitmapChunk bmpChunk, Color brakeColor)
            : base(meshChunk, bmpChunk)
        {
            foreach (var poly in _polys)
            {
                switch (poly.Label)
                {
                    case "rt_rear":
                        _rightRearWheel = poly;
                        break;
                    case "lt_rear":
                        _leftRearWheel = poly;
                        break;
                    case "rt_frnt":
                        _rightFrontWheel = poly;
                        break;
                    case "lt_frnt":
                        _leftFrontWheel = poly;
                        break;
                    case "bkll":
                        _leftBrakeLight = poly;
                        break;
                    case "bklr":
                        _rightBrakeLight = poly;
                        break;
                }
            }
            var tyreEntry = bmpChunk.FindByName("tyr1");
            if (tyreEntry != null)
                _wheelTexture = tyreEntry.Texture;

            // This seems like it should be done in a shader but I couldn't get it to work well enough
            // (dealing with original paletted colors doesn't work so well in a texture stretched over a polygon)

            var rsidPoly = _polys.FirstOrDefault(a => a.TextureName == "rsid");
            if (rsidPoly != null)
            {
                // Generate a new texture for brake lights on.
                Color[] pixels = new Color[rsidPoly.Texture.Width * rsidPoly.Texture.Height];
                rsidPoly.Texture.GetData<Color>(pixels);
                for (int i = 0; i < pixels.Length; i++)
                {
                    if (pixels[i] == brakeColor)
                        pixels[i] = BrakeOnColor;
                }

                _brakeOnTexture = new Texture2D(Engine.Instance.Device, rsidPoly.Texture.Width, rsidPoly.Texture.Height);
                _brakeOnTexture.SetData<Color>(pixels);

                // Generate a new texture for brake lights off.
                for (int i = 0; i < pixels.Length; i++)
                {
                    if (pixels[i] == BrakeOnColor)
                        pixels[i] = BrakeOffColor;
                }

                _brakeOffTexture = new Texture2D(Engine.Instance.Device, _leftBrakeLight.Texture.Width, _leftBrakeLight.Texture.Height);
                _brakeOffTexture.SetData<Color>(pixels);
            }
        }
Exemple #2
0
        private void ReadPolygonBlock(BinaryReader reader, int polygonCount, BinaryReader polygonVertexMap)
        {
            for (int i = 0; i < polygonCount; i++)
            {
                byte typeFlag = reader.ReadByte();
                int shapeId = typeFlag & (0xff >> 5); // type = 3 or 4.  Held in the first 3 bits
                //bool computeTextureUVs = (typeFlag & (0x1 << 3)) != 0; // bit 3 is set if there are *no* texture co-ords (and we should infer them?)

                byte b1 = reader.ReadByte();
                byte textureNumber = reader.ReadByte();
                byte b2 = reader.ReadByte();
                int verticesIndex = reader.ReadInt32();
                int textureCoordsIndex = reader.ReadInt32();

                if (shapeId != 3 && shapeId != 4)
                {
                    // something in Burnt Sienna has a value of 2.  Haven't investigated yet.
                    continue;
                }

                // if these 2 indexes are the same, there are no texture coords
                bool computeTextureUVs = verticesIndex == textureCoordsIndex;

                Polygon polygon = new Polygon((PolygonShape)shapeId, computeTextureUVs);
                polygon.TextureName = _textureNames[textureNumber];

                if (polygon.TextureName == "dkfr")
                {

                }

                //Vertices for polygon
                polygonVertexMap.BaseStream.Position = verticesIndex * sizeof(int);
                int v1 = polygonVertexMap.ReadInt32();
                int v2 = polygonVertexMap.ReadInt32();
                int v3 = polygonVertexMap.ReadInt32();
                int v4 = polygonVertexMap.ReadInt32();

                //Debug.WriteLine(String.Format("Poly {8} {0} {9} ({7}): {1},{2},{3},{4}, / {5} {6} computeUvs: {10}", i, v1, v2, v3, v4, b1, b2, polygon.TextureName, polygon.Shape, typeFlag.ToString("X"), computeTextureUVs));

                //Texture co-ords for vertices
                if (!computeTextureUVs)
                {
                    polygonVertexMap.BaseStream.Position = textureCoordsIndex * sizeof(int);
                    int t1 = polygonVertexMap.ReadInt32();
                    int t2 = polygonVertexMap.ReadInt32();
                    int t3 = polygonVertexMap.ReadInt32();
                    int t4 = polygonVertexMap.ReadInt32();

                    polygon.Vertices[0] = _vertices[v1];
                    polygon.Vertices[1] = _vertices[v2];
                    polygon.Vertices[2] = _vertices[v3];
                    polygon.TextureUVs[0] = _vertexTextureMap[t1];
                    polygon.TextureUVs[1] = _vertexTextureMap[t2];
                    polygon.TextureUVs[2] = _vertexTextureMap[t3];

                    if (polygon.Shape == PolygonShape.Quad)
                    {
                        polygon.Vertices[3] = _vertices[v1];
                        polygon.Vertices[4] = _vertices[v3];
                        polygon.Vertices[5] = _vertices[v4];
                        polygon.TextureUVs[3] = _vertexTextureMap[t1];
                        polygon.TextureUVs[4] = _vertexTextureMap[t3];
                        polygon.TextureUVs[5] = _vertexTextureMap[t4];
                    }
                }
                else
                {
                    if (polygon.Shape == PolygonShape.Quad)
                    {
                        polygon.Vertices[0] = _vertices[v1];
                        polygon.Vertices[1] = _vertices[v2];
                        polygon.Vertices[2] = _vertices[v3];
                        polygon.Vertices[3] = _vertices[v1];
                        polygon.Vertices[4] = _vertices[v3];
                        polygon.Vertices[5] = _vertices[v4];
                    }
                    else
                    {
                        throw new NotImplementedException();
                    }
                }

                _polygons.Add(polygon);
            }
        }
Exemple #3
0
        private void ReadPolygonBlock(BinaryReader reader, int polygonCount, BinaryReader polygonVertexMap)
        {
            for (int i = 0; i < polygonCount; i++)
            {
                byte typeFlag = reader.ReadByte();
                int  shapeId  = typeFlag & (0xff >> 5);               // type = 3 or 4.  Held in the first 3 bits
                //bool computeTextureUVs = (typeFlag & (0x1 << 3)) != 0; // bit 3 is set if there are *no* texture co-ords (and we should infer them?)

                byte b1                 = reader.ReadByte();
                byte textureNumber      = reader.ReadByte();
                byte b2                 = reader.ReadByte();
                int  verticesIndex      = reader.ReadInt32();
                int  textureCoordsIndex = reader.ReadInt32();

                if (shapeId != 3 && shapeId != 4)
                {
                    // something in Burnt Sienna has a value of 2.  Haven't investigated yet.
                    continue;
                }

                // if these 2 indexes are the same, there are no texture coords
                bool computeTextureUVs = verticesIndex == textureCoordsIndex;

                Polygon polygon = new Polygon((PolygonShape)shapeId, computeTextureUVs);
                polygon.TextureName = _textureNames[textureNumber];

                if (polygon.TextureName == "dkfr")
                {
                }

                //Vertices for polygon
                polygonVertexMap.BaseStream.Position = verticesIndex * sizeof(int);
                int v1 = polygonVertexMap.ReadInt32();
                int v2 = polygonVertexMap.ReadInt32();
                int v3 = polygonVertexMap.ReadInt32();
                int v4 = polygonVertexMap.ReadInt32();

                //Debug.WriteLine(String.Format("Poly {8} {0} {9} ({7}): {1},{2},{3},{4}, / {5} {6} computeUvs: {10}", i, v1, v2, v3, v4, b1, b2, polygon.TextureName, polygon.Shape, typeFlag.ToString("X"), computeTextureUVs));

                //Texture co-ords for vertices
                if (!computeTextureUVs)
                {
                    polygonVertexMap.BaseStream.Position = textureCoordsIndex * sizeof(int);
                    int t1 = polygonVertexMap.ReadInt32();
                    int t2 = polygonVertexMap.ReadInt32();
                    int t3 = polygonVertexMap.ReadInt32();
                    int t4 = polygonVertexMap.ReadInt32();

                    polygon.Vertices[0]   = _vertices[v1];
                    polygon.Vertices[1]   = _vertices[v2];
                    polygon.Vertices[2]   = _vertices[v3];
                    polygon.TextureUVs[0] = _vertexTextureMap[t1];
                    polygon.TextureUVs[1] = _vertexTextureMap[t2];
                    polygon.TextureUVs[2] = _vertexTextureMap[t3];

                    if (polygon.Shape == PolygonShape.Quad)
                    {
                        polygon.Vertices[3]   = _vertices[v1];
                        polygon.Vertices[4]   = _vertices[v3];
                        polygon.Vertices[5]   = _vertices[v4];
                        polygon.TextureUVs[3] = _vertexTextureMap[t1];
                        polygon.TextureUVs[4] = _vertexTextureMap[t3];
                        polygon.TextureUVs[5] = _vertexTextureMap[t4];
                    }
                }
                else
                {
                    if (polygon.Shape == PolygonShape.Quad)
                    {
                        polygon.Vertices[0] = _vertices[v1];
                        polygon.Vertices[1] = _vertices[v2];
                        polygon.Vertices[2] = _vertices[v3];
                        polygon.Vertices[3] = _vertices[v1];
                        polygon.Vertices[4] = _vertices[v3];
                        polygon.Vertices[5] = _vertices[v4];
                    }
                    else
                    {
                        throw new NotImplementedException();
                    }
                }

                _polygons.Add(polygon);
            }
        }
Exemple #4
0
        private Vector3 GetWheelAxlePoint(Polygon wheelPoly)
        {
            float y = (wheelPoly.Vertices.Max(a => a.Y) + wheelPoly.Vertices.Min(a => a.Y)) / 2;
            float z = (wheelPoly.Vertices.Max(a => a.Z) + wheelPoly.Vertices.Min(a => a.Z)) / 2;

            // X value is always the same
            return new Vector3(wheelPoly.Vertices[0].X, y, z);
        }