DirectX 10 - 完全对象闪烁,然后仅绘制点

时间:2015-06-20 22:26:33

标签: c# sharpdx directx-10

我正在使用C#和SharpDX库对我正在处理的项目进行一些渲染。但是,我只能让对象在第一次传递时完全绘制,每次后续传递它只会绘制点。如果我使用FillMode.Solid或FillMode.Wireframe并不重要。我也禁用了剔除。如果我围绕物体旋转相机,我仍然只能看到点。我有图像显示我的文件中的问题和关键点。在过去几天看过这段代码之后,我完全没有想法,也许一些新鲜的眼睛能够弄明白。

此外,它似乎只绘制第一个三角形,而不是第二个三角形。

以下是图片: Imgur Imgur

这是我的代码:

网格初始化

        rCom.mesh = new Components.Mesh3D(this.render.Device,
            new Components.VertexStructures.Color[] {
new Components.VertexStructures.Color(
    new SharpDX.Vector3(-1.0f, -1.0f, 0.0f),  new SharpDX.Vector4(1.0f, 0.0f, 0.0f, 1.0f)),
new Components.VertexStructures.Color(
    new SharpDX.Vector3(-1.0f, 1.0f, 0.0f),  new SharpDX.Vector4(0.0f, 1.0f, 0.0f, 1.0f)),
new Components.VertexStructures.Color(
    new SharpDX.Vector3(1.0f, 1.0f, 0.0f),  new SharpDX.Vector4(0.0f, 0.0f, 1.0f, 1.0f)),
new Components.VertexStructures.Color(
    new SharpDX.Vector3(1.0f, -1.0f, 0.0f),  new SharpDX.Vector4(1.0f, 1.0f, 1.0f, 1.0f))
            },
            new[] {
                0, 1, 2, 0, 2, 3
            }
        );

顶点结构 - 颜色

public static class VertexStructures
{
    ...
    public struct Color
    {
        public SharpDX.Vector3 pos;
        public SharpDX.Vector4 col;
        public static int sizeInBytes
        { get { return Vector3.SizeInBytes + Vector4.SizeInBytes; } }
        public Color(SharpDX.Vector3 pos, SharpDX.Vector4 col)
        { this.pos = pos; this.col = col; }
    }
    ...
}

网格类

public class Mesh3D
{
    private D3D10.Device device;
    public D3D10.VertexBufferBinding vertexBuffer;
    public D3D10.Buffer indexBuffer;
    public int numberOfVertices;
    public int numberOfIndices;

    public static D3D10.Buffer CreateBuffer<T>(D3D10.Device device, BindFlags bindFlags, params T[] items)
        where T : struct
    {
        var len = Utilities.SizeOf(items);
        var stream = new DataStream(len, true, true);
        foreach (var item in items)
            stream.Write(item);
        stream.Position = 0;
        var buffer = new D3D10.Buffer(device, stream, len, ResourceUsage.Default,
            bindFlags, CpuAccessFlags.None, ResourceOptionFlags.None);
        return buffer;
    }
    ...
    public Mesh3D(D3D10.Device device, VertexStructures.Color[] vertices, int[] indices = null)
    {
        this.numberOfVertices = vertices.Length;
        this.numberOfIndices = indices.Length;

        this.vertexBuffer = new VertexBufferBinding(
            CreateBuffer<VertexStructures.Color>(device, BindFlags.VertexBuffer, vertices),
            VertexStructures.Color.sizeInBytes, 0);
        if (indices != null)
            this.indexBuffer = CreateBuffer<int>(device, BindFlags.IndexBuffer, indices);
    }
    ...
}

渲染更新代码

    public override void Update(double timeDelta = 0.0f)
    {
        // Clear our backbuffer with the rainbow color
        d3d10Device.ClearRenderTargetView(this.renderTargetView, (Color4)SharpDX.Color.CornflowerBlue);

        float time = (float)(timeDelta / 1000.0f); // time in milliseconds?

        // Do actual drawing here
        foreach (RenderComponent com in this._components)
        {
            // Get any required components
            PositionComponent pos = com.entity.GetComponent<PositionComponent>();

            // Set up required buffers
            var inputAssembler = this.d3d10Device.InputAssembler;
            inputAssembler.SetVertexBuffers(0, com.mesh.vertexBuffer);
            if (com.mesh.indexBuffer != null)
                inputAssembler.SetIndexBuffer(com.mesh.indexBuffer, Format.R32_UInt, 0);

            // Set up effect variables
            // These matrices should always be defined in the shader, even if they're not used
            com.shader.shader.GetVariableByIndex(0).AsMatrix().SetMatrix(this.camera.viewMatrix);
            com.shader.shader.GetVariableByIndex(1).AsMatrix().SetMatrix(this.camera.projectionMatrix);
            com.shader.shader.GetVariableByIndex(2).AsMatrix().SetMatrix(pos.rotationXMatrix);
            com.shader.shader.GetVariableByIndex(3).AsMatrix().SetMatrix(pos.rotationYMatrix);
            com.shader.shader.GetVariableByIndex(4).AsMatrix().SetMatrix(pos.rotationZMatrix);
            com.shader.shader.GetVariableByIndex(5).AsMatrix().SetMatrix(pos.scalingMatrix);
            com.shader.shader.GetVariableByIndex(6).AsMatrix().SetMatrix(pos.translationLocalMatrix);
            com.shader.shader.GetVariableByIndex(7).AsMatrix().SetMatrix(pos.translationWorldMatrix);
            foreach (var shaderVars in com.shader.vars)
            {
                // Eventually, we'll use this to set all the required variables needed
            }

            // Run through each technique, pass, draw
            int i = 0, j = 0;
            foreach (var techniqueContainer in com.shader.inputLayouts)
            {
                var technique = com.shader.shader.GetTechniqueByIndex(i);
                foreach (var passContainer in techniqueContainer)
                {
                    var pass = technique.GetPassByIndex(j);
                    inputAssembler.InputLayout = passContainer;

                    pass.Apply();
                    this.d3d10Device.Draw(com.mesh.numberOfVertices, 0);

                    j += 1;
                }
                i += 1;
            }
        }

        // Present our drawn scene waiting for one vertical sync
        this.swapChain.Present(1, PresentFlags.None);
    }

最后,我的着色器文件

matrix View;
matrix Projection;

matrix rotationXMatrix;
matrix rotationYMatrix;
matrix rotationZMatrix;
matrix scalingMatrix;
matrix translationLocalMatrix;
matrix translationWorldMatrix;

struct VS_IN
{
    float4 pos : POSITION;
    float4 col : COLOR;
};

struct PS_IN
{
    float4 pos : SV_POSITION;
    float4 col : COLOR;
};

PS_IN VS( VS_IN input )
{
    PS_IN output = (PS_IN)0;

    input.pos = mul(input.pos, rotationXMatrix);
    input.pos = mul(input.pos, rotationYMatrix);
    input.pos = mul(input.pos, rotationZMatrix);

    input.pos = mul(input.pos, scalingMatrix);

    input.pos = mul(input.pos, translationLocalMatrix);
    input.pos = mul(input.pos, translationWorldMatrix);

    input.pos = mul(input.pos, View);
    input.pos = mul(input.pos, Projection);

    output.pos = input.pos;
    output.col = float4(1.0f, 1.0f, 1.0f, 1.0f);//input.col;

    return output;
}

float4 PS( PS_IN input ) : SV_Target
{
    return input.col;
}

technique10 Render
{
    pass P0
    {
        SetGeometryShader( 0 );
        SetVertexShader( CompileShader( vs_4_0, VS() ) );
        SetPixelShader( CompileShader( ps_4_0, PS() ) );
    }
}

如果需要任何进一步的信息或代码,请告诉我。我真的希望有人可以帮助我,我无法弄清楚。

2 个答案:

答案 0 :(得分:1)

在上面的代码中,您似乎没有设置拓扑(三角形与线对点)。例如,请参阅MSDN documentation on Primitive Toplogies,以及the SharpDX documentation on InputAssemblyStage.PrimitiveToplogythe SharpDX documentation on the PrimitiveTopology enum

在您的Update()方法中,您可能想要添加

inputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;

出于性能原因,您可能应该将此设置在foreach之前,因为它不会发生变化。

答案 1 :(得分:0)

您没有在update()阶段的三角形块中整理您的积分。