I am currently trying to get used to the DirectX API and I am wondering what is the usual approach to render a sprite in DirectX 11 (e.g. for a tetris clone).
Is there a simmilar interface as ID3DX10Sprite
, and if not, which would be the usual method to draw sprites in DirectX 11?
Edit: Here is the HLSL code that worked for me (the computation of the projection coordinate could be done better):
struct SpriteData
{
float2 position;
float2 size;
float4 color;
};
struct VSOut
{
float4 position : SV_POSITION;
float4 color : COLOR;
};
cbuffer ScreenSize : register(b0)
{
float2 screenSize;
float2 padding; // cbuffer must have at least 16 bytes
}
StructuredBuffer<SpriteData> spriteData : register(t0);
float2 GetVertexPosition(uint VID)
{
[branch] switch(VID)
{
case 0:
return float2(0, 0);
case 1:
return float2(1, 0);
case 2:
return float2(0, 1);
default:
return float2(1, 1);
}
}
float4 ComputePosition(float2 positionInScreenSpace, float2 size, float2 vertexPosition)
{
float2 origin = float2(-1, 1);
float2 vertexPositionInScreenSpace = positionInScreenSpace + (size * vertexPosition);
return float4(origin.x + (vertexPositionInScreenSpace.x / (screenSize.x / 2)), origin.y - (vertexPositionInScreenSpace.y / (screenSize.y / 2)), 1, 1);
}
VSOut VShader(uint VID : SV_VertexID, uint SIID : SV_InstanceID)
{
VSOut output;
output.color = spriteData[SIID].color;
output.position = ComputePosition(spriteData[SIID].position, spriteData[SIID].size, GetVertexPosition(VID));
return output;
}
float4 PShader(float4 position : SV_POSITION, float4 color : COLOR) : SV_TARGET
{
return color;
}