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 TextureEffectEx {
public class Game : Microsoft.Xna.Framework.Game {
//システム
private GraphicsDeviceManager graphics; //グラフィックス
private float aspectRatio;//アスペクト比
//モデル
private Model model; //モデル
private Texture texture;//テクスチャ
//エフェクト
private Effect effect; //エフェクト
private EffectParameter worldViewProjParam;//変換行列
//コンストラクタ
public Game() {
graphics=new GraphicsDeviceManager(this);
Content.RootDirectory="Content";
}
//初期化
protected override void Initialize() {
base.Initialize();
}
//グラフィクスコンテントの読み込み
protected override void LoadContent() {
//アスペクト比
aspectRatio=
(float)GraphicsDevice.Viewport.Width/
(float)GraphicsDevice.Viewport.Height;
//モデルの読み込み
model=Content.Load<Model>("box");
//テクスチャの読み込み
texture=Content.Load<Texture2D>("mokume2");
//エフェクトの読み込み
effect=Content.Load<Effect>("TextureEffect");
effect.CurrentTechnique=effect.Techniques["TextureTech"];
worldViewProjParam=effect.Parameters["worldViewProj"];
//エフェクトにテクスチャを関連付け
effect.Parameters["cubeTexture"].SetValue(texture);
//モデルにエフェクトを関連付け
foreach (ModelMesh mesh in model.Meshes) {
for(int i=0;i<mesh.MeshParts.Count;i++) {
mesh.MeshParts[i].Effect=effect;
}
}
//ワールド変換行列
Matrix world=
Matrix.CreateTranslation(new Vector3(0.0f,0.0f,0.0f));//モデル位置
//ビュー変換行列
Matrix view=Matrix.CreateLookAt(
new Vector3(5.0f,5.0f,5.0f),//カメラ位置
Vector3.Zero, //参照点
Vector3.Up //UPベクトル
);
//射影変換行列
Matrix projection=Matrix.CreatePerspectiveFieldOfView(
MathHelper.ToRadians(45.0f),//視野角
aspectRatio, //アスペクト比
1.0f, //ニアクリップ面の距離
100.0f //ファークリップ面の距離
);
//エフェクトのコミットチェンジ
effect.CommitChanges();
//ワールドxビューx射影行列
worldViewProjParam.SetValue(world*view*projection);
}
//グラフィクスコンテントの解放
protected override void UnloadContent() {
}
//更新
protected override void Update(GameTime gameTime) {
base.Update(gameTime);
}
//描画
protected override void Draw(GameTime gameTime) {
//背景色
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
//モデルの描画
foreach (ModelMesh mesh in model.Meshes) {
mesh.Draw();
}
//ベースの描画
base.Draw(gameTime);
}
}
}
|