Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » World to Screen Space

This thread is locked; no one can reply to it. rss feed Print
World to Screen Space
Erin Maus
Member #7,537
July 2006
avatar

Another thread, and this time I have a problem that I'm totally stumped on and have exhausted all avenues that I can think of.

I have a matrix that is the culmination of the world, view, and projection matrices:

(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 do not know how it is calculated (e.g., what parameters construct this example matrix).

I need to go from world space to screen space using this matrix. I figured I'd use the origin point, (0, 0, 0), and transform this by the "WorldViewProjection" matrix, like so:

#SelectExpand
1 D3DMATRIX matrix; 2 D3DXVECTOR4 position; 3 D3DXVECTOR4 input; 4 D3DVIEWPORT9 viewport; 5 6 GetVertexShaderConstantF(0, (float *)&matrix, 4); 7 GetViewport(&viewport); 8 9 input.x = 0.0f; 10 input.y = 0.0f; 11 input.z = 0.0f; 12 input.w = 1.0f; 13 14 D3DXVec4Transform(&position, &input, (D3DXMATRIX *)&matrix); 15 float x = ((position.x / position.w) * 0.5f + 0.5f) * viewport.Width; 16 float y = (1.0f - ((position.y / position.w) * 0.5f + 0.5f)) * viewport.Height;

However, it is apparent my math is off. No matter what point I supply to D3DXVec4Transform as input, the final process makes the point somewhere in the range 450 X and 370 Y -- basically, the dead center of the screen.

My question is, is it possible to transform from world space to screen space by transforming the origin? If not, how would I calculate, if possible, the screen space coordinates simply with a WorldViewProjection matrix and the viewport?

Thanks.

---
ItsyRealm, a quirky 2D/3D RPG where you fight, skill, and explore in a medieval world with horrors unimaginable.
they / she

verthex
Member #11,340
September 2009
avatar

I'll be honest with you and just say that direct 3d is dead and everyone has moved on to irrlicht instead (And you're on a.cc where allegro 5 is mostly the topic). You might with some luck find 1 person on this site who cares about it, or knows anything about it. I used direct3d myself maybe 6 years and C.R.S about it. ;)

Erin Maus
Member #7,537
July 2006
avatar

I fixed it. After pulling my hair for hours, I resorted to figuring out how to use PIX. Turns out the shader in question uses a transpose of the matrix (What..?). Like anyone would have guessed that.

Final code:

#SelectExpand
1D3DMATRIX matrix; 2 D3DXVECTOR4 position; 3 D3DXVECTOR4 input; 4 D3DVIEWPORT9 viewport; 5 6 GetVertexShaderConstantF(0, (float *)&matrix, 4); 7 GetViewport(&viewport); 8 9 input.x = 0.0f; 10 input.y = 0.0f; 11 input.z = 0.0f; 12 input.w = 1.0f; 13 14 D3DXMatrixTranspose((D3DXMATRIX *)&matrix, (D3DXMATRIX *)&matrix); 15 16 position.x = input.x * matrix._11 + input.y * matrix._21 + input.z * matrix._31 + matrix._41; 17 position.y = input.x * matrix._12 + input.y * matrix._22 + input.z * matrix._32 + matrix._42; 18 position.z = input.x * matrix._13 + input.y * matrix._23 + input.z * matrix._33 + matrix._43; 19 position.w = input.x * matrix._14 + input.y * matrix._24 + input.z * matrix._34 + matrix._44; 20 21 float x = ((position.x / position.w) * (viewport.Width / 2)) + viewport.X + (viewport.Width / 2); 22 float y = viewport.Y + (viewport.Height / 2) - ((position.y / position.w) * (viewport.Height / 2));

And verthex, your reply was useless...

---
ItsyRealm, a quirky 2D/3D RPG where you fight, skill, and explore in a medieval world with horrors unimaginable.
they / she

verthex
Member #11,340
September 2009
avatar

And verthex, your reply was useless...

So was yours.

Trent Gamblin
Member #261
April 2000
avatar

I think it depends on order of multiplication of matrices. If you you multiply A*B then you could also multiply B*transpose(A) or something like that. Not sure if they both have to be transposed, or which one has to be... but it's better to get the multiplication order right.

Erin Maus
Member #7,537
July 2006
avatar

Sadly I don't have access to the target software's internals. I can only access what is sent to the graphics hardware. Therefore, I did not know the order of multiplication, whether or not a matrix was transposed, etc. In fact, I'm surprised I've accomplished what I have in 6 days...

verthex said:

So was yours.

Erm, I posted the solution to my problem, for the future generations of mankind to laugh at how stupid (e.g., simple) the solution was. You contributed nothing besides telling me not to use DirectX and some random gibberish. The only insightful thing in your post was that this is an Allegro forum...

To clarify, I must use DirectX for this project. The game I am debugging uses DirectX much better (as in: much more sensibly) than it does during the equivalent OpenGL calls.

---
ItsyRealm, a quirky 2D/3D RPG where you fight, skill, and explore in a medieval world with horrors unimaginable.
they / she

verthex
Member #11,340
September 2009
avatar

If you you multiply A*B then you could also multiply B*transpose(A) or something like that. Not sure if they both have to be transposed, or which one has to be... but it's better to get the multiplication order right.

If you multiply row times column its different than column times the row. There are exceptions if the matrices are commutative.

Erm, I posted the solution to my problem, for the future generations of mankind to laugh at how stupid (e.g., simple) the solution was. You contributed nothing besides telling me not to use DirectX and some random gibberish. The only insightful thing in your post was that this is an Allegro forum...

None one cares about direct x. I'm pretty sure there is a consensus around the internet. Find a forum for one.

Erin Maus
Member #7,537
July 2006
avatar

Verthex...my question only used the DirectX API. It was not related to DirectX in any other form. I asked a question about matrices and projection. The problem and solution are platform independent and just as useful in OpenGL (and heck, even Allegro!) as they are in DirectX.

---
ItsyRealm, a quirky 2D/3D RPG where you fight, skill, and explore in a medieval world with horrors unimaginable.
they / she

verthex
Member #11,340
September 2009
avatar

Verthex...my question only used the DirectX API. It was not related to DirectX in any other form. I asked a question about matrices and projection. The problem and solution are platform independent and just as useful in OpenGL (and heck, even Allegro!) as they are in DirectX.

Not really because not many people know the direct3d types and those are dependent on direct3d only. I don't know what they do without looking them up and that could take hours to relearn. I guess the calculations you posted as your answer are platform independent.

Trent Gamblin
Member #261
April 2000
avatar

verthex said:

If you multiply row times column its different than column times the row.

It's different if the matrices are square too.

verthex
Member #11,340
September 2009
avatar

It's different if the matrices are square too.

If they are complex valued then I think they can be Hermitian and thats also self adjoint, I might be mistaken.

LennyLen
Member #5,313
December 2004
avatar

And verthex, your reply was useless...

His replies always are. Just ignore them if you actually want anything useful.

verthex
Member #11,340
September 2009
avatar

LennyLen said:

His replies always are. Just ignore them if you actually want anything useful.

His replies always are. Just ignore them if you actually want anything useful.

Heres the directx forum. Take a look at all the unanswered threads and tell me that DX is alive.

CursedTyrant
Member #7,080
April 2006
avatar

Aaron: I believe any of your future DirectX related questions will be more successful on GameDev, because they have an entire DirectX and XNA section on their forums.

---------
Signature.
----
[My Website] | [My YouTube Channel]

james_lohr
Member #1,947
February 2002

Erm, I posted the solution to my problem, for the future generations of mankind to laugh at how stupid (e.g., simple) the solution was. You contributed nothing besides telling me not to use DirectX and some random gibberish. The only insightful thing in your post was that this is an Allegro forum...

I'm hoping that one day he'll get the message and stop polluting this forum with his useless posts. :P

verthex
Member #11,340
September 2009
avatar

I'm hoping that one day he'll get the message and stop polluting this forum with his useless posts.

Just ignore James, his proven himself to be completely useless many times in my threads too.

relpatseht
Member #5,034
September 2004
avatar

I'm not sure if verthex is trolling or just plain ignorant. Regardless, neither is really a good thing.

Arthur Kalliokoski
Second in Command
February 2005
avatar

Maui Waui fryer.

They all watch too much MSNBC... they get ideas.

verthex
Member #11,340
September 2009
avatar

I'm not sure if verthex is trolling or just plain ignorant. Regardless, neither is really a good thing.

Sure and when I'm ignorant with my directx question I'll ask that question on this site and all of you will help me right. Its not ignorant at all because every interview I've gone to on this island has landed me on the hotseat question about what api do I know and I say directx sort of and they need opengl, open scenegraph. Thats one job at oceanic imaging consultants and another at hyperspective, but good luck with getting those dream jobs.

relpatseht
Member #5,034
September 2004
avatar

Two companies and one tiny island do not make up the norm, especially when they do not directly pertain to games.

I'm not even saying DirectX is more popular than OpenGL as far as games are concerned (it probably is, but I'm certainly not saying that), or DirectX is better, but DirectX does, certainly, matter in game companies.

Subjectively, I'd say DirectX 11 is a better API than OpenGL, but a bad choice as you lose out on far too much market share without XP support.

With cross platform/indie games growing in consumer base, OpenGL does continue to gain market share (conjecture), but DirectX is still very dominant in large studios and AAA titles, as well as everything on the XBox.

jmasterx
Member #11,410
October 2009

These days though, I do not think there is a single company that would start writing a game specifically for DirectX (There used to be a lot). What is done nowdays is an engine is created to support Xbox, PS3,Wii, PC, etc which abstracts away any underlying API. There is such an abundance of engines now that a small company can just license one and not even know what DirectX is.

I prefer GL personally. It's cross platform and at least it remembers your textures when you resize the window :-X

Go to: