0
\$\begingroup\$

Forgive me for my English. I am trying to create a 4x4 matrix in vertex shader, but it is not initialized correctly. Why is this happening? Vertex shader:

struct LogicBufferPosition
{
    matrix Pos;
};

struct LogicBufferBool
{
    bool flag;
};

StructuredBuffer<LogicBufferPosition> arrayPositions: register(t0);
StructuredBuffer<LogicBufferBool> arrayBools: register(t1);

cbuffer ConstantBufferVS: register(b0)
{ 
    matrix World; 
    matrix View; 
    matrix Perspective; 
}; 

struct VS_INPUT 
{ 
    float3 Pos: POSITION; 
    float3 TexCoord: TEXCOORD;
    float3 Normal: NORMAL;
}; 

struct GS_INPUT
{ 
    float4 Pos: SV_POSITION; 
    float3 TexCoord: TEXCOORD0;
    float3 Normal: TEXCOORD1;
    bool visible: VISIBLE_FLAG; // отрисовывать ли примитив?
};

GS_INPUT main(VS_INPUT input, uint id: SV_InstanceID) 
{ 
    GS_INPUT _out = (GS_INPUT)0; 
    if(arrayBools[id].flag == false)
    {
        _out.visible = arrayBools[id].flag;
        return _out;
    }


    float4x4 translateCenter = 
    {
    1.f,0.f,0.f,-input.Pos.x,
    0.f,1.f,0.f,-input.Pos.y, 
    0.f,0.f,1.f,-input.Pos.z,
    0.f,0.f,0.f,1.f,
    };

    float4x4 translateCenterInverse = 
    {
    1,0,0,input.Pos.x,
    0,1,0,input.Pos.x,
    0,0,1,input.Pos.x,
    0,0,0,1
    };


    _out.Pos = mul(float4(input.Pos, 1), World); 
    _out.Pos = mul(_out.Pos, translateCenter);
    _out.Pos = mul(_out.Pos, arrayPositions[id].Pos);
    _out.Pos = mul(_out.Pos, translateCenterInverse);

    _out.Pos = mul(_out.Pos, View); 
    _out.Pos = mul(_out.Pos, Perspective); 
    _out.TexCoord = input.TexCoord;
    _out.Normal = input.Normal;
    _out.visible = arrayBools[id].flag;
    return _out; 
}

enter image description here

\$\endgroup\$
  • \$\begingroup\$ The most common issue is that your C++ code likely uses row-major matrices, while HLSL shaders default to column-major matrices. This means you typically need to Transpose your matrix as you set it into the constant buffer. See MSDN. \$\endgroup\$ – Chuck Walbourn Oct 10 '18 at 23:43
  • \$\begingroup\$ @ChuckWalbourn All matrices that are sent to the shader are transposed. The problem is in the matrices created in the shader itself. look at the screenshot. \$\endgroup\$ – Range Oct 11 '18 at 6:35
0
\$\begingroup\$

The issue is resolved. I thought that the matrix by columns is filled as I showed above. And it should be in the usual way:

float4x4 translateCenter = 
{
1.f,0.f,0.f,0.f,
0.f,1.f,0.f,0.f, 
0.f,0.f,1.f,0.f,
-center.x,-center.y,-center.z,1.f
};

float4x4 translateCenterInverse = 
{
1.f,0.f,0.f,0.f,
0.f,1.f,0.f,0.f,
0.f,0.f,1.f,0.f,
center.x,center.y,center.z,1.f
};
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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