Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

C# Unable to draw Primitives on Texture in SharpDX

Kordi3112

New Coder
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 backBuffer... but unfortunately renderTexture do not contains primitives which should be drawn. Texture is only filled by color.

Whole project on github: https://github.com/Kordi3112/SharpDXTest11

Main code part with rendering methods:
Code:
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() primitive should be drawn

What the code above do:
Capture.JPG

...what it should do:

Capture.JPG

Buffers declaration:

Code:
//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 u know whats wrong with my code?
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom