• 追加された行はこの色です。
  • 削除された行はこの色です。
*XNA [#hd6ed4ec]
このページの情報は古いです。
2008年頃の情報です。
XNAのバージョン2.0くらいです。
[[Wikipedia:XNA:http://ja.wikipedia.org/wiki/Microsoft_XNA]]


----

#contents

** 初期設定 [#hfdb116f]
XNAのバージョン2.0くらい。
- インストールするもの
 - Visual C# 2005 Express Edition(Visual studio2005が無い人)
XNA Game Studio
 
 - Visual C# 2005 Express Edition
http://www.microsoft.com/japan/msdn/vstudio/express/past/2005/
 
 - XNA Game Studio 2.0
http://www.microsoft.com/downloads/details.aspx?FamilyId=DF80D533-BA87-40B4-ABE2-1EF12EA506B7&displaylang=en



- プロジェクトの作成
Visual C# を起動。
[ファイル]タブ -> [新しいプロジェクト] -> 左側の[プロジェクトの種類]をXNA Game Studio 2.0に -> 右側の[Windows Game(2.0)]を選択 -> OK
 
あとは普通にビルドすれば青い画面が表示されるはずです。


** Vsync同期をOFFにする [#ld52e0ab]
 GraphicsDeviceManager.SynchronizeWithVerticalRetrace = false;

** パフォーマンス計測 [#m195c67b]
 System.Diagnostics.Stopwatch Stopwatch = new System.Diagnostics.Stopwatch();
 Stopwatch.Start();
 /// コード
 Stopwatch.End();
 
 float time = Stopwatch.ElapsedTicks / (float)(System.Diagnostics.Stopwatch.Frequency * (1.0f / 60.0f)) * 100.0f;

** カリング面の変更 [#n73b748b]
 GraphicsDevice.RenderState.CullMode = ;
 
 // モードの種類
 CullMode.CullCounterClockwiseFace;
 CullMode.CullClockwiseFace;
 CullMode.None;

** 深度バッファ(Zバッファ)をONにする [#cc1b5428]
 GraphicsDevice.RenderState.DepthBufferEnable = true;





** ポリゴンの描画 [#y59313f3]
XNAのバージョン2.0くらい。

- 変数
 VertexDeclaration   m_VertexDeclaration = null; // 頂点データ定義
 VertexPositionColor[] m_Vertices = null; // 頂点データ
 BasicEffect      m_BasicEffect = null; // シェーダ


- コンストラクタ
 // 頂点データ定義
 m_VertexDeclaration = new VertexDeclaration(GraphicsDevice, VertexPositionColor.VertexElements);
  
 // 頂点データ
 m_Vertices = new VertexPositionColor[3];
  
 m_Vertices[0] = new VertexPositionColor(new Vector3(0.0f, 3.0f, 0.0f), Color.Red);
 m_Vertices[1] = new VertexPositionColor(new Vector3(3.0f, -2.0f, 0.0f), Color.Blue);
 m_Vertices[2] = new VertexPositionColor(new Vector3(-3.0f, -2.0f, 0.0f), Color.Green);
  
 // 基本シェーダ
 m_BasicEffect = new BasicEffect(GraphicsDevice, null);
 m_BasicEffect.VertexColorEnabled = true;


- 描画
 // 頂点データの定義
 GraphicsDevice.VertexDeclaration = m_VertexDeclaration;
  
 // ViewMatrix
 m_BasicEffect.View = Matrix.CreateLookAt(
   new Vector3(0.0f, 0.0f, 10.0f), // pos
   Vector3.Zero, // view
   Vector3.Up // upper
 );
  
 // Projection
 m_BasicEffect.Projection = Matrix.CreatePerspectiveFieldOfView(
   MathHelper.ToRadians(45.0f), // pers
   (float)GraphicsDevice.Viewport.Width / (float)GraphicsDevice.Viewport.Height, // aspect
   1.0f, // near
   100.0f // far
 );
  
 // begin effect
 m_BasicEffect.Begin();
  
 // パスの数だけ描画
 foreach (EffectPass pass in m_BasicEffect.CurrentTechnique.Passes)
 {
   pass.Begin();
 
   // ポリゴンを描画する
   GraphicsDevice.DrawUserPrimitives(
     PrimitiveType.TriangleList, // 描画モード
     m_Vertices, // 頂点データ
     0, // 
     1 // ポリゴン枚数
   );
   pass.End();
 }
 m_BasicEffect.End();


** シェーダ [#id53f63e]
*** Contentで生成 [#h1210f0d]
 Effect effect = i_Content.Load("shader/test"); // ./Shader/test.fxをロード
 effect.CurrentTechnique = effect.Techniques["main"]; // Technique:mainを使用
 EffectParameter uniform = effect.Parameters["WVP"]; // Uniform値を取得


*** 文字列から生成 [#ce7d806b]
 string src = "..." // ここにソース文字列
 CompiledEffect compiledEffect = Effect.CompileEffectFromSource(src, null, null, CompilerOptions.Debug, TargetPlatform.Windows);
 Effect effect = new Effect(GraphicsDevice,
   compiledEffect.GetEffectCode(),
   CompilerOptions.Debug,
   null);
 effect.CurrentTechnique = effect.Techniques["main"];
 EffectParameter uniform = effect.Parameters["WVP"];




*** 使用 [#rea7d6e7]
 uniform.SetValue(Matrix);
 effect.Begin();
 foreach(EffectPass pass in effect.CurrentTechnique.Passes)
 {
   pass.Begin();
   // 描画
   pass.End();
 }
 effect.End();

*** 生成時のエラー:X3000 [#jd879c51]
なにやら文字コードの関係で、日本語を使用すると発生する可能性有り。
Asciiに戻してからロードするか、コメントも英語でやるか。


** CONTENT [#wf839a04]
外部リソースデータをコンパイルして使用できるようにする物です。

***一番簡単な生のtxtをstringに落とし込む事を。 [#c189e5e4]

1.ソリューション エクスプローラ
->ソリューションを右クリック
->追加
->新しいプロジェクト
->Content Pipeline Extension Library

2.できた.csに以下の記述を。
 //========================================
 // インポーター
 //========================================
 // Attributeで拡張子と、説明文を設定する
 [ContentImporter(".txt", DisplayName = "TextFileImporter")]
 public class TextContentImporter : ContentImporter
 {
   // 読み込みメソッドのオーバーライド
   public override string Import(string hfilename, ContentImporterContext hcontext)
   {
     string ret = "";
     using (StreamReader reader = File.OpenText(hfilename))
     {
       ret = reader.ReadToEnd();
     }
     return ret;
   }
 }

3.ビルド

4.実行プロジェクトの方へ戻る
Contentに"test.txt"を追加
->右クリック
->プロパティ
->パラメータを以下のように
 ContentImporter:TextFileImporter(先ほどライブラリとして作ったクラスのAttribute名)
 ContentProcessor:No Processing Required
 ビルドアクション:コンパイル

->Microsoft.Xna.Framework.Gameを継承したクラスのLoadContentで
string txt = Content.Load("test.txt");

で読み込めているはず。


** RenderTexture [#p60b3114]
*** 初期化 [#x0e3062b]
 GraphicsDevice device      = getDevice();  // デバイス
 RenderTarget2D target      = null;
 RenderTarget   last_target = null;
 target = new RenderTarget2D(device,
   i_Width,
   i_Height,
   1,    // mip level
   SurfaceFormat.Bgr565); // format

*** 設定 [#t0460b8b]
 last_target = device.GetRenderTarget(0);
 device.SetRenderTarget(0, target);  // 0番のターゲットを
 device.Clear(ClearOptions.Target, Color.CornflowerBlue, 0, 0);


設定した後は普通にモデル描画

*** 解除 [#g311fc7c]
 device.SetRenderTarget(0, last_target as RenderTarget2D);
 last_target = null;


*** テクスチャとして、描画したものを取得 [#s3109689]
 Texture2D tex = target.GetTexture();







** Tips [#af351901]



    ホーム 一覧 単語検索 最終更新 バックアップ リンク元   ヘルプ   最終更新のRSS