1
\$\begingroup\$

Whenever I multiply my transformation matrix, which is passed to the shader via constant buffer, my triangle disappears. Here is the code for the passed matrix:

fast_matrix mat = transpose_matrix(
        rotation_z_matrix(0) *
        scale_matrix(1.0)*
        translation_matrix(vec3(0.0f, 0.0f, 0.f))*
        perspecitve_projection_matrix(800, 600, 0.5, 10)
        );

And here is the implementation of my perspective_projection_matrix:

fast_matrix perspecitve_projection_matrix(const uint32_t width, const uint32_t height,
        const float_t near_z, const float_t far_z)
    {
        fast_matrix proj;

        float_t d_near_z = 2 * near_z;

        proj.elements[0][0] = d_near_z / width;
        proj.elements[0][1] = 0;
        proj.elements[0][2] = 0;
        proj.elements[0][3] = 0;

        proj.elements[1][0] = 0;
        proj.elements[1][1] = d_near_z / height;
        proj.elements[1][2] = 0;
        proj.elements[1][3] = 0;

        proj.elements[2][0] = 0;
        proj.elements[2][1] = 0;
        proj.elements[2][2] = far_z / (far_z - near_z);
        proj.elements[2][3] = 1;

        proj.elements[3][0] = 0;
        proj.elements[3][1] = 0;
        proj.elements[3][2] = (-1 * near_z * far_z) / (far_z - near_z);
        proj.elements[3][3] = 0;

        return proj;
    }

All other functions like transpose_matrix work correctly and have been tested.

Here is my vertex shader:

struct VS_Out
{
    float4 cl : COLOR;
    float4 pos : SV_POSITION;
};

cbuffer CBuf
{
    matrix transform;
};

VS_Out main(float4 pos : POSITION, float4 col : COLOR)
{
    VS_Out vso;
    vso.pos = mul(transform, pos);
    vso.cl = float4(col.x, col.y, col.z, 1);
    return vso;
}

Vertex and index buffer:

{0, 0.5, 1, 1.f}
{0.5, -0.5, 1, 1.f}
{-0.5, -0.5, 1, 1.f}

{0, 1, 2}

Does anyone have an idea what may be wrong and how I can get my triangle to be rendered with perspective?

\$\endgroup\$
1
  • \$\begingroup\$ I don't recognize your matrix library, so you might want to try DirectXMath or it's SimpleMath wrapper. There's a lot of possible causes, but a common one is that HLSL defaults to 'column major' and many math libraries are 'row-major'. See Microsoft Docs. \$\endgroup\$ – Chuck Walbourn Apr 8 '20 at 3:26

Your Answer

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

Browse other questions tagged or ask your own question.