Recently I start to study SharpDx, so I am a newbie for it. I try to create a cube which has difference color on each vertex such as the picture below.
However, what I created after my coding is something I'm not understand such as the picture below.
I have checked my code many time but still couldn't find out what was wrong with it. I hope somebody can point it out for me. Many Thanks.
Hear is My Code
InitializeDirectX:
int width = renderForm.ClientSize.Width;
int height = renderForm.ClientSize.Height;
Rational rational = new Rational(60, 1);
ModeDescription backBufferDesc = new ModeDescription(width, height, rational, Format.R8G8B8A8_UNorm);
SwapChainDescription swapchainDesc = new SwapChainDescription
{
ModeDescription = backBufferDesc,
SampleDescription = new SampleDescription(1, 0),
Usage = Usage.RenderTargetOutput,
BufferCount = 1,
OutputHandle = renderForm.Handle,
IsWindowed = true,
SwapEffect = SwapEffect.Discard
};
SharpDX.Direct3D11.Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.None, swapchainDesc, out device, out swapChain);
context = device.ImmediateContext;
RasterizerStateDescription rasterDescription = RasterizerStateDescription.Default();
rasterDescription.FillMode = FillMode.Solid;
rasterDescription.CullMode = CullMode.None;
rasterDescription.IsDepthClipEnabled = false;
RasterizerState rasterizerState = new RasterizerState(device, rasterDescription);
context.Rasterizer.State = rasterizerState;
DepthStencilStateDescription depthStencilDesciption = DepthStencilStateDescription.Default();
depthStencilDesciption.DepthComparison = Comparison.LessEqual;
depthStencilDesciption.IsDepthEnabled = true;
DepthStencilState depthStencilState = new DepthStencilState(device, depthStencilDesciption);
context.OutputMerger.SetDepthStencilState(depthStencilState);
Utilities.Dispose<RenderTargetView>(ref renderTargetView);
Texture2D backBuffer = swapChain.GetBackBuffer<Texture2D>(0);
renderTargetView = new RenderTargetView(device, backBuffer);
backBuffer.Dispose();
Texture2D zBuffer = new Texture2D(device, new Texture2DDescription
{
Format = Format.D16_UNorm,
ArraySize = 1,
MipLevels = 1,
Width = renderForm.ClientSize.Width,
Height = renderForm.ClientSize.Height,
SampleDescription = new SampleDescription(1, 0),
Usage = ResourceUsage.Default,
BindFlags = BindFlags.DepthStencil,
CpuAccessFlags = CpuAccessFlags.None,
OptionFlags = ResourceOptionFlags.None
});
depthStencilView = new DepthStencilView(device, zBuffer);
zBuffer.Dispose();
context.Rasterizer.SetViewport(0, 0, renderForm.ClientSize.Width, renderForm.ClientSize.Height);
context.OutputMerger.SetTargets(depthStencilView, renderTargetView);
My Hlsl:
cbuffer CameraConstant:register(b0) {
matrix World;
matrix View;
matrix Projection;
};
struct VS_IN
{
float4 position : POSITION;
float4 color : COLOR;
};
struct PS_IN
{
float4 position : SV_POSITION;
float4 color : COLOR;
};
PS_IN VS(VS_IN input)
{
PS_IN output = (PS_IN)0;
output.position = mul(input.position, World);
output.position = mul(output.position, View);
output.position = mul(output.position, Projection);
output.color = input.color;
return output;
}
float4 PS(PS_IN input) : SV_Target
{
return input.color;
}
LoadContent:
filename = "Hlsls\\cube.txt";
shaderByteCode = ShaderBytecode.CompileFromFile(filename, "VS", "vs_5_0");
vertexShader = new VertexShader(device, shaderByteCode);
shaderSignature = ShaderSignature.GetInputSignature(shaderByteCode);
inputLayout = new InputLayout(device, shaderSignature, new InputElement[]
{
new InputElement("POSITION",0,Format.R32G32B32_Float,0,0),
new InputElement("COLOR",0,Format.R32G32B32_Float,12,0)
});
shaderByteCode = ShaderBytecode.CompileFromFile(filename, "PS", "ps_5_0");
pixelShader = new PixelShader(device, shaderByteCode);
Eye = new Vector3(0.0f, 1.0f, 1.5f);
Target = new Vector3(0.0f, -0.1f, 0.0f);
CameraUp = Vector3.UnitY;
Angle = (float)Math.PI / 3.0f;
Ratio = (float)(renderForm.ClientSize.Width) / (float)(renderForm.ClientSize.Height);
NearestSight = 0.1f;
FarrestSight = 100.0f;
Projection = Matrix.Transpose(Matrix.PerspectiveFovLH(Angle, Ratio, NearestSight, FarrestSight));
View = Matrix.Transpose(Matrix.LookAtLH(Eye, Target, CameraUp));
World = Matrix.Transpose(Matrix.RotationY(0));
CameraConstant camera = new CameraConstant();
camera.World = World;
camera.View = View;
camera.Projection = Projection;
constantBuffer = SharpDX.Direct3D11.Buffer.Create(device, BindFlags.ConstantBuffer, ref camera);
verts = new PositionColorVertex[]
{
new PositionColorVertex(new Vector3(-0.5f,-0.5f,-0.5f),new Vector3(0.0f,0.0f,0.0f)),
new PositionColorVertex(new Vector3(-0.5f,-0.5f,0.5f),new Vector3(0.0f,0.0f,1.0f)),
new PositionColorVertex(new Vector3(-0.5f,0.5f,-0.5f),new Vector3(0.0f,1.0f,0.0f)),
new PositionColorVertex(new Vector3(-0.5f,0.5f,0.5f),new Vector3(0.0f,1.0f,1.0f)),
new PositionColorVertex(new Vector3(0.5f,-0.5f,-0.5f),new Vector3(1.0f,0.0f,1.0f)),
new PositionColorVertex(new Vector3(0.5f,-0.5f,0.5f),new Vector3(1.0f,0.0f,1.0f)),
new PositionColorVertex(new Vector3(0.5f,0.5f,-0.5f),new Vector3(1.0f,1.0f,0.0f)),
new PositionColorVertex(new Vector3(0.5f,0.5f,0.5f),new Vector3(1.0f,1.0f,1.0f))
};
vertexBuffer = SharpDX.Direct3D11.Buffer.Create(device, BindFlags.VertexBuffer, verts);
indeces = new int[]
{
0,2,1,1,2,3,
4,5,6,5,7,6,
0,1,5,0,5,4,
2,6,7,2,7,3,
0,4,6,0,6,2,
1,3,7,1,7,5
};
indexBuffer = SharpDX.Direct3D11.Buffer.Create(device, BindFlags.IndexBuffer, indeces);
RenderEffect:
context.ClearRenderTargetView(renderTargetView, backgroundColor);
context.ClearDepthStencilView(depthStencilView, DepthStencilClearFlags.Depth, 1.0f, 0);
context.VertexShader.Set(vertexShader);
context.VertexShader.SetConstantBuffer(0, constantBuffer);
context.PixelShader.Set(pixelShader);
context.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vertexBuffer, Utilities.SizeOf<PositionColorVertex>(), 0));
context.InputAssembler.SetIndexBuffer(indexBuffer, Format.R16_UInt, 0);
context.InputAssembler.PrimitiveTopology = SharpDX.Direct3D.PrimitiveTopology.TriangleList;
context.InputAssembler.InputLayout = inputLayout;
context.DrawIndexed(indeces.Length, 0, 0);
swapChain.Present(1, PresentFlags.None);
Many Thanks to the one who can point out my problems in these codes.