1
\$\begingroup\$

So I want to create one of these in my directX11 project.

enter image description here

The little axis there that shows you the direction.

I have a nice 3d shader for my world geometry that I tweaked from Frank D. Luna (He writes books on directX.) and another for my 2D UI so I thought I'd look at those and see what I could conjure up since I didn't know all that much about shaders to begin with. But this is what I came up with.

float4x4 gWorldViewProj;

struct VertexIn
{
    float3 PosL    : POSITION;
    float3 NormalL : NORMAL;
    float2 Tex     : TEXCOORD;
};

struct VertexOut
{
    float4 PosH       : SV_POSITION;
    float2 Tex        : TEXCOORD;
};

VertexOut VS(VertexIn vin)
{
    VertexOut vout;

    // Transform to homogeneous clip space.
    vout.PosH = mul(float4(vin.PosL, 1.0f), gWorldViewProj);

    // There are no transformations for texture coordinates.
    vout.Tex = vin.Tex;

    return vout;
}

This code works if I pass in a quad and render a texture too it. Then for gWorldViewProj I give it just the world matrix of the UI. Renders fine.

But when I tried it with 3D objects (anything that wasn't a quad.) nothing would render. The only way I could get it to render is by passing in world * view * proj but then it rotates around the scene when you look around because of the view matrix.

So the obvious work around was to create a view matrix with no transformations and that worked.

I thought I could just give it a world matrix since that's what I did with the 2D UI. I also tried world * proj but no prevail. Why does this shader render quads without a view matrix but can't render other more complex sets of geometry without the view matrix. Here is how I generated the quad.

\$\endgroup\$
  • \$\begingroup\$ I don't exactly follow you... Please remove all of the code except the actual VS. Please, then, add the code you use to generate the quad. \$\endgroup\$ – Jon Apr 22 '15 at 4:03
1
\$\begingroup\$

Please also expand on this: "world matrix of the UI" and show how it's constructed.

If you do not explicitly supply a World, View, or Projection, Identity is used. This has the effect of multiplying everything by 1 (no change). Using a world matrix without a view matrix is the same as using a view matrix without a world matrix; (X*1)==(1*X)==(X). I might be misunderstanding, but I suspect your confusion is stemming from that.

If World, View, and Projection are all Identity, then world-space, view-space, projection-space, and screen-space all have the same origin and units. If any matrix is not Identity, coordinates from the previous coordinate system are being moved/scaled into the next. When all objects have been located in world-space by their individual world-matrices, the entire world is moved/rotated/scaled at once by the view matrix. If the view matrix happens to be Identity, the objects are, now, already in projection space.

This is what you are doing, but its not clear, to me, exactly how yet; will update as necessary.

Keep in mind that:

translating coordinates (away from (0,0)), Then rotating them (around (0,0))  

is not the same as:

rotating coordinates (around (0,0)), Then translating them (away from (0,0))

So the obvious work around was to create a view matrix with no transformations and that worked.

Your intuition is spot-on; it is very common to keep the rotation component of the view matrix separate.

Another note about view matrices:
The view matrix does not typically scale coordinates; it only moves or rotates them. That means that world-units and camera-units are usually the same; only the origin and orientation are changed. When the camera moves "left 5", the objects in view appear to move "right 5", instead. View-space's origin (0,0) is the "camera"; to "make the camera become (0,0)", the entire world (including the camera) is "un-moved" by the camera's 3D position. Similarly, the world is "un-rotated" by the camera's rotation. A view-matrix is an "un-world-matrix".

If you wanted to render a mesh representing a camera (not THE camera, obviously), you'd locate the mesh in world-space with a world matrix. That same world matrix (excluding scale), inverted, is the view matrix that you'd use to render from that camera's perspective.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.