示例#1
0
        public void DrawModel(ModelObjectModel model)
        {
            if (model == null)
            {
                return;
            }

            string basedir = Environment.CurrentDirectory;

            if (model.Accessor is FileAccessor)
            {
                basedir = System.IO.Path.GetDirectoryName((model.Accessor as FileAccessor).FileName);
            }

            if (mvarRenderModels)
            {
                #region Load Materials
                if (!model.MaterialsLoaded && !model.MaterialsLoading)
                {
                    model.MaterialsLoading = true;

                    if (model.Accessor is FileAccessor)
                    {
                        Console.WriteLine("Loading materials for model: \"" + (model.Accessor as FileAccessor).FileName + "\"");
                    }
                    else
                    {
                        Console.WriteLine("Loading materials for model");
                    }

                    foreach (ModelMaterial mat1 in model.Materials)
                    {
                        Console.Write("Loading material " + model.Materials.IndexOf(mat1).ToString() + " / " + model.Materials.Count.ToString() + "... ");

                        foreach (ModelTexture texture in mat1.Textures)
                        {
                            if (texture.TexturePicture != null)
                            {
                                // Image has already been preloaded
                                if (texture.TextureID == null)
                                {
                                    // Store texture ID for this texture
                                    Texture t1 = Texture.FromPicture(texture.TexturePicture);
                                    texture.TextureID = t1.ID;
                                }
                            }
                            else
                            {
                                if (!String.IsNullOrEmpty(texture.TextureFileName))
                                {
                                    if (texture.Flags != ModelTextureFlags.None)
                                    {
                                        if (texture.TextureID == null)
                                        {
                                            // Store texture ID for this texture
                                            string textureImageFullFileName = UniversalEditor.Common.Path.MakeAbsolutePath(texture.TextureFileName, basedir);
                                            if (!System.IO.File.Exists(textureImageFullFileName))
                                            {
                                                Console.WriteLine("texture image not found: " + textureImageFullFileName);
                                                continue;
                                            }

                                            Texture t1 = Texture.FromFile(textureImageFullFileName);
                                            texture.TextureID = t1.ID;
                                        }
                                    }
                                }
                            }

                            if ((texture.Flags & (ModelTextureFlags.Map | ModelTextureFlags.AddMap)) != ModelTextureFlags.None)
                            {
                                if (texture.TexturePicture != null)
                                {
                                    // Image has already been preloaded
                                    if (texture.MapID == null)
                                    {
                                        // Store texture ID for this texture
                                        Texture t1 = Texture.FromPicture(texture.TexturePicture);
                                        texture.MapID = t1.ID;
                                    }
                                }
                                else
                                {
                                    if (!String.IsNullOrEmpty(texture.MapFileName))
                                    {
                                        if (texture.Flags != ModelTextureFlags.None)
                                        {
                                            if (texture.TextureID == null)
                                            {
                                                // Store texture ID for this texture
                                                string textureImageFullFileName = UniversalEditor.Common.Path.MakeAbsolutePath(texture.MapFileName, basedir);
                                                if (!System.IO.File.Exists(textureImageFullFileName))
                                                {
                                                    Console.WriteLine("texture image not found: " + textureImageFullFileName);
                                                    continue;
                                                }

                                                Texture t1 = Texture.FromFile(textureImageFullFileName);
                                                texture.MapID = t1.ID;
                                            }
                                        }
                                    }
                                }
                            }
                        }

                        Console.WriteLine("done!");
                    }
                    model.MaterialsLoading = false;
                    model.MaterialsLoaded  = true;
                }
                #endregion

                int vertexIndex = 0;
                if (model.Materials.Count > 0)
                {
                    foreach (ModelMaterial mat in model.Materials)
                    {
                        // update the texture index
                        if (mat.TextureIndex < (mat.Textures.Count - 1))
                        {
                            mat.TextureIndex++;
                        }
                        else
                        {
                            mat.TextureIndex = 0;
                        }

                        // 輪郭・影有無で色指定方法を変える
                        // Contour - How to specify color change with or without shadow

                        // 半透明でなければポリゴン裏面を無効にする
                        // To disable the reverse side must be semi-transparent polygons

                        /*
                         * if (mat.DiffuseColor.Alpha >= 255)
                         * {
                         *      CullingMode = Caltron.CullingMode.Disabled;
                         * }
                         * else
                         * {
                         *      CullingMode = Caltron.CullingMode.Front;
                         * }
                         */

                        // テクスチャ・スフィアマップの処理
                        // Processing of the texture map sphere
                        ModelTextureFlags fTexFlag = ModelTextureFlags.None;
                        if (mat.TextureIndex > -1 && mat.Textures.Count > 0)
                        {
                            fTexFlag = mat.Textures[mat.TextureIndex].Flags;
                        }

                        if (((fTexFlag & ModelTextureFlags.Texture) == ModelTextureFlags.Texture) && mat.Textures[mat.TextureIndex].TextureID != null)
                        {
                            // テクスチャありならBindする
                            // Bind the texture to be there
                            Texture = Texture.FromID(mat.Textures[mat.TextureIndex].TextureID.Value);

                            EnableTexturing          = true;
                            EnableTextureGenerationS = false;
                            EnableTextureGenerationT = false;
                        }
                        else if ((((fTexFlag & ModelTextureFlags.Map) == ModelTextureFlags.Map) || ((fTexFlag & ModelTextureFlags.AddMap) == ModelTextureFlags.AddMap)) && (mat.Textures[mat.TextureIndex].MapID != null))
                        {
                            // スフィアマップありならBindする
                            // Bind sphere map, if it exists
                            // Texture = Texture.FromID(mat.MapID.Value);
                            Texture = Texture.FromID(mat.Textures[mat.TextureIndex].MapID.Value);

                            EnableTexturing          = false;
                            EnableTextureGenerationS = true;
                            EnableTextureGenerationT = true;
                        }
                        else
                        {
                            // テクスチャもスフィアマップもなし
                            // A texture map sphere without any

                            EnableTexturing          = false;
                            EnableTextureGenerationS = false;
                            EnableTextureGenerationT = false;
                        }

                        if (!mat.AlwaysLight && (mat.EdgeFlag || model.IgnoreEdgeFlag))
                        {
                            // 輪郭・影有りのときは照明を有効にする
                            // Contour - When the shadow is there to enable the lighting
                            Internal.OpenGL.Methods.glMaterialfv(Internal.OpenGL.Constants.GLFace.Both, Internal.OpenGL.Constants.GL_DIFFUSE, new float[] { (float)mat.DiffuseColor.Red, (float)mat.DiffuseColor.Green, (float)mat.DiffuseColor.Blue, (float)mat.DiffuseColor.Alpha });
                            Internal.OpenGL.Methods.glMaterialfv(Internal.OpenGL.Constants.GLFace.Both, Internal.OpenGL.Constants.GL_AMBIENT, new float[] { (float)mat.AmbientColor.Red, (float)mat.AmbientColor.Green, (float)mat.AmbientColor.Blue, (float)mat.AmbientColor.Alpha });
                            Internal.OpenGL.Methods.glMaterialfv(Internal.OpenGL.Constants.GLFace.Both, Internal.OpenGL.Constants.GL_SPECULAR, new float[] { (float)mat.SpecularColor.Red, (float)mat.SpecularColor.Green, (float)mat.SpecularColor.Blue, (float)mat.SpecularColor.Alpha });
                            Internal.OpenGL.Methods.glMaterialf(Internal.OpenGL.Constants.GLFace.Both, Internal.OpenGL.Constants.GL_SHININESS, (float)mat.Shininess);
                            EnableLighting = true;
                        }
                        else
                        {
                            // 輪郭・影無しのときは照明を無効にする
                            // Contour - When you disable the lighting without shadows
                            Internal.OpenGL.Methods.glColor4f((float)((mat.AmbientColor.Red + mat.DiffuseColor.Red)), (float)((mat.AmbientColor.Green + mat.DiffuseColor.Green)), (float)((mat.AmbientColor.Blue + mat.DiffuseColor.Blue)), (float)((mat.AmbientColor.Alpha + mat.DiffuseColor.Alpha)));

                            EnableLighting = false;
                        }

                        // 頂点インデックスを指定してポリゴン描画
                        // Specifies the index vertex polygon drawing

                        Begin(Caltron.RenderMode.Triangles);
                        foreach (ModelTriangle tri in mat.Triangles)
                        {
                            DrawTriangle(tri);
                        }
                        End();
                    }
                }
            }
            else
            {
                EnableLighting  = false;
                EnableTexturing = false;

                Internal.OpenGL.Methods.glColor4f(1, 1, 1, 1);

                for (int i = 0; i < model.Surfaces[0].Vertices.Count; i += 3)
                {
                    DrawTriangle(new ModelTriangle(model.Surfaces[0].Vertices[i], model.Surfaces[0].Vertices[i + 1], model.Surfaces[0].Vertices[i + 2]));
                }
            }
            if (mvarRenderBones)
            {
                Internal.OpenGL.Methods.glDisable(Internal.OpenGL.Constants.GLCapabilities.CullFace);
                // Internal.OpenGL.Methods.glFrontFace(Internal.OpenGL.Constants.GLFaceOrientation.Clockwise);
                // Internal.OpenGL.Methods.glCullFace(Internal.OpenGL.Constants.GL_BACK);

                EnableTexturing = false;

                foreach (ModelBone bone in model.Bones)
                {
                    Matrix.Push();

                    float[] ary = bone.Position.ToFloatArray();
                    Matrix.Multiply(ary);

                    Color = Color.FromRGBA(1.0f, 0.0f, 1.0f, 1.0f);
                    //glutSolidCube( 0.3f );
                    float fSize = 0.3f;
                    Begin(Caltron.RenderMode.Quads);
                    DrawVertex(-fSize / 2.0f, fSize / 2.0f, -fSize / 2.0f);
                    DrawVertex(fSize / 2.0f, fSize / 2.0f, -fSize / 2.0f);
                    DrawVertex(fSize / 2.0f, -fSize / 2.0f, -fSize / 2.0f);
                    DrawVertex(-fSize / 2.0f, -fSize / 2.0f, -fSize / 2.0f);
                    DrawVertex(fSize / 2.0f, fSize / 2.0f, -fSize / 2.0f);
                    DrawVertex(fSize / 2.0f, fSize / 2.0f, fSize / 2.0f);
                    DrawVertex(fSize / 2.0f, -fSize / 2.0f, fSize / 2.0f);
                    DrawVertex(fSize / 2.0f, -fSize / 2.0f, -fSize / 2.0f);
                    DrawVertex(fSize / 2.0f, fSize / 2.0f, fSize / 2.0f);
                    DrawVertex(-fSize / 2.0f, fSize / 2.0f, fSize / 2.0f);
                    DrawVertex(-fSize / 2.0f, -fSize / 2.0f, fSize / 2.0f);
                    DrawVertex(fSize / 2.0f, -fSize / 2.0f, fSize / 2.0f);
                    DrawVertex(fSize / 2.0f, fSize / 2.0f, -fSize / 2.0f);
                    DrawVertex(fSize / 2.0f, fSize / 2.0f, fSize / 2.0f);
                    DrawVertex(fSize / 2.0f, -fSize / 2.0f, fSize / 2.0f);
                    DrawVertex(fSize / 2.0f, -fSize / 2.0f, -fSize / 2.0f);
                    End();

                    Matrix.Pop();
                    Matrix.Push();

                    Color = Color.FromRGBA(1.0f, 1.0f, 1.0f, 1.0f);

                    if (bone.ParentBone != null)
                    {
                        Begin(Caltron.RenderMode.Lines);
                        DrawVertex(bone.ParentBone.Position.ToFloatArray());
                        DrawVertex(bone.Position.ToFloatArray());
                        End();
                    }

                    Matrix.Pop();
                }
            }
        }