Exemple #1
0
		/// <summary>
		/// Load a model from file
		/// </summary>
		/// <param name="name"></param>
		/// <param name="path"></param>
		/// <param name="height">pass zero to retain original height</param>
		/// <returns></returns>
		public static IModel LoadStaticModel( string name, string path, float height ) {
			if(!System.IO.File.Exists(path)) {
				throw new System.IO.FileNotFoundException("Could not find model '" + path + "'", path);
			}

			Log.LogMessage( "Loading static model " + path + name );

			Model loadedModel = new Model();
			loadedModel._key = name;
			try {
				loadedModel._mesh = Engine.TV3DScene.CreateMeshBuilder( name );
				loadedModel._mesh.Load3DSMesh( path, true, true, false, true, true );
			}
			catch(Exception e) {
				throw new ModelNotLoadedException(path, e);
			}

			//loadedModel._mesh.ShowBoundingBox( true );
			D3DVECTOR boxmin = new D3DVECTOR();
			D3DVECTOR boxmax = new D3DVECTOR();
			loadedModel._mesh.GetBoundingBox( ref boxmin, ref boxmax, true );
			loadedModel._height = height;
			if ( height != 0 ) {
				// scale the model to the correct height and get new bounding box info
				float scale_factor = height / ( boxmax.y - boxmin.y );
				loadedModel._mesh.ScaleMesh( scale_factor, scale_factor, scale_factor );
				boxmin.x *= scale_factor;
				boxmin.y *= scale_factor;
				boxmin.z *= scale_factor;
				boxmax.x *= scale_factor;
				boxmax.y *= scale_factor;
				boxmax.z *= scale_factor;
			}
			loadedModel._width = Math.Max( (boxmax.x - boxmin.x ), (boxmax.z - boxmin.z ) );
			loadedModel._depth = Math.Min( (boxmax.x - boxmin.x ), (boxmax.z - boxmin.z ) );
			loadedModel._boxmin = new Vector3D( boxmin.x, boxmin.y, boxmin.z );
			loadedModel._boxmax = new Vector3D( boxmax.x, boxmax.y, boxmax.z );

			loadedModel._mesh.Optimize();
			loadedModel._mesh.ComputeNormals();
			loadedModel._mesh.ComputeSphere();
			loadedModel._mesh.ComputeBoundingVolumes();
			//loadedModel._mesh.ShowBoundingBox( true );
			
			float radius = 0;
			DxVBLibA.D3DVECTOR center = new DxVBLibA.D3DVECTOR();
			loadedModel._mesh.GetBoundingSphere(ref center, ref radius, true );
			loadedModel._RadiusSquared = radius * radius;
			loadedModel._position = new Math3D.Vector3D( 0, 0, 0 );
			loadedModel._rotation = new Math3D.Vector3D( 0, 0, 0 );
			// TODO: could get rid of 'offset' if we prenormalize the models
			// outside
			loadedModel._offset = new Math3D.Vector3D(
				(boxmax.x + boxmin.x)/2,
				(boxmax.y + boxmin.y)/2,
				(boxmax.z + boxmin.z)/2
			);
			loadedModel._id = loadedModel._mesh.GetMeshIndex();
			//loadedModel._mesh.InitLOD(0);
			return loadedModel;
		}
Exemple #2
0
		public static Model CreateBox( string name, float width, float height, float depth, ITexture texture ) {
			Model loadedModel = new Model();
			loadedModel._key = name;
			loadedModel._mesh = Engine.TV3DScene.CreateMeshBuilder( name );
			loadedModel._mesh.CreateBox( width, height, depth, false );
			if ( texture != null ) loadedModel._mesh.SetTexture( texture.ID, 0 );
			loadedModel._RadiusSquared = 0;
			loadedModel._position = new Math3D.Vector3D( 0, 0, 0 );
			loadedModel._rotation = new Math3D.Vector3D( 0, 0, 0 );
			// TODO: could get rid of 'offset' if we prenormalize the models
			// outside
			loadedModel._offset = new Math3D.Vector3D( 0,0,0 );
			loadedModel._id = loadedModel._mesh.GetMeshIndex();
			return loadedModel;
		}
Exemple #3
0
		public static Model CreatePlane( string name, ITexture texture, Vector3D p1, Vector3D p2, Vector3D p3, Vector3D p4 ) {
			Model t = new Model();
			t._mesh = Engine.TV3DScene.CreateMeshBuilder( name );
			// NB: note the point order gets changed here,
			// because I think it makes more sense this way
			t._mesh.AddFaceFromPoint( texture.ID, p1.X, p1.Y, p1.Z, p2.X, p2.Y, p2.Z, p4.X, p4.Y, p4.Z, p3.X, p3.Y, p3.Z, 1, 1, true, false );
			t._mesh.Optimize();
			t._mesh.ComputeBoundingVolumes();
			t._offset = new Math3D.Vector3D( 0,0,0 );

			// TODO: bumpmapping would be cool :)
			//t._mesh.SetBumpMapping( true, texture.ID, -1, -1, 10 );
			t._key = name;
			t._id = t._mesh.GetMeshIndex();
			t._RadiusSquared = 0;
			return t;
		}
Exemple #4
0
		public IModel Duplicate( string name, float height ) {
			Model dup = new Model();
			dup._key = name;
			dup._mesh = this._mesh.DuplicateMesh( name );
			dup._mesh.SetPosition( 0, 0, 0 );
			dup._mesh.SetRotation( 0, 0, 0 );

			//loadedModel._mesh.ShowBoundingBox( true );
			D3DVECTOR boxmin = new D3DVECTOR();
			D3DVECTOR boxmax = new D3DVECTOR();
			dup._mesh.GetBoundingBox( ref boxmin, ref boxmax, true );

			dup._height = height;
			if ( height != 0 ) {
				// scale the model to the correct height and get new bounding box info
				float scale_factor = height / ( boxmax.y - boxmin.y );
				dup._mesh.ScaleMesh( scale_factor, scale_factor, scale_factor );
				boxmin.x *= scale_factor;
				boxmin.y *= scale_factor;
				boxmin.z *= scale_factor;
				boxmax.x *= scale_factor;
				boxmax.y *= scale_factor;
				boxmax.z *= scale_factor;
			}
			dup._width = Math.Max( (boxmax.x - boxmin.x ), (boxmax.z - boxmin.z ) );
			dup._depth = Math.Min( (boxmax.x - boxmin.x ), (boxmax.z - boxmin.z ) );
			dup._boxmin = new Vector3D( boxmin.x, boxmin.y, boxmin.z );
			dup._boxmax = new Vector3D( boxmax.x, boxmax.y, boxmax.z );

			float radius = 0;
			DxVBLibA.D3DVECTOR center = new DxVBLibA.D3DVECTOR();
			dup._mesh.GetBoundingSphere(ref center, ref radius, true );
			dup._RadiusSquared = radius * radius;
			dup._position = new Math3D.Vector3D( 0, 0, 0 );
			dup._rotation = new Math3D.Vector3D( 0, 0, 0 );
			// TODO: could get rid of 'offset' if we prenormalize the models
			// outside
			dup._offset = new Math3D.Vector3D(
				(boxmax.x + boxmin.x)/2,
				(boxmax.y + boxmin.y)/2,
				(boxmax.z + boxmin.z)/2
				);
			dup._id = dup._mesh.GetMeshIndex();
			return dup;
		}