EDN Admin
Well-known member
Here is my code:
using System;<br/>
using System.Collections.Generic;<br/>
using System.Linq;<br/>
using Microsoft.Xna.Framework;<br/>
using Microsoft.Xna.Framework.Audio;<br/>
using Microsoft.Xna.Framework.Content;<br/>
using Microsoft.Xna.Framework.GamerServices;<br/>
using Microsoft.Xna.Framework.Graphics;<br/>
using Microsoft.Xna.Framework.Input;<br/>
using Microsoft.Xna.Framework.Media;<br/>
<br/>
namespace ShaderEffects1<br/>
{<br/>
/// <summary><br/>
/// This is the main type for your game<br/>
/// </summary><br/>
public class Game1 : Microsoft.Xna.Framework.Game<br/>
{<br/>
GraphicsDeviceManager graphics;<br/>
SpriteBatch spriteBatch;<br/>
Model model;<br/>
BasicEffect effect;<br/>
Texture2D texture;<br/>
VertexBuffer vb;<br/>
<br/>
public Game1()<br/>
{<br/>
graphics = new GraphicsDeviceManager(this);<br/>
Content.RootDirectory = "Content";<br/>
}<br/>
<br/>
/// <summary><br/>
/// Allows the game to perform any initialization it needs to before starting to run.<br/>
/// This is where it can query for any required services and load any non-graphic<br/>
/// related content. Calling base.Initialize will enumerate through any components<br/>
/// and initialize them as well.<br/>
/// </summary><br/>
protected override void Initialize()<br/>
{<br/>
// TODO: Add your initialization logic here<br/>
<br/>
base.Initialize();<br/>
}<br/>
<br/>
/*struct VertexColor : IVertexType<br/>
{<br/>
public Vector3 position;<br/>
public Vector3 normal;<br/>
public Vector2 uv;<br/>
public Color color;<br/>
public static readonly VertexDeclaration _decl = new VertexDeclaration(new VertexElement(0, VertexElementFormat.Vector3, VertexElementUsage.Position, 0), new VertexElement(12, VertexElementFormat.Vector3,
VertexElementUsage.Normal, 0), new VertexElement(24, VertexElementFormat.Vector2, VertexElementUsage.TextureCoordinate, 0), new VertexElement(32, VertexElementFormat.Color, VertexElementUsage.Color, 0));<br/>
public VertexColor(VertexPositionNormalTexture data)<br/>
{<br/>
position = data.Position;<br/>
normal = data.Normal;<br/>
uv = data.TextureCoordinate;<br/>
color = Color.White;<br/>
}<br/>
public VertexDeclaration VertexDeclaration<br/>
{<br/>
get { return _decl; }<br/>
}<br/>
} */<br/>
<br/>
/// <summary><br/>
/// LoadContent will be called once per game and is the place to load<br/>
/// all of your content.<br/>
/// </summary><br/>
protected override void LoadContent()<br/>
{<br/>
// Create a new SpriteBatch, which can be used to draw textures.<br/>
spriteBatch = new SpriteBatch(GraphicsDevice);<br/>
model = Content.Load<Model>("box");<br/>
texture = Content.Load<Texture2D>("cat");<br/>
effect = new BasicEffect(GraphicsDevice);<br/>
effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, GraphicsDevice.Viewport.AspectRatio, 1.0f, 100.0f);<br/>
effect.View = Matrix.CreateLookAt(new Vector3(2, 3, 2), Vector3.Zero, Vector3.Up);<br/>
effect.TextureEnabled = true;<br/>
effect.Texture = texture;<br/>
effect.EnableDefaultLighting();<br/>
//effect.VertexColorEnabled = true;<br/>
// TODO: use this.Content to load your game content here<br/>
}<br/>
<br/>
private Matrix GetParentTransform(Model m, ModelBone mb)<br/>
{<br/>
return (mb == m.Root) ? mb.Transform : mb.Transform * GetParentTransform(m, mb.Parent);<br/>
}<br/>
<br/>
private void DrawModel(Model m, Matrix world, BasicEffect be)<br/>
{<br/>
foreach (ModelMesh mm in model.Meshes)<br/>
{<br/>
foreach (ModelMeshPart mmp in mm.MeshParts)<br/>
{<br/>
be.World = GetParentTransform(m, mm.ParentBone) * world;<br/>
GraphicsDevice.SetVertexBuffer(mmp.VertexBuffer, mmp.VertexOffset);<br/>
GraphicsDevice.Indices = mmp.IndexBuffer;<br/>
be.CurrentTechnique.Passes[0].Apply();<br/>
GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, mmp.NumVertices, mmp.StartIndex, mmp.PrimitiveCount);<br/>
}<br/>
}<br/>
}<br/>
<br/>
/// <summary><br/>
/// UnloadContent will be called once per game and is the place to unload<br/>
/// all content.<br/>
/// </summary><br/>
protected override void UnloadContent()<br/>
{<br/>
// TODO: Unload any non ContentManager content here<br/>
}<br/>
<br/>
/// <summary><br/>
/// Allows the game to run logic such as updating the world,<br/>
/// checking for collisions, gathering input, and playing audio.<br/>
/// </summary><br/>
/// <param name="gameTime Provides a snapshot of timing values.</param><br/>
protected override void Update(GameTime gameTime)<br/>
{<br/>
// Allows the game to exit<br/>
if (Keyboard.GetState().IsKeyDown(Keys.Escape) || GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)<br/>
this.Exit();<br/>
<br/>
// TODO: Add your update logic here<br/>
<br/>
base.Update(gameTime);<br/>
}<br/>
<br/>
/// <summary><br/>
/// This is called when the game should draw itself.<br/>
/// </summary><br/>
/// <param name="gameTime Provides a snapshot of timing values.</param><br/>
protected override void Draw(GameTime gameTime)<br/>
{<br/>
GraphicsDevice.Clear(Color.CornflowerBlue);<br/>
effect.SpecularPower = (float)gameTime.TotalGameTime.TotalSeconds * 2.0f;<br/>
<br/>
DrawModel(model, Matrix.Identity, effect);<br/>
<br/>
// TODO: Add your drawing code here<br/>
<br/>
base.Draw(gameTime);<br/>
}<br/>
}<br/>
}<br/>
(Please ignore the commented out things, I am using a textbook and typed a little code in advance so I could use it later)
My problem is that the effect.VertexColorEnabled makes the screen go white and my DrawModel method has an error saying it is missing a color, yet all of the parameters are satsified with their requirement. Yet, if I get rid of the vertex line, then it works
perfectly, lighting the top of the box and properly dimming the sides. Why does this line fail? My textbook just says basically to add the line and it should work.
I am using XNA Game Studio 4.0 Programming, written by Tom Miller and Dean Johnson, members of the actual XNA development team, so Im surprised by the errors whether typing errors or code errors, with no guidance about how to go about them. Any help would
be appreciated, Thanks!
Domenic
View the full article
using System;<br/>
using System.Collections.Generic;<br/>
using System.Linq;<br/>
using Microsoft.Xna.Framework;<br/>
using Microsoft.Xna.Framework.Audio;<br/>
using Microsoft.Xna.Framework.Content;<br/>
using Microsoft.Xna.Framework.GamerServices;<br/>
using Microsoft.Xna.Framework.Graphics;<br/>
using Microsoft.Xna.Framework.Input;<br/>
using Microsoft.Xna.Framework.Media;<br/>
<br/>
namespace ShaderEffects1<br/>
{<br/>
/// <summary><br/>
/// This is the main type for your game<br/>
/// </summary><br/>
public class Game1 : Microsoft.Xna.Framework.Game<br/>
{<br/>
GraphicsDeviceManager graphics;<br/>
SpriteBatch spriteBatch;<br/>
Model model;<br/>
BasicEffect effect;<br/>
Texture2D texture;<br/>
VertexBuffer vb;<br/>
<br/>
public Game1()<br/>
{<br/>
graphics = new GraphicsDeviceManager(this);<br/>
Content.RootDirectory = "Content";<br/>
}<br/>
<br/>
/// <summary><br/>
/// Allows the game to perform any initialization it needs to before starting to run.<br/>
/// This is where it can query for any required services and load any non-graphic<br/>
/// related content. Calling base.Initialize will enumerate through any components<br/>
/// and initialize them as well.<br/>
/// </summary><br/>
protected override void Initialize()<br/>
{<br/>
// TODO: Add your initialization logic here<br/>
<br/>
base.Initialize();<br/>
}<br/>
<br/>
/*struct VertexColor : IVertexType<br/>
{<br/>
public Vector3 position;<br/>
public Vector3 normal;<br/>
public Vector2 uv;<br/>
public Color color;<br/>
public static readonly VertexDeclaration _decl = new VertexDeclaration(new VertexElement(0, VertexElementFormat.Vector3, VertexElementUsage.Position, 0), new VertexElement(12, VertexElementFormat.Vector3,
VertexElementUsage.Normal, 0), new VertexElement(24, VertexElementFormat.Vector2, VertexElementUsage.TextureCoordinate, 0), new VertexElement(32, VertexElementFormat.Color, VertexElementUsage.Color, 0));<br/>
public VertexColor(VertexPositionNormalTexture data)<br/>
{<br/>
position = data.Position;<br/>
normal = data.Normal;<br/>
uv = data.TextureCoordinate;<br/>
color = Color.White;<br/>
}<br/>
public VertexDeclaration VertexDeclaration<br/>
{<br/>
get { return _decl; }<br/>
}<br/>
} */<br/>
<br/>
/// <summary><br/>
/// LoadContent will be called once per game and is the place to load<br/>
/// all of your content.<br/>
/// </summary><br/>
protected override void LoadContent()<br/>
{<br/>
// Create a new SpriteBatch, which can be used to draw textures.<br/>
spriteBatch = new SpriteBatch(GraphicsDevice);<br/>
model = Content.Load<Model>("box");<br/>
texture = Content.Load<Texture2D>("cat");<br/>
effect = new BasicEffect(GraphicsDevice);<br/>
effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, GraphicsDevice.Viewport.AspectRatio, 1.0f, 100.0f);<br/>
effect.View = Matrix.CreateLookAt(new Vector3(2, 3, 2), Vector3.Zero, Vector3.Up);<br/>
effect.TextureEnabled = true;<br/>
effect.Texture = texture;<br/>
effect.EnableDefaultLighting();<br/>
//effect.VertexColorEnabled = true;<br/>
// TODO: use this.Content to load your game content here<br/>
}<br/>
<br/>
private Matrix GetParentTransform(Model m, ModelBone mb)<br/>
{<br/>
return (mb == m.Root) ? mb.Transform : mb.Transform * GetParentTransform(m, mb.Parent);<br/>
}<br/>
<br/>
private void DrawModel(Model m, Matrix world, BasicEffect be)<br/>
{<br/>
foreach (ModelMesh mm in model.Meshes)<br/>
{<br/>
foreach (ModelMeshPart mmp in mm.MeshParts)<br/>
{<br/>
be.World = GetParentTransform(m, mm.ParentBone) * world;<br/>
GraphicsDevice.SetVertexBuffer(mmp.VertexBuffer, mmp.VertexOffset);<br/>
GraphicsDevice.Indices = mmp.IndexBuffer;<br/>
be.CurrentTechnique.Passes[0].Apply();<br/>
GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, mmp.NumVertices, mmp.StartIndex, mmp.PrimitiveCount);<br/>
}<br/>
}<br/>
}<br/>
<br/>
/// <summary><br/>
/// UnloadContent will be called once per game and is the place to unload<br/>
/// all content.<br/>
/// </summary><br/>
protected override void UnloadContent()<br/>
{<br/>
// TODO: Unload any non ContentManager content here<br/>
}<br/>
<br/>
/// <summary><br/>
/// Allows the game to run logic such as updating the world,<br/>
/// checking for collisions, gathering input, and playing audio.<br/>
/// </summary><br/>
/// <param name="gameTime Provides a snapshot of timing values.</param><br/>
protected override void Update(GameTime gameTime)<br/>
{<br/>
// Allows the game to exit<br/>
if (Keyboard.GetState().IsKeyDown(Keys.Escape) || GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)<br/>
this.Exit();<br/>
<br/>
// TODO: Add your update logic here<br/>
<br/>
base.Update(gameTime);<br/>
}<br/>
<br/>
/// <summary><br/>
/// This is called when the game should draw itself.<br/>
/// </summary><br/>
/// <param name="gameTime Provides a snapshot of timing values.</param><br/>
protected override void Draw(GameTime gameTime)<br/>
{<br/>
GraphicsDevice.Clear(Color.CornflowerBlue);<br/>
effect.SpecularPower = (float)gameTime.TotalGameTime.TotalSeconds * 2.0f;<br/>
<br/>
DrawModel(model, Matrix.Identity, effect);<br/>
<br/>
// TODO: Add your drawing code here<br/>
<br/>
base.Draw(gameTime);<br/>
}<br/>
}<br/>
}<br/>
(Please ignore the commented out things, I am using a textbook and typed a little code in advance so I could use it later)
My problem is that the effect.VertexColorEnabled makes the screen go white and my DrawModel method has an error saying it is missing a color, yet all of the parameters are satsified with their requirement. Yet, if I get rid of the vertex line, then it works
perfectly, lighting the top of the box and properly dimming the sides. Why does this line fail? My textbook just says basically to add the line and it should work.
I am using XNA Game Studio 4.0 Programming, written by Tom Miller and Dean Johnson, members of the actual XNA development team, so Im surprised by the errors whether typing errors or code errors, with no guidance about how to go about them. Any help would
be appreciated, Thanks!
Domenic
View the full article