I am trying to convert from world space to screen space. However, due to the way the library I am using handles matrices, I am only able to retrieve the culmination of the world, view, and projection matrices; I cannot inspect either on its own. Here is an example matrix:
(1.65879, -0.000909163, -1.4656, -1721.39)
(1.20199, -1.64605, 1.36146, 3048.55)
(0.450536, 0.75107, 0.510306, 9264.52)
(0.444465, 0.740951, 0.50343, 9339.69)
I had assumed that finding the location of the object in screen space would be a simple matter of transforming the origin [a point at (0, 0, 0)] by this value. However, this is distinctly wrong:
D3DMATRIX matrix;
D3DXVECTOR4 position;
D3DXVECTOR4 input;
D3DVIEWPORT9 viewport;
GetVertexShaderConstantF(0, (float *)&matrix, 4);
GetViewport(&viewport);
input.x = 0.0f;
input.y = 0.0f;
input.z = 0.0f;
input.w = 1.0f;
D3DXVec4Transform(&position, &input, (D3DXMATRIX *)&matrix);
float x = ((position.x / position.w) * 0.5f + 0.5f) * viewport.Width + viewport.X;
float y = (1.0f - ((position.y / position.w) * 0.5f + 0.5f)) * viewport.Height + viewport.Y;
This method only returns the result of 450, 375 (+/- a few decimals), with the viewport being double that in size; basically, it gives the dead center of the viewport.
What am I doing wrong in the above code snippet?
(A side note, I am simply trying to debug a program I do not have the source to. Therefore, when the shader receives its inputs, this is the only point at which I can intercept the position of a model in screen space).
After
you have to call
because DirectX uses row-major matrices in the code and column-major ones in shaders (or the other way around, I don’t remember now).