使用SharpDX Toolkit渲染纹理地形

时间:2014-01-13 23:03:32

标签: c# directx hlsl toolkit sharpdx

所以我最近从XNA迁移到SharpDX Toolkit并开始了一些基础知识。 2d部分很容易,因为几乎没有任何变化,但我真的很喜欢3d部分。例如,我想基于riemers terrain教程渲染高度图但是出现了以下问题:

我有一个小的map(或terrain)类,它包含索引和顶点数组作为缓冲区,如下所示:

private SharpDX.Toolkit.Graphics.Buffer<VertexPositionNormalTexture> _mesh;
private SharpDX.Toolkit.Graphics.Buffer _indexBuffer;
private readonly static VertexInputLayout InputLayout = VertexInputLayout.New<VertexPositionNormalTexture>(0); // copied from the basic geometry

这可以填充属性并使用VertexPositionColor进行渲染。 渲染了常见的XNA方式:

//set effect parameters and resources
foreach (EffectPass pass in _effect.CurrentTechnique.Passes) {
    pass.Apply();

    //Globals.DefaultGraphicsDevice.SetRasterizerState(CullNone);

    Globals.DefaultGraphicsDevice.SetVertexBuffer(0, _mesh);

    // Setup the Vertex Buffer Input layout
    Globals.DefaultGraphicsDevice.SetVertexInputLayout(InputLayout);

    // Setup the index Buffer
    Globals.DefaultGraphicsDevice.SetIndexBuffer(_indexBuffer, true);

    // Finally Draw this mesh
    Globals.DefaultGraphicsDevice.DrawIndexed(PrimitiveType.TriangleList, _indexBuffer.ElementCount);
}

现在用我自己的半转换半重影着色器得到

a)黑色地形或

b)HRESULT:[0x80070057](参数错误)

并且我无法弄清楚为什么(最有可能因为我从未对3d着色器做过多次)

着色器:

struct VertexShaderInput {
    float4 Position     : SV_Position;
    float3 Normal       : NORMAL;
    float2 TextureCoords: TEXCOORD0;
};

struct VertexShaderOutput {
    float4 Position     : SV_Position;
    //float4 VPosition      : SV_Position; //im not used to SV_Position and SV_Target so this could be the main problem
    float LightingFactor: TEXCOORD0;
    float2 TextureCoords: TEXCOORD1;
};

struct PixelShaderOutput {
    float4 Color        : SV_Target;
};

//------- Constants --------
float4x4 xView;
float4x4 xProjection;
float4x4 xWorld;
float3 xLightDirection;
float xAmbient;

Texture2D xTexture;
sampler TextureSampler = sampler_state {
    texture = <xTexture>;
    magfilter = LINEAR;
    minfilter = LINEAR;
    mipfilter = LINEAR;
    AddressU = mirror;
    AddressV = mirror;
};

VertexShaderOutput TexturedVS(VertexShaderInput VSin) {
    VertexShaderOutput Output = (VertexShaderOutput)0;
    float4x4 preViewProjection = mul(xView, xProjection);
    float4x4 preWorldViewProjection = mul(xWorld, preViewProjection);

    Output.Position = mul(VSin.Position, preWorldViewProjection);
    Output.TextureCoords = VSin.TextureCoords;

    float3 Normal = normalize(mul(normalize(VSin.Normal), xWorld));
    Output.LightingFactor = dot(Normal, -xLightDirection);

    return Output;    
}

PixelShaderOutput TexturedPS(VertexShaderOutput PSIn) {
    PixelShaderOutput Output = (PixelShaderOutput)0;

    Output.Color = xTexture.Sample(TextureSampler, PSIn.TextureCoords);
    Output.Color.rgb *= saturate(PSIn.LightingFactor) + xAmbient;

    return Output;
}

technique Textured {
    pass p0 {
        Profile = 9.1;
        AlphaBlendEnable = false;
        CullMode = None;
        PixelShader = TexturedPS;
        VertexShader = TexturedVS;
    }
}

感谢任何帮助

1 个答案:

答案 0 :(得分:0)

当我发布这个问题时,我非常接近结果......我所要做的就是修复纹理变形并缩小纹理坐标,因为它的绘制方式太详细了。黑色地图是由纹理上的1px黑色边框和纹理采样器的夹紧引起的。现在它的工作几乎应该如此。只有AddressU = wrap;现在不想工作。