Beispiel #1
0
        public BatchData(Grendgine_Collada_Mesh mesh)
        {
            Batches = new List <Batch>();

            foreach (Grendgine_Collada_Triangles tri in mesh.Triangles)
            {
                Batch batch = new Batch(tri);
                Batches.Add(batch);
            }

            ActiveAttributesPerBatch = new List <VertexAttributes> [Batches.Count];

            foreach (Batch bat in Batches)
            {
                for (int i = 0; i < Batches.Count; i++)
                {
                    if (ActiveAttributesPerBatch[i] == null)
                    {
                        ActiveAttributesPerBatch[i] = bat.ActiveAttributes;
                        bat.AttributeIndex          = i;
                        break;
                    }
                    else
                    {
                        if (ActiveAttributesPerBatch[i].SequenceEqual(bat.ActiveAttributes))
                        {
                            bat.AttributeIndex = i;
                            break;
                        }
                    }
                }
            }
        }
Beispiel #2
0
        public BatchData(Grendgine_Collada_Mesh mesh, DrawData drw1)
        {
            Batches = new List <Batch>();

            foreach (Grendgine_Collada_Triangles tri in mesh.Triangles)
            {
                // Tri count was zero, couldn't initialize batch
                if (tri.Count == 0)
                {
                    continue;
                }

                Batch batch = new Batch(tri, drw1);
                Batches.Add(batch);
            }

            ActiveAttributesPerBatch = new List <List <VertexAttributes> >();

            // Convert our Collada Data to the data needed for each SHP1.Batch
            foreach (var batch in Batches)
            {
                batch.ConvertDataToFinalFormat(this);
            }

            // Get final packet count
            foreach (var batch in Batches)
            {
                PacketCount += batch.BatchPackets.Count;
            }
        }
Beispiel #3
0
 private void FillAttributeLists(Grendgine_Collada_Mesh mesh)
 {
     foreach (KeyValuePair <VertexAttributes, string> val in activeAttributes)
     {
         if (val.Key == VertexAttributes.Position)
         {
             // This is a hack. It needs to be fixed because it is a hack.
             // Should probably get the vertex positions directly from the vertex
             // class in the mesh.
             Positions = GetVertexData <Vector3>(mesh.Source.First(x => x.ID.Contains("POSITION")));
         }
         else if (val.Key == VertexAttributes.Normal)
         {
             Normals = GetVertexData <Vector3>(mesh.Source.First(x => x.ID.Contains(val.Value)));
         }
         else if (val.Key >= VertexAttributes.Tex0 && val.Key <= VertexAttributes.Tex7)
         {
             int uvIndex = (int)val.Key - 13; // 13 is the value of the Tex0 attribute
             UVData[uvIndex] = GetVertexData <Vector2>(mesh.Source.First(x => x.ID.Contains(val.Value)));
         }
         else if (val.Key == VertexAttributes.Color0 || val.Key == VertexAttributes.Color1)
         {
             int colorIndex = (int)val.Key - 11; // 11 is the value of the Color0 attribute
             ColorData[colorIndex] = GetVertexData <Vector4>(mesh.Source.First(x => x.ID.Contains(val.Value)));
         }
     }
 }
Beispiel #4
0
        private void GetActiveVertAttributes(Grendgine_Collada_Mesh mesh)
        {
            foreach (Grendgine_Collada_Triangles tri in mesh.Triangles)
            {
                foreach (Grendgine_Collada_Input_Shared input in tri.Input)
                {
                    switch (input.Semantic)
                    {
                    case Grendgine_Collada_Input_Semantic.VERTEX:
                        if (!activeAttributes.Keys.Contains(VertexAttributes.Position))
                        {
                            activeAttributes.Add(VertexAttributes.Position, input.source.Remove(0, 1));
                        }
                        break;

                    case Grendgine_Collada_Input_Semantic.NORMAL:
                        if (!activeAttributes.Keys.Contains(VertexAttributes.Normal))
                        {
                            activeAttributes.Add(VertexAttributes.Normal, input.source.Remove(0, 1));
                        }
                        break;

                    case Grendgine_Collada_Input_Semantic.COLOR:
                        if (input.Set > 2)
                        {
                            Console.WriteLine(string.Format("BMD only supports two Color attributes. Skipping color attribute [0].", input.Set));
                            break;
                        }
                        if (!activeAttributes.Keys.Contains(VertexAttributes.Color0 + input.Set))
                        {
                            activeAttributes.Add(VertexAttributes.Color0 + input.Set, input.source.Remove(0, 1));
                        }
                        break;

                    case Grendgine_Collada_Input_Semantic.TEXCOORD:
                        if (input.Set > 8)
                        {
                            Console.Write(string.Format("BMD only supports 8 UV attributes. Skipping UV attribute [0].", input.Set));
                            break;
                        }
                        if (!activeAttributes.Keys.Contains(VertexAttributes.Tex0 + input.Set))
                        {
                            activeAttributes.Add(VertexAttributes.Tex0 + input.Set, input.source.Remove(0, 1));
                        }
                        break;
                    }
                }
            }

            SortedAttributes = activeAttributes.Keys.ToList();
            SortedAttributes.Sort();
        }
Beispiel #5
0
        public BatchData(Grendgine_Collada_Mesh mesh, DrawData drw1)
        {
            Batches = new List <Batch>();

            Grendgine_Collada_Geometry_Common_Fields[] tris = null;

            if (mesh.Triangles != null)
            {
                tris = mesh.Triangles;
            }
            else if (mesh.Polylist != null)
            {
                tris = mesh.Polylist;
            }

            if (tris == null)
            {
                throw new System.ArgumentException("Could not load indices from mesh. (unknown list?)");
            }

            foreach (Grendgine_Collada_Geometry_Common_Fields tri in tris)
            {
                // Tri count was zero, couldn't initialize batch
                if (tri.Count == 0)
                {
                    continue;
                }

                Batch batch = new Batch(tri, drw1);
                Batches.Add(batch);
            }

            ActiveAttributesPerBatch = new List <List <VertexAttributes> >();

            // Convert our Collada Data to the data needed for each SHP1.Batch
            foreach (var batch in Batches)
            {
                batch.ConvertDataToFinalFormat(this);
            }

            // Get final packet count
            foreach (var batch in Batches)
            {
                PacketCount += batch.BatchPackets.Count;
            }
        }
Beispiel #6
0
        public MeshData Load(string filename)
        {
            if (!IsFileValid(filename))
            {
                return(new MeshData());
            }

            meshData         = Grendgine_Collada.Grendgine_Load_File(filename).Library_Geometries.Geometry[0].Mesh;
            indicesPerVertex = CalcIndexesPerVertex();
            numOfVertices    = CalcNumberOfVerticesInMesh();

            BuildIndicesArray();
            BuildVertexArray();

            SwitchUpAxisFromZtoY();

            return(new MeshData(vertices, indices));
        }
Beispiel #7
0
        public VertexData(Grendgine_Collada_Mesh mesh, Matrix4 bindShape, DataTypes position = DataTypes.F32, DataTypes normal = DataTypes.F32,
                          DataTypes uv = DataTypes.F32, ColorDataTypes color = ColorDataTypes.RGBA8)
        {
            Vertices = new List <Vertex>();

            Positions = new List <Vector3>();
            Normals   = new List <Vector3>();
            UVData    = new List <Vector2> [8];
            ColorData = new List <Vector4> [2];

            PositionType = position;
            NormalType   = normal;
            UVType       = uv;
            ColorType    = color;

            SortedAttributes = new List <VertexAttributes>();

            activeAttributes = new Dictionary <VertexAttributes, string>();
            GetActiveVertAttributes(mesh);
            FillAttributeLists(mesh);

            PositionFractionalBitVal = GetFractionalVec3(Positions);
            NormalFractionalBitVal   = GetFractionalVec3(Normals);
            UVFractionalBitVal       = 15;

            StringBuilder bld = new StringBuilder();

            bindPose = bindShape;

            foreach (List <Vector2> list2 in UVData)
            {
                if (list2 != null)
                {
                    for (int i = 0; i < list2.Count; i++)
                    {
                        Vector2 vec = list2[i];
                        vec.Y    = 1 - list2[i].Y;
                        list2[i] = vec;
                    }
                }
            }
        }
Beispiel #8
0
        public VertexData(Grendgine_Collada_Mesh mesh, DataTypes position = DataTypes.F32, DataTypes normal = DataTypes.F32,
                          DataTypes uv = DataTypes.F32, ColorDataTypes color = ColorDataTypes.RGB8)
        {
            Vertices = new List <Vertex>();

            Positions = new List <Vector3>();
            Normals   = new List <Vector3>();
            UVData    = new List <Vector2> [8];
            ColorData = new List <Vector3> [2];

            PositionType = position;
            NormalType   = normal;
            UVType       = uv;
            ColorType    = color;

            activeAttributes = new Dictionary <VertexAttributes, string>();
            GetActiveVertAttributes(mesh);
            FillAttributeLists(mesh);

            PositionFractionalBitVal = GetFractionalVec3(Positions);
            NormalFractionalBitVal   = GetFractionalVec3(Normals);
            UVFractionalBitVal       = 15;
        }
Beispiel #9
0
 /// <summary>
 /// Search a Grendgine_Collada_Mesh for a source by a given ID
 /// </summary>
 /// <param name="id">Source id including #</param>
 private Grendgine_Collada_Source FindSourceByID(Grendgine_Collada_Mesh mesh, string id)
 {
     return(mesh.Source.First <Grendgine_Collada_Source>(x => x.ID == id.TrimStart('#')));
 }
Beispiel #10
0
        private void GetActiveVertAttributes(Grendgine_Collada_Mesh mesh)
        {
            Grendgine_Collada_Geometry_Common_Fields[] tris = null;

            if (mesh.Triangles != null)
            {
                tris = mesh.Triangles;
            }
            else if (mesh.Polylist != null)
            {
                tris = mesh.Polylist;
            }

            if (tris == null)
            {
                throw new ArgumentException("Could not load indices from mesh. (unknown list?)");
            }

            foreach (Grendgine_Collada_Geometry_Common_Fields tri in tris)
            {
                foreach (Grendgine_Collada_Input_Shared input in tri.Input)
                {
                    switch (input.Semantic)
                    {
                    case Grendgine_Collada_Input_Semantic.VERTEX:
                        if (!activeAttributes.Keys.Contains(VertexAttributes.Position))
                        {
                            activeAttributes.Add(VertexAttributes.Position, input.source.Remove(0, 1));
                        }
                        break;

                    case Grendgine_Collada_Input_Semantic.NORMAL:
                        if (!activeAttributes.Keys.Contains(VertexAttributes.Normal))
                        {
                            activeAttributes.Add(VertexAttributes.Normal, input.source.Remove(0, 1));
                        }
                        break;

                    case Grendgine_Collada_Input_Semantic.COLOR:
                        if (input.Set > 2)
                        {
                            Console.WriteLine(string.Format("BMD only supports two Color attributes. Skipping color attribute [0].", input.Set));
                            break;
                        }
                        if (!activeAttributes.Keys.Contains(VertexAttributes.Color0 + input.Set))
                        {
                            activeAttributes.Add(VertexAttributes.Color0 + input.Set, input.source.Remove(0, 1));
                        }
                        break;

                    case Grendgine_Collada_Input_Semantic.TEXCOORD:
                        if (input.Set > 8)
                        {
                            Console.Write(string.Format("BMD only supports 8 UV attributes. Skipping UV attribute [0].", input.Set));
                            break;
                        }
                        if (!activeAttributes.Keys.Contains(VertexAttributes.Tex0 + input.Set))
                        {
                            activeAttributes.Add(VertexAttributes.Tex0 + input.Set, input.source.Remove(0, 1));
                        }
                        break;
                    }
                }
            }

            SortedAttributes = activeAttributes.Keys.ToList();
            SortedAttributes.Sort();
        }
Beispiel #11
0
        public void WriteLibrary_Geometries()
        {
            // Geometry library.  this is going to be fun...
            Grendgine_Collada_Library_Geometries libraryGeometries = new Grendgine_Collada_Library_Geometries();

            libraryGeometries.ID = cgfData.RootNode.Name;
            // Make a list for all the geometries objects we will need. Will convert to array at end.  Define the array here as well
            List <Grendgine_Collada_Geometry> geometryList = new List <Grendgine_Collada_Geometry>();

            // For each of the nodes, we need to write the geometry.
            // Need to figure out how to assign the right material to the node as well.
            // Use a foreach statement to get all the node chunks.  This will get us the meshes, which will contain the vertex, UV and normal info.
            foreach (CgfData.ChunkNode nodeChunk in cgfData.CgfChunks.Where(a => a.chunkType == ChunkType.Node))
            {
                // Create a geometry object.  Use the chunk ID for the geometry ID
                // Will have to be careful with this, since with .cga/.cgam pairs will need to match by Name.
                Grendgine_Collada_Geometry tmpGeo = new Grendgine_Collada_Geometry();
                tmpGeo.Name = nodeChunk.Name;
                tmpGeo.ID   = nodeChunk.id.ToString();
                // Now make the mesh object.  This will have 3 sources, 1 vertices, and 1 triangles (with material ID)
                // If the Object ID of Node chunk points to a Helper or a Controller though, place an empty.
                // Will have to figure out transforms here too.
                Grendgine_Collada_Mesh tmpMesh = new Grendgine_Collada_Mesh();
                // need to make a list of the sources and triangles to add to tmpGeo.Mesh
                List <Grendgine_Collada_Source>    sourceList  = new List <Grendgine_Collada_Source>();
                List <Grendgine_Collada_Triangles> triList     = new List <Grendgine_Collada_Triangles>();
                CgfData.ChunkDataStream            tmpNormals  = new CgfData.ChunkDataStream();
                CgfData.ChunkDataStream            tmpUVs      = new CgfData.ChunkDataStream();
                CgfData.ChunkDataStream            tmpVertices = new CgfData.ChunkDataStream();
                CgfData.ChunkDataStream            tmpVertsUVs = new CgfData.ChunkDataStream();

                if (cgfData.ChunkDictionary[nodeChunk.Object].chunkType == ChunkType.Mesh)
                {
                    // Get the mesh chunk and submesh chunk for this node.
                    CgfData.ChunkMesh        tmpMeshChunk   = (CgfData.ChunkMesh)cgfData.ChunkDictionary[nodeChunk.Object];
                    CgfData.ChunkMeshSubsets tmpMeshSubsets = tmpMeshSubsets = (CgfData.ChunkMeshSubsets)cgfData.ChunkDictionary[tmpMeshChunk.MeshSubsets];  // Listed as Object ID for the Node

                    // Get pointers to the vertices data
                    if (tmpMeshChunk.VerticesData != 0)
                    {
                        tmpVertices = (CgfData.ChunkDataStream)cgfData.ChunkDictionary[tmpMeshChunk.VerticesData];
                    }
                    if (tmpMeshChunk.NormalsData != 0)
                    {
                        tmpNormals = (CgfData.ChunkDataStream)cgfData.ChunkDictionary[tmpMeshChunk.NormalsData];
                    }
                    if (tmpMeshChunk.UVsData != 0)
                    {
                        tmpUVs = (CgfData.ChunkDataStream)cgfData.ChunkDictionary[tmpMeshChunk.UVsData];
                    }
                    if (tmpMeshChunk.VertsUVsData != 0)
                    {
                        tmpVertsUVs = (CgfData.ChunkDataStream)cgfData.ChunkDictionary[tmpMeshChunk.VertsUVsData];
                    }

                    // need a collada_source for position, normal, UV and color, what the source is (verts), and the tri index
                    Grendgine_Collada_Source    posSource  = new Grendgine_Collada_Source();
                    Grendgine_Collada_Source    normSource = new Grendgine_Collada_Source();
                    Grendgine_Collada_Source    uvSource   = new Grendgine_Collada_Source();
                    Grendgine_Collada_Vertices  verts      = new Grendgine_Collada_Vertices();
                    Grendgine_Collada_Triangles tris       = new Grendgine_Collada_Triangles();

                    posSource.ID    = nodeChunk.Name + "_pos"; // I want to use the chunk ID, but that may be daunting.
                    posSource.Name  = nodeChunk.Name + "_pos";
                    normSource.ID   = nodeChunk.Name + "_norm";
                    normSource.Name = nodeChunk.Name + "_norm";
                    uvSource.Name   = nodeChunk.Name + "_UV";
                    uvSource.ID     = nodeChunk.Name + "_UV";

                    // Create a float_array object to store all the data
                    Grendgine_Collada_Float_Array floatArray = new Grendgine_Collada_Float_Array();
                    floatArray.ID    = posSource.Name + "_array";
                    floatArray.Count = (int)tmpVertices.NumElements;
                    // Build the string of vertices with a stringbuilder
                    StringBuilder vertString = new StringBuilder();
                    for (int i = 0; i < floatArray.Count; i++)
                    {
                        // this is an array of Vector3s?
                    }
                    //floatArray =


                    normSource.ID   = nodeChunk.Name + "_pos";
                    normSource.Name = nodeChunk.Name + "_pos";

                    uvSource.ID   = nodeChunk.Name + "_UV";
                    uvSource.Name = nodeChunk.Name + "_UV";

                    // make a vertices eliment.  Only one, so no list needed.

                    // tris are easy.  Just the index of faces
                }
                else if (cgfData.ChunkDictionary[nodeChunk.Object].chunkType == ChunkType.Helper)
                {
                }
                else if (cgfData.ChunkDictionary[nodeChunk.Object].chunkType == ChunkType.Controller)
                {
                }
                tmpGeo.Mesh = tmpMesh;

                // Add the tmpGeo geometry to the list

                geometryList.Add(tmpGeo);
            }
            libraryGeometries.Geometry   = geometryList.ToArray();
            daeObject.Library_Geometries = libraryGeometries;
        }