Skip to content

Instantly share code, notes, and snippets.

@aldoKelvianto
Last active December 5, 2016 19:08
Show Gist options
  • Save aldoKelvianto/13895538668754ec2ff4 to your computer and use it in GitHub Desktop.
Save aldoKelvianto/13895538668754ec2ff4 to your computer and use it in GitHub Desktop.
Loading 3D Model XNA snippet
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
public class GameMain : Microsoft.Xna.Framework.Game {
GraphicsDeviceManager graphics;
Camera camera;
Model robot;
public GameMain() {
graphics = new GraphicsDeviceManager(this);
}
protected override void Initialize() {
graphics.PreferredBackBufferHeight = 450;
graphics.PreferredBackBufferWidth = 450;
graphics.ApplyChanges();
camera = new Camera();
Content.RootDirectory = "Content";
base.Initialize();
}
protected override void LoadContent() {
robot = Content.Load<Model>("Robot");
}
protected override void Update(GameTime gameTime) {
KeyboardState keys = Keyboard.GetState();
if (keys.IsKeyDown(Keys.Escape))
this.Exit();
camera.Update(gameTime);
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime) {
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
graphics.GraphicsDevice.VertexDeclaration = new VertexDeclaration(
graphics.GraphicsDevice, VertexPositionColor.VertexElements);
foreach (BasicEffect effect in robot.Meshes["Robot"].Effects) {
effect.EnableDefaultLighting();
effect.EmissiveColor = Color.White.ToVector3();
effect.View = camera.View;
effect.Projection = camera.Projection;
effect.World = robot.Bones["Robot"].Transform * camera.World;
}
robot.Meshes["Robot"].Draw();
base.Draw(gameTime);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment