Jump to content

  • Log In with Google      Sign In   
  • Create Account


Vertex Shader Position Offset?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 Orangeatang   Members   -  Reputation: 1431

Like
0Likes
Like

Posted 22 April 2013 - 04:00 AM

Hi there,
 
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 :
 
float4x4 World;
float4x4 View;
float4x4 Projection;

struct VertexShaderInput
{
    float4 myColour : COLOR0;
    float2 myTexCoord : TEXCOORD0;
    float4 myPosition : POSITION0;
};

VertexShaderOutput VS( VertexShaderInput input )
{
    VertexShaderOutput output;

    float4 worldPosition  = mul(input.myPosition, World);
    float4 viewPosition  = mul(worldPosition, View);
    
    output.myPosition  = mul(viewPosition, Projection);
    output.myTexCoord  = input.myTexCoord;

    return output;
}

 

 

 
When using the default shader, objects are rendered at the correct position :
 
MebFiAJ.png
 
But when I use my vertex shader, they're rendered at an offset :
 
YvdEqSK.png
 
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).
 
Cheers!


Sponsor:

#2 Orangeatang   Members   -  Reputation: 1431

Like
0Likes
Like

Posted 22 April 2013 - 08:03 AM

Managed to fix the issue...

 

Just playing around I made the view matrix an identity matrix, and used :

 

Matrix.CreateOrthographicOffCenter

 

Instead, that seems to get things rendering in the correct position



#3 Orangeatang   Members   -  Reputation: 1431

Like
0Likes
Like

Posted 22 April 2013 - 08:36 AM

Bah, that only kind of works - it completely screws up parallax effects (assuming a parallax factor is applied to the world transform of the object being rendered)


Edited by Orangeatang, 22 April 2013 - 08:37 AM.


#4 Orangeatang   Members   -  Reputation: 1431

Like
0Likes
Like

Posted 22 April 2013 - 08:45 AM

Ah - doesn't look like you can modify the parameters of an effect after calling SpriteBatch.Begin()

 

There goes that optimization plan smile.png

 

I guess I can probably hide the parallax factor in a COLOR0 input to the vertex shader, and update the word transform accordingly


Edited by Orangeatang, 22 April 2013 - 09:01 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS