The first, is to interop Direct2D with Direct3D. This seemed like a lot of work to do, and I didn't need all the functionality of Direct2D. The second, was to use Direct3D but use an ortho matrix in place of the projection matrix. I'm sure it's something on my part, but I could never get this working correctly, plus the examples I've seen would either use a non-constant buffer for the vertex data, or have to recreate it each time the sprite was to move.
So, I came across another "solution". My question is, it seems far to simple to not have problems, otherwise why wouldn't it have been done that way in place of the ortho matrix. Does anyone see any potential problems with doing 2D sprites in this way?
When I create the quad to display the 2D image, I give it pixel size of the width and height to create it. I also store a Position value, in pixels, of where the quad should be displayed. I built a seperate vertex and pixel shaders to be used with the 2D images, where the screen size and quad position are loaded into a constant buffer. Vertex Shader below, where the vertex position is added to the position (pX and pY) in the buffer, multiplied by 2 and divided by screen size (to get the vertex position in the range of 0.0 to 2.0), then reduced by 1 to get range of -1.0 to 1.0
Vertex Shader:
//Sprites.vs
//--------------------------------------------------------------------------------------
// Buffers
//--------------------------------------------------------------------------------------
cbuffer ModelVCBSTRUCT : register( b0 )
{
float sWidth;
float sHeight;
float pX;
float pY;
}
//--------------------------------------------------------------------------------------
// Typedefs
//--------------------------------------------------------------------------------------
struct VertexInput{
float4 Pos : POSITION;
float2 Tex : TEXCOORD0;
};
struct PixelInput{
float4 Pos : SV_POSITION;
float2 Tex : TEXCOORD0;
};
//--------------------------------------------------------------------------------------
// Vertex Shader
//--------------------------------------------------------------------------------------
PixelInput VS( VertexInput input )
{
PixelInput output;
output.Pos = input.Pos;
output.Pos[0] = (((output.Pos[0] + pX) * 2) / sWidth) - 1;
output.Pos[1] = (((output.Pos[1] + pY) * 2) / sHeight) - 1;
output.Tex = input.Tex;
return output;
}
Again, I just want to know if this will have some problem with it, as the solution seems far too simple compared to the other options I've seen so far. Thanks in advance for feedback!