| Poster | : cpyburn | | Posts | : 29 | | Country | : UnitedStats | | City | : Memphis |
| | | | Posted by cpyburn on 28/05/2008 at 16:00:45
| | //XNA 2.0 -Triangle Strip- Make sure you added streettexture.dds from last chapter to your content folder
using System;
using System.Collections.Generic;
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.Net;
using Microsoft.Xna.Framework.Storage;
namespace XNAtutorialSeries3
// Altered for XNA 2.0 using Series 1 Your first triangle as Starting Point and Making Changes for Series 3
// Everything added by me is // Added and ended with // Ended. This follows everything up to this point.
// I will continue to update these and post on the forums as I complete them in my free time.
// This tutorial assumes you made a FX file named OurHLSLfile.fx. If you need help, go to content, right click, add, new item, effects file
// Cut and paiste this code in below. Alteration By: Cpyburn
/* DONT INCLUDE THE COMMENT CODE AKA /**/
/* //DONT INCLUDE THIS LINE
struct VertexToPixel
{
float4 Position : POSITION;
float2 TexCoords : TEXCOORD0;
};
struct PixelToFrame
{
float4 Color : COLOR0;
};
float4x4 xViewProjection;
Texture xColoredTexture;
sampler ColoredTextureSampler = sampler_state { texture = <xColoredTexture> ; magfilter = LINEAR; minfilter = LINEAR; mipfilter=LINEAR; AddressU = mirror; AddressV = mirror;};
VertexToPixel SimplestVertexShader( float4 inPos : POSITION, float2 inTexCoords : TEXCOORD0)
{
VertexToPixel Output = (VertexToPixel)0;
Output.Position = mul(inPos, xViewProjection);
Output.TexCoords = inTexCoords;
return Output;
}
PixelToFrame OurFirstPixelShader(VertexToPixel PSIn)
{
PixelToFrame Output = (PixelToFrame)0;
Output.Color = tex2D(ColoredTextureSampler, PSIn.TexCoords);
return Output;
}
technique Simplest
{
pass Pass0
{
VertexShader = compile vs_1_1 SimplestVertexShader();
PixelShader = compile ps_1_1 OurFirstPixelShader();
}
}
*/
//DONT INCLUDE THIS LINE
{
public class Game1 : Microsoft.Xna.Framework.Game
{
// Added
private struct myownvertexformat
{
private Vector3 position;
//private Color color; // Removed
private Vector2 TexCoord; // Added
public myownvertexformat(Vector3 position, Vector2 TexCoord) // Removed Color color parameter
{
this.position = position;
//this.color = color; // Removed
this.TexCoord = TexCoord; // Added
}
public static VertexElement[] Elements =
{
new VertexElement(0, 0, VertexElementFormat.Vector3, VertexElementMethod.Default, VertexElementUsage.Position, 0),
new VertexElement(0, sizeof(float)*3, VertexElementFormat.Vector2, VertexElementMethod.Default, VertexElementUsage.TextureCoordinate, 0), // Fog
};
public static int SizeInBytes = sizeof(float) * (3 + 2); // Altered (3 + 1) because vector2 uses 2 floats, color only used one
}// End Added
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
GraphicsDevice device;
Effect effect;
// Added
//VertexPositionColor[] vertices;
VertexBuffer vb;
VertexDeclaration myVertexDeclaration;
Matrix viewMatrix;
Matrix projectionMatrix;
Texture2D StreetTexture;
// End Added
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
graphics.PreferredBackBufferWidth = 500;
graphics.PreferredBackBufferHeight = 500;
graphics.IsFullScreen = false;
graphics.ApplyChanges();
Window.Title = "Riemers series 3 altered by cpyburn to xna 2.0";
base.Initialize();
}
// Added
private void SetUpVertices()
{
myownvertexformat[] vertices = new myownvertexformat[12];
vertices[0] = new myownvertexformat(new Vector3(-20, -10, 0), new Vector2(-0.25f, 25.0f));
vertices[1] = new myownvertexformat(new Vector3(-20, 100, 0), new Vector2(-0.25f, 0.0f));
vertices[2] = new myownvertexformat(new Vector3(2, -10, 0), new Vector2(0.25f, 25.0f));
vertices[3] = new myownvertexformat(new Vector3(2, 100, 0), new Vector2(0.25f, 0.0f));
vertices[4] = new myownvertexformat(new Vector3(2, -10, 1), new Vector2(0.375f, 25.0f));
vertices[5] = new myownvertexformat(new Vector3(2, 100, 1), new Vector2(0.375f, 0.0f));
vertices[6] = new myownvertexformat(new Vector3(3, -10, 1), new Vector2(0.5f, 25.0f));
vertices[7] = new myownvertexformat(new Vector3(3, 100, 1), new Vector2(0.5f, 0.0f));
vertices[8] = new myownvertexformat(new Vector3(13, -10, 1), new Vector2(0.75f, 25.0f));
vertices[9] = new myownvertexformat(new Vector3(13, 100, 1), new Vector2(0.75f, 0.0f));
vertices[10] = new myownvertexformat(new Vector3(13, -10, 21), new Vector2(1.25f, 25.0f));
vertices[11] = new myownvertexformat(new Vector3(13, 100, 21), new Vector2(1.25f, 0.0f));
vb = new VertexBuffer(device, myownvertexformat.SizeInBytes * 12, BufferUsage.WriteOnly);
vb.SetData(vertices);
myVertexDeclaration = new VertexDeclaration(device, myownvertexformat.Elements);
// End Added
}// End Added
protected override void LoadContent()
{
device = graphics.GraphicsDevice;
spriteBatch = new SpriteBatch(GraphicsDevice);
effect = Content.Load<Effect>("OurHLSLfile");
// Added
StreetTexture = Content.Load<Texture2D>("streettexture");
SetUpVertices();
SetUpCamera();
// End Added
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
device.Clear(Color.DarkSlateBlue);
// Added
effect.CurrentTechnique = effect.Techniques["Simplest"];
//effect.Parameters["xView"].SetValue(viewMatrix);
effect.Parameters["xViewProjection"].SetValue(viewMatrix * projectionMatrix);
effect.Parameters["xColoredTexture"].SetValue(StreetTexture); // Added to send texture to the .FX file
//effect.Parameters["xWorld"].SetValue(Matrix.Identity);
// End Added
effect.Begin();
foreach (EffectPass pass in effect.CurrentTechnique.Passes)
{
pass.Begin();
// Added
device.VertexDeclaration = myVertexDeclaration;
device.Vertices[0].SetSource(vb, 0, myownvertexformat.SizeInBytes); // Make sure to pur the correct size!!!
device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 10);
// End Added
pass.End();
}
effect.End();
base.Draw(gameTime);
}
// Added
private void SetUpCamera()
{
viewMatrix = Matrix.CreateLookAt(new Vector3(-25, -18, 13), new Vector3(0, 12, 2), new Vector3(0, 0, 1)); // Grrrr I hate when Z is up?
projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, device.Viewport.AspectRatio, 1.0f, 300.0f);
}// End Added
}
}
| |
|
|