Hello Guys,
i have a big problem, i want to create an example for Instance Rendering, but the World Matricen dosen't work, only the Scale Informations comes to the Shader, the Translation and Rotation Values not :(
Here is my Code:
Shader:
matrix World : WORLD;
matrix View : VIEW;
matrix Projection : PROJECTION;
float3 DiffuseColor = {1.0f, 0.0f, 0.0f};
struct Vertex
{
float3 Position : POSITION;
matrix instance : INSTANCE;
};
struct Pixel
{
float4 Position : SV_POSITION;
};
float4 TransformVertex(Vertex input)
{
float4 objectSpace = float4(input.Position.xyz, 1.0f);
float4 worldSpace = mul(objectSpace, (World * input.instance));
float4 viewSpace = mul(worldSpace, View);
float4 projectionSpace = mul(viewSpace, Projection);
return projectionSpace;
}
Pixel vertexShader(Vertex input)
{
Pixel result = (Pixel) 0;
result.Position = TransformVertex(input);
return result;
}
float4 pixelShader(Pixel input) : SV_Target0
{
return float4(DiffuseColor.rgb, 1.0f);
}
technique11 Main
{
pass p0
{
SetVertexShader(CompileShader(vs_5_0, vertexShader()));
SetHullShader(NULL);
SetDomainShader(NULL);
SetGeometryShader(NULL);
SetPixelShader(CompileShader(ps_5_0, pixelShader()));
}
}
Initialize Shader:
this.simpleShader = new ShaderDescription(this.deviceEngine.Device, "simpleShader", "SimpleShader", @"C:\Path\SimpleShader.fx", "fx_5_0", new[]
{
new InputElement("POSITION", 0, Format.R32G32B32_Float, 0, 0, InputClassification.PerVertexData, 0),
new InputElement("INSTANCE", 0, Format.R32G32B32A32_Float, 0, 1, InputClassification.PerInstanceData, 1),
new InputElement("INSTANCE", 1, Format.R32G32B32A32_Float, 16, 1, InputClassification.PerInstanceData, 1),
new InputElement("INSTANCE", 2, Format.R32G32B32A32_Float, 32, 1, InputClassification.PerInstanceData, 1),
new InputElement("INSTANCE", 3, Format.R32G32B32A32_Float, 48, 1, InputClassification.PerInstanceData, 1),
});
Write Matrix in Buffer:
this.device = device;
int sizeInBytes = 64;
int totalSizeInBytes = 1 * sizeInBytes;
Matrix world = Matrix.Scaling(2, 1, 1) * Matrix.RotationYawPitchRoll(50, 0, 0) * Matrix.Translation(3, 0, 0);
DataStream vertexStream = new DataStream(totalSizeInBytes, true, true);
vertexStream.WriteRange(world.ToArray());
vertexStream.Position = 0;
this.buffer = new Buffer(device, vertexStream, new BufferDescription(totalSizeInBytes,
ResourceUsage.Default, BindFlags.VertexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0));
this.bufferBinding = new VertexBufferBinding(this.buffer, sizeInBytes, 0);
DrawInstanced:
this.device.SetVertexBuffer(0, this.vertexBuffer.BufferBinding);
this.device.SetVertexBuffer(1, this.matricenBuffer.BufferBinding);
this.device.DeviceContext.InputAssembler.PrimitiveTopology = mesh.VerticesType;
this.Apply(mesh.Material, world, view, projection);
this.device.DeviceContext.DrawInstanced(mesh.Vertices.Count, 1, this.startPosition, 0);
I don't get it what i make wrong, hope you guys can help me!?
Greets
John