Hello there,
I'm trying to learn how to render a simple scene in DirectX 10 with SharpDX's low level wrappers. I have already created a simple
application with a single triangle on the screen and a camera. Now i am trying to add a single directed light to the scene (like in the DX10 Example 6).
It works fine with hard-coded normals in the Effect-File (active line in the PS shader), but if I try to pass normals from my vector buffer (commented out line below) I don't see anything.
What i don't understand is why the type of the position vector should be float3 in the element definition and why is it float4 in the shader file (copied from DX10 example). And how maps the shader this two structures?
My setup for the test triangle (like in the DX10 Example) and my effect file follows:
thanks in advance!
ShaderBytecode shaderBytes = ShaderBytecode.CompileFromFile(_ShaderFile, "fx_4_0", ShaderFlags.None, EffectFlags.None, null, null);
_SimpleEffect = new Effect(device, shaderBytes);
EffectTechnique technique = _SimpleEffect.GetTechniqueByName("Render");
EffectPass pass = technique.GetPassByIndex(0);
var elements = new[] {
new InputElement(Helper3D.POSITION_ELEMENT_NAME, 0, Format.R32G32B32_Float, 0, 0),
new InputElement(Helper3D.NORMAL_ELEMENT_NAME, 0, Format.R32G32B32_Float, 0, 12)
};
_LightDir = new Vector4(1.0f, 1.0f, 1.0f, 1.0f);
_LightDir.Normalize();
_LightColor = new Vector4( 0.5f, 0.5f, 0.5f, 1.0f );
_VertexLayout = new InputLayout(device, pass.Description.Signature, elements);
_VertexStream = new DataStream(3 * 24, true, true);
_VertexStream.Write(new Vector3(0.0f, 0.5f, 0.5f)); _VertexStream.Write(new Vector3(0.0f, 0.0f, 1.0f));
_VertexStream.Write(new Vector3(0.5f, -0.5f, 0.5f)); _VertexStream.Write(new Vector3(0.0f, 0.0f, 1.0f));
_VertexStream.Write(new Vector3(-0.5f, -0.5f, 0.5f)); _VertexStream.Write(new Vector3(0.0f, 0.0f, 1.0f));
_VertexStream.Position = 0;
_Vertices = new SharpDX.Direct3D10.Buffer(device, _VertexStream, new BufferDescription()
{
BindFlags = BindFlags.VertexBuffer,
CpuAccessFlags = CpuAccessFlags.None,
OptionFlags = ResourceOptionFlags.None,
SizeInBytes = 3 * 24,
Usage = ResourceUsage.Default
});
_Camera.Position = new Vector3(0.0f, 0.0f, -20.0f);
matrix view;
matrix projection;
float4 vLightDir;
float4 vLightColor;
struct VS_IN
{
float4 pos : POSITION;
float3 norm : NORMAL;
};
struct PS_IN
{
float4 pos : SV_POSITION;
float3 norm : TEXCOORD0;
};
PS_IN VS( VS_IN input )
{
PS_IN output = (PS_IN)0;
output.pos = mul(input.pos, view);
output.pos = mul(output.pos, projection);
output.norm = input.norm;
return output;
}
float4 PS( PS_IN input ) : SV_Target
{
float4 finalColor = 0;
//finalColor = saturate( dot((float3)vLightDir, input.norm) * vLightColor);
finalColor = saturate(dot((float3)vLightDir, float3(0,0,1)) * vLightColor);
finalColor.a = 1;
return finalColor;
}
float4 PSSolid( PS_IN input) : SV_Target
{
return float4(1,0,0,1);
}
technique10 Render
{
pass P0
{
SetVertexShader( CompileShader( vs_4_0, VS() ) );
SetGeometryShader( 0 );
SetPixelShader( CompileShader( ps_4_0, PS() ) );
}
}
technique10 RenderLight
{
pass P0
{
SetVertexShader( CompileShader( vs_4_0, VS() ) );
SetGeometryShader( 0 );
SetPixelShader( CompileShader( ps_4_0, PSSolid() ) );
}
}