PlayStation Mobile Studio
 PlayStation(R)Mobile SDK 1.21.01

■Playstation Mobile インデックスバッファとテクスチャーを使用する Prev Top Next
関連ページ:なし


説明するようなことは何もないなあ。

Simple.vcg バーテックスシェーダーのcgファイル
Simple.fcg フラグメントシェーダーのcgファイル
AppMain.cs main関数があるソースファイル


---Simple.vcg---  ↑

// バーテックスシェーダー

void main( float3 in a_Position  : POSITION,
           float2 in a_Texel     : TEXCOORD,
           float4 out v_Position : POSITION,
           float2 out v_Texel    : TEXCOORD,
           uniform float4x4 WorldViewProj
                  )
{
   v_Position = mul( float4( a_Position, 1 ), WorldViewProj );
   v_Texel = a_Texel;
}

---Simple.fcg---  ↑

// フラグメントシェーダー( Direct3Dでいうところのピクセルシェーダー )

void main( float2 in v_Texel  : TEXCOORD,
           uniform sampler2D s_Texture : TEXUNIT0,
           float4 out Color   : COLOR )
{
   Color = tex2D( s_Texture, v_Texel );
}

---AppMain.cs---  ↑


using System;
using System.Collections.Generic;

using Sce.PlayStation.Core;
using Sce.PlayStation.Core.Environment;
using Sce.PlayStation.Core.Graphics;
using Sce.PlayStation.Core.Input;

namespace PSM_Samples
{
   public class AppMain : IDisposable
   {
      const int Width = 800;
      const int Height = 480;

      private static GraphicsContext graphics;
      private static ShaderProgram shader;
      private static VertexBuffer triangles;
      // テクスチャー
      private static Texture2D texture;

      private static float rotationY;
      private static Matrix4 matWVP;

      public static void Main (string[] args)
      {
         Initialize ();

         while (true) 
         {
            SystemEvents.CheckEvents ();
            Update ();
            Render ();
         }
      }

      // 初期化
      public static void Initialize ()
      {
         // フレームバッファの解像度はウィンドウの解像度と同じサイズにする
         graphics = new GraphicsContext( Width, Height, PixelFormat.Rgba, PixelFormat.Depth16, MultiSampleMode.None );

         // シェーダーの作成
         // cgxはvcgとfcgをコンパイル後、結合したcgファイルらしい
         // パスは固定でないとだめ?
         shader = new ShaderProgram("/Application/shaders/Simple.cgx" );

         // アトリビュート変数( 頂点バッファから渡される変数 )の変数名とインデックスの関連付け
         shader.SetAttributeBinding( 0, "a_Position" );
         shader.SetAttributeBinding( 1, "a_Texel" );
         // ユニフォーム変数( Direct3Dでいうところの定数バッファ )の変数名とインデック値の関連付け
         shader.SetUniformBinding( 0, "WorldViewProj" );

         // 頂点バッファ作成( インデックスバッファの配列数を第2引数に追加し、頂点カラーをテクセル座標に変更 )
         triangles = new VertexBuffer( 4, 6, VertexFormat.Float3, VertexFormat.Float2 );
         // 頂点座標を設定
         triangles.SetVertices( 0,                     // エレメント0
                           new float[]{-20f,  20f, 0f,
                                        20f,  20f, 0f,
                                        20f, -20f, 0f,
                                       -20f, -20f, 0f } );
         // テクセルを設定
         triangles.SetVertices( 1,                     // エレメント1
                            new float[]{ 0f, 0f,
                                         1f, 0f,
                                         1f, 1f,
                                         0f, 1f } );
         // インデックスバッファ作成
         triangles.SetIndices( new ushort[]{ 1, 0, 3, 1, 3, 2 } );

         // テクスチャー( 当然だがddsファイルはサポートしない )
         texture = new Texture2D( "/Application/resources/Texture.png", false );
         texture.SetFilter( TextureFilterMode.Nearest );
         texture.SetWrap( TextureWrapMode.Repeat );

         rotationY = 0;
      }

      public void Dispose()
      {
         texture.Dispose();
         triangles.Dispose();
         shader.Dispose();
         graphics.Dispose();
      }

      // フレーム更新処理
      public static void Update ()
      {
         rotationY += 0.01f;
         if( rotationY > (float)FMath.PI * 2.0f )
            rotationY -= (float)FMath.PI * 2.0f;

         // ワールド行列
         Matrix4 matRotationY = Matrix4.RotationY( rotationY );
         Matrix4 matTranslation = Matrix4.Translation( 0.0f, 0.0f, -100.0f );  // OpenGL は右手座標系のためZ値が負の数となる )
         Matrix4 matWorld = matTranslation * matRotationY;  // 積算方向が右から左へ

         // 射影行列
         Matrix4 matProj = Matrix4.Perspective( (float)FMath.PI / 5.0f, (float)Width / (float)Height, 10.0f, 1000.0f );

         matWVP = matProj * matWorld;
      }

      // レンダリング
      public static void Render ()
      {
         graphics.SetClearColor (0.2f, 0.2f, 1, 1 );
         // 描画用フレームバッファ( Direct3Dでいうところのバックバッファ )をクリア
         graphics.Clear ();

         // カリングモードを有効にする
         graphics.Enable( EnableMode.CullFace, true );
         // 反時計回りで裏面となるポリゴンを描画しない
         graphics.SetCullFace( CullFaceMode.Back, CullFaceDirection.Ccw );

         // ブレンドモードを無効にする
         graphics.Enable( EnableMode.Blend, false );

         // 深度テストを無効にする
         graphics.Enable( EnableMode.DepthTest, false );

         // ユニフォーム変数をシェーダーに設定
         shader.SetUniformValue( 0, ref matWVP );

         // シェーダーを設定
         graphics.SetShaderProgram( shader );

         // 頂点バッファを設定
         graphics.SetVertexBuffer( 0, triangles );

         // テクスチャーを設定する
         graphics.SetTexture( 0, texture );

         // レンダリング( インデックスバッファを使用するときは頂点数ではなくインデックスバッファ数を指定するっポイ )
         graphics.DrawArrays( DrawMode.Triangles, 0, triangles.IndexCount );

         // スワップ
         graphics.SwapBuffers ();
      }
   }
}

Applicationフォルダ直下にresourcesフォルダを作成してテクスチャーを置いてください。
ドキュメントを確認したところ、手動でテクスチャーを置いただけだと実機にリソースを転送できないかもしれません。
ですので、以下の手順でPSM Studioのソリューションエクスプローラの設定を行ってください。

1.PSM Studioのソリューションエクスプローラ上で [ 追加 - 新しいフォルダ ] をクリックして、resourcesフォルダを作成します。
2.resourcesフォルダ上で右クリックし、[ 追加 - ファイルを追加 ] をクリックします。
3.「ファイルを追加」ウィンドウ上でテクスチャーを選択して追加します。

4.追加したテクスチャー上で右クリックし、「ビルドアクション」で「Content」を選択します。

試していないので断定できませんが、まあドキュメントにそう書いてあるのでやったほうがいいでしょう。


最後に注意点について説明します。
まずリソースのパスおよびファイル名は大文字小文字区別してファイル検索を行います。Windows上では大文字小文字の区別が行われないためシミュレータ上では問題なく動作しますが、
Vita上で動作しなくなりますので、ご注意ください。

次にテクスチャーについてです。Windows上では問題なかったのですが、Vita上では1成分8ビットつまりRGBアルファチャンネルありで 8 x 4 = 32ビットで作成する必要があるようです。
ご注意ください。


Prev Top Next
inserted by FC2 system