08/12/9

XNA 3.0 #002 - XNA Game Studio in 3D, I - 在畫面中顯示一個3D模型

XNA文件中這一系列共有五篇:

  1. (如何顯示) Displaying a 3D Model on the Screen
  2. (輸入控制) Making Your Model Move Using Input
  3. (如何配音) Making Sounds with XNA Game Studio
  4. (建立遊戲) Make a Game in 60 Minutes
  5. (合作連線) Adding Multiplayer and Networking Support to the Game

似乎跟著這幾篇教學,就可以做出簡易的小遊戲,那就把"簡易版連線小蜜蜂"當做短期目標吧!

在這系列的文件中共有五個官方範例,先都去下載下來吧,之後會用得到的。

  1. http://go.microsoft.com/fwlink/?LinkId=125477&clcid=0x409
  2. http://go.microsoft.com/fwlink/?LinkId=125481&clcid=0x409
  3. http://go.microsoft.com/fwlink/?LinkId=125489&clcid=0x409
  4. http://go.microsoft.com/fwlink/?LinkId=125492&clcid=0x409
  5. http://go.microsoft.com/fwlink/?LinkId=125493&clcid=0x409

在畫面中顯示一個3D模型

專案建立

從新增專案中 -> 選取Windows Game(3.0)

xna_t01_creatproj

按下確定後,基本的空殼就完成了。VS2008會自動在資料夾中建立基本的檔案結構。

xna_t01_projdir

接著直接執行預設的程式碼,可以看到一個藍色底的視窗,如此便完成基本的專案建立。

xna_t01_emptyapp

匯入模型

XNA對於模型素材等有一套他的管理機制,所以現在先將下載sample1中的飛機模型匯入。

首先,在方案總管中找到content,並且幫他新增models與textures兩個資料夾。接著,再選取加入現有項目,分別將範例中的p1_wedge.fbx及wedge_p1_diff_v1.tga載入。

xna_t01_loadmodel1 xna_t01_addfolder

於是可以看到他們都在content的管理之下。

xna_t01_loadmodel2

另外,打開專案存放的資料夾,也會看見匯入的資料都複製了一份存放著而不是reference的方式。

xna_t01_loadmodel3

載入並顯示模型

現在有了各項素材,剩下的就是將這些材料讓我們的程式載入並且畫出來。

先在LoadContent裡面載入這些素材,特別注意填寫素材名稱的時候不需含副檔名

// Set the 3D model to draw.
Model myModel;

// The aspect ratio determines how to scale 3d to 2d projection.
float aspectRatio;

protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);

myModel = Content.Load<Model>("Models\\p1_wedge");
aspectRatio = graphics.GraphicsDevice.Viewport.AspectRatio;
}


接著,在Draw function 畫出模型,這裡需要一些T&L(transform and lighting)的處理,才能讓模型正確畫出來。



// Set the position of the model in world space, and set the rotation.
Vector3 modelPosition = Vector3.Zero;
float modelRotation = 0.0f;

// Set the position of the camera in world space, for our view matrix.
Vector3 cameraPosition = new Vector3(0.0f, 50.0f, 5000.0f);

protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

// Copy any parent transforms.
Matrix[] transforms = new Matrix[myModel.Bones.Count];
myModel.CopyAbsoluteBoneTransformsTo(transforms);

// Draw the model. A model can have multiple meshes, so loop.
foreach (ModelMesh mesh in myModel.Meshes)
{
// This is where the mesh orientation is set, as well as our camera and projection.
foreach (BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();
effect.World = transforms[mesh.ParentBone.Index] * Matrix.CreateRotationY(modelRotation)
* Matrix.CreateTranslation(modelPosition);
effect.View = Matrix.CreateLookAt(cameraPosition, Vector3.Zero, Vector3.Up);
effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f),
aspectRatio, 1.0f, 10000.0f);
}
// Draw the mesh, using the effects set above.
mesh.Draw();
}
base.Draw(gameTime);
}


如果你想要讓各項屬性(座標、血量等等)有所改變,就可以在Update裡面添加各種料,製作出"動"的感覺。



protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();

modelRotation += (float)gameTime.ElapsedGameTime.TotalMilliseconds * MathHelper.ToRadians(0.1f);

base.Update(gameTime);
}


按下F5執行程式後,就可以看到一架飛機在畫面中轉動。



xna_t01_demo1



完成這些基本處理後,更重要的是來看看這裡面的架構與原理。

1 意見:

匿名 提到...

不錯
恩 好人