Help with WorldViewProjection

Started by
4 comments, last by korvax 9 years, 6 months ago

Hi, Im having some problem with moving out my Position Clip space calculation out of the shader and in to cpu space. That is creating a WorldViewProjection matrix and use that in the shaders.

The following shader code works fine

PS Shader.


struct PS_INPUT
{
	float4 PositionSS : SV_POSITION;
	float2 TexCoord : TEXCOORD0;	
};
//--------------------------------------------------------------------------------------
// Pixel Shader
//--------------------------------------------------------------------------------------
PSOutput main(in PS_INPUT input)
{
	PSOutput output;
	if (isTextured)
		output.Color = txDiffuse.Sample(samLinear, input.TexCoord);
	else
		output.Color = diffuse;
	
	return output;
}

cbuffer VSConstants : register(cb0 )
{
	matrix World;
	matrix View;
	matrix Projection;	
	matrix WordViewProjection;
}
//--------------------------------------------------------------------------------------
struct VS_INPUT
{
    float3 Position	: POSITION;
    float2 TexCoord	: TEXCOORD0;
		float3 Normal	: NORMAL;		
};

struct VS_OUTPUT
{
	float4 PositionCS : SV_POSITION;
	float2 TexCoord : TEXCOORD0;	
};

//--------------------------------------------------------------------------------------
// Vertex Shader
//--------------------------------------------------------------------------------------
VS_OUTPUT main(in VS_INPUT input)
{  
	VS_OUTPUT output = (VS_OUTPUT)0;
	float4 position = float4(input.Position, 1.0f);		
	output.PositionCS = mul(position, World);		
	output.PositionCS = mul(output.PositionCS, View);              
	output.PositionCS = mul(output.PositionCS, Projection);	
	output.TexCoord = input.TexCoord;	  
  return output;
}

But I want to replace


output.PositionCS = mul(output.PositionCS, View);
output.PositionCS = mul(output.PositionCS, Projection);	

with


output.PositionCS = mul(position, WordViewProjection);

and my calculations for ConstantBuffer is


VSC_WORLD PipelineManagerDX11::CalculateModelSpace(const IRenderable3D &renderble)
{	
	XMMATRIX model_matrix = renderble.World.GetTransformationMatrix();
	VSC_WORLD vsS;	
	vsS.mWorld = XMMatrixTranspose(model_matrix);
	vsS.mView = XMMatrixTranspose(this->m_View);
	vsS.mProjection = XMMatrixTranspose(this->m_Projection);
	XMMATRIX WorldView = model_matrix*this->m_View;
	XMMATRIX ViewProjection = this->m_View*this->m_Projection;
	vsS.mWordViewProjection = XMMatrixTranspose(WorldView*ViewProjection);;
	return vsS;
}

Im not sure what I'm doing wrong here. But its not working, my output is completely black so something is going badly wrong with the PositionCS Calc. Im pretty sure I'm missing something basic, not sure what though.

All the help is much appreciated.

Advertisement

It appears you're calculating model_matrix * view * view * projection.

WorldView = model_matrix * view;

ViewProjection = view * projection; // another view matrix

mWord(sp?)ViewProjection = Transpose( WorldView * ViewProjection);

EDIT: You can save a little calculation by just XMMatrixTranspose( model_matrix * view * projection ), rather than having all the intermediate variables.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Hi, thx, but even if I change to vsS.mWordViewProjection = XMMatrixTranspose(model_matrix*this->m_View*this->m_Projection); Im still getting same black output. I think it has to be with my WVP matrix, because it works when im doing the calculation on the shader.

You might not have to do all those XMMatrixTranspose's on your matrices. Check if you're compiling your shaders with D3DCOMPILE_PACK_MATRIX_COLUMN_MAJOR vs D3DCOMPILE_PACK_MATRIX_ROW_MAJOR...

Thx tonemgub, im now using ROW, so atleast I have no transpose..

HI, thx for you help, problem solved your help was much appreciated.

This topic is closed to new replies.

Advertisement