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

This topic is 2161 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

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.

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
I think my game already provides the world, view, and projection matrices:

 uniform half3x4 m_W; uniform half3x4 m_V; uniform half4x4 m_P; uniform half3x4 m_WV; uniform half4x4 m_VP; uniform half4x4 m_WVP; 

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=1]...

[indent=1]return outPos;

}

##### 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=1]...

[indent=1]return outPos;

}

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

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.

Sorry but I disagree. It might be true that I'm not supposed 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 not possible 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 correct geometric transformation formula. 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 angle between the view direction and the location vector of the spot in question (which can simply be calculated with the dot product as I've learned), then it might be possible to calculate the horizontal distance from that spot to the screen center 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: http://www-history.m...s/Hoyer/S3.html

For a start it would already be great to know which kind of projection needs to be done here:

• Gnomonic Projection, or
• Stereographic Projection, or
• Orthographic Projection?

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.

• 35
• 12
• 10
• 9
• 9
• ### Forum Statistics

• Total Topics
631355
• Total Posts
2999521
×