Exemplo n.º 1
0
        private void ReadParameters(byte[] file, int address, int count)
        {
            for (int i = 0; i < count; i++)
            {
                switch ((ParameterType)ByteConverter.ToInt32(file, address))
                {
                case ParameterType.VtxAttrFmt:
                    VtxAttrFmtParameter vtx_param = new VtxAttrFmtParameter();
                    vtx_param.Read(file, address + 4);
                    Parameters.Add(vtx_param);
                    break;

                case ParameterType.IndexAttributeFlags:
                    IndexAttributeParameter index_param = new IndexAttributeParameter();
                    index_param.Read(file, address + 4);
                    Parameters.Add(index_param);
                    break;

                case ParameterType.Lighting:
                    LightingParameter light_param = new LightingParameter();
                    light_param.Read(file, address + 4);
                    Parameters.Add(light_param);
                    break;

                case ParameterType.BlendAlpha:
                    BlendAlphaParameter blend_param = new BlendAlphaParameter();
                    blend_param.Read(file, address + 4);
                    Parameters.Add(blend_param);
                    break;

                case ParameterType.AmbientColor:
                    AmbientColorParameter ambient_param = new AmbientColorParameter();
                    ambient_param.Read(file, address + 4);
                    Parameters.Add(ambient_param);
                    break;

                case ParameterType.Texture:
                    TextureParameter texture_param = new TextureParameter();
                    texture_param.Read(file, address + 4);
                    Parameters.Add(texture_param);
                    break;

                case ParameterType.Unknown_9:
                    Unknown9Parameter unk9_param = new Unknown9Parameter();
                    unk9_param.Read(file, address + 4);
                    Parameters.Add(unk9_param);
                    break;

                case ParameterType.TexCoordGen:
                    TexCoordGenParameter mip_param = new TexCoordGenParameter();
                    mip_param.Read(file, address + 4);
                    Parameters.Add(mip_param);
                    break;
                }

                address += 8;
            }
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
0
        private MeshInfo ProcessMesh(Mesh m, bool hasUV, bool hasVColor, bool useAlpha)
        {
            List <SAModel.VertexData> vertData = new List <SAModel.VertexData>();
            List <Poly> polys = new List <Poly>();

            foreach (Parameter param in m.Parameters)
            {
                if (param.ParameterType == ParameterType.Texture)
                {
                    TextureParameter tex = param as TextureParameter;
                    cur_mat.TextureID = tex.TextureID;
                    if (!tex.Tile.HasFlag(TextureParameter.TileMode.MirrorU))
                    {
                        cur_mat.FlipU = true;
                    }
                    if (!tex.Tile.HasFlag(TextureParameter.TileMode.MirrorV))
                    {
                        cur_mat.FlipV = true;
                    }
                    if (!tex.Tile.HasFlag(TextureParameter.TileMode.WrapU))
                    {
                        cur_mat.ClampU = true;
                    }
                    if (!tex.Tile.HasFlag(TextureParameter.TileMode.WrapV))
                    {
                        cur_mat.ClampV = true;
                    }

                    cur_mat.ClampU &= tex.Tile.HasFlag(TextureParameter.TileMode.Unk_1);
                    cur_mat.ClampV &= tex.Tile.HasFlag(TextureParameter.TileMode.Unk_1);
                }
                else if (param.ParameterType == ParameterType.TexCoordGen)
                {
                    TexCoordGenParameter gen = param as TexCoordGenParameter;
                    if (gen.TexGenSrc == GXTexGenSrc.Normal)
                    {
                        cur_mat.EnvironmentMap = true;
                    }
                    else
                    {
                        cur_mat.EnvironmentMap = false;
                    }
                }
                else if (param.ParameterType == ParameterType.BlendAlpha)
                {
                    BlendAlphaParameter blend = param as BlendAlphaParameter;
                    cur_mat.SourceAlpha      = blend.SourceAlpha;
                    cur_mat.DestinationAlpha = blend.DestinationAlpha;
                }
            }

            foreach (Primitive prim in m.Primitives)
            {
                List <Poly> newPolys = new List <Poly>();
                switch (prim.PrimitiveType)
                {
                case GXPrimitiveType.Triangles:
                    for (int i = 0; i < prim.Vertices.Count / 3; i++)
                    {
                        newPolys.Add(new Triangle());
                    }
                    break;

                case GXPrimitiveType.TriangleStrip:
                    newPolys.Add(new Strip(prim.Vertices.Count, false));
                    break;
                }

                for (int i = 0; i < prim.Vertices.Count; i++)
                {
                    if (prim.PrimitiveType == GXPrimitiveType.Triangles)
                    {
                        newPolys[i / 3].Indexes[i % 3] = (ushort)vertData.Count;
                    }
                    else
                    {
                        newPolys[0].Indexes[i] = (ushort)vertData.Count;
                    }

                    vertData.Add(new SAModel.VertexData(
                                     VertexData.Positions[(int)prim.Vertices[i].PositionIndex],
                                     VertexData.Normals.Count > 0 ? VertexData.Normals[(int)prim.Vertices[i].NormalIndex] : new Vector3(0, 1, 0),
                                     hasVColor ? VertexData.Color_0[(int)prim.Vertices[i].Color0Index] : new GC.Color {
                        R = 1, G = 1, B = 1, A = 1
                    },
                                     hasUV ? VertexData.TexCoord_0[(int)prim.Vertices[i].UVIndex] : new Vector2()
                    {
                        X = 0, Y = 0
                    }));
                }
                polys.AddRange(newPolys);
            }

            cur_mat.UseAlpha = useAlpha;
            var result = new MeshInfo(cur_mat, polys.ToArray(), vertData.ToArray(), hasUV, hasVColor);

            return(result);
        }
Exemplo n.º 4
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.AmbientColor = 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
            SAModel.VertexData[] vertData = new SAModel.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));
        }