コード例 #1
0
 public CMDLViewer(CGFX Resource, CMDL Model)
 {
     this.Resource = Resource;
     this.Model    = Model;
     InitializeComponent();
     simpleOpenGlControl1.MouseWheel += new MouseEventHandler(simpleOpenGlControl1_MouseWheel);
 }
コード例 #2
0
        public static void N3DS_NW4C_GFX_ToOBJ(String BCMDLPath, int ModelIdx, String OBJPath)
        {
            CGFX c = new CGFX(File.ReadAllBytes(BCMDLPath));

            if (c.Data.Models == null || c.Data.Models.Length <= ModelIdx)
            {
                throw new Exception("Model does not exist in the specified CGFX file!");
            }
            CMDL Model = c.Data.Models[ModelIdx];
            OBJ  o     = Model.ToOBJ();

            o.MTLPath = Path.GetFileNameWithoutExtension(OBJPath) + ".mtl";
            MTL m = Model.ToMTL(Path.GetFileNameWithoutExtension(OBJPath) + "_Tex");

            byte[] d  = o.Write();
            byte[] d2 = m.Write();
            File.Create(OBJPath).Close();
            File.WriteAllBytes(OBJPath, d);
            File.Create(Path.ChangeExtension(OBJPath, "mtl")).Close();
            File.WriteAllBytes(Path.ChangeExtension(OBJPath, "mtl"), d2);
            Directory.CreateDirectory(Path.GetDirectoryName(OBJPath) + "\\" + Path.GetFileNameWithoutExtension(OBJPath) + "_Tex");
            foreach (var v in c.Data.Textures)
            {
                if (!(v is ImageTextureCtr))
                {
                    continue;
                }
                ((ImageTextureCtr)v).GetBitmap().Save(Path.GetDirectoryName(OBJPath) + "\\" + Path.GetFileNameWithoutExtension(OBJPath) + "_Tex\\" + v.Name + ".png");
            }
        }
コード例 #3
0
ファイル: CGFX.cs プロジェクト: clienthax/EveryFileExplorer
            public DATA(EndianBinaryReader er)
            {
                Signature = er.ReadString(Encoding.ASCII, 4);
                if (Signature != "DATA")
                {
                    throw new SignatureNotCorrectException(Signature, "DATA", er.BaseStream.Position - 4);
                }
                SectionSize       = er.ReadUInt32();
                DictionaryEntries = new DictionaryInfo[16];
                for (int i = 0; i < 16; i++)
                {
                    DictionaryEntries[i] = new DictionaryInfo(er);
                }
                Dictionaries = new DICT[16];
                for (int i = 0; i < 16; i++)
                {
                    if (i == 15 && DictionaryEntries[i].NrItems == 0x54434944)
                    {
                        DictionaryEntries[i].NrItems = 0;
                        DictionaryEntries[i].Offset  = 0;
                    }
                    if (DictionaryEntries[i].Offset != 0)
                    {
                        long curpos = er.BaseStream.Position;
                        er.BaseStream.Position = DictionaryEntries[i].Offset;
                        Dictionaries[i]        = new DICT(er);
                        er.BaseStream.Position = curpos;
                    }
                    else
                    {
                        Dictionaries[i] = null;
                    }
                }

                if (Dictionaries[0] != null)
                {
                    Models = new CMDL[Dictionaries[0].Count];
                    for (int i = 0; i < Dictionaries[0].Count; i++)
                    {
                        long curpos = er.BaseStream.Position;
                        er.BaseStream.Position = Dictionaries[0][i].DataOffset;
                        Models[i] = new CMDL(er);
                        er.BaseStream.Position = curpos;
                    }
                }

                if (Dictionaries[1] != null)
                {
                    Textures = new TXOB[Dictionaries[1].Count];
                    for (int i = 0; i < Dictionaries[1].Count; i++)
                    {
                        long curpos = er.BaseStream.Position;
                        er.BaseStream.Position = Dictionaries[1][i].DataOffset;
                        Textures[i]            = TXOB.FromStream(er);             //new TXOB(er);
                        er.BaseStream.Position = curpos;
                    }
                }
                if (Dictionaries[9] != null)
                {
                    SkeletonAnimations = new CANM[Dictionaries[9].Count];
                    for (int i = 0; i < Dictionaries[9].Count; i++)
                    {
                        long curpos = er.BaseStream.Position;
                        er.BaseStream.Position = Dictionaries[9][i].DataOffset;
                        SkeletonAnimations[i]  = new CANM(er);
                        er.BaseStream.Position = curpos;
                    }
                }
                if (Dictionaries[10] != null)
                {
                    MaterialAnimations = new CANM[Dictionaries[10].Count];
                    for (int i = 0; i < Dictionaries[10].Count; i++)
                    {
                        long curpos = er.BaseStream.Position;
                        er.BaseStream.Position = Dictionaries[10][i].DataOffset;
                        MaterialAnimations[i]  = new CANM(er);
                        er.BaseStream.Position = curpos;
                    }
                }
                if (Dictionaries[11] != null)
                {
                    VisibilityAnimations = new CANM[Dictionaries[11].Count];
                    for (int i = 0; i < Dictionaries[11].Count; i++)
                    {
                        long curpos = er.BaseStream.Position;
                        er.BaseStream.Position  = Dictionaries[11][i].DataOffset;
                        VisibilityAnimations[i] = new CANM(er);
                        er.BaseStream.Position  = curpos;
                    }
                }
            }
コード例 #4
0
ファイル: FileIO.cs プロジェクト: TriggerEdge/Ohana3DS-Simple
        /// <summary>
        ///     Exports a file of a given type.
        ///     Formats available to export will depend on the type of the data.
        /// </summary>
        /// <param name="type">Type of the data to be exported</param>
        /// <param name="data">The data</param>
        /// <param name="arguments">Optional arguments to be used by the exporter</param>
        public static void export(fileType type, object data, params int[] arguments)
        {
            string fileName = null;

            switch (type)
            {
            case fileType.container:
                foreach (OContainer.fileEntry file in ((OContainer)data).content)
                {
                    Directory.CreateDirectory(System.Environment.CurrentDirectory + "\\" + ((OContainer)data).name);

                    fileName = Path.Combine(System.Environment.CurrentDirectory + "\\" + ((OContainer)data).name, file.name);
                    string dir = Path.GetDirectoryName(fileName);
                    if (!Directory.Exists(dir))
                    {
                        Directory.CreateDirectory(dir);
                    }

                    byte[] buffer;

                    if (file.loadFromDisk)
                    {
                        buffer = new byte[file.fileLength];
                        ((OContainer)data).data.Seek(file.fileOffset, SeekOrigin.Begin);
                        ((OContainer)data).data.Read(buffer, 0, buffer.Length);
                    }
                    else
                    {
                        buffer = file.data;
                    }

                    if (file.doDecompression)
                    {
                        buffer = LZSS_Ninty.decompress(buffer);
                    }

                    File.WriteAllBytes(fileName, buffer);
                    Console.WriteLine("Extracted file: " + fileName);
                }
                break;

            case fileType.model:
                for (int i = 0; i < ((RenderBase.OModelGroup)data).model.Count; i++)
                {
                    fileName = Path.Combine(System.Environment.CurrentDirectory, ((RenderBase.OModelGroup)data).model[i].name);

                    switch (arguments[0])
                    {
                    case 0: DAE.export(((RenderBase.OModelGroup)data), fileName + ".dae", i); break;

                    case 1: SMD.export(((RenderBase.OModelGroup)data), fileName + ".smd", i); break;

                    case 2: OBJ.export(((RenderBase.OModelGroup)data), fileName + ".obj", i); break;

                    case 3: CMDL.export(((RenderBase.OModelGroup)data), fileName + ".cmdl", i); break;
                    }

                    Console.WriteLine("Extracted file: " + fileName);
                }
                break;

            case fileType.texture:
                if (data.GetType().Equals(typeof(RenderBase.OModelGroup)))       //if extracting textures from a model
                {
                    Directory.CreateDirectory(System.Environment.CurrentDirectory + "\\" + ((RenderBase.OModelGroup)data).model[0].name + "_tex");

                    foreach (RenderBase.OTexture tex in ((RenderBase.OModelGroup)data).texture)
                    {
                        fileName = Path.Combine(System.Environment.CurrentDirectory + "\\" + ((RenderBase.OModelGroup)data).model[0].name + "_tex", tex.name + ".png");
                        tex.texture.Save(fileName);
                        Console.WriteLine("Extracted file: " + fileName);
                    }
                }
                else     // not a model
                {
                    fileName = Path.Combine(System.Environment.CurrentDirectory, ((RenderBase.OTexture)data).name + ".png");
                    ((RenderBase.OTexture)data).texture.Save(fileName);
                    Console.WriteLine("Extracted file: " + fileName);
                }
                break;

            case fileType.skeletalAnimation:
                fileName = Path.Combine(System.Environment.CurrentDirectory, ((RenderBase.OModelGroup)data).model[0].name);
                SMD.export((RenderBase.OModelGroup)data, fileName, arguments[0], arguments[1]);
                Console.WriteLine("Extracted file: " + fileName);
                break;
            }
            Console.WriteLine("Extracting files completed!");
        }
コード例 #5
0
        public static void FromOBJ(CGFX c, String OBJPath, String ModelName = "EFEModel")
        {
            OBJ Model = new OBJ(File.ReadAllBytes(OBJPath));

            if (Model.MTLPath == null)
            {
                throw new Exception("Model without materials not supported!");
            }
            String        MtlPath  = Path.GetDirectoryName(OBJPath) + "\\" + Model.MTLPath;
            MTL           MatLib   = new MTL(File.ReadAllBytes(MtlPath));
            List <String> MatNames = new List <string>();

            foreach (var f in Model.Faces)
            {
                if (!MatNames.Contains(f.Material))
                {
                    MatNames.Add(f.Material);
                }
            }
            Bitmap[] Textures   = new Bitmap[MatLib.Materials.Count];
            int      q          = 0;
            int      NrTextures = 0;

            foreach (var v in MatLib.Materials)
            {
                if (!MatNames.Contains(v.Name))
                {
                    q++; continue;
                }
                if (v.DiffuseMapPath != null)
                {
                    Textures[q] = new Bitmap(new MemoryStream(File.ReadAllBytes(Path.GetDirectoryName(MtlPath) + "\\" + v.DiffuseMapPath)));
                    NrTextures++;
                }
                q++;
            }
            c.Data.Dictionaries[0] = new DICT();
            c.Data.Dictionaries[0].Add(ModelName);
            c.Data.Models    = new CMDL[1];
            c.Data.Models[0] = new CMDL(ModelName);
            CMDL cmdl = c.Data.Models[0];

            //Mesh Node Visibility
            {
                cmdl.NrMeshNodes = 1;
                cmdl.MeshNodeVisibilitiesDict = new DICT();
                cmdl.MeshNodeVisibilitiesDict.Add("CompleteModel");
                cmdl.MeshNodeVisibilities = new CMDL.MeshNodeVisibilityCtr[] { new CMDL.MeshNodeVisibilityCtr("CompleteModel") };
            }
            //Meshes
            {
                cmdl.NrMeshes = (uint)MatNames.Count;
                cmdl.Meshes   = new CMDL.Mesh[MatNames.Count];
                for (int i = 0; i < MatNames.Count; i++)
                {
                    cmdl.Meshes[i] = new CMDL.Mesh();
                    cmdl.Meshes[i].MeshNodeName  = "CompleteModel";
                    cmdl.Meshes[i].MaterialIndex = (uint)i;
                    cmdl.Meshes[i].ShapeIndex    = (uint)i;
                }
            }
            //Materials
            {
                cmdl.NrMaterials   = (uint)MatNames.Count;
                cmdl.MaterialsDict = new DICT();
                cmdl.Materials     = new CMDL.MTOB[MatNames.Count];
                for (int i = 0; i < MatNames.Count; i++)
                {
                    cmdl.MaterialsDict.Add(MatNames[i]);
                    cmdl.Materials[i] = new CMDL.MTOB(MatNames[i]);
                    cmdl.Materials[i].FragShader.TextureCombiners[0].SrcRgb   = 0xEE0;
                    cmdl.Materials[i].FragShader.TextureCombiners[0].SrcAlpha = 0xEE0;
                    for (int qq = 1; qq < 6; qq++)
                    {
                        cmdl.Materials[i].FragShader.TextureCombiners[qq].SrcRgb   = 0xEEF;
                        cmdl.Materials[i].FragShader.TextureCombiners[qq].SrcAlpha = 0xEEF;
                    }
                    Bitmap tex = Textures[MatLib.Materials.IndexOf(MatLib.GetMaterialByName(MatNames[i]))];
                    if (tex != null)
                    {
                        cmdl.Materials[i].NrActiveTextureCoordiators  = 1;
                        cmdl.Materials[i].TextureCoordiators[0].Scale = new LibEveryFileExplorer.Collections.Vector2(1, 1);
                        cmdl.Materials[i].Tex0 = new CMDL.MTOB.TexInfo(MatNames[i]);
                        cmdl.Materials[i].FragShader.TextureCombiners[0].SrcRgb       = 0xE30;
                        cmdl.Materials[i].FragShader.TextureCombiners[0].SrcAlpha     = 0xE30;
                        cmdl.Materials[i].FragShader.TextureCombiners[0].CombineRgb   = 1;
                        cmdl.Materials[i].FragShader.TextureCombiners[0].CombineAlpha = 1;
                    }
                }
            }
            //Shapes
            {
                cmdl.NrShapes = (uint)MatNames.Count;
                cmdl.Shapes   = new CMDL.SeparateDataShape[MatNames.Count];
                for (int i = 0; i < MatNames.Count; i++)
                {
                    cmdl.Shapes[i] = new CMDL.SeparateDataShape();
                    Vector3 min       = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
                    Vector3 max       = new Vector3(float.MinValue, float.MinValue, float.MinValue);
                    int     nrfaces   = 0;
                    bool    texcoords = false;
                    foreach (var f in Model.Faces)
                    {
                        if (f.Material != MatNames[i])
                        {
                            continue;
                        }
                        nrfaces++;
                        if (f.TexCoordIndieces.Count > 0)
                        {
                            texcoords = true;
                        }
                        foreach (var qqq in f.VertexIndieces)
                        {
                            if (Model.Vertices[qqq].X < min.X)
                            {
                                min.X = Model.Vertices[qqq].X;
                            }
                            if (Model.Vertices[qqq].Y < min.Y)
                            {
                                min.Y = Model.Vertices[qqq].Y;
                            }
                            if (Model.Vertices[qqq].Z < min.Z)
                            {
                                min.Z = Model.Vertices[qqq].Z;
                            }
                            if (Model.Vertices[qqq].X > max.X)
                            {
                                max.X = Model.Vertices[qqq].X;
                            }
                            if (Model.Vertices[qqq].Y > max.Y)
                            {
                                max.Y = Model.Vertices[qqq].Y;
                            }
                            if (Model.Vertices[qqq].Z > max.Z)
                            {
                                max.Z = Model.Vertices[qqq].Z;
                            }
                        }
                    }
                    ((OrientedBoundingBox)cmdl.Shapes[i].BoundingBox).CenterPosition = (min + max) / 2;
                    ((OrientedBoundingBox)cmdl.Shapes[i].BoundingBox).Size           = max - min;
                    cmdl.Shapes[i].NrPrimitiveSets  = 1;
                    cmdl.Shapes[i].PrimitiveSets    = new CMDL.SeparateDataShape.PrimitiveSet[1];
                    cmdl.Shapes[i].PrimitiveSets[0] = new CMDL.SeparateDataShape.PrimitiveSet();
                    cmdl.Shapes[i].PrimitiveSets[0].NrPrimitives  = 1;
                    cmdl.Shapes[i].PrimitiveSets[0].Primitives    = new CMDL.SeparateDataShape.PrimitiveSet.Primitive[1];
                    cmdl.Shapes[i].PrimitiveSets[0].Primitives[0] = new CMDL.SeparateDataShape.PrimitiveSet.Primitive();
                    cmdl.Shapes[i].PrimitiveSets[0].Primitives[0].NrBufferObjects = 1;
                    cmdl.Shapes[i].PrimitiveSets[0].Primitives[0].BufferObjects   = new uint[] { 0 };
                    cmdl.Shapes[i].PrimitiveSets[0].Primitives[0].NrIndexStreams  = 1;
                    cmdl.Shapes[i].PrimitiveSets[0].Primitives[0].IndexStreams    = new CMDL.SeparateDataShape.PrimitiveSet.Primitive.IndexStreamCtr[1];
                    cmdl.Shapes[i].PrimitiveSets[0].Primitives[0].IndexStreams[0] = new CMDL.SeparateDataShape.PrimitiveSet.Primitive.IndexStreamCtr();
                    if (nrfaces * 3 > 255)
                    {
                        cmdl.Shapes[i].PrimitiveSets[0].Primitives[0].IndexStreams[0].FormatType = 0x1403;
                    }
                    cmdl.Shapes[i].PrimitiveSets[0].Primitives[0].IndexStreams[0].FaceDataLength = (uint)(nrfaces * 3 * ((nrfaces * 3 > 255) ? 2 : 1));
                    cmdl.Shapes[i].PrimitiveSets[0].Primitives[0].IndexStreams[0].FaceData       = new byte[cmdl.Shapes[i].PrimitiveSets[0].Primitives[0].IndexStreams[0].FaceDataLength];
                    int offs = 0;
                    int idx  = 0;
                    foreach (var f in Model.Faces)
                    {
                        if (f.Material != MatNames[i])
                        {
                            continue;
                        }
                        if (nrfaces * 3 > 255)
                        {
                            IOUtil.WriteU16LE(cmdl.Shapes[i].PrimitiveSets[0].Primitives[0].IndexStreams[0].FaceData, offs, (ushort)idx);
                            IOUtil.WriteU16LE(cmdl.Shapes[i].PrimitiveSets[0].Primitives[0].IndexStreams[0].FaceData, offs + 2, (ushort)(idx + 1));
                            IOUtil.WriteU16LE(cmdl.Shapes[i].PrimitiveSets[0].Primitives[0].IndexStreams[0].FaceData, offs + 4, (ushort)(idx + 2));
                            offs += 2 * 3;
                        }
                        else
                        {
                            cmdl.Shapes[i].PrimitiveSets[0].Primitives[0].IndexStreams[0].FaceData[idx]     = (byte)idx;
                            cmdl.Shapes[i].PrimitiveSets[0].Primitives[0].IndexStreams[0].FaceData[idx + 1] = (byte)(idx + 1);
                            cmdl.Shapes[i].PrimitiveSets[0].Primitives[0].IndexStreams[0].FaceData[idx + 2] = (byte)(idx + 2);
                            offs += 3;
                        }
                        idx += 3;
                    }
                    cmdl.Shapes[i].NrVertexAttributes = 2;
                    cmdl.Shapes[i].VertexAttributes   = new CMDL.SeparateDataShape.VertexAttributeCtr[2];
                    //interleaved
                    cmdl.Shapes[i].VertexAttributes[0] = new CMDL.SeparateDataShape.InterleavedVertexStreamCtr();
                    ((CMDL.SeparateDataShape.InterleavedVertexStreamCtr)cmdl.Shapes[i].VertexAttributes[0]).NrVertexStreams  = (texcoords ? 2u : 1u);
                    ((CMDL.SeparateDataShape.InterleavedVertexStreamCtr)cmdl.Shapes[i].VertexAttributes[0]).VertexStreams    = new CMDL.SeparateDataShape.InterleavedVertexStreamCtr.VertexStreamCtr[texcoords ? 2 : 1];
                    ((CMDL.SeparateDataShape.InterleavedVertexStreamCtr)cmdl.Shapes[i].VertexAttributes[0]).VertexStreams[0] = new CMDL.SeparateDataShape.InterleavedVertexStreamCtr.VertexStreamCtr(CMDL.SeparateDataShape.VertexAttributeCtr.VertexAttributeUsage.Position, CMDL.SeparateDataShape.DataType.GL_FLOAT, 3, 0);
                    if (texcoords)
                    {
                        ((CMDL.SeparateDataShape.InterleavedVertexStreamCtr)cmdl.Shapes[i].VertexAttributes[0]).VertexStreams[1] = new CMDL.SeparateDataShape.InterleavedVertexStreamCtr.VertexStreamCtr(CMDL.SeparateDataShape.VertexAttributeCtr.VertexAttributeUsage.TextureCoordinate0, CMDL.SeparateDataShape.DataType.GL_FLOAT, 2, 12);
                    }
                    ((CMDL.SeparateDataShape.InterleavedVertexStreamCtr)cmdl.Shapes[i].VertexAttributes[0]).VertexDataEntrySize = (texcoords ? 20u : 12u);
                    byte[] Result = new byte[nrfaces * 3 * ((CMDL.SeparateDataShape.InterleavedVertexStreamCtr)cmdl.Shapes[i].VertexAttributes[0]).VertexDataEntrySize];
                    offs = 0;
                    foreach (var f in Model.Faces)
                    {
                        if (f.Material != MatNames[i])
                        {
                            continue;
                        }
                        for (int qqq = 0; qqq < 3; qqq++)
                        {
                            Vector3 Pos = Model.Vertices[f.VertexIndieces[qqq]];
                            IOUtil.WriteSingleLE(Result, offs, Pos.X);
                            IOUtil.WriteSingleLE(Result, offs + 4, Pos.Y);
                            IOUtil.WriteSingleLE(Result, offs + 8, Pos.Z);
                            offs += 12;
                            if (texcoords)
                            {
                                Vector2 Tex = Model.TexCoords[f.TexCoordIndieces[qqq]];
                                IOUtil.WriteSingleLE(Result, offs, Tex.X);
                                IOUtil.WriteSingleLE(Result, offs + 4, Tex.Y);
                                offs += 8;
                            }
                        }
                    }
                    ((CMDL.SeparateDataShape.InterleavedVertexStreamCtr)cmdl.Shapes[i].VertexAttributes[0]).VertexStreamLength = (uint)Result.Length;
                    ((CMDL.SeparateDataShape.InterleavedVertexStreamCtr)cmdl.Shapes[i].VertexAttributes[0]).VertexStream       = Result;
                    //color
                    cmdl.Shapes[i].VertexAttributes[1] = new CMDL.SeparateDataShape.VertexParamAttributeCtr(CMDL.SeparateDataShape.VertexAttributeCtr.VertexAttributeUsage.Color, 1, 1, 1, MatLib.GetMaterialByName(MatNames[i]).Alpha);
                }
            }
            if (NrTextures != 0)
            {
                c.Data.Dictionaries[1] = new DICT();
                c.Data.Textures        = new TXOB[NrTextures];
                int qqq = 0;
                int idx = 0;
                foreach (Bitmap b in Textures)
                {
                    if (b == null)
                    {
                        qqq++; continue;
                    }
                    c.Data.Dictionaries[1].Add(MatLib.Materials[qqq].Name);
                    c.Data.Textures[idx] = new ImageTextureCtr(MatLib.Materials[qqq].Name, b, GPU.Textures.ImageFormat.ETC1A4);
                    idx++;
                    qqq++;
                }
            }
        }
コード例 #6
0
        void RenderModel()
        {
            CMDL m = Model;

            //foreach (_3DS.CGFX.DATA.CMDL m in cgfx.Data.Models)
            //{
            //int i = 0;
            foreach (var mm in m.Meshes)
            {
                var     vv = m.Shapes[mm.ShapeIndex];
                Polygon p  = vv.GetVertexData(m);

                var mat = m.Materials[mm.MaterialIndex];
                mat.FragShader.AlphaTest.GlApply();
                mat.FragmentOperation.BlendOperation.GlApply();

                Gl.glMatrixMode(Gl.GL_TEXTURE);
                if (mat.Tex0 != null)
                {
                    Gl.glActiveTexture(Gl.GL_TEXTURE0);
                    Gl.glBindTexture(Gl.GL_TEXTURE_2D, mm.MaterialIndex * 4 + 0 + 1);
                    Gl.glEnable(Gl.GL_TEXTURE_2D);
                    Gl.glLoadIdentity();
                    float[] mtx = new float[16];
                    Array.Copy(mat.TextureCoordiators[0].Matrix, mtx, 4 * 3);
                    mtx[15] = 1;
                    mtx[12] = mtx[3];
                    mtx[13] = mtx[7];
                    mtx[14] = mtx[11];
                    mtx[3]  = 0;
                    mtx[7]  = 0;
                    mtx[11] = 0;
                    Gl.glLoadMatrixf(mtx);
                    //if (mat.TextureCoordiators[0].MappingMethod == 2)
                    //{
                    //	Gl.glTexGeni(Gl.GL_S, Gl.GL_TEXTURE_GEN_MODE, Gl.GL_SPHERE_MAP);
                    //	Gl.glTexGeni(Gl.GL_T, Gl.GL_TEXTURE_GEN_MODE, Gl.GL_SPHERE_MAP);
                    //}
                    //Gl.glTranslatef(mat.TextureCoordiators[0].Scale.X / mat.TextureCoordiators[0].Translate.X, mat.TextureCoordiators[0].Translate.Y * mat.TextureCoordiators[0].Scale.Y, 0);
                    //Gl.glRotatef(mat.TextureCoordiators[0].Rotate, 0, 1, 0);
                    //Gl.glScalef(mat.TextureCoordiators[0].Scale.X, mat.TextureCoordiators[0].Scale.Y, 1);
                }
                if (mat.Tex1 != null)
                {
                    Gl.glActiveTexture(Gl.GL_TEXTURE1);
                    Gl.glBindTexture(Gl.GL_TEXTURE_2D, mm.MaterialIndex * 4 + 1 + 1);
                    Gl.glEnable(Gl.GL_TEXTURE_2D);
                    Gl.glLoadIdentity();
                    float[] mtx = new float[16];
                    Array.Copy(mat.TextureCoordiators[1].Matrix, mtx, 4 * 3);
                    mtx[15] = 1;
                    mtx[12] = mtx[3];
                    mtx[13] = mtx[7];
                    mtx[14] = mtx[11];
                    mtx[3]  = 0;
                    mtx[7]  = 0;
                    mtx[11] = 0;
                    Gl.glLoadMatrixf(mtx);
                    //if (mat.TextureCoordiators[1].MappingMethod == 2)
                    //{
                    //	Gl.glTexGeni(Gl.GL_S, Gl.GL_TEXTURE_GEN_MODE, Gl.GL_SPHERE_MAP);
                    //	Gl.glTexGeni(Gl.GL_T, Gl.GL_TEXTURE_GEN_MODE, Gl.GL_SPHERE_MAP);
                    //}
                }
                if (mat.Tex2 != null)
                {
                    Gl.glActiveTexture(Gl.GL_TEXTURE2);
                    Gl.glBindTexture(Gl.GL_TEXTURE_2D, mm.MaterialIndex * 4 + 2 + 1);
                    Gl.glEnable(Gl.GL_TEXTURE_2D);
                    Gl.glLoadIdentity();
                    float[] mtx = new float[16];
                    Array.Copy(mat.TextureCoordiators[2].Matrix, mtx, 4 * 3);
                    mtx[15] = 1;
                    mtx[12] = mtx[3];
                    mtx[13] = mtx[7];
                    mtx[14] = mtx[11];
                    mtx[3]  = 0;
                    mtx[7]  = 0;
                    mtx[11] = 0;
                    Gl.glLoadMatrixf(mtx);
                    //if (mat.TextureCoordiators[2].MappingMethod == 2)
                    //{
                    //	Gl.glTexGeni(Gl.GL_S, Gl.GL_TEXTURE_GEN_MODE, Gl.GL_SPHERE_MAP);
                    //	Gl.glTexGeni(Gl.GL_T, Gl.GL_TEXTURE_GEN_MODE, Gl.GL_SPHERE_MAP);
                    //}
                }

                /*if (mat.Tex3 != null)
                 * {
                 *      MessageBox.Show("Tex3!!!!!!!!!!!");
                 *      Gl.glActiveTexture(Gl.GL_TEXTURE3);
                 *      Gl.glBindTexture(Gl.GL_TEXTURE_2D, mm.MaterialIndex * 4 + 3 + 1);
                 *      Gl.glEnable(Gl.GL_TEXTURE_2D);
                 *      Gl.glLoadIdentity();
                 * }*/
                Gl.glMatrixMode(Gl.GL_MODELVIEW);
                Gl.glPushMatrix();
                Gl.glTranslatef(vv.PositionOffset.X, vv.PositionOffset.Y, vv.PositionOffset.Z);

                if (glversion.StartsWith("2.") && Shaders[mm.MaterialIndex] == null)
                {
                    List <int> tex = new List <int>();
                    if (mat.Tex0 != null)
                    {
                        tex.Add((int)mm.MaterialIndex * 4 + 0 + 1);
                    }
                    else
                    {
                        tex.Add(0);
                    }
                    if (mat.Tex1 != null)
                    {
                        tex.Add((int)mm.MaterialIndex * 4 + 1 + 1);
                    }
                    else
                    {
                        tex.Add(0);
                    }
                    if (mat.Tex2 != null)
                    {
                        tex.Add((int)mm.MaterialIndex * 4 + 2 + 1);
                    }
                    else
                    {
                        tex.Add(0);
                    }
                    //if (mat.Tex3 != null) tex.Add((int)mm.MaterialIndex * 4 + 3 + 1);
                    /*else */
                    tex.Add(0);

                    Shaders[mm.MaterialIndex] = new CGFXShader(mat, tex.ToArray());
                    Shaders[mm.MaterialIndex].Compile();
                }
                if (glversion.StartsWith("2."))
                {
                    Shaders[mm.MaterialIndex].Enable();
                }

                //Gl.glMaterialfv(Gl.GL_FRONT_AND_BACK, Gl.GL_EMISSION, new float[] { mat.Emission_2.R / 255f, mat.Emission_2.G / 255f, mat.Emission_2.B / 255f, mat.Emission_2.A / 255f });
                //Gl.glMaterialfv(Gl.GL_FRONT_AND_BACK, Gl.GL_AMBIENT, new float[] { mat.Ambient_1.R / 255f, mat.Ambient_1.G / 255f, mat.Ambient_1.B / 255f, mat.Ambient_1.A / 255f });
                //Gl.glMaterialfv(Gl.GL_FRONT_AND_BACK, Gl.GL_DIFFUSE, new float[] { mat.Diffuse_2.R / 255f, mat.Diffuse_2.G / 255f, mat.Diffuse_2.B / 255f, mat.Diffuse_2.A / 255f });
                //Gl.glColorMaterial(Gl.GL_FRONT_AND_BACK, Gl.GL_DIFFUSE);
                //Gl.glEnable(Gl.GL_COLOR_MATERIAL);

                foreach (var q in vv.PrimitiveSets[0].Primitives[0].IndexStreams)
                {
                    Vector3[] defs = q.GetFaceData();

                    Gl.glBegin(Gl.GL_TRIANGLES);
                    foreach (Vector3 d in defs)
                    {
                        if (p.Normals != null)
                        {
                            Gl.glNormal3f(p.Normals[(int)d.X].X, p.Normals[(int)d.X].Y, p.Normals[(int)d.X].Z);
                        }
                        if (p.Colors != null)
                        {
                            Gl.glColor4f(p.Colors[(int)d.X].R / 255f, p.Colors[(int)d.X].G / 255f, p.Colors[(int)d.X].B / 255f, p.Colors[(int)d.X].A / 255f);
                        }
                        else
                        {
                            Gl.glColor4f(1, 1, 1, 1);
                        }
                        if (p.TexCoords != null)
                        {
                            Gl.glMultiTexCoord2f(Gl.GL_TEXTURE0, p.TexCoords[(int)d.X].X, p.TexCoords[(int)d.X].Y);
                        }
                        if (p.TexCoords2 != null)
                        {
                            Gl.glMultiTexCoord2f(Gl.GL_TEXTURE1, p.TexCoords2[(int)d.X].X, p.TexCoords2[(int)d.X].Y);
                        }
                        if (p.TexCoords3 != null)
                        {
                            Gl.glMultiTexCoord2f(Gl.GL_TEXTURE2, p.TexCoords3[(int)d.X].X, p.TexCoords3[(int)d.X].Y);
                        }
                        Gl.glVertex3f(p.Vertex[(int)d.X].X, p.Vertex[(int)d.X].Y, p.Vertex[(int)d.X].Z);

                        if (p.Normals != null)
                        {
                            Gl.glNormal3f(p.Normals[(int)d.Y].X, p.Normals[(int)d.Y].Y, p.Normals[(int)d.Y].Z);
                        }
                        if (p.Colors != null)
                        {
                            Gl.glColor4f(p.Colors[(int)d.Y].R / 255f, p.Colors[(int)d.Y].G / 255f, p.Colors[(int)d.Y].B / 255f, p.Colors[(int)d.Y].A / 255f);
                        }
                        else
                        {
                            Gl.glColor4f(1, 1, 1, 1);
                        }
                        if (p.TexCoords != null)
                        {
                            Gl.glMultiTexCoord2f(Gl.GL_TEXTURE0, p.TexCoords[(int)d.Y].X, p.TexCoords[(int)d.Y].Y);
                        }
                        if (p.TexCoords2 != null)
                        {
                            Gl.glMultiTexCoord2f(Gl.GL_TEXTURE1, p.TexCoords2[(int)d.Y].X, p.TexCoords2[(int)d.Y].Y);
                        }
                        if (p.TexCoords3 != null)
                        {
                            Gl.glMultiTexCoord2f(Gl.GL_TEXTURE2, p.TexCoords3[(int)d.Y].X, p.TexCoords3[(int)d.Y].Y);
                        }
                        Gl.glVertex3f(p.Vertex[(int)d.Y].X, p.Vertex[(int)d.Y].Y, p.Vertex[(int)d.Y].Z);

                        if (p.Normals != null)
                        {
                            Gl.glNormal3f(p.Normals[(int)d.Z].X, p.Normals[(int)d.Z].Y, p.Normals[(int)d.Z].Z);
                        }
                        if (p.Colors != null)
                        {
                            Gl.glColor4f(p.Colors[(int)d.Z].R / 255f, p.Colors[(int)d.Z].G / 255f, p.Colors[(int)d.Z].B / 255f, p.Colors[(int)d.Z].A / 255f);
                        }
                        else
                        {
                            Gl.glColor4f(1, 1, 1, 1);
                        }
                        if (p.TexCoords != null)
                        {
                            Gl.glMultiTexCoord2f(Gl.GL_TEXTURE0, p.TexCoords[(int)d.Z].X, p.TexCoords[(int)d.Z].Y);
                        }
                        if (p.TexCoords2 != null)
                        {
                            Gl.glMultiTexCoord2f(Gl.GL_TEXTURE1, p.TexCoords2[(int)d.Z].X, p.TexCoords2[(int)d.Z].Y);
                        }
                        if (p.TexCoords3 != null)
                        {
                            Gl.glMultiTexCoord2f(Gl.GL_TEXTURE2, p.TexCoords3[(int)d.Z].X, p.TexCoords3[(int)d.Z].Y);
                        }
                        Gl.glVertex3f(p.Vertex[(int)d.Z].X, p.Vertex[(int)d.Z].Y, p.Vertex[(int)d.Z].Z);
                    }
                    Gl.glEnd();
                }
                Gl.glPopMatrix();
            }
        }
コード例 #7
0
        private void ok()
        {
            if (!Directory.Exists(TxtOutFolder.Text))
            {
                MessageBox.Show("Invalid output directory!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            Settings.Default.meOutFolder = TxtOutFolder.Text;

            int format = 0;

            if (RadioSMD.Checked)
            {
                format = 1;
            }
            else if (RadioOBJ.Checked)
            {
                format = 2;
            }
            else if (RadioCMDL.Checked)
            {
                format = 3;
            }

            Settings.Default.meFormat        = format;
            Settings.Default.meExportAllMdls = ChkExportAllModels.Checked;
            Settings.Default.meExportAllAnms = ChkExportAllAnimations.Checked;

            Settings.Default.Save();

            if (ChkExportAllModels.Checked && !ChkExportAllAnimations.Checked)
            {
                for (int i = 0; i < mdls.model.Count; i++)
                {
                    string fileName = Path.Combine(TxtOutFolder.Text, mdls.model[i].name);

                    switch (format)
                    {
                    case 0: DAE.export(mdls, fileName + ".dae", i); break;

                    case 1: SMD.export(mdls, fileName + ".smd", i); break;

                    case 2: OBJ.export(mdls, fileName + ".obj", i); break;

                    case 3: CMDL.export(mdls, fileName + ".cmdl", i); break;
                    }
                }
            }
            else if (mdlIndex > -1)
            {
                string fileName = Path.Combine(TxtOutFolder.Text, TxtModelName.Text);

                switch (format)
                {
                case 0: DAE.export(mdls, fileName + ".dae", mdlIndex); break;

                case 1:
                    SMD.export(mdls, fileName + ".smd", mdlIndex);
                    if (ChkExportAllAnimations.Checked)
                    {
                        for (int i = 0; i < mdls.skeletalAnimation.list.Count; i++)
                        {
                            string name = mdls.skeletalAnimation.list[i].name + ".smd";
                            SMD.export(mdls, Path.Combine(TxtOutFolder.Text, name), mdlIndex, i);
                        }
                    }
                    break;

                case 2: OBJ.export(mdls, fileName + ".obj", mdlIndex); break;

                case 3: CMDL.export(mdls, fileName + ".cmdl", mdlIndex); break;
                }
            }

            Close();
        }