0
\$\begingroup\$

I have a little problem with rendering in my SharpDX Direct11 App.

I had being tested rendering scene on a texture, and then draw this texture on the backBuffer. Unfortunately the renderTexture does not contain the primitives which should be drawn. Texture is only filled by color.

Whole project is here on github.

Main code part with rendering methods:

public override void Render()
{
    //Camera
    var proj = Matrix.OrthoLH(3 * Form.Bounds.Width / Form.Bounds.Height, 3, 0.01f, 100f);
    var view = Matrix.LookAtLH(new Vector3(0, 0, -10), new Vector3(0, 0, 20), Vector3.UnitY);

    var world = Matrix.Identity;

    var worldViewProj = world * view * proj;
    worldViewProj.Transpose();

    //Update wvp matrix
    Context.UpdateSubresource(ref worldViewProj, ContantBuffer);

    DrawOnTexture();

    //Set BackBuffer as render target
    Context.OutputMerger.SetTargets(depthView, renderView);

    // Clear views
    Context.ClearDepthStencilView(depthView, DepthStencilClearFlags.Depth, 1.0f, 0);
    Context.ClearRenderTargetView(renderView, Color.Pink);

    //Set TextureColor Shader
    Effect2.ApplyShader(Context);

    //Set Buffers
    Context.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(VertexBuffer2, Utilities.SizeOf<VertexPositionColorTexture>(), 0));
    Context.InputAssembler.SetIndexBuffer(IndexBuffer, Format.R32_UInt, 0);

    //Set Texture to Shader
    Context.PixelShader.SetShaderResource(0, RenderTexture.ShaderResourceView);

    //Draw 
    Context.DrawIndexed(6, 0, 0);

    // Present!
    SwapChain.Present(0, PresentFlags.None);
}

private void DrawOnTexture()
{
    //Set Color Shader
    Effect1.ApplyShader(Context);

    //Set Buffers
    Context.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(VertexBuffer, Utilities.SizeOf<VertexPositionColor>(), 0));
    Context.InputAssembler.SetIndexBuffer(IndexBuffer, Format.R32_UInt, 0);

    //Set Target
    RenderTexture.SetRenderTarget(Context, depthView);

    //Clear Targets - Green Bgound
    RenderTexture.ClearRenderTarget(Context, depthView, 0, 1, 0, 1);

    //Draw on RenderTarget
    Context.DrawIndexed(6, 0, 0);

}

After call: Context.DrawIndexed(6, 0, 0); in private void DrawOnTexture(), the primitive should be drawn.

What the code above does: What the code above do ...what it should do: ...what it should do

Buffers declaration:

//Position Color
    VertexBuffer = Buffer.Create(Device, BindFlags.VertexBuffer, new[] {
        new VertexPositionColor(new Vector4(-1, -1, 0, 1), Color.Red.ToVector4()),
        new VertexPositionColor(new Vector4(-1, 1, 0, 1), Color.Green.ToVector4()),
        new VertexPositionColor(new Vector4(1, 1, 0, 1), Color.Blue.ToVector4()),
        new VertexPositionColor(new Vector4(1, -1, 0, 1), Color.Yellow.ToVector4())
    });

    //Position Color Texture
    VertexBuffer2 = Buffer.Create(Device, BindFlags.VertexBuffer, new[] {
        new VertexPositionColorTexture(new Vector4(-1, -1, 0, 1), Color.White.ToVector4(), new Vector2(0,1)),
        new VertexPositionColorTexture(new Vector4(-1, 1, 0, 1), Color.White.ToVector4(),new Vector2(0,0)),
        new VertexPositionColorTexture(new Vector4(1, 1, 0, 1), Color.White.ToVector4(),new Vector2(1,0)),
        new VertexPositionColorTexture(new Vector4(1, -1, 0, 1), Color.White.ToVector4(),new Vector2(1,1))
    });

    IndexBuffer = Buffer.Create(Device, BindFlags.IndexBuffer, new[] {
        0,1,2,
        0,2,3
    });

Do you know what is wrong with my code?

\$\endgroup\$

0

You must log in to answer this question.

Browse other questions tagged .