■XNAで3Dメッシュ | Prev Top Next |
今回は、3Dメッシュのレンダリングを行います。3DメッシュのフォーマットはDirect3Dで一般的なXファイルを使用します。フリーツールではMetasequoiaで作成できます。
namespace Tutrial { /// /// This is the main type for your game /// public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; ContentManager content; //**************************************************************** //オブジェクトの定義 //**************************************************************** private Model model = null; //3Dメッシュ private Texture2D texture2D = null; //テクスチャー public Game1() { graphics = new GraphicsDeviceManager(this); //リソースフォルダのルートパスを指定 content = new ContentManager(Services, "Resource"); } /// /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// protected override void Initialize() { //**************************************************************** //オブジェクトの初期化 //**************************************************************** //3Dメッシュのロード this.model = this.content.Load("Cube"); //テクスチャーのロード(注意1) this.texture2D = this.content.Load ("Texture"); base.Initialize(); } /// /// Load your graphics content. If loadAllContent is true, you should /// load content from both ResourceManagementMode pools. Otherwise, just /// load ResourceManagementMode.Manual content. /// /// Which type of content to load. protected override void LoadGraphicsContent(bool loadAllContent) { if (loadAllContent) { // TODO: Load any ResourceManagementMode.Automatic content } // TODO: Load any ResourceManagementMode.Manual content } /// /// Unload your graphics content. If unloadAllContent is true, you should /// unload content from both ResourceManagementMode pools. Otherwise, just /// unload ResourceManagementMode.Manual content. Manual content will get /// Disposed by the GraphicsDevice during a Reset. /// /// Which type of content to unload. protected override void UnloadGraphicsContent(bool unloadAllContent) { if (unloadAllContent == true) { content.Unload(); } } /// /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input and playing audio. /// /// Provides a snapshot of timing values. protected override void Update(GameTime gameTime) { // Allows the default game to exit on Xbox 360 and Windows if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); // TODO: Add your update logic here base.Update(gameTime); } /// /// This is called when the game should draw itself. /// /// Provides a snapshot of timing values. protected override void Draw(GameTime gameTime) { //**************************************************************** //レンダリング //**************************************************************** //背景色をグレーで塗りつぶす graphics.GraphicsDevice.Clear(Color.Gray); //Zバッファを有効にする graphics.GraphicsDevice.RenderState.DepthBufferEnable = true; //Zバッファへの書き込みを許可する graphics.GraphicsDevice.RenderState.DepthBufferWriteEnable = true; foreach (ModelMesh modelMesh in model.Meshes) { foreach (BasicEffect effect in modelMesh.Effects) { //射影座標変換 effect.Projection = Matrix.CreatePerspectiveFieldOfView( MathHelper.ToRadians(45.0f), //視野角 (float)graphics.GraphicsDevice.Viewport.Width / (float)graphics.GraphicsDevice.Viewport.Height, //スクリーンサイズのアスペクト比 1.0f, //前方クリップ位置 100.0f //後方クリップ位置 ); //ビュー座標変換 effect.View = Matrix.CreateLookAt( new Vector3(0.0f, 0.0f, 10.0f), //カメラ位置 Vector3.Zero, //カメラの注視点 Vector3.Up //カメラの上方向 ); //ワールド座標系 effect.World = Matrix.CreateTranslation(0.0f, 0.0f, -10.0f); //**************************************************************** //照明(平行光源) //**************************************************************** //照明を有効にする effect.LightingEnabled = true; //0番目のを照明を有効にする effect.DirectionalLight0.Enabled = true; //平行光源の拡散色の設定 effect.DirectionalLight0.DiffuseColor = new Vector3(1.0f, 1.0f, 1.0f); //平行光源の方向ベクトルの設定 effect.DirectionalLight0.Direction = Vector3.Normalize(new Vector3(-1.0f, -1.0f, -1.0f)); //**************************************************************** //マテリアル //**************************************************************** //環境光の設定 effect.AmbientLightColor = new Vector3(0.15f, 0.15f, 0.15f); //拡散光の設定 effect.DiffuseColor = new Vector3(1.0f, 1.0f, 1.0f); //テクスチャーを有効にする effect.TextureEnabled = true; //テクスチャーを設定する effect.Texture = texture2D; } //メッシュのレンダリング modelMesh.Draw(); } base.Draw(gameTime); } } }
3Dメッシュが表示されました。
(注意1) XNA Game Studio Express に入ってるサンプルゲーム SpeceWar のソースを見ると、テクスチャーのファイル名はXファイルに記述されているファイル名から取得することはできないようです。不便だなあ。