Skip to content

Instantly share code, notes, and snippets.

@RobertoEstrada
Created March 3, 2012 17:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RobertoEstrada/1967076 to your computer and use it in GitHub Desktop.
Save RobertoEstrada/1967076 to your computer and use it in GitHub Desktop.
XNA Showroom
/*
* Program.cs
*
* Copyright 2012 Roberto Estrada
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
namespace XNAShowRoom
{
#if WINDOWS || XBOX
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main(string[] args)
{
using (ShowRoom game = new ShowRoom())
{
game.Run();
}
}
}
#endif
}
/*
* ShowRoom.cs
*
* Copyright 2012 Roberto Estrada
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace XNAShowRoom
{
/// <summary>
/// The 3D XNA Showroom
/// </summary>
public class ShowRoom : Microsoft.Xna.Framework.Game
{
// Field of view
static float FOV = 60.0f;
// Graphics adapter variables
float aspectRatio;
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
// Model to be shown
Model shownModel;
// Position of the model (X, Y, Z)
Vector3 modelPos;
// Model rotation (in degrees)
float modelRotation;
// Camera position in world
Vector3 cameraPos;
Matrix lookAtMatrix;
Matrix projectionMatrix;
public ShowRoom()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
/// <summary>
/// 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.
/// </summary>
protected override void Initialize()
{
// TODO: Add your initialization logic here
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
// Getting viewport aspect ratio
aspectRatio = graphics.GraphicsDevice.Viewport.AspectRatio;
// Content loading
shownModel = Content.Load<Model>("Models\\desklamp");
// Setting camera
cameraPos = new Vector3(0.0f, 100.0f, 80.0f);
// Matrix precomputation
lookAtMatrix = Matrix.CreateLookAt(cameraPos, new Vector3(0.0f, 50.0f, 0.0f), Vector3.Up);
projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(FOV), aspectRatio, 1.0f, 10000.0f);
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// No unmanaged contents to unload
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
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.01f);
base.Update(gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// Drawing code
Matrix[] transformBoneMatrixes = new Matrix[shownModel.Bones.Count];
shownModel.CopyAbsoluteBoneTransformsTo(transformBoneMatrixes);
// Model draw, for each mesh in the model...
foreach (ModelMesh mesh in shownModel.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.EnableDefaultLighting();
effect.World = transformBoneMatrixes[mesh.ParentBone.Index] *
Matrix.CreateRotationY(modelRotation) *
Matrix.CreateTranslation(modelPos);
effect.View = lookAtMatrix;
effect.Projection = projectionMatrix;
}
mesh.Draw();
}
base.Draw(gameTime);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment