3.3.11. Complete source code
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;
using BetaCell.Debug;
using BetaCell.Util.GlobalInfo.Content;
using BetaCell.Environment.Camera;
using BetaCell.Util.GlobalInfo;
using BetaCell.Environment.Terrain;
using BetaCell.Environment.Light;
using BetaCell.Environment.Mesh.Visitors;
using BetaCell.Environment.Mesh;
using BetaCell.Environment;
using BetaCell.Effects.Composer;
using BetaCell.Environment.Texture.Feeder;
using BetaCell.Environment.Texture;
using BetaCell.Effects;
using BetaCell.Common.Vertex;
namespace Starter
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
//Game attributes
GraphicsDeviceManager
graphics;
GraphicsDevice device;
ContentManager content;
BCFirstPersonHumanCamera
camera;
//end game attributes
//This tutorial's attributes
BCTerrain floor;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
graphics.PreferredDepthStencilFormat = DepthFormat.Depth24Stencil8;
content = new ContentManager(Services);
#if(XBOX360)
graphics.PreferredBackBufferWidth = 1280;
graphics.PreferredBackBufferHeight = 720;
#else
graphics.PreferredBackBufferWidth = 852;
graphics.PreferredBackBufferHeight = 480;
#endif
}
protected override void
Initialize()
{
//base initialization
device = graphics.GraphicsDevice;
BCLogger.instance.init(device, content);
BCInitializationManager.initialize(content);
OnActivated(null, null);
BCMaterialBase material = new BCMaterialBase();
material.specRange = 0.8f;
material.diff = Color.Gray;
material.spec = Color.White;
material.amb = Color.DarkGray;
BCMaterialVisitor materialVisitor = new BCMaterialVisitor(material);
//Color set effect composition part
BCColorSet colorSet = new BCColorSet(Color.Black);
BCFunction baseColor = colorSet.getFunction("Shader.ColorSet");
BCDynamicEffectVisitor colorSetter = new
BCDynamicEffectVisitor(baseColor, colorSet, 0);
//Light effect composition part
BCBasicLight light = new BCBasicLight();
light.transform = Matrix.CreateTranslation(-1f, -1f, -1f);
light.range = 3000;
light.cubAtten = 0;
light.constAtten = 1;
light.linAtten = 0;
light.useShadows = false;
BCFunction lightPart =
light.getFunction("Shader.Light.Classic.VertexDirectionLight");
BCDynamicEffectVisitor lightSetter = new
BCDynamicEffectVisitor(lightPart, light, 1);
//Creating a sampler for the texture
BCSampler sampler = new BCSampler(
"LINEAR", "LINEAR", "LINEAR", "4",
"WRAP", "WRAP", "0xffffffff"
);
//Creating a texture feeder for the texture
//Adding a sampler per expected texture of the channel blend effect part
BCTextureFeeder texFeeder = new BCTextureFeeder();
texFeeder.addSampler("gBlendMap", sampler);
texFeeder.addSampler("gTex0", sampler);
texFeeder.addSampler("gTex1", sampler);
texFeeder.addSampler("gTex2", sampler);
//Getting the texture effect part function from the texture feeder
BCFunction channelBlendPart =
texFeeder.getFunction("Shader.Texture.ChannelBlend");
//Creating the texture effect setter visitor
BCDynamicEffectVisitor textureEffectSetter =
new BCDynamicEffectVisitor(channelBlendPart, texFeeder,
BCDynamicEffect.MAX_FUNCTIONS);
//Setting texture
Texture2D reference =
content.Load<Texture2D>("Content/gameAssets/channelsR");
BCMemoryTextureStrategy referenceStrategy = new
BCMemoryTextureStrategy(reference);
BCTextureVisitor blendMapTex = new BCTextureVisitor("gBlendMap",
referenceStrategy);
//Alternate way to initiate the texture in a single definition
BCTextureVisitor grassTex = new BCTextureVisitor(
"gTex0",
new
BCMemoryTextureStrategy(content.Load<Texture2D>("Content/gameAssets/grass"))
);
grassTex.strategy.setScale(new Vector2(30, 30));
BCTextureVisitor roadTex = new BCTextureVisitor(
"gTex1",
new
BCMemoryTextureStrategy(content.Load<Texture2D>("Content/gameAssets/stone"))
);
roadTex.strategy.setScale(new Vector2(30, 30));
BCTextureVisitor stoneTex = new BCTextureVisitor(
"gTex2",
new
BCMemoryTextureStrategy(content.Load<Texture2D>("Content/gameAssets/road"))
);
stoneTex.strategy.setScale(new Vector2(30, 30));
floor = new BCTerrain(device,
"Content/gameAssets/hm_128", 1.0f, 1.0f, 128, 128,
0.8f, 0, 1, 2, null, new BCVertexPosNorTexContainer(), content);
BCMeshGraphicUtil.init(floor.terrainMesh, device);
floor.terrainMesh.visit(blendMapTex);
floor.terrainMesh.visit(grassTex);
floor.terrainMesh.visit(roadTex);
floor.terrainMesh.visit(stoneTex);
floor.terrainMesh.visit(materialVisitor);
floor.terrainMesh.visit(colorSetter);
floor.terrainMesh.visit(lightSetter);
floor.terrainMesh.visit(textureEffectSetter);
base.Initialize();
}
protected override void
OnActivated(object sender, EventArgs args)
{
buildViewMatrix();
base.OnActivated(sender, args);
}
void buildViewMatrix()
{
Vector3 pos = new Vector3(-20, 100, -20);
Vector3 look = new Vector3(0, 0, 1);
look.Normalize();
camera = new BCFirstPersonHumanCamera(look, pos,
MathHelper.PiOver4,
(float)this.Window.ClientBounds.Width /
(float)this.Window.ClientBounds.Height,
1f, 200);
camera.attachWalkable(floor, 2);
BCGlobalInfo.instance.setMatrix(
BCGlobalInfo.VIEW_INDEX,
camera.getViewMatrix(-1)
);
BCGlobalInfo.instance.setMatrix(
BCGlobalInfo.PROJECTION_INDEX,
camera.getProjectionMatrix(-1)
);
}
protected override void
Update(GameTime gameTime)
{
moveCamera(gameTime);
}
private void
moveCamera(GameTime gameTime)
{
float df = 0;
float ds = 0;
float dy = 0;
float pitch = 0;
float angleY = 0;
float speed = 5f;
#if (XBOX360)
GamePadState gamepad = GamePad.GetState(PlayerIndex.One);
df =
gamepad.ThumbSticks.Left.Y;
ds =
gamepad.ThumbSticks.Left.X;
pitch = -gamepad.ThumbSticks.Right.Y * 0.05f;
angleY = gamepad.ThumbSticks.Right.X * 0.05f;
if
(gamepad.IsButtonDown(Buttons.RightShoulder))
{
dy += 0.25f;
}
if
(gamepad.IsButtonDown(Buttons.LeftShoulder))
{
dy -= 0.25f;
}
speed = new Vector3(df, ds, dy).Length() * 30;
#else
KeyboardState keys = Keyboard.GetState();
MouseState mouse = Mouse.GetState();
if
(keys.IsKeyDown(Keys.W))
{
df += 1;
}
if
(keys.IsKeyDown(Keys.S))
{
df -= 1;
}
if
(keys.IsKeyDown(Keys.D))
{
ds += 1;
}
if
(keys.IsKeyDown(Keys.A))
{
ds -= 1;
}
if
(keys.IsKeyDown(Keys.Q))
{
dy += 1;
}
if
(keys.IsKeyDown(Keys.E))
{
dy -= 1;
}
speed = 10f;
if
(keys.IsKeyDown(Keys.LeftControl))
{
speed = 20f;
}
angleY = ((float)this.Window.ClientBounds.Width / 2.0f -
(float)mouse.X) * -0.05f;
pitch = ((float)this.Window.ClientBounds.Height / 2.0f -
(float)mouse.Y) * -0.05f;
Mouse.SetPosition(this.Window.ClientBounds.Width / 2,
this.Window.ClientBounds.Height / 2);
#endif
camera.update(df, dy, ds, pitch, angleY, speed,
(float)gameTime.ElapsedGameTime.TotalSeconds);
BCGlobalInfo.instance.setMatrix(BCGlobalInfo.VIEW_INDEX,
camera.getViewMatrix(-1));
}
protected override void
Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(ClearOptions.Target |
ClearOptions.DepthBuffer, Color.White, 1.0f, 0);
floor.terrainMesh.Draw(gameTime);
BCLogger.instance.printFPS(gameTime);
BCLogger.instance.flush();
base.Draw(gameTime);
}
}
}