とりあえず、3D空間に三角形を表示するプログラムを書いてみた。シェーダも単純で、頂点シェーダはプログラムから受け取った頂点座標をそのまま設定しているだけ。ピクセルシェーダもUniformな変数で受け取った物をそのまま設定しているだけ。以下新規プロジェクトのテンプレートにちょっとだけ追加したソースコード。シェーダのファイル名は頂点シェーダがtest.vcg、ピクセルシェーダがtest.fcg。
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 Triangle { public class AppMain { private static GraphicsContext graphics; private static ShaderProgram program; private static VertexBuffer vbuffer; //ここで三角形の色を指定する private static Vector4 triangleColor = new Vector4(1.0f, 1.0f, 1.0f, 1.0f); public static void Main (string[] args) { Initialize(); while(true){ SystemEvents.CheckEvents(); Update(); Render(); } } public static void Initialize() { graphics = new GraphicsContext(); program = new ShaderProgram("/Application/shaders/test.cgx"); vbuffer = new VertexBuffer(3, VertexFormat.Float3); //シェーダのUniform変数の0番目は三角形の色 program.SetUniformBinding(0, "MaterialColor"); //シェーダに渡すAttribute変数の0番目は頂点座標 program.SetAttributeBinding(0, "a_Position"); //三角形の頂点座標(x, y, z) float[] positions = { 0.0f, 0.577f, 0.0f, -0.5f, -0.289f, 0.0f, 0.5f, -0.289f, 0.0f, }; vbuffer.SetVertices(0, positions); } public static void Update() { var gamePadData = GamePad.GetData(0); } public static void Render() { graphics.SetViewport(0, 0, graphics.Screen.Width, graphics.Screen.Height); graphics.SetClearColor(0.0f, 0.0f, 0.0f, 0.0f); graphics.Clear(); graphics.SetShaderProgram(program); graphics.SetVertexBuffer(0, vbuffer); //シェーダに三角形の色を渡す program.SetUniformValue(0, ref triangleColor); graphics.DrawArrays(DrawMode.TriangleStrip, 0, 3); graphics.SwapBuffers(); } } }
頂点シェーダ
void main(float4 in a_Position : POSITION,
float4 out v_Position : POSITION)
{
v_Position = a_Position;
}
ピクセルシェーダ
void main(float4 out Color : COLOR, uniform float4 MaterialColor)
{
Color = MaterialColor;
}