locked
Building orthographic scene

    Question

  • Hi,

    I’m trying to setup orthographics scene where I can draw vertices with positions that range acroos screen WidthXHeight (rather than -1..+1). Essentially I want to be able to draw vertices as if I’m in 2D bitmap world (Z==1). For example, if my screen resolution is 1600X900 I want to be able to have vertices list like (40,40,1),(80,40,1), (80,80,1) etc.

    In XNA I manage to do so by the following:

    float tilt = MathHelper.ToRadians(0);

    worldMatrix = Matrix.CreateRotationX(tilt) * Matrix.CreateRotationY(tilt);

    projectionMatrix = Matrix.CreateOrthographicOffCenter(0, getVisibleWidth(), getVisibleHeight(), 0, -5, 10);

    viewMatrix = Matrix.Identity;

    When I try to convert the same commands to DX11 constant buffer initialization (I calculated the matrices based on 1600X900 resolution in XNA and copied the values to the respective DX matrices):

           m_constantBufferData.projection = float4x4(

                0.00125f,     0.00000000f,    0.00000000f,  0.00000000f,

                0.00000000f,  -0.002222222f,  0.0f,         0.00000000f,

                0.00000000f,  0.0f,           -0.06666667f, 0.0f,

                -1.00000000f, 1.00000000f,    0.3333333f,   1.00000000f

                );

            m_constantBufferData.view = float4x4(

                 1.00000000f, 0.00000000f,  0.00000000f,  0.00000000f,

                 0.00000000f, 1.00000000f,  0.00000000f,  0.00000000f,

                 0.00000000f, 0.00000000f,  1.00000000f,  0.00000000f,

                 0.00000000f, 0.00000000f,  0.00000000f,  1.00000000f

                );

    m_constantBufferData.model = rotationY(0);

            const D3D11_INPUT_ELEMENT_DESC basicVertexLayoutDesc[] =

            {

                { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0,  0, D3D11_INPUT_PER_VERTEX_DATA, 0 },

                { "COLOR",    0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },

            };

            SimpleCubeVertex cubeVertices[] =

            {

                { float3(40.0f, 40.0f, 1.0f), float3(0.0f, 1.0f, 1.0f) },

                { float3(80.0f, 40.0f, 1.0f), float3(0.0f, 1.0f, 1.0f) },

                { float3(80.0f, 80.0f, 1.0f), float3(0.0f, 1.0f, 1.0f) },

                { float3(40.0f, 80.0f, 1.0f), float3(0.0f, 1.0f, 1.0f) }

            };

    Renders to empty screen.

    VS 2012 project can be found in Here

    If that’s not possible, I will happily live with -1..+1 ranges for both X/Y axis as long as the left and top position will indeed be -1 rather than resolution dependent values.

    What am I missing?

    Thanks,

    Meir
    Monday, July 30, 2012 5:23 AM

Answers

  • You should look at using DirectXMath's XMMatrixOrthographicOffCenterLH function. As noted, remember that HLSL can use either ROW MAJOR or COLUMN MAJOR matricies, but typically COLUMN MAJOR is more efficient in shaders. That means when using ROW MAJOR computations you need to transpose the resutl (XMMatrixTranspose!) as you put the results into the constant buffer so they are in COLUMN MAJOR form for the shader.

    Tuesday, July 31, 2012 11:54 PM

All replies

  • The answer is that the projection matrix (as well as view and model but since they are identity it has no effect) has to be transposed before getting into the vertex shader so its multiplication with other matrices will be legal.
    Tuesday, July 31, 2012 8:03 PM
  • You should look at using DirectXMath's XMMatrixOrthographicOffCenterLH function. As noted, remember that HLSL can use either ROW MAJOR or COLUMN MAJOR matricies, but typically COLUMN MAJOR is more efficient in shaders. That means when using ROW MAJOR computations you need to transpose the resutl (XMMatrixTranspose!) as you put the results into the constant buffer so they are in COLUMN MAJOR form for the shader.

    Tuesday, July 31, 2012 11:54 PM