1
votes

I am currently developing a Direct3D 11 rendering engine using C#.

I have already succeded in drawing triangles, applying color and textures, etc. The object I am trying to display now is a rotated cube with a side length of 1 placed at the origin of the world space. But I am getting unsatisfying results when applying the view and especially the projection matrix. As it is difficult to explain, I just show you what result I get for different view/projection combinations:


HLSL snippet from my vertex shader:

float4x4 wvpMatrix = mul(World, mul(View, Projection));
output.Position = mul(input.Position, wvpMatrix);

The world matrix I use:

// Static rotation around all three axes
matrices.World = Matrix.Identity * Matrix.CreateFromYawPitchRoll(0.5f, 0.6f, 1.0f);

Test A

Simple view vector from origin in the positive z direction. No projection used. You can see that the back of the cube is being clipped.

matrices.View = Matrix.CreateLookAt(new Vector3(0, 0, 0), new Vector3(0, 0, 1), Vector3.Up);
matrices.Projection = Matrix.Identity;

Cube with back corners clipped


Test B

View matrix stays the same, but now I am using an orthographic projection matrix. This time the front of the cube vanishes. The parts you can see are those which were clipped in Test A.

matrices.View = Matrix.CreateLookAt(new Vector3(0, 0, 0), new Vector3(0, 0, 1), Vector3.Up);
matrices.Projection = Matrix.CreateOrthographic(2, 2, 0.01f, 100.0f);

Cube with front corner clipped


Test C

Again no projection, just like in Test A. This time the scene is being viewed from a point three units in the negative z direction. The cube looks distorted when the camera position is not set to the origin of the system. I think there is also some clipping.

matrices.View = Matrix.CreateLookAt(new Vector3(0, 0, -3), new Vector3(0, 0, 1), Vector3.Up);
matrices.Projection = Matrix.Identity;

Distorted cube


Test D

Finally, I use a perspective projection. Up to now I have not found a camera position from which any part of the cube is visible. All I get is fullscreen cornflower blue.

matrices.View = Matrix.CreateLookAt(new Vector3(0, 0, -3), new Vector3(0, 0, 1), Vector3.Up);
matrices.Projection = Matrix.CreatePerspectiveFieldOfView(1, 1, 0.1f, 100);

No visible parts of the cube


First thing I want to do is drawing the full cube - no clipping. I tried different combinations of near/far plane values but I could not find an acceptable solution. My final goal would be to have a perspective projection. But as you see I can't even get a good orthagonal view of the scene.

1
Hi, here are some tips. Make sure that the transformation matrix is multiplied in the correct order. Whit this i mean that a vertex should be multiplied first by the world matrix, the result should be multiplied by the view matrix, etc. This could be easy tested by modifying HLSL code. Also, try to use orthogonal projection again and set the near/far plain to 0.000001f/10000.0f. The last thing you could check is if you are using triangle primitives for the cube or if you are modelling the cube as tringle-strip. In this case you will need to render it using triangle or triangle-strip. - brano
Hello, the current order was world, view, projection. I even split the multiplication apart now, nothing changed (as I expected). Additionally, changing the near and far plane didn't affect the result. Only in the range from 0 till 0.45 some more clipping occurred (more than in the second image). Topology is TriangleList. - Lucius
could you post the coords of the cube? - brano
Thank you, I figured it out on my own ;) - Lucius

1 Answers

0
votes

The matrices must be transposed at some point.

Reason:

The layout of the Microsoft.Xna.Framework.Matrix struct is row-based. Matrices in HLSL are column-based.

From the XNA Documentation:

Matrices use a row vector layout in the XNA Framework.

From the Wikipedia article on matrix order:

Column-major order is a similar method of flattening arrays onto linear memory, but the columns are listed in sequence. The [...] shading languages GLSL and HLSL use column-major ordering.


Solution A:

Transpose all matrices before they are sent to the shader:

var projection = Matrix.CreatePerspectiveFieldOfView(1, 1, 0.1f, 100);
projection = Matrix.Transpose(projection);

Solution B:

Put this line in your shader code:

#pragma pack_matrix( row_major )