# Beginner Question: 3D to 2D projection of a point in pixel shader

This topic is 2010 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi everyone!

I've got a very basic question (I'm really bad in math, so please no introductions into vector geometry):

In my pixel shader I want to draw a point whose 3D world-space coordinates I have onto the screen. How do I calculate its correct 2D screen-space coordinates, given the camera/view direction and the field of view angle (83 degrees) of the screen?

##### Share on other sites
You need to setup the projection matrix, that is all. Both, OpenGL(glut) and DirectX , provide according utility methods to calculate the projection matrix for you. Edited by Ashaman73

##### Share on other sites
[quote name='Ashaman73' timestamp='1342181474' post='4958764']
You need to setup the projection matrix, that is all. Both, OpenGL(glut) and DirectX , provide according utility methods to calculate the projection matrix for you.
[/quote]

Thanks. I'm using DirectX, so what utility methods would I need here and what parameters to feed them?

EDIT:
I'm in a pixel shader, so I can only use HLSL code, no D3d... functions! Edited by Meltac

##### Share on other sites
[url="http://msdn.microsoft.com/en-us/library/windows/desktop/bb147302%28v=vs.85%29.aspx"]Setting up your projection matrix in DX9.[/url]

##### Share on other sites
I think my game already provides the world, view, and projection matrices:

[CODE]
uniform half3x4 m_W;
uniform half3x4 m_V;
uniform half4x4 m_P;
uniform half3x4 m_WV;
uniform half4x4 m_VP;
uniform half4x4 m_WVP;
[/CODE]

But which one do I have to use here, and how to apply it?

##### Share on other sites
If I understand you correctly, you are in a vertex shader that likely has a world space position as input and you are trying to output the appropriate position. To do this you will need to do something like the following:

{
[indent=1]float4 outPos = mul(float4(worldPos, 1), m_WVP);[/indent]
[indent=1]...[/indent]
[indent=1]return outPos;[/indent]
}

##### Share on other sites
[quote name='JBurnside' timestamp='1342203655' post='4958858']
If I understand you correctly, you are in a vertex shader that likely has a world space position as input and you are trying to output the appropriate position. To do this you will need to do something like the following:

{
[indent=1]float4 outPos = mul(float4(worldPos, 1), m_WVP);[/indent]
[indent=1]...[/indent]
[indent=1]return outPos;[/indent]
}
[/quote]

No, as mentioned in my first post I'm in a pixel shader, so how would I do it then?

##### Share on other sites
You can't do it in a pixel shader - the projection has already been done a coupla stages in the pipeline before that and can't be modifed. You need to use a vertex shader instead.

##### Share on other sites
[quote name='mhagain' timestamp='1342296217' post='4959123']
You can't do it in a pixel shader - the projection has already been done a coupla stages in the pipeline before that and can't be modifed. You need to use a vertex shader instead.
[/quote]

Sorry but I disagree. It might be true that I'm [b]not supposed[/b] to do this in a pixel shader, as it's normally part of the vertex shader's work. But that doesn't necessarily mean that it's [b]not possible[/b] to do in a pixel shader - only the required approach would be different.

I can do all math I want in my pixel shader, so what I just need here is the [b]correct geometric transformation formula[/b]. That's a purely mathematical issue, not one of any GPU pipeline thingy.

So are there any other ideas here? One alternate option that came into my mind was if I have the actual [b]angle between the view direction and the location vector of the spot[/b] in question (which can simply be calculated with the dot product as I've learned), then it might be possible to calculate the [b]horizontal distance from that spot to the screen center[/b] when its projected to screen space by some formula. However that formula is not linear, it's likely to involve some circle calculation or something, so I've not been able to figure it out so far.

Any help here?

EDIT:
I guess I'll need some like kind of Azimuthal Projection: See here: [url="http://www-history.mcs.st-and.ac.uk/Projects/Hoyer/S3.html"]http://www-history.m...s/Hoyer/S3.html[/url]

For a start it would already be great to know which kind of projection needs to be done here:[list]
[*]Gnomonic Projection, or
[*]Stereographic Projection, or
[*]Orthographic Projection?
[/list]

EDIT 2:
Apparently what I need is perspective projection, so probably none of the above. But still this is pure math, isn't it? So there must be some formula! Edited by Meltac

##### Share on other sites
You can certainly do whatever math you want in a pixel shader, but you can't change what pixel is being defined at that point in the pipeline. The math I posted for the vertex shader would work in a pixel shader if you have the right data, but the pixel being processed has already been defined.

##### Share on other sites
[quote name='JBurnside' timestamp='1342535337' post='4960009']
You can certainly do whatever math you want in a pixel shader, but you can't change what pixel is being defined at that point in the pipeline. The math I posted for the vertex shader would work in a pixel shader if you have the right data, but the pixel being processed has already been defined.
[/quote]

That wouldn't be the issue. I don't need to [i]change[/i] any pixel's position either in world nor in screen space. I just need want to do the following:
[list=1]
[*]Take the world space sun position and mirror that spot on the axis of the camera to get a virtual world point opposite to the sun
[*]Calculate the screen space coordinate of that point with perspective projection
[*]Check whether the currently processed pixel is within some screen-space distance (in 2D) range to that projected spot.
[*]If so, output some color blended into the original pixel color.
[/list]

When done this would result in a rainbow being drawn as some sort of post process effect from within my pixel shader.

So, is this possible with that code snippet above, and if so, why does the mul(float4(worldPos, 1), m_WVP) result in an incorrect screen position?

EDIT:
I have tested several different projection approaches and matrices. The overall result is always the same: When I turned the camera to the left or right, the calculated screen position is not accurate.

More precisely, I "moves" over the screen while turning in the correct manner as long as near the screen center. Then, when about 1/4 of the screen away from center, it starts moving "faster", meaning the calculated position is further away from the center than it should be. Then, when coming to some 3/4 of the screen (i.e. near the left or right border), the calculated point moves too slow, i.e. is not enough right/left positioned.

This happens not abruptly but fluently, like some roller coaster movement, first the calculated position wanders too much left/right, then too few left/right. Seems to depend on the circular movement of turning, but I couldn't figure out how to express this in a formula or what adjustments must take place to correct this. Edited by Meltac

##### Share on other sites
[quote name='Ashaman73' timestamp='1342181781' post='4958767']
[url="http://msdn.microsoft.com/en-us/library/windows/desktop/bb147302%28v=vs.85%29.aspx"]Setting up your projection matrix in DX9.[/url]
[/quote]

I've tried building my own projection matrix both with the above approach and with this one:

[url="http://www.geeks3d.com/20090729/howto-perspective-projection-matrix-in-opengl/"]http://www.geeks3d.c...trix-in-opengl/[/url]

Both didn't help. Strange thing is, in both cases [b]only changing the value of FOV changes the result, while near and far plane values seem to be ignored at all.[/b]

Why this? Is this kind of matrix / projection the thing I need here at all? Looks to me very much as if I'm missing something incredibly basic, but somehow I couldn't figure out what's wrong here.

EDIT: typos. Edited by Meltac