XNAでテクスチャ貼れた

ひげねこさんから教えて頂いたおかげで、BasicEffectを使ってテクスチャを表示することができた。ありがとうございます。

エフェクト(.fx)ファイルを使ったサンプルはたくさんあったのだが、HLSLなんてものは私の脳みそが受け付けてくれなかったので、BasicEffectクラスと格闘した。


各クラスのメソッド呼び出しの順序が難しいのだが、
Draw()内で
graphics.GraphicsDevice.BeginScene()
basicEffect.Begin()
pass.Begin()
graphics.GraphicsDevice.DrawUserPrimitives()
pass.End()
basicEffect.End()
graphics.GraphicsDevice.EndScene()
の順序で動いた。


ついでに、ん・ぱか工房さん>のコードを参考にキーボードのカーソルキーに反応するようにした。
丸一日かかってて、うれしいので、コードをベットリと張っておこう。
誰もいらないと思うけど、プロジェクト全体のダウンロードはこちら。WindowsGame1.zip

    • -

うあーっ、書いたあとにひげねこさんのページ見たら正解のコードが書いてあった。
頂点データの作成は、こう書くみたいです。Vector3.Backwardってのは(0, 0, 1)だそうです。


VertexPositionNormalTexture[] vertices =
{
new VertexPositionNormalTexture( new Vector3( 0, 1,0 ), Vector3.Backward, Vector2.Zero ),
new VertexPositionNormalTexture( new Vector3( 1,-1,0 ), Vector3.Backward, Vector2.Zero ),
new VertexPositionNormalTexture( new Vector3(-1,-1,0 ), Vector3.Backward, Vector2.Zero ),
};

あと、Draw()の中でnewしないで、graphics.DeviceCreatedイベントでやったほうがいいそうです。私のコードは真似しないでね。


using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Components;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;

namespace WindowsGame1 {
///


/// This is the main type for your game
///

partial class Game1 : Microsoft.Xna.Framework.Game {
private float m_fPlayerX = 0.5f;
private float m_fPlayerY = 0.5f;
public Game1() {
InitializeComponent();
}

protected override void Update() {
// The time since Update was called last
float elapsed = (float)ElapsedTime.TotalSeconds;

//http://www.saturn.dti.ne.jp/~npaka/xna/KeyEx/index.html からパクりました。すいません
//キー状態の取得
KeyboardState keyboardState = Keyboard.GetState();

//左右
if (keyboardState.IsKeyDown(Keys.Left)) {
m_fPlayerX -= 0.1f;
} else if (keyboardState.IsKeyDown(Keys.Right)) {
m_fPlayerX += 0.1f;
}

//上下
if (keyboardState.IsKeyDown(Keys.Up)) {
m_fPlayerY -= 0.1f;
} else if (keyboardState.IsKeyDown(Keys.Down)) {
m_fPlayerY += 0.1f;
}

//終了
if (keyboardState.IsKeyDown(Keys.Escape)) {
Exit();
}

// Let the GameComponents update
UpdateComponents();
}

protected override void Draw() {
// よく分かってないので、中身は信用しないで下さい
// Initializeで実行すべきことも分けずに、すべてここに書いてます

// Make sure we have a valid device
if (!graphics.EnsureDevice()) {
return;
}

VertexPositionNormalTexture[] vpnt = new VertexPositionNormalTexture[6];
//2番目と3番目の引数が分からないのでとりあえず0で初期化
vpnt[0] = new VertexPositionNormalTexture(new Vector3(-1.0f, -1.0f, 0.0f), new Vector3(0.0f, 0.0f, 0.0f), new Vector2(0.0f, 0.0f));
vpnt[1] = new VertexPositionNormalTexture(new Vector3(1.0f, -1.0f, 0.0f), new Vector3(0.0f, 0.0f, 0.0f), new Vector2(0.0f, 0.0f));
vpnt[2] = new VertexPositionNormalTexture(new Vector3(0.0f, 1.0f, 0.0f), new Vector3(0.0f, 0.0f, 0.0f), new Vector2(0.0f, 0.0f));
vpnt[3] = new VertexPositionNormalTexture(new Vector3(-1.5f, -0.5f, 0.1f), new Vector3(0.0f, 0.0f, 0.0f), new Vector2(0.0f, 0.0f));
vpnt[4] = new VertexPositionNormalTexture(new Vector3(-0.5f, -0.5f, 0.1f), new Vector3(0.0f, 0.0f, 0.0f), new Vector2(0.0f, 0.0f));
vpnt[5] = new VertexPositionNormalTexture(new Vector3(-0.5f, 0.5f, 0.1f), new Vector3(0.0f, 0.0f, 0.0f), new Vector2(0.0f, 0.0f));

graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
graphics.GraphicsDevice.BeginScene(); //--------------------------------------------------------------------------

using (VertexDeclaration decl = new VertexDeclaration(graphics.GraphicsDevice, VertexPositionColor.VertexElements)) {
graphics.GraphicsDevice.VertexDeclaration = decl;
graphics.GraphicsDevice.RenderState.CullMode = CullMode.CullClockwiseFace;
using (BasicEffect basicEffect = new BasicEffect(graphics.GraphicsDevice, null)) {

basicEffect.Alpha = 1.0f;
basicEffect.AmbientColor = new Vector3(0.1f, 0.1f, 0.1f); //環境光反射
basicEffect.DiffuseColor = new Vector3(1.0f, 1.0f, 1.0f); //拡散反射 (陰影をつける反射)
//basicEffect.EmissiveColor = new Vector3(0.5f, 0.5f, 0.5f); //放射光 (自分自身の発光)
basicEffect.SpecularColor = new Vector3(1.0f, 1.0f, 1.0f); //反射光 (鏡のような反射)
basicEffect.SpecularPower = 2.0f; //大きいほど反射しない?
basicEffect.AmbientLightColor = new Vector3(0.75f, 0.75f, 0.75f);

//点光源1
basicEffect.DirectionalLight0.Enable = true;
basicEffect.DirectionalLight0.DiffuseColor = Vector3.One;
basicEffect.DirectionalLight0.Direction = Vector3.Normalize(new Vector3(-1.0f, -1.0f, -1.0f));
basicEffect.DirectionalLight0.SpecularColor = Vector3.One;

//点光源2
basicEffect.DirectionalLight1.Enable = true;
basicEffect.DirectionalLight1.DiffuseColor = new Vector3(0.5f, 0.5f, 0.5f);
basicEffect.DirectionalLight1.Direction = Vector3.Normalize(new Vector3(-1.0f, -1.0f, 1.0f));
basicEffect.DirectionalLight1.SpecularColor = new Vector3(0.5f, 0.5f, 0.5f);

basicEffect.LightingEnable = true;

//投影行列 (投影面の角度、アスペクト比、近い方のクリッピング距離、遠い方のクリッピング距離)
basicEffect.Projection = Matrix.CreatePerspectiveFieldOfView((float)Math.PI / 2.0f, 1.0f, 0.1f, 1000.0f);
//ビュー行列 (カメラの座標、どの方向を見るか、カメラの傾き)
basicEffect.View = Matrix.CreateLookAt(new Vector3(m_fPlayerX, m_fPlayerY, 1.5f), Vector3.Zero, Vector3.Up);
//ワールド行列
basicEffect.World = Matrix.CreateScale(1.0f, 1.0f, 1.0f);

//テクスチャ
basicEffect.Texture = Texture2D.FromFile(graphics.GraphicsDevice, "Brick3.jpg");

basicEffect.CommitChanges();
basicEffect.Begin(EffectStateOptions.Default);//--------------------------------
foreach (EffectPass pass in basicEffect.Techniques["BasicShader"].Passes) {
pass.Begin();//----------------------------------------------

graphics.GraphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, 2, vpnt);
basicEffect.CommitChanges();
pass.End();//------------------------------------------------
}
basicEffect.End();//------------------------------------------------------------
}
}

// Let the GameComponents draw
DrawComponents();

graphics.GraphicsDevice.EndScene();//--------------------------------------------------------------------------
graphics.GraphicsDevice.Present();
}

}
}



(2006/09/06 00:31)
あらー? 今日コンパイルしなおしてみたら、テクスチャが出ないでシェードだけになってしまった。何もいじってないのに。謎だ。