我正在尝试在另一个班级中绘制模型。我试图在另一个班级中绘制模型。但是,我的模型根本无法使用我所做的代码进行渲染。
这里是 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);
}
}
好的,我的观点只是想在另一个类中绘制 3D 模型,,
好吧,我该怎么做才能解决这个问题?
我希望你不介意帮助我,你能理解我的意思..