So basically I wrote a shader a long time ago that uses all World and WVP matrices. Now I am working in an engine that will only give me access to ModelView, MVP, and model to world space matrices. First I tried just using the model to world space matrix for my previous World matrix and the MVP matrix instead of my WVP matrix, but now my object isnt there and the vertices are sort of just flying at the camera; which makes me think these are probably the problem. Here are the shaders just incase Original:
//--------------------------------------------------------------------------------------
// Global variables
//--------------------------------------------------------------------------------------
float g_fTime; // App's time in seconds
float4x4 g_mWorld; // World matrix for object
float4x4 g_mWorldViewProjection; // World View Projection matrix
//--------------------------------------------------------------------------------------
// Vertex shader output structure
//--------------------------------------------------------------------------------------
struct VS_OUTPUT
{
float4 vPosition : POSITION; // vertex position
float4 Diffuse : COLOR0;
float3 Normal :TEXCOORD0; //extra variable for toon shading
};
VS_OUTPUT RenderSceneVSOutline( float4 vPos : POSITION,
float3 vNormal : NORMAL,
float2 vTexCoord0 : TEXCOORD0,
float2 vTexCoord1 : TEXCOORD1
)
{
VS_OUTPUT Output=(VS_OUTPUT)0;
float3 vNormalWorldSpace;
//This is where you want to put your simple vertex animation code.
//you should convert a vNormal to a float of 4 and multiply (sin(g_fTime)+1.0f)*0.5f
//add the result of those operations to the incoming position
float4 normal=float4(vNormal,0.0f);
float4 result= mul(normal, (sin(0.3f)+1.0f)*0.1f );
vPos += result;
// Transform the position from object space to homogeneous projection space by:
// Multiplying together the world and the world view projection. Then, apply that to the position
float4x4 temp4x4= mul(g_mWorld,g_mWorldViewProjection);
float4 pos=mul(vPos,temp4x4);
Output.vPosition = pos;
// Transform the normal from object space to world space
// Convert the world to a float3x3. Multiply the normal with converted world. Normalize the entire result
float3x3 anewworld=(float3x3)g_mWorld;
vNormalWorldSpace = mul(vNormal,anewworld);
normalize(vNormalWorldSpace);
float3 lightDir ={ 0.0f, 0.0f, 1.0f };
Output.Normal=vNormalWorldSpace;
float4 dotresult=dot(vNormalWorldSpace,lightDir);
//Take the max of 0, and the result of the dot product of vNormalWorldSpace and the lightDir. This is
//a simple lambert diffuse lighting
Output.Diffuse = max(0,dotresult);
return Output;
}
//--------------------------------------------------------------------------------------
// Pixel shader output structure
//--------------------------------------------------------------------------------------
struct PS_OUTPUT
{
float4 RGBColor : COLOR0; // Pixel color
};
PS_OUTPUT RenderScenePSOutline( VS_OUTPUT In )
{
PS_OUTPUT Output=(PS_OUTPUT)0;
// Lookup mesh texture and modulate it with diffuse
float4 fBlue = { 0.0f, 0.0f, 0.0f, 0.0f };
//Multiply In.Diffuse by 0.8 and combine (by adding)
//the result with the vector for blue.
Output.RGBColor = fBlue;
return Output;
}
New Engine /ModelView edition:
float4x4 matMVP : register(c8);
float4x4 World : register(c20); //the model to worldspace matrix
struct VS_IN
{
float4 ObjPos : POSITION;
float3 Normal :NORMAL;
float2 UV0 : TEXCOORD0;
float2 UV1 :TEXCOORD1;
};
struct VS_OUT
{
float4 ProjPos : SV_Position;
float4 Diffuse: COLOR0;
float2 Normal : TEXCOORD0;
};
VS_OUT vs_main( VS_IN In )
{
//VS_OUT Out;
VS_OUT Output=(VS_OUT)0;
float3 vNormalWorldSpace;
//This is where you want to put your simple vertex animation code.
//you should convert a vNormal to a float of 4 and multiply (sin(g_fTime)+1.0f)*0.5f
//add the result of those operations to the incoming position
float4 normal=float4(In.Normal,0.0f);
float4 result= mul(normal, (sin(0.3f)+1.0f)*0.1f );
In.ObjPos += result;
// Transform the position from object space to homogeneous projection space by:
// Multiplying together the world and the world view projection. Then, apply that to the position
float4x4 temp4x4= mul(World,matMVP);
float4 pos=mul(In.ObjPos,temp4x4);
Output.ProjPos = pos;
// Transform the normal from object space to world space
// Convert the world to a float3x3. Multiply the normal with converted world. Normalize the entire result
float3x3 anewworld=(float3x3)World;
vNormalWorldSpace = mul(In.Normal,anewworld);
normalize(vNormalWorldSpace);
float3 lightDir ={ 0.0f, 0.0f, 1.0f };
Output.Normal=vNormalWorldSpace;
float4 dotresult=dot(vNormalWorldSpace,lightDir);
//Take the max of 0, and the result of the dot product of vNormalWorldSpace and the lightDir. This is
//a simple lambert diffuse lighting
Output.Diffuse = max(0,dotresult);
return Output;
}
struct PS_IN
{
float4 ProjPos : POSITION;
float4 Diffuse: COLOR0;
float2 Normal : TEXCOORD0;
};
struct PS_OUTPUT
{
float4 RGBColor : COLOR0;
};
PS_OUTPUT ps_main( PS_IN In ) : SV_Target
{
PS_OUTPUT Output=(PS_OUTPUT)0;
// Lookup mesh texture and modulate it with diffuse
float4 fBlue = { 0.0f, 0.0f, 0.0f, 0.0f };
//Multiply In.Diffuse by 0.8 and combine (by adding)
//the result with the vector for blue.
Output.RGBColor = fBlue;
return Output;
}
The only difference between the two is variable name changes (and values if MVP and WVP aren't the same). So does anybody know how I could convert from WVP to MVP?