Search code examples
c#shaderdirectx-11sharpdxperspectivecamera

Wrong result, drawing a cube in SharpDX


I want to draw a cube in SharpDX. And this is my current result:

Cube

As you can see, at some point, parts of the cube are cut.

Cube2

I don't know what setting is wrong, what could it be? I think it's something wrong with the perspective. I tried to create a perspective view, but i never got another result than an empty window.

Here is my code:

struct Vertex
{
    public Vector4 position;
    public Color4 color;
    //...
}

SharpDX.Direct3D11.Buffer vertexBuffer;
SharpDX.Direct3D11.Buffer projBuffer;

Matrix projMatrix;

Vertex[] vertices;

Init:

//...
BufferDescription description = new BufferDescription(sizeof(float) * 8 * vertices.Length, ResourceUsage.Dynamic, BindFlags.VertexBuffer, CpuAccessFlags.Write, ResourceOptionFlags.None, 0);
vertexBuffer = SharpDX.Direct3D11.Buffer.Create(Device, vertices, description);

//To pass projection matrix to shader
projBuffer = new SharpDX.Direct3D11.Buffer(Device, Matrix.SizeInBytes, ResourceUsage.Default, BindFlags.ConstantBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0);
DeviceContext.VertexShader.SetConstantBuffer(1, projBuffer);

Update:

projMatrix = Matrix.OrthoOffCenterLH(-10, 10, -screenRatio*10, screenRatio*10, 1f, 100f);

//How can i create a perspective view?
//projMatrix = Matrix.PerspectiveFovLH(1f, (float)width/height, 1f, 100f);

projMatrix *= Matrix.Rotation...

DeviceContext.UpdateSubresource(ref projMatrix, projBuffer);

Shader:

struct VOut
{
  float4 position : SV_POSITION;
  float4 color    : COLOR;
};

cbuffer meshBuffer : register(b1)
{
  float4x4 projMatrix;
}

VOut VShader(float4 position : POSITION, float4 color : COLOR)
{
  VOut output;

  output.position = mul(position, projMatrix);
  output.color = color;

  return output;
}

float4 PShader(float4 position : SV_POSITION, float4 color : COLOR) : SV_TARGET
{
  return color;
}

What is wrong?


Solution

  • As Alex pointed out: There is something wrong with the clipping plane. I changed in my RasterizerStateDescription IsDepthClipEnabled to false. Then i had my full cube, but not with the right perspective.

    My problem about the perspective view had multiple reasons. One reason was, that i didn't transposed the projection matrix (pointed out by Roger Rowland).

    I asked here (gamedev.stackexchange.com) and:

    The second one was, that i used some transformations after i set my perspective. That caused some weird deformations.

    The third reason was, that i put wrong coordinates for the cube. The result was a wrong culling.