Example #1
0
 public void RemoveModel(Model model)
 {
     if (this.models.Contains(model))
     {
         this.models.Remove(model);
     }
 }
Example #2
0
        public static BoundingBox ComputeBoundingBox( Model model )
        {
            if ( model != null )
            {
                Vector3 center = model.Transform.Translation;
                Vector3 model_min = Vector3.Zero;
                Vector3 model_max = Vector3.Zero;

                foreach ( Mesh mesh in model.Meshes ) {
                    Vector3 mesh_min = Vector3.Zero;
                    Vector3 mesh_max = Vector3.Zero;

                    for ( int i = 0; i < mesh.VertexData.Length; i++ ) {
                        Vector3 vert_pos = mesh.VertexData[ i ].Position;

                        mesh_min = Vector3.Min( mesh_min, vert_pos );
                        mesh_max = Vector3.Max( mesh_max, vert_pos );
                    }

                    model_min = Vector3.Min( model_min, mesh_min );
                    model_max = Vector3.Max( model_max, mesh_max );
                }

                Vector3 extents = Vector3.One;
                extents.X = ( Math.Abs( model_max.X - model_min.X ) / 2.0f );
                extents.Y = ( Math.Abs( model_max.Y - model_min.Y ) / 2.0f );
                extents.Z = ( Math.Abs( model_max.Z - model_min.Z ) / 2.0f );

                return new BoundingBox( center, extents );
            }

            return new Pico3D.Primitives.BoundingBox( );
        }
Example #3
0
        public Model Import( string filename )
        {
            this.Initialize( );

            this.model = new Model( this.model_name );
            string path = Path.Combine( this.source_dir, filename );
            this.model_path = path.Replace( Path.GetFileName( path ), string.Empty );

            if ( File.Exists( path ) && path.EndsWith( ".pmf" ) ) {
                foreach ( string[] lineTokens in this.GetLineTokens( path ) ) {
                    this.Parse( lineTokens );
                }

                if ( this.current_mesh != null ) {
                    this.CalculateNormals( );

                    this.current_mesh.SetVertexData( this.vertices );
                    this.vertices = null;
                    this.current_mesh.SetIndexData( this.indices );
                    this.indices = null;
                    this.current_mesh.CreateVertexBuffer( this.g_device );

                    this.meshes.Add( this.current_mesh );
                    this.current_mesh = null;
                }

                this.model.SetMeshes( this.meshes );
                //this.model.SetBoundingBox( this.ComputeBoundingBox( ) );
            }

            return model;
        }
Example #4
0
        void Initialize( )
        {
            this.model = null;

            this.positions = new List <Vector3>( );
            this.texcoords = new List <Vector2>( );

            this.vertex_data = new List <Vertex>( );

            this.meshes = new List <Mesh>( );
        }
Example #5
0
 public void AddModel(Model model)
 {
     if (!this.models.Contains(model))
     {
         if (model.Material.ContainsDiffuseMap( ))
         {
             this.effect = this.texture_shader.Effect;
         }
         else
         {
             this.effect = this.default_shader.Effect;
         }
         this.models.Add(model);
     }
 }
Example #6
0
        public Model Import(string filename)
        {
            this.Initialize( );

            this.model = new Model(this.model_name);
            string path = Path.Combine(this.source_dir, filename);

            this.model_path = path.Replace(Path.GetFileName(path), string.Empty);

            if (File.Exists(path) && path.EndsWith(".pmf"))
            {
                foreach (string[] lineTokens in this.GetLineTokens(path))
                {
                    this.Parse(lineTokens);
                }

                if (this.current_mesh != null)
                {
                    this.CalculateNormals( );

                    this.current_mesh.SetVertexData(this.vertices);
                    this.vertices = null;
                    this.current_mesh.SetIndexData(this.indices);
                    this.indices = null;
                    this.current_mesh.CreateVertexBuffer(this.g_device);

                    this.meshes.Add(this.current_mesh);
                    this.current_mesh = null;
                }

                this.model.SetMeshes(this.meshes);
                //this.model.SetBoundingBox( this.ComputeBoundingBox( ) );
            }

            return(model);
        }
Example #7
0
        void Parse(string[] lineTokens)
        {
            switch (lineTokens[0].ToLower( ))
            {
            // Model.
            case "model":
                this.model_name = lineTokens[1];
                this.model      = new Model(this.model_name);
                break;

            // Mesh.
            case "mesh":
                if (this.current_mesh != null)
                {
                    this.CalculateNormals( );

                    this.current_mesh.SetVertexData(this.vertices);
                    this.vertices = null;
                    this.current_mesh.SetIndexData(this.indices);
                    this.indices = null;
                    this.current_mesh.CreateVertexBuffer(this.g_device);

                    this.meshes.Add(this.current_mesh);
                    this.current_mesh = null;
                }
                else
                {
                    this.current_mesh = new Mesh(lineTokens[1]);
                }
                break;

            // BeginMaterial.
            case "bmat":
                this.material = new Material(lineTokens[1]);
                break;

            // DiffuseMap.
            case "dmap":
                if (this.material != null)
                {
                    Texture2D tex = this.LoadTexture(Path.Combine(this.model_path, lineTokens[1]));
                    this.material.AddTexture(TextureType.DiffuseMap, tex);
                    tex = null;
                }
                break;

            // NormalMap.
            case "nmap":
                if (this.material != null)
                {
                    Texture2D tex = this.LoadTexture(Path.Combine(this.model_path, lineTokens[1]));
                    this.material.AddTexture(TextureType.NormalMap, tex);
                    tex = null;
                }
                break;

            // SpecularMap.
            case "smap":
                if (this.material != null)
                {
                    Texture2D tex = this.LoadTexture(Path.Combine(this.model_path, lineTokens[1]));
                    this.material.AddTexture(TextureType.SpecularMap, tex);
                    tex = null;
                }
                break;

            // EmissiveMap.
            case "emap":
                if (this.material != null)
                {
                    Texture2D tex = this.LoadTexture(Path.Combine(this.model_path, lineTokens[1]));
                    this.material.AddTexture(TextureType.EmissiveMap, tex);
                    tex = null;
                }
                break;

            // EndMaterial.
            case "emat":
                this.model.SetMaterial(this.material);
                this.material = null;
                break;

            // Position.
            case "v":
                this.positions.Add(this.ParseVector3(lineTokens));
                break;

            // TextureCoordinate.
            case "vt":
                Vector2 vt = this.ParseVector2(lineTokens);
                vt.Y = (1.0f - vt.Y);
                this.texcoords.Add(vt);
                break;

            // Face.
            case "f":
                for (int vi = 1; vi <= 3; vi++)
                {
                    string[] indices   = lineTokens[vi].Split('/');
                    int      pos_index = (int.Parse(indices[0], CultureInfo.InvariantCulture) - 1);

                    int     tc_index  = 0;
                    Vector2 tex_coord = Vector2.Zero;

                    if (int.TryParse(indices[1], out tc_index))
                    {
                        tex_coord = this.texcoords[tc_index - 1];
                    }

                    this.vertex_data.Add(new Vertex(this.positions[pos_index], tex_coord, Vector3.Zero));
                }
                break;

            // Unknown command.
            default:
                break;
            }
        }
Example #8
0
 public void AddModel( Model model )
 {
     if ( !this.models.Contains( model ) ) {
         if ( model.Material.ContainsDiffuseMap( ) ) {
             this.effect = this.texture_shader.Effect;
         }
         else {
             this.effect = this.default_shader.Effect;
         }
         this.models.Add( model );
     }
 }
Example #9
0
 public void RemoveModel( Model model )
 {
     if ( this.models.Contains( model ) ) {
         this.models.Remove( model );
     }
 }
Example #10
0
        void Parse( string[] lineTokens )
        {
            switch ( lineTokens[ 0 ].ToLower( ) ) {
                // Model.
                case "model":
                    this.model_name = lineTokens[ 1 ];
                    this.model = new Model( this.model_name );
                    break;
                // Mesh.
                case "mesh":
                    if ( this.current_mesh != null ) {
                        this.CalculateNormals( );

                        this.current_mesh.SetVertexData( this.vertices );
                        this.vertices = null;
                        this.current_mesh.SetIndexData( this.indices );
                        this.indices = null;
                        this.current_mesh.CreateVertexBuffer( this.g_device );

                        this.meshes.Add( this.current_mesh );
                        this.current_mesh = null;
                    }
                    else {
                        this.current_mesh = new Mesh( lineTokens[ 1 ] );
                    }
                    break;
                // BeginMaterial.
                case "bmat":
                    this.material = new Material( lineTokens[ 1 ] );
                    break;
                // DiffuseMap.
                case "dmap":
                    if ( this.material != null ) {
                        Texture2D tex = this.LoadTexture( Path.Combine( this.model_path, lineTokens[ 1 ] ) );
                        this.material.AddTexture( TextureType.DiffuseMap, tex );
                        tex = null;
                    }
                    break;
                // NormalMap.
                case "nmap":
                    if ( this.material != null ) {
                        Texture2D tex = this.LoadTexture( Path.Combine( this.model_path, lineTokens[ 1 ] ) );
                        this.material.AddTexture( TextureType.NormalMap, tex );
                        tex = null;
                    }
                    break;
                // SpecularMap.
                case "smap":
                    if ( this.material != null ) {
                        Texture2D tex = this.LoadTexture( Path.Combine( this.model_path, lineTokens[ 1 ] ) );
                        this.material.AddTexture( TextureType.SpecularMap, tex );
                        tex = null;
                    }
                    break;
                // EmissiveMap.
                case "emap":
                    if ( this.material != null ) {
                        Texture2D tex = this.LoadTexture( Path.Combine( this.model_path, lineTokens[ 1 ] ) );
                        this.material.AddTexture( TextureType.EmissiveMap, tex );
                        tex = null;
                    }
                    break;
                // EndMaterial.
                case "emat":
                    this.model.SetMaterial( this.material );
                    this.material = null;
                    break;
                // Position.
                case "v":
                    this.positions.Add( this.ParseVector3( lineTokens ) );
                    break;
                // TextureCoordinate.
                case "vt":
                    Vector2 vt = this.ParseVector2( lineTokens );
                    vt.Y = ( 1.0f - vt.Y );
                    this.texcoords.Add( vt );
                    break;
                // Face.
                case "f":
                    for ( int vi = 1; vi <= 3; vi++ ) {
                        string[] indices = lineTokens[ vi ].Split( '/' );
                        int pos_index = ( int.Parse( indices[ 0 ], CultureInfo.InvariantCulture ) - 1 );

                        int tc_index = 0;
                        Vector2 tex_coord = Vector2.Zero;

                        if ( int.TryParse( indices[ 1 ], out tc_index ) ) {
                            tex_coord = this.texcoords[ tc_index - 1 ];
                        }

                        this.vertex_data.Add( new Vertex( this.positions[ pos_index ], tex_coord, Vector3.Zero ) );
                    }
                    break;
                // Unknown command.
                default:
                    break;
            }
        }
Example #11
0
        void Initialize( )
        {
            this.model = null;

            this.positions = new List<Vector3>( );
            this.texcoords = new List<Vector2>( );

            this.vertex_data = new List<Vertex>( );

            this.meshes = new List<Mesh>( );
        }