別のクラスでモデルを描画しようとしています。別のクラスでモデルを描画しようとしました。ただし、私のモデルは、私が作成したコードではまったくレンダリングされません。
ここに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);
}
}
OK、私のポイントは、別のクラスで 3D モデルを描画したいだけです,,
さて、これを解決するにはどうすればよいですか?
私はあなたが私を助けても構わないと願っています.