I'm trying to set up a custom effect so I can modify the world matrix for each object that I'm drawing.
I'm drawing everything in 2D using a sprite batch, and I don't want to have to have separate calls to SpriteBatch.Begin() every time I need to use a different matrix within single frame (I'm modifying the world matrix to account for parallax scrolling).
I've tried to replicate the functionality of MonoGame's BasicEffect for the vertex shader :
float4 myColour : COLOR0;
float2 myTexCoord : TEXCOORD0;
float4 myPosition : POSITION0;
VertexShaderOutput VS( VertexShaderInput input )
float4 worldPosition = mul(input.myPosition, World);
float4 viewPosition = mul(worldPosition, View);
output.myPosition = mul(viewPosition, Projection);
output.myTexCoord = input.myTexCoord;
When using the default shader, objects are rendered at the correct position :
But when I use my vertex shader, they're rendered at an offset :
My view matrix (myPosition defaults to (0,0) ) :
Matrix.CreateLookAt( new Vector3(myPosition, 0.0f), new Vector3(myPosition, 1.0f), Vector3.Up );
My projection matrix :
public Matrix Projection( PresentationParameters someParameters )
int width = someParameters.BackBufferWidth;
int height = someParameters.BackBufferHeight;
return Matrix.CreateOrthographic( width, height, -1.0f, 1.0f );
There shouldn't be a problem with my world matrix, as I used to use it directly in the SpriteBatch.Begin() method.
The way I'm drawing my objects :
Vector2 objectCenter = new Vector2(texture.Size / 2.0f, texture.Width / 2.0f);
spriteBatch.Draw( texture, myObject.Position, null, Color.White, myObject.Rotation, objectCenter, myObject.Scale, SpriteEffects.None, 0.0f );
Any ideas what I might be doing wrong here? I figure it must be in either the view or the projection matrices, since the world matrix I'm using works to offset the sprite batch when I'm using the default effect.
I'd debug the shaders with GLDebugger or something similar, but GLDebugger doesn't seem to pick up on my GLControl draw calls (I'm using a GLControl in a WindowsFormsHost... it's a WPF application I'm building).