Exemple #1
0
        /// <summary>
        /// Create a parameter object from a file and address
        /// </summary>
        /// <param name="file">The file contents</param>
        /// <param name="address">The address at which the parameter is located</param>
        /// <returns>Any of the parameter types</returns>
        public static GCParameter Read(byte[] file, int address)
        {
            GCParameter   result    = null;
            ParameterType paramType = (ParameterType)BitConverter.ToUInt32(file, address);

            switch (paramType)
            {
            case ParameterType.VtxAttrFmt:
                result = new VtxAttrFmtParameter(GCVertexAttribute.Null);
                break;

            case ParameterType.IndexAttributeFlags:
                result = new IndexAttributeParameter();
                break;

            case ParameterType.Lighting:
                result = new LightingParameter();
                break;

            case ParameterType.BlendAlpha:
                result = new BlendAlphaParameter();
                break;

            case ParameterType.AmbientColor:
                result = new AmbientColorParameter();
                break;

            case ParameterType.Texture:
                result = new TextureParameter();
                break;

            case ParameterType.Unknown_9:
                result = new Unknown9Parameter();
                break;

            case ParameterType.TexCoordGen:
                result = new TexCoordGenParameter();
                break;
            }

            result.data = ByteConverter.ToUInt32(file, address + 4);

            return(result);
        }
Exemple #2
0
        /// <summary>
        /// Creates meshinfo to render
        /// </summary>
        /// <param name="material">A material with the current material properties</param>
        /// <param name="positions">The position data</param>
        /// <param name="normals">The normal data</param>
        /// <param name="colors">The color data</param>
        /// <param name="uvs">The uv data</param>
        /// <returns>A mesh info for the mesh</returns>
        public MeshInfo Process(NJS_MATERIAL material, List <IOVtx> positions, List <IOVtx> normals, List <IOVtx> colors, List <IOVtx> uvs)
        {
            // setting the material properties according to the parameters
            foreach (GCParameter param in parameters)
            {
                switch (param.type)
                {
                case ParameterType.BlendAlpha:
                    BlendAlphaParameter blend = param as BlendAlphaParameter;
                    material.SourceAlpha      = blend.NJSourceAlpha;
                    material.DestinationAlpha = blend.NJDestAlpha;
                    break;

                case ParameterType.AmbientColor:
                    AmbientColorParameter ambientCol = param as AmbientColorParameter;
                    material.DiffuseColor = ambientCol.AmbientColor.SystemCol;
                    break;

                case ParameterType.Texture:
                    TextureParameter tex = param as TextureParameter;
                    material.TextureID = tex.TextureID;
                    material.FlipU     = tex.Tile.HasFlag(GCTileMode.MirrorU);
                    material.FlipV     = tex.Tile.HasFlag(GCTileMode.MirrorV);
                    material.ClampU    = tex.Tile.HasFlag(GCTileMode.WrapU);
                    material.ClampV    = tex.Tile.HasFlag(GCTileMode.WrapV);

                    // no idea why, but ok
                    material.ClampU &= tex.Tile.HasFlag(GCTileMode.Unk_1);
                    material.ClampV &= tex.Tile.HasFlag(GCTileMode.Unk_1);
                    break;

                case ParameterType.TexCoordGen:
                    TexCoordGenParameter gen = param as TexCoordGenParameter;
                    material.EnvironmentMap = gen.TexGenSrc == GCTexGenSrc.Normal;
                    break;
                }
            }

            // filtering out the double loops
            List <Loop> corners = new List <Loop>();
            List <Poly> polys   = new List <Poly>();

            foreach (GCPrimitive prim in primitives)
            {
                int      j       = 0;
                ushort[] indices = new ushort[prim.loops.Count];
                foreach (Loop l in prim.loops)
                {
                    ushort t = (ushort)corners.FindIndex(x => x.Equals(l));
                    if (t == 0xFFFF)
                    {
                        indices[j] = (ushort)corners.Count;
                        corners.Add(l);
                    }
                    else
                    {
                        indices[j] = t;
                    }
                    j++;
                }


                // creating the polygons
                if (prim.primitiveType == GCPrimitiveType.Triangles)
                {
                    for (int i = 0; i < indices.Length; i += 3)
                    {
                        Triangle t = new Triangle();
                        t.Indexes[0] = indices[i];
                        t.Indexes[1] = indices[i + 1];
                        t.Indexes[2] = indices[i + 2];
                        polys.Add(t);
                    }
                }
                else if (prim.primitiveType == GCPrimitiveType.TriangleStrip)
                {
                    polys.Add(new Strip(indices, false));
                }
            }

            // creating the vertex data
            VertexData[] vertData   = new VertexData[corners.Count];
            bool         hasNormals = normals != null;
            bool         hasColors  = colors != null;
            bool         hasUVs     = uvs != null;

            for (int i = 0; i < corners.Count; i++)
            {
                Loop l = corners[i];
                vertData[i] = new SAModel.VertexData(
                    (Vector3)positions[l.PositionIndex],
                    hasNormals ? (Vector3)normals[l.NormalIndex] : new Vector3(0, 1, 0),
                    hasColors ? (Color)colors[l.Color0Index] : new Color(255, 255, 255, 255),
                    hasUVs ? (UV)uvs[l.UV0Index] : new UV(0, 0)
                    );
            }

            return(new MeshInfo(new NJS_MATERIAL(material), polys.ToArray(), vertData, hasUVs, hasColors));
        }