예제 #1
0
        public static string ToSourceFile(List <SegaSaturnPolygonData> list, bool exportTextures, bool preprocessorInclusionProtection, string filename)
        {
            StringBuilder sb = new StringBuilder();

            string defaultName = Regex.Replace(Path.GetFileNameWithoutExtension(filename), "[^A-Za-z0-9]", "");

            if (preprocessorInclusionProtection)
            {
                #region Header

                sb.AppendLine("/*");
                sb.AppendLine(String.Format("   3D model generated by {0}", Editor.Instance.Text));
                sb.AppendLine("*/");
                sb.AppendLine();
                sb.AppendLine(String.Format("#ifndef __3D_MODEL{0}_H__", defaultName.ToUpperInvariant()));
                sb.AppendLine(String.Format("# define __3D_MODEL{0}_H__", defaultName.ToUpperInvariant()));
                sb.AppendLine();

                #endregion
            }

            foreach (SegaSaturnPolygonData pd in list)
            {
                sb.Append(SegaSaturnConverter.ToSourceFile(pd, exportTextures, false));
            }

            #region load_mesh

            if (list.Any(item => item.Textures.Count > 0))
            {
                sb.AppendLine("/* Call this function in you code to load all textures needed by the mesh */");
                sb.AppendLine(String.Format("static __jo_force_inline void       load_{0}_mesh_textures(void)", defaultName.ToLowerInvariant()));
                sb.AppendLine("{");
                foreach (SegaSaturnPolygonData pd in list)
                {
                    if (pd.Textures.Count > 0)
                    {
                        sb.AppendLine(String.Format("\tload_{0}_textures();", pd.Name.ToLowerInvariant()));
                    }
                }
                sb.AppendLine("}");
                sb.AppendLine();
            }

            #endregion

            #region display_mesh

            if (list.Count > 0)
            {
                sb.AppendLine("/* Call this function in you code to display all objects */");
                sb.AppendLine(String.Format("static __jo_force_inline void       display_{0}_mesh(void)", defaultName.ToLowerInvariant()));
                sb.AppendLine("{");
                for (int i = 0; i < list.Count; ++i)
                {
                    if (i == 1)
                    {
                        sb.AppendLine("\t/* Add matrix transformation here like jo_3d_rotate_matrix() */");
                    }
                    sb.AppendLine(String.Format("\tjo_3d_mesh_draw(&Mesh{0});", list[i].Name));
                }
                sb.AppendLine("}");
                sb.AppendLine();
            }

            #endregion

            if (preprocessorInclusionProtection)
            {
                #region Footer

                sb.AppendLine(String.Format("#endif /* !__3D_MODEL{0}_H__ */", defaultName.ToUpperInvariant()));

                #endregion
            }

            return(sb.ToString());
        }
예제 #2
0
        public static string ToSourceFile(SegaSaturnPolygonData pd, bool exportTextures, bool preprocessorInclusionProtection)
        {
            if (String.IsNullOrWhiteSpace(pd.Name))
            {
                pd.Name = "Unnamed";
            }
            StringBuilder sb = new StringBuilder();

            if (preprocessorInclusionProtection)
            {
                #region Header

                sb.AppendLine("/*");
                sb.AppendLine(String.Format("   3D model generated by {0}", Editor.Instance.Text));
                sb.AppendLine("*/");
                sb.AppendLine();
                sb.AppendLine(String.Format("#ifndef __{0}_H__", pd.Name.ToUpperInvariant()));
                sb.AppendLine(String.Format("# define __{0}_H__", pd.Name.ToUpperInvariant()));
                sb.AppendLine();

                #endregion
            }

            #region Points

            sb.AppendLine(String.Format("static POINT    Point{0}[] =", pd.Name));
            sb.AppendLine("{");
            foreach (SegaSaturnPoint point in pd.Points)
            {
                sb.AppendLine(String.Format("\t{{{0}, {1}, {2}}},", point.X, point.Y, point.Z));
            }
            sb.AppendLine("};");
            sb.AppendLine();

            #endregion

            #region Polygons

            sb.AppendLine(String.Format("static POLYGON    Polygon{0}[] =", pd.Name));
            sb.AppendLine("{");
            foreach (SegaSaturnPolygon point in pd.Polygons)
            {
                string normal   = String.Format("{{{{{0}, {1}, {2}}}", point.Normal.X, point.Normal.Y, point.Normal.Z);
                string vertices = String.Format("{{{0}, {1}, {2}, {3}}}}}", point.Vertices.Vertice1, point.Vertices.Vertice2, point.Vertices.Vertice3, point.Vertices.Vertice4);
                sb.AppendLine(String.Format("\t{0}, {1},", normal, vertices));
            }
            sb.AppendLine("};");
            sb.AppendLine();

            #endregion

            #region Attributes

            sb.AppendLine(String.Format("static ATTR    Attribute{0}[] =", pd.Name));
            sb.AppendLine("{");
            foreach (SegaSaturnAttribute attr in pd.Attributes)
            {
                sb.AppendLine(String.Format("\tATTRIBUTE({0}, {1}, {2}, {3}, CL32KRGB | No_Gouraud, {4}, {5}, {6}),",
                                            attr.FrontBackPlane == SegaSaturnAttribute.FrontBackPlaneEnum.Dual ? "Dual_Plane" : "Single_Plane",
                                            attr.ZSortSpecification == SegaSaturnAttribute.ZSortSpecificationEnum.Bfr ? "SORT_BFR" : attr.ZSortSpecification == SegaSaturnAttribute.ZSortSpecificationEnum.Cen ? "SORT_CEN" : attr.ZSortSpecification == SegaSaturnAttribute.ZSortSpecificationEnum.Max ? "SORT_MAX" : "SORT_MIN",
                                            attr.SpriteIndex.HasValue ? attr.SpriteIndex.Value.ToString() : "No_Texture",
                                            attr.Color != null ? attr.Color.HexString : "No_Palet",
                                            attr.UseScreenDoors ? "CL32KRGB | MESHon" : "CL32KRGB | MESHoff",
                                            attr.SpriteIndex.HasValue ? "sprNoflip" : "sprPolygon",
                                            attr.UseLight ? "UseLight" : "No_Option"));
            }
            sb.AppendLine("};");
            sb.AppendLine();

            #endregion

            #region Textures

            if (exportTextures && pd.Textures != null && pd.Textures.Count > 0)
            {
                foreach (SegaSaturnTexture texture in pd.Textures)
                {
                    sb.Append(SegaSaturnConverter.ToSourceFile(texture, false));
                }
                sb.AppendLine(String.Format("static __jo_force_inline void       load_{0}_textures(void)", pd.Name.ToLowerInvariant()));
                sb.AppendLine("{");
                sb.AppendLine("\tint\ti;");
                sb.AppendLine("\tint\t\tfirst_sprite_id;");
                sb.AppendLine();
                bool first = true;
                foreach (SegaSaturnTexture texture in pd.Textures)
                {
                    if (String.IsNullOrWhiteSpace(texture.Name))
                    {
                        texture.Name = "Unnamed";
                    }
                    if (first)
                    {
                        sb.AppendLine(String.Format("\tfirst_sprite_id = jo_sprite_add(&Sprite{0});", texture.Name));
                    }
                    else
                    {
                        sb.AppendLine(String.Format("\tjo_sprite_add(&Sprite{0});", texture.Name));
                    }
                    first = false;
                }
                sb.AppendLine(String.Format("\tfor (i = 0; i < {0}; ++i)", pd.Attributes.Count));
                sb.AppendLine(String.Format("\t\tAttribute{0}[i].texno += first_sprite_id;", pd.Name));

                sb.AppendLine("}");
                sb.AppendLine();
            }

            #endregion

            #region PDATA

            sb.AppendLine(String.Format("jo_3d_mesh    Mesh{0} =", pd.Name));
            sb.AppendLine("{");
            sb.AppendLine("\t.data =");
            sb.AppendLine("\t{");
            sb.AppendLine(String.Format("\t\tPoint{0},", pd.Name));
            sb.AppendLine(String.Format("\t\t{0},", pd.Points.Count));
            sb.AppendLine(String.Format("\t\tPolygon{0},", pd.Name));
            sb.AppendLine(String.Format("\t\t{0},", pd.Polygons.Count));
            sb.AppendLine(String.Format("\t\tAttribute{0}", pd.Name));
            sb.AppendLine("\t}");
            sb.AppendLine("};");
            sb.AppendLine();

            #endregion

            if (preprocessorInclusionProtection)
            {
                #region Footer

                sb.AppendLine(String.Format("#endif /* !__{0}_H__ */", pd.Name.ToUpperInvariant()));

                #endregion
            }

            return(sb.ToString());
        }