Ejemplo n.º 1
0
        /// <summary>
        /// Creates a primitive group for the vertex buffer
        /// </summary>
        /// <param name="type"></param>
        /// <param name="Vertices"></param>
        /// <param name="Attributes"></param>
        /// <returns></returns>
        private GX_PrimitiveGroup Compress(GXPrimitiveType type, GX_Vertex[] Vertices, GXAttribName[] Attributes)
        {
            GX_PrimitiveGroup g = new GX_PrimitiveGroup();

            g.PrimitiveType = type;
            g.Indices       = new GX_IndexGroup[Vertices.Length];
            int IndexGroupIndex = 0;

            foreach (GX_Vertex v in Vertices)
            {
                GX_IndexGroup ig = new GX_IndexGroup();
                ig.Indices = new ushort[Attributes.Length];

                int i = 0;
                foreach (var b in Attributes)
                {
                    switch (b)
                    {
                    case GXAttribName.GX_VA_CLR0:
                        ig.Clr0 = v.CLR0.ToBytes();
                        break;

                    case GXAttribName.GX_VA_CLR1:
                        ig.Clr1 = v.CLR1.ToBytes();
                        break;

                    case GXAttribName.GX_VA_PNMTXIDX:
                        ig.Indices[i] = v.PNMTXIDX;
                        break;

                    case GXAttribName.GX_VA_TEX0MTXIDX:
                        ig.Indices[i] = v.TEX0MTXIDX;
                        break;

                    case GXAttribName.GX_VA_TEX1MTXIDX:
                        ig.Indices[i] = v.TEX1MTXIDX;
                        break;

                    case GXAttribName.GX_VA_NULL: break;

                    case GXAttribName.GX_VA_POS: ig.Indices[i] = GetIndex(b, new float[] { v.POS.X, v.POS.Y, v.POS.Z }); break;

                    case GXAttribName.GX_VA_NRM: ig.Indices[i] = GetIndex(b, new float[] { v.NRM.X, v.NRM.Y, v.NRM.Z }); break;

                    case GXAttribName.GX_VA_NBT: ig.Indices[i] = GetIndex(b, new float[] { v.NRM.X, v.NRM.Y, v.NRM.Z, v.BITAN.X, v.BITAN.Y, v.BITAN.Z, v.TAN.X, v.TAN.Y, v.TAN.Z }); break;

                    case GXAttribName.GX_VA_TEX0: ig.Indices[i] = GetIndex(b, new float[] { v.TEX0.X, v.TEX0.Y }); break;

                    case GXAttribName.GX_VA_TEX1: ig.Indices[i] = GetIndex(b, new float[] { v.TEX1.X, v.TEX1.Y }); break;

                    case GXAttribName.GX_VA_TEX2: ig.Indices[i] = GetIndex(b, new float[] { v.TEX2.X, v.TEX2.Y }); break;

                    case GXAttribName.GX_VA_TEX3: ig.Indices[i] = GetIndex(b, new float[] { v.TEX3.X, v.TEX3.Y }); break;

                    case GXAttribName.GX_VA_TEX4: ig.Indices[i] = GetIndex(b, new float[] { v.TEX4.X, v.TEX4.Y }); break;

                    case GXAttribName.GX_VA_TEX5: ig.Indices[i] = GetIndex(b, new float[] { v.TEX5.X, v.TEX5.Y }); break;

                    case GXAttribName.GX_VA_TEX6: ig.Indices[i] = GetIndex(b, new float[] { v.TEX6.X, v.TEX6.Y }); break;

                    case GXAttribName.GX_VA_TEX7: ig.Indices[i] = GetIndex(b, new float[] { v.TEX7.X, v.TEX7.Y }); break;

                    //case GXAttribName.GX_VA_CLR0: ig.Indices[i] = GetIndex(b, v.CLR0); break;
                    default:
                        throw new Exception("Error Building " + b);
                    }
                    i++;
                }
                g.Indices[IndexGroupIndex++] = ig;
            }

            return(g);
        }
Ejemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="PrimitiveGroup"></param>
        /// <param name="Attributes"></param>
        /// <returns></returns>
        private static GX_Vertex[] GetDecodedVertices(GX_PrimitiveGroup PrimitiveGroup, GX_Attribute[] Attributes)
        {
            // Create Vertex List
            List <GX_Vertex> Vertices = new List <GX_Vertex>();

            // Decode
            foreach (GX_IndexGroup ig in PrimitiveGroup.Indices)
            {
                GX_Vertex Vertex = new GX_Vertex();
                for (int i = 0; i < Attributes.Length; i++)
                {
                    var attribute = Attributes[i];

                    int     index = ig.Indices[i];
                    float[] f     = new float[4];

                    if (attribute.AttributeType != GXAttribType.GX_DIRECT)
                    {
                        var values = attribute.GetDecodedDataAt(index);

                        // convert to float
                        f = new float[values.Length];
                        for (int j = 0; j < f.Length; j++)
                        {
                            f[j] = (float)values[j];
                        }
                    }

                    switch (attribute.AttributeName)
                    {
                    case GXAttribName.GX_VA_NULL:
                        break;

                    case GXAttribName.GX_VA_PNMTXIDX:
                        if (attribute.AttributeType == GXAttribType.GX_DIRECT)
                        {
                            Vertex.PNMTXIDX = (ushort)index;
                        }
                        break;

                    case GXAttribName.GX_VA_TEX0MTXIDX:
                        if (attribute.AttributeType == GXAttribType.GX_DIRECT)
                        {
                            Vertex.TEX0MTXIDX = (ushort)index;
                        }
                        break;

                    case GXAttribName.GX_VA_TEX1MTXIDX:
                        if (attribute.AttributeType == GXAttribType.GX_DIRECT)
                        {
                            Vertex.TEX1MTXIDX = (ushort)index;
                        }
                        break;

                    case GXAttribName.GX_VA_POS:

                        if (attribute.AttributeType != GXAttribType.GX_DIRECT)
                        {
                            if (f.Length > 0)
                            {
                                Vertex.POS.X = f[0];
                            }
                            if (f.Length > 1)
                            {
                                Vertex.POS.Y = f[1];
                            }
                            if (f.Length > 2)
                            {
                                Vertex.POS.Z = f[2];
                            }
                        }
                        break;

                    case GXAttribName.GX_VA_NRM:
                        if (attribute.AttributeType != GXAttribType.GX_DIRECT)
                        {
                            Vertex.NRM.X = f[0];
                            Vertex.NRM.Y = f[1];
                            Vertex.NRM.Z = f[2];
                        }
                        break;

                    case GXAttribName.GX_VA_NBT:
                        if (attribute.AttributeType != GXAttribType.GX_DIRECT)
                        {
                            Vertex.NRM.X   = f[0];
                            Vertex.NRM.Y   = f[1];
                            Vertex.NRM.Z   = f[2];
                            Vertex.BITAN.X = f[3];
                            Vertex.BITAN.Y = f[4];
                            Vertex.BITAN.Z = f[5];
                            Vertex.TAN.X   = f[6];
                            Vertex.TAN.Y   = f[7];
                            Vertex.TAN.Z   = f[8];
                        }
                        break;

                    case GXAttribName.GX_VA_TEX0:
                        if (attribute.AttributeType != GXAttribType.GX_DIRECT)
                        {
                            Vertex.TEX0.X = f[0];
                            Vertex.TEX0.Y = f[1];
                        }
                        break;

                    case GXAttribName.GX_VA_TEX1:
                        if (attribute.AttributeType != GXAttribType.GX_DIRECT)
                        {
                            Vertex.TEX1.X = f[0];
                            Vertex.TEX1.Y = f[1];
                        }
                        break;

                    case GXAttribName.GX_VA_CLR0:
                        if (attribute.AttributeType == GXAttribType.GX_DIRECT)
                        {
                            Vertex.CLR0.R = ig.Clr0[0] / 255f;
                            Vertex.CLR0.G = ig.Clr0[1] / 255f;
                            Vertex.CLR0.B = ig.Clr0[2] / 255f;
                            Vertex.CLR0.A = ig.Clr0[3] / 255f;
                        }
                        else
                        {
                            Vertex.CLR0.R = f[0];
                            Vertex.CLR0.G = f[1];
                            Vertex.CLR0.B = f[2];
                            Vertex.CLR0.A = f[3];
                        }
                        break;

                    case GXAttribName.GX_VA_CLR1:
                        if (attribute.AttributeType == GXAttribType.GX_DIRECT)
                        {
                            Vertex.CLR1.R = ig.Clr1[0] / 255f;
                            Vertex.CLR1.G = ig.Clr1[1] / 255f;
                            Vertex.CLR1.B = ig.Clr1[2] / 255f;
                            Vertex.CLR1.A = ig.Clr1[3] / 255f;
                        }
                        else
                        {
                            Vertex.CLR1.R = f[0];
                            Vertex.CLR1.G = f[1];
                            Vertex.CLR1.B = f[2];
                            Vertex.CLR1.A = f[3];
                        }
                        break;

                    default:
                        Console.WriteLine("To be implemented: " + attribute.AttributeName);
                        break;
                    }
                }
                Vertices.Add(Vertex);
            }

            return(Vertices.ToArray());
        }
Ejemplo n.º 3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="PrimitiveGroup"></param>
        /// <param name="Attributes"></param>
        /// <returns></returns>
        private static GX_Vertex[] GetDecodedVertices(GX_PrimitiveGroup PrimitiveGroup, List <GX_Attribute> Attributes, HSD_POBJ Polygon, int shapeset)
        {
            // Create Vertex List
            List <GX_Vertex> Vertices = new List <GX_Vertex>();

            // Decode
            foreach (GX_IndexGroup ig in PrimitiveGroup.Indices)
            {
                GX_Vertex Vertex = new GX_Vertex();
                for (int i = 0; i < Attributes.Count; i++)
                {
                    var attribute = Attributes[i];

                    int     index = ig.Indices[i];
                    float[] f     = new float[4];

                    // check if index is in range of buffer
                    if (attribute.AttributeType != GXAttribType.GX_DIRECT &&
                        attribute.Buffer != null &&
                        index >= attribute.Count)
                    {
                        System.Diagnostics.Debug.WriteLine($"Warning: Attribute index out of range {index} >= {attribute.Count}");
                        continue;
                    }

                    // check if data is direct
                    if (attribute.AttributeType != GXAttribType.GX_DIRECT)
                    {
                        f = attribute.GetDecodedDataAt(index);
                    }

                    switch (attribute.AttributeName)
                    {
                    case GXAttribName.GX_VA_NULL:
                        break;

                    case GXAttribName.GX_VA_PNMTXIDX:
                        if (attribute.AttributeType == GXAttribType.GX_DIRECT)
                        {
                            Vertex.PNMTXIDX = (ushort)index;
                        }
                        break;

                    case GXAttribName.GX_VA_TEX0MTXIDX:
                        if (attribute.AttributeType == GXAttribType.GX_DIRECT)
                        {
                            Vertex.TEX0MTXIDX = (ushort)index;
                        }
                        break;

                    case GXAttribName.GX_VA_TEX1MTXIDX:
                        if (attribute.AttributeType == GXAttribType.GX_DIRECT)
                        {
                            Vertex.TEX1MTXIDX = (ushort)index;
                        }
                        break;

                    case GXAttribName.GX_VA_POS:
                        if (attribute.AttributeType != GXAttribType.GX_DIRECT)
                        {
                            if (Polygon.ShapeSet != null)
                            {
                                var ss = Polygon.ShapeSet.VertexIndices[shapeset];
                                f = attribute.GetDecodedDataAt(ss[index]);
                            }

                            if (f.Length > 0)
                            {
                                Vertex.POS.X = f[0];
                            }
                            if (f.Length > 1)
                            {
                                Vertex.POS.Y = f[1];
                            }
                            if (f.Length > 2)
                            {
                                Vertex.POS.Z = f[2];
                            }
                        }
                        break;

                    case GXAttribName.GX_VA_NRM:
                        if (attribute.AttributeType != GXAttribType.GX_DIRECT)
                        {
                            if (Polygon.ShapeSet != null)
                            {
                                var ss = Polygon.ShapeSet.NormalIndicies[shapeset];
                                f = attribute.GetDecodedDataAt(ss[index]);
                            }

                            Vertex.NRM.X = f[0];
                            Vertex.NRM.Y = f[1];
                            Vertex.NRM.Z = f[2];
                        }
                        break;

                    case GXAttribName.GX_VA_NBT:
                        if (attribute.AttributeType != GXAttribType.GX_DIRECT)
                        {
                            Vertex.NRM.X   = f[0];
                            Vertex.NRM.Y   = f[1];
                            Vertex.NRM.Z   = f[2];
                            Vertex.BITAN.X = f[3];
                            Vertex.BITAN.Y = f[4];
                            Vertex.BITAN.Z = f[5];
                            Vertex.TAN.X   = f[6];
                            Vertex.TAN.Y   = f[7];
                            Vertex.TAN.Z   = f[8];
                        }
                        break;

                    case GXAttribName.GX_VA_TEX0:
                        if (attribute.AttributeType != GXAttribType.GX_DIRECT)
                        {
                            Vertex.TEX0.X = f[0];
                            Vertex.TEX0.Y = f[1];
                        }
                        break;

                    case GXAttribName.GX_VA_TEX1:
                        if (attribute.AttributeType != GXAttribType.GX_DIRECT)
                        {
                            Vertex.TEX1.X = f[0];
                            Vertex.TEX1.Y = f[1];
                        }
                        break;

                    case GXAttribName.GX_VA_TEX2:
                        if (attribute.AttributeType != GXAttribType.GX_DIRECT)
                        {
                            Vertex.TEX2.X = f[0];
                            Vertex.TEX2.Y = f[1];
                        }
                        break;

                    case GXAttribName.GX_VA_TEX3:
                        if (attribute.AttributeType != GXAttribType.GX_DIRECT)
                        {
                            Vertex.TEX3.X = f[0];
                            Vertex.TEX3.Y = f[1];
                        }
                        break;

                    case GXAttribName.GX_VA_TEX4:
                        if (attribute.AttributeType != GXAttribType.GX_DIRECT)
                        {
                            Vertex.TEX4.X = f[0];
                            Vertex.TEX4.Y = f[1];
                        }
                        break;

                    case GXAttribName.GX_VA_TEX5:
                        if (attribute.AttributeType != GXAttribType.GX_DIRECT)
                        {
                            Vertex.TEX5.X = f[0];
                            Vertex.TEX5.Y = f[1];
                        }
                        break;

                    case GXAttribName.GX_VA_TEX6:
                        if (attribute.AttributeType != GXAttribType.GX_DIRECT)
                        {
                            Vertex.TEX6.X = f[0];
                            Vertex.TEX6.Y = f[1];
                        }
                        break;

                    case GXAttribName.GX_VA_TEX7:
                        if (attribute.AttributeType != GXAttribType.GX_DIRECT)
                        {
                            Vertex.TEX7.X = f[0];
                            Vertex.TEX7.Y = f[1];
                        }
                        break;

                    case GXAttribName.GX_VA_CLR0:
                        if (attribute.AttributeType == GXAttribType.GX_DIRECT)
                        {
                            Vertex.CLR0.R = ig.Clr0[0] / 255f;
                            Vertex.CLR0.G = ig.Clr0[1] / 255f;
                            Vertex.CLR0.B = ig.Clr0[2] / 255f;
                            Vertex.CLR0.A = ig.Clr0[3] / 255f;
                        }
                        else
                        {
                            Vertex.CLR0.R = f[0];
                            Vertex.CLR0.G = f[1];
                            Vertex.CLR0.B = f[2];
                            Vertex.CLR0.A = f[3];
                        }
                        break;

                    case GXAttribName.GX_VA_CLR1:
                        if (attribute.AttributeType == GXAttribType.GX_DIRECT)
                        {
                            Vertex.CLR1.R = ig.Clr1[0] / 255f;
                            Vertex.CLR1.G = ig.Clr1[1] / 255f;
                            Vertex.CLR1.B = ig.Clr1[2] / 255f;
                            Vertex.CLR1.A = ig.Clr1[3] / 255f;
                        }
                        else
                        {
                            Vertex.CLR1.R = f[0];
                            Vertex.CLR1.G = f[1];
                            Vertex.CLR1.B = f[2];
                            Vertex.CLR1.A = f[3];
                        }
                        break;

                    default:
                        Console.WriteLine("To be implemented: " + attribute.AttributeName);
                        break;
                    }
                }
                Vertices.Add(Vertex);
            }

            return(Vertices.ToArray());
        }