我試圖在另一個類中繪製模型。 我試圖在另一個班級中繪製模型。然而,我的模型根本不會得到我所做的代碼的渲染。我可以在另一個班級中繪製模型嗎?
這裏的Game1代碼:(注:這些代碼很好的工作的Game1)
private Vector3 position = new Vector3(0, 0, 0);
private Matrix world;
private Matrix view;
private Matrix projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45), 800f/600f, 0.1f, 100f);
private Model car;
SpriteBatch spriteBatch;
GraphicsDeviceManager graphics;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
Vector3 transformedReference = Vector3.Transform(new Vector3(0, 5, 15), Matrix.CreateRotationY(0f));
view = Matrix.CreateLookAt(position + transformedReference, position, Vector3.Up);
}
protected override void Initialize()
{
Components.Add(new Car(this, view, projection));
world = Matrix.CreateTranslation(position);
Vector3 transformedReference = Vector3.Transform(new Vector3(0, 5, 15), Matrix.CreateRotationY(0f));
view = Matrix.CreateLookAt(position + transformedReference, position, Vector3.Up);
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
car = Content.Load <Model> ("car\\car");
}
public void DrawModel(Model model, Matrix world, Matrix view, Matrix projection)
{
foreach(ModelMesh mesh in model.Meshes)
{
foreach(BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();
effect.PreferPerPixelLighting = true;
effect.World = mesh.ParentBone.Transform * world;
effect.View = view;
effect.Projection = projection;
}
mesh.Draw();
}
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
world = Matrix.CreateTranslation(position);
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
DrawModel(car, world, view, projection);
base.Draw(gameTime);
}
,在這裏我的代碼另一個類:(注:這些代碼沒有按不行,或者模型不能在Game1圖形上渲染)
public class Car: DrawableGameComponent
{
public Model CarModel
{
get;
set;
}
private Vector3 position = new Vector3(0, 0, 0);
private Matrix World = Matrix.CreateTranslation(new Vector3(0, 0, 0));
public Matrix Camera
{
get;
set;
}
public Matrix Projection
{
get;
set;
}
public Game1 GameParent
{
get;
set;
}
public Car(Game1 game, Matrix view, Matrix projection): base(game)
{
view = Camera;
Projection = projection;
Camera = view;
GameParent = game;
World = Matrix.CreateTranslation(position);
base.Initialize();
}
public static void DrawModel(Model model, Matrix world, Matrix view, Matrix projection)
{
foreach(ModelMesh mesh in model.Meshes)
{
foreach(BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();
effect.PreferPerPixelLighting = true;
effect.World = mesh.ParentBone.Transform * world;
effect.View = view;
effect.Projection = projection;
}
mesh.Draw();
}
}
protected override void LoadContent()
{
CarModel = GameParent.Content.Load <Model> ("car\\car");
base.LoadContent();
}
public override void Update(GameTime gameTime)
{
base.Update(gameTime);
}
public override void Draw(GameTime gameTime)
{
DrawModel(CarModel, World, Camera, Projection); //DOESN'T WORK WELL!!
base.Draw(gameTime);
}
}
好的,我的觀點只是w螞蟻在另一個班級繪製3D模型,,
好吧,我該怎麼做才能解決這個問題?
我希望你不介意幫我,你能明白我的意思..
當然可以,要做到這一點的一種方法是在每個模型(或模型列表底部的方法調用平局的功能! )。我沒有把這個作爲答案,因爲我沒有時間做一個工作的例子 – Sayse 2013-03-15 07:54:20
我想你應該問你的問題在http://gamedev.stackexchange.com/ – 2013-03-15 07:57:52
@Sayse:請給我的例子,如果你沒有你可以給我鏈接,這個問題的相關鏈接 – Ricks 2013-03-15 08:12:34