0
\$\begingroup\$

I've been working on a Quad Renderer and have gotten close. However, It still does not render correctly. Can anyone spot what I'm doing wrong or give me a better option. See the rendered results below the Code.

I'm about at wits end with this thing.

    public void Initialize(DeviceManager deviceManager, string vertexShaderFile, string pixelShaderFile = null)
    {
        m_DeviceManager = deviceManager;

        VertexShaderFile = vertexShaderFile;
        PixelShaderFile = pixelShaderFile ?? vertexShaderFile;

        RemoveAndDispose(ref m_VertexShader);
        RemoveAndDispose(ref m_PixelShader);
        RemoveAndDispose(ref m_InputLayout);
        RemoveAndDispose(ref m_ShaderResourceView);
        RemoveAndDispose(ref m_SamplerState);

        using (var vertexShaderByteCode = SharpDX.D3DCompiler.ShaderBytecode.CompileFromFile(VertexShaderFile, "VSMain", "vs_5_0", SharpDX.D3DCompiler.ShaderFlags.Debug))
        {
            m_VertexShader = AddDisposable(new SharpDX.Direct3D11.VertexShader(m_DeviceManager.Direct3DDevice, vertexShaderByteCode));

            m_InputLayout = AddDisposable(new SharpDX.Direct3D11.InputLayout(m_DeviceManager.Direct3DDevice,
                vertexShaderByteCode.Bytecode.GetPart(SharpDX.D3DCompiler.ShaderBytecodePart.InputSignatureBlob),
                new[]
                {
                    new SharpDX.Direct3D11.InputElement("SV_Position", 0, SharpDX.DXGI.Format.R32G32B32A32_Float, 0, 0),
                }));
        }

        using (var pixelShaderByteCode = SharpDX.D3DCompiler.ShaderBytecode.CompileFromFile(PixelShaderFile, "PSMain", "ps_5_0", SharpDX.D3DCompiler.ShaderFlags.Debug))
        {
            m_PixelShader = AddDisposable(new SharpDX.Direct3D11.PixelShader(m_DeviceManager.Direct3DDevice, pixelShaderByteCode));
        }

        var samplerStateDescription = new SharpDX.Direct3D11.SamplerStateDescription
        {
            AddressU = SharpDX.Direct3D11.TextureAddressMode.Wrap,
            AddressV = SharpDX.Direct3D11.TextureAddressMode.Wrap,
            AddressW = SharpDX.Direct3D11.TextureAddressMode.Wrap,
            Filter = SharpDX.Direct3D11.Filter.MinMagMipLinear
        };



        m_ShaderResourceView =
            AddDisposable(TextureLoader.CreateShaderResourceView(m_DeviceManager.Direct3DDevice, "backgroundDefault.jpg"));

        m_SamplerState =
            AddDisposable(new SharpDX.Direct3D11.SamplerState(m_DeviceManager.Direct3DDevice, samplerStateDescription));

        m_VertexBuffer =
            AddDisposable(SharpDX.Direct3D11.Buffer.Create(m_DeviceManager.Direct3DDevice,
                SharpDX.Direct3D11.BindFlags.VertexBuffer, new SimpleVertex[]
                {
                    /*  Position: float x 3 */
                    new SimpleVertex(-1.0f, -1.0f, 0.5f), new SimpleVertex(-1.0f, 1.0f, 0.5f),
                    new SimpleVertex(1.0f, -1.0f, 0.5f), new SimpleVertex(1.0f, 1.0f, 0.5f),
                }));

        m_VertexBufferBinding = new SharpDX.Direct3D11.VertexBufferBinding(m_VertexBuffer, SharpDX.Utilities.SizeOf<SimpleVertex>(), 0);

        // Triangle strip:
        // v1   v3
        // |\   |
        // | \ B|
        // | A\ |
        // |   \|
        // v0   v2
    }

    public void Render()
    {
        var direct3DContext = m_DeviceManager.Direct3DDeviceContext;

        using (var shaderRestore = new ShaderRestore(direct3DContext))
        {
            direct3DContext.PixelShader.SetShaderResource(0, m_ShaderResourceView);

            direct3DContext.PixelShader.Set(m_PixelShader);

            direct3DContext.VertexShader.Set(m_VertexShader);

            //direct3DContext.InputAssembler.InputLayout = null;
            direct3DContext.InputAssembler.InputLayout = m_InputLayout;

            direct3DContext.InputAssembler.PrimitiveTopology = SharpDX.Direct3D.PrimitiveTopology.TriangleStrip;

            direct3DContext.InputAssembler.SetVertexBuffers(0, m_VertexBufferBinding);

            direct3DContext.Draw(4, 0);

            shaderRestore.Restore();
        }

    }

    private DeviceManager m_DeviceManager;

    private SharpDX.Direct3D11.VertexShader m_VertexShader;
    private SharpDX.Direct3D11.InputLayout m_InputLayout;

    private SharpDX.Direct3D11.PixelShader m_PixelShader;

    private SharpDX.Direct3D11.ShaderResourceView m_ShaderResourceView;
    private SharpDX.Direct3D11.SamplerState m_SamplerState;

    private SharpDX.Direct3D11.Buffer m_VertexBuffer;
    private SharpDX.Direct3D11.VertexBufferBinding m_VertexBufferBinding;

    public string VertexShaderFile { get; private set; }
    public string PixelShaderFile { get; private set; }

bad quad renderer

Shader Code. I could get it to format correctly.

Quad.hlsl

\$\endgroup\$
2
  • \$\begingroup\$ Can you post the shader you are using? \$\endgroup\$ Feb 1, 2017 at 0:08
  • \$\begingroup\$ Added picture of code. I couldn't get it to stay formatted correctly, \$\endgroup\$
    – user96556
    Feb 1, 2017 at 1:58

1 Answer 1

0
\$\begingroup\$

This turned out the quad points were in the wrong order. Once figuring out my issue, I created a static method to create Quads from now on.

public static Quad CreateQuad(IDevice3DManager device3DManager, float x = -1, float y = 1)
    {
        var baseVector = new Vector2(x, y);

        var vertices = new[]
        {
            new TexturedVertex(GetVectorP1(baseVector), new Vector2(0,0)),
            new TexturedVertex(GetVectorP2(baseVector), new Vector2(1,0)),
            new TexturedVertex(GetVectorP3(baseVector), new Vector2(1,1)),
            new TexturedVertex(GetVectorP4(baseVector), new Vector2(0,1)),
        };

        var indices = new[] { 0, 1, 2, 2, 3, 0 };
        var quad = new Quad(device3DManager)
        {
            VertexBuffer = Buffer.Create(device3DManager.Device, BindFlags.VertexBuffer, vertices.ToArray()),
            IndexBuffer = Buffer.Create(device3DManager.Device, BindFlags.IndexBuffer, indices.ToArray()),
            VertexSize = Utilities.SizeOf<TexturedVertex>()
        };

        quad.MeshSubsets.Add(new MeshSubset()
        {
            DiffuseColor = new Vector4(1, 1, 1, 1),
            IndexCount = indices.Count()
        });

        return quad;
    }

/// <summary>
/// Textured Vertex
/// </summary>
public struct TexturedVertex
{
    /// <summary>
    /// Position
    /// </summary>
    public Vector3 Position;

    /// <summary>
    /// Texture coordinate
    /// </summary>
    public Vector2 TextureCoordinate;

    /// <summary>
    /// Constructor
    /// </summary>
    /// <param name="position">Position</param>
    /// <param name="textureCoordinate">Texture Coordinate</param>
    public TexturedVertex(Vector3 position, Vector2 textureCoordinate)
    {
        Position = position;
        TextureCoordinate = textureCoordinate;
    }
}

private static Vector3 GetVectorP1(Vector2 vector, float size = 2)
    {
        return new Vector3(vector.X, vector.Y, 0);
    }

    private static Vector3 GetVectorP2(Vector2 vector, float size = 2)
    {
        return new Vector3(vector.X + size, vector.Y, 0);
    }

    private static Vector3 GetVectorP3(Vector2 vector, float size = 2)
    {
        return new Vector3(vector.X + size, vector.Y - size, 0);
    }

    private static Vector3 GetVectorP4(Vector2 vector, float size = 2)
    {
        return new Vector3(vector.X, vector.Y - size, 0);
    }

No more problem...

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .