Saturday, July 7, 2012

PlayStation Mobile SDK–Draw Texture

If i dont use “GameEngine2D”(Sce.Pss.HighLevel.GameEngine2D) ,then i need serveral steps to do.

Befor Getting Start,there are something i need to do.

First:Create Empty text file named “app.cfg” and open it,write down

 

memory:
resource_heap_size : 16384
managed_heap_size:16384
input:
gamepad : false;
touch : false;
motion : false;



 


PS: If you want “gamepad”work,then “gamepad : true”.


 


Two:Create folders : Resource,Shaders


 


Three:Create two empty text file rename “Sprite.fcg””Sprite.vcg”. (shader language)


 


Open Sprite.fcg file

void main(float2 in v_TexCoord:TEXCOORD0,float4 in v_Color:COLOR0,float4 out color:COLOR,
uniform sampler2D s_Texture:TEXUNIT0)
{
float4 tmpColor = tex2D(s_Texture,v_TexCoord);
tmpColor.r = tmpColor.r * v_Color.r;
tmpColor.g = tmpColor.g * v_Color.g;
tmpColor.b = tmpColor.b * v_Color.b;
tmpColor.a = tmpColor.a * v_Color.a;

if(tmpColor.a == 0.0f)
{
discard;
}
else
{
color = tmpColor;
}
}
Open Sprite.vcg File
void main(float4 in a_Position:POSITION,float2 in a_TexCoord:TEXCOORD0,float4 in a_VertexColor:COLOR0,
float4 out v_Position:POSITION,float2 out v_TexCoord:TEXCOORD0,float4 out v_Color:COLOR0,
uniform float4x4 u_WorldMatrix)
{
v_Position = mul(a_Position,u_WorldMatrix);
v_TexCoord = a_TexCoord;
v_Color = a_VertexColor;
}
 
Four:
Declare
        static ShaderProgram shaderProgram;
static Texture2D texture;
static float TextureWidth;
static float TextureHeight;

static float[] vertices = new float[12];
static float[] texcoords = {
0.0f,0.0f,//At Texture's Top Left
0.0f,1.0f,//At Texture's Top Right
1.0f,0.0f,//At Texture's Down Left
1.0f,1.0f,//At Texture's Down Right
};
static float[] colors = {
1.0f,1.0f,1.0f,1.0f,
1.0f,1.0f,1.0f,1.0f,
1.0f,1.0f,1.0f,1.0f,
1.0f,1.0f,1.0f,1.0f,
};

const int indexSize = 4;
static ushort[] indices;
static VertexBuffer vertexBuffer;
static Matrix4 unitScreenMatrix;


ShaderProgram: make sure using Sce.Pss.Graphics  .
Texture2D: farmilar with xna.
texcoords:Texture’s Position in shader.
colors: Set Texture color.
At Initialize Method
        public static void Initialize()
{
graphics = new GraphicsContext();
ImageRect rectScreen = graphics.Screen.Rectangle;

texture = new Texture2D("/Application/Resource/Images/Player.png",false);
shaderProgram = new ShaderProgram("/Application/Shaders/Sprite.cgx");
shaderProgram.SetUniformBinding(0,"u_WorldMatrix");

TextureWidth = texture.Width;
TextureHeight = texture.Height;

for(int i = 0; i < 3; i++)
{
vertices[i] = 0.0f;
}

vertices[3] = 0.0f;
vertices[4] = 1.0f;
vertices[5] = 0.0f;

vertices[6] = 1.0f;
vertices[7] = 0.0f;
vertices[8] = 0.0f;

vertices[9] = 1.0f;
vertices[10] = 1.0f;
vertices[11] = 0.0f;

indices = new ushort[indexSize];
indices[0] =0;
indices[1] =1;
indices[2] =2;
indices[3] =3;


//VertexBuffer
vertexBuffer = new VertexBuffer(4,indexSize,VertexFormat.Float3,VertexFormat.Float2,VertexFormat.Float4);

vertexBuffer.SetVertices(0,vertices);
vertexBuffer.SetVertices(1,texcoords);
vertexBuffer.SetVertices(2,colors);

vertexBuffer.SetIndices(indices);
graphics.SetVertexBuffer(0,vertexBuffer);

unitScreenMatrix = new Matrix4(TextureWidth * 2.0f/rectScreen.Width,0.0f,0.0f,0.0f,
0.0f,TextureHeight*(-2.0f) / rectScreen.Height,0.0f,0.0f,
0.0f,0.0f,1.0f,0.0f,
0.0f,0.0f,0.0f,1.0f
);
}
 
ImageRect: get Client Screen width and height.
Texture2D:new Texture2D(“file Location”,mipmap is false).


ShaderProgram : only when compiler getting start Sprite.cgx will be created by compiler.Make sure name is correct!
 






At Draw() Method

        public static void Draw()
{
graphics.Clear();
graphics.SetShaderProgram(shaderProgram);
graphics.SetTexture(0,texture);
shaderProgram.SetUniformValue(0,ref unitScreenMatrix);
graphics.DrawArrays(DrawMode.TriangleStrip,0,indexSize);
graphics.SwapBuffers();
}
 
And Done
image
 

No comments:

Post a Comment