Example #1
0
        public void Render(ThreeDSModel4LegacyOpenGL model, PolygonModes mode)
        {
            if (TriangleIndexes == null)
            {
                return;
            }

            GL.PolygonMode(PolygonModeFaces.FrontAndBack, mode);

            // Draw every triangle in the entity
            foreach (var item in this.usingMaterialIndexesList)
            {
                var material = model.MaterialDict[item.Item1];

                GL.Materialfv(GL.GL_FRONT_AND_BACK, GL.GL_AMBIENT, material.Ambient);
                GL.Materialfv(GL.GL_FRONT_AND_BACK, GL.GL_DIFFUSE, material.Diffuse);
                GL.Materialfv(GL.GL_FRONT_AND_BACK, GL.GL_SPECULAR, material.Specular);
                GL.Materialf(GL.GL_FRONT_AND_BACK, GL.GL_SHININESS, material.Shininess);

                Texture2D[] textures = new Texture2D[] { material.GetTexture(), material.GetBumpTexture(), material.GetReflectionTexture(), };
                bool        drawn    = false;
                foreach (var texture in textures)
                {
                    if (!(drawn && texture == null)) // 如果没有贴图,就只画一次。
                    {
                        if (texture != null)
                        {
                            GL.Enable(GL.GL_TEXTURE_2D);
                            texture.Bind();
                        }

                        DrawTriangles(item, texture);

                        if (texture != null)
                        {
                            texture.Unbind();
                            GL.Disable(GL.GL_TEXTURE_2D);
                        }
                    }

                    drawn = true;
                }
            }

            //Console.WriteLine ( GL.GetError () );
        }
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.open3DSDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                try
                {
                    var parser = new ThreeDSParser();
                    var filename = this.open3DSDlg.FileName;
                    var tree = parser.Parse(filename);
                    ThreeDSModel4LegacyOpenGL model = null;
                    tree.Dump(out model);
                    if (model.Entities == null)
                    {
                        MessageBox.Show("No entity found!");
                        return;
                    }
                    var builder = new StringBuilder();
                    int i = 1;
                    bool emptyVerticesFound = false, emptyIndicesFound = false, emptyTexCoordsFound = false;
                    foreach (var entity in model.Entities)
                    {
                        builder.Append("entity "); builder.Append(i++); builder.Append(":");
                        if (entity.Vertexes == null)
                        {
                            if (!emptyVerticesFound)
                            { MessageBox.Show("No vertices in some entity!"); emptyVerticesFound = true; }
                        }
                        else
                        { builder.Append(" "); builder.Append(entity.Vertexes.Length); builder.Append(" vertices"); }

                        if (entity.TriangleIndexes == null)
                        {
                            if (!emptyIndicesFound)
                            {
                                MessageBox.Show("No faces in some entity.");
                                emptyIndicesFound = true;
                            }
                        }
                        else
                        { builder.Append(" "); builder.Append(entity.TriangleIndexes.Length); builder.Append(" indices"); }

                        if (entity.TexCoords == null)
                        {
                            if (!emptyTexCoordsFound)
                            {
                                MessageBox.Show("No UV in some entity.");
                                emptyTexCoordsFound = true;
                            }
                        }
                        else
                        { builder.Append(" "); builder.Append(entity.TexCoords.Length); builder.Append(" UVs"); }

                        builder.AppendLine();
                    }

                    if (i == 1)
                    { builder.Append("no entity found."); }

                    this.model = model;

                    MessageBox.Show(builder.ToString(), "Info");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error");
                }
            }
        }
        public void Render(ThreeDSModel4LegacyOpenGL model, PolygonModes mode)
        {
            if (TriangleIndexes == null) return;

            GL.PolygonMode(PolygonModeFaces.FrontAndBack, mode);

            // Draw every triangle in the entity
            foreach (var item in this.usingMaterialIndexesList)
            {
                var material = model.MaterialDict[item.Item1];

                GL.Materialfv(GL.GL_FRONT_AND_BACK, GL.GL_AMBIENT, material.Ambient);
                GL.Materialfv(GL.GL_FRONT_AND_BACK, GL.GL_DIFFUSE, material.Diffuse);
                GL.Materialfv(GL.GL_FRONT_AND_BACK, GL.GL_SPECULAR, material.Specular);
                GL.Materialf(GL.GL_FRONT_AND_BACK, GL.GL_SHININESS, material.Shininess);

                Texture2D[] textures = new Texture2D[] { material.GetTexture(), material.GetBumpTexture(), material.GetReflectionTexture(), };
                bool drawn = false;
                foreach (var texture in textures)
                {
                    if (!(drawn && texture == null)) // 如果没有贴图,就只画一次。
                    {
                        if (texture != null)
                        {
                            GL.Enable(GL.GL_TEXTURE_2D);
                            texture.Bind();
                        }

                        DrawTriangles(item, texture);

                        if (texture != null)
                        {
                            texture.Unbind();
                            GL.Disable(GL.GL_TEXTURE_2D);
                        }
                    }

                    drawn = true;
                }
            }
            if (this.usingMaterialIndexesList.Count == 0)
            {
                GL.Begin(GL.GL_TRIANGLES);
                foreach (var tri in this.TriangleIndexes)
                {
                    // Vertex 1
                    if (normalized)
                    {
                        var normal = this.normals[tri.vertex1];
                        GL.Normal3d(normal.X, normal.Y, normal.Z);
                    }
                    {
                        var vertex = this.Vertexes[tri.vertex1];
                        GL.Vertex3d(vertex.X, vertex.Y, vertex.Z);
                    }

                    // Vertex 2
                    if (normalized)
                    {
                        var normal = this.normals[tri.vertex2];
                        GL.Normal3d(normal.X, normal.Y, normal.Z);
                    }
                    {
                        var vertex = this.Vertexes[tri.vertex2];
                        GL.Vertex3d(vertex.X, vertex.Y, vertex.Z);
                    }

                    // Vertex 3
                    if (normalized)
                    {
                        var normal = this.normals[tri.vertex3];
                        GL.Normal3d(normal.X, normal.Y, normal.Z);
                    }
                    {
                        var vertex = this.Vertexes[tri.vertex3];
                        GL.Vertex3d(vertex.X, vertex.Y, vertex.Z);
                    }
                }
                GL.End();
            }

            //Console.WriteLine ( GL.GetError () );
        }