Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

Probably, the terms "model-view-projection" matrix and "world-view-project" matrix refer to the same conceptual transform. The two terms can be used interchangeably, and the difference in terminology could stem from one source being more familiar with OpenGL (which uses the term "modelview") or D3D (which splits the concept into "world" and "view" transforms).

The "modelview" transform bring an object from model space to view space (conceptually passing through world space on the way). The "world" transform would bring an object from model space to world space, and the "view" transform" from world to view space. Thus, the combined "world-view" transform is equivalent to the GL-style "modelview." Obviously, then, when you tack on the projection transformation at the end, you get the same logical origin (model space) and destination (clip space) spaces.

All of which is a roundabout way of saying that the problem you're seeing in your code is likely due to the values you're assigning to these matrices before you present the scene. Unless you're using some seriously non-standard terminology, there's no way to convert between a "modelview-projection" matrix and a "world-view-projection" matrix because they are the same thing; there isn't even a need.

I'll point out that your shaders aren't exactly identical except for variable renames. There's also some input layout differences, it seems. But the actual problem appears to be in the C++ code that causes these shaders to run, not the shaders themselves.

share|improve this answer
    
Thanks for the reply! Okay so if my original shader just needs a World matrix and a WVP matrix. Then I should only need the Model matrix and the MVP matrix for the engine right? The only problem is the way that the engine gives me access to the matrices. I'm not sure how to send the matrix info my self from the c++ side in their engine yet (The engine is not released yet, but I asked their support how to do it) and the way that I have access to certain matrices (such as the MVP) is through predefined registers. These registers do not have a Model matrix on its own. –  user31999 Jun 18 '13 at 15:29
    
But it does have a ModelView, View, Projection, MVP, and Model to world (which I am using in the sample). Also the function parameters are different, I tried to match what the engine does originally by using structs instead of sending multiple variables. –  user31999 Jun 18 '13 at 15:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.