# Sun position in "texcoord space"

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

## Recommended Posts

Let me explain my problem:

I have a sun, and let say it is at position 100, 50, 20

And then i use this code to obtain sun position in "texcoord space"
[code]
viewProj = cam.view * cam.proj;
...
D3DXVECTOR2 sunPosCS;
D3DXVECTOR4 clipSpaceLightPos;
D3DXVec4Transform(&clipSpaceLightPos, &D3DXVECTOR4(sunPos.x, sunPos.y, sunPos.z, 1.f), &viewProj);
D3DXVECTOR4 screenSpaceLightPos = clipSpaceLightPos / clipSpaceLightPos.w;
sunPosCS = D3DXVECTOR2(screenSpaceLightPos.x, screenSpaceLightPos.y);
sunPosCS.x = ((sunPosCS.x + 1.0f) / 2.0f);
sunPosCS.y = ((-sunPosCS.y + 1.0f) / 2.0f);
...
effect->SetValue(hSunPosCS, sunPosCS, sizeof(D3DXVECTOR2));
...
[/code]

Then i render fullscreen quad and use this pixel shader to be able to see where the sun is:
[code]
OUT.Color = distance(IN.TexCoord0, LightPosCS) < 0.05f ? float4(1.0f, 0.0f, 0.0f, 1.0f) : float4(0.0f, 0.0f, 0.0f, 1.0f);
[/code]

When i face my camera to look at the sun, and then just move left or right, the red circle (representing the sun) is also moving but it should not, because sun in real life is quite far away from the viewer and its movement on the screen should not be noticeable. If you understand what i mean? Any suggestions?

Thank you for your time. Edited by belfegor

##### Share on other sites
Do something like:
1.Define the sun as a direction instead of a position
2.Transform the point into camera space
3.If Z is negative, sun is not visible. Then throw the Z away.
4.now you have x,y, multiply that by some FoV and stuff and check if the resulting point is on the screen, then render sun there if it is.

##### Share on other sites
I just tried something:
[code]
sunPos = sunDir * 100000.0f;// move it far away
[/code]

and it is barely seen to move.

@Waterlimon
1 What you mean by "define as direction"? Just take sun direction?
2. Transform sun direction by view matrix?
[code]
D3DXVec4Transform(&out, &D3DXVECTOR4(sunDir.x, sunDir.y, sunDir.z, 0.0f, &view);
[/code]

##### Share on other sites
By sun direction, since the sun is practically infinitely far away, i mean take the direction from the origin of the world to where on the sky you want the sun to be.

Not sure about the matrix. I mean take the world-space sun direction, and somehow transform it into camera-space (like the camera was in the origin, facing somewhere, and the direction was a point 1 unit away from the origin, in the direction of the sun, then move it into camera-space)

1.Take direction of the sun from the origin of the world (0,1,0) would be directly above
2.Move that direction to the object space of the camera. So if camera faced down, the direction from cameras perspective would be 0,-1,0 if in world space it was 0,1,0
I dont exactly remember how this matrix stuff works, but it MIGHT go like:
a)Take camera rotation (throw position away)
b)Take inverse of that
c)Multiply the world space sun direction with that (not sure about multiplication order)
3.Now make sure Z of the resulting vector is positive/negative to make sure the sun is not behind the camera
4.Throw Z away, leaving you with X and Y
5.Multiply or divide X and Y with some values related to the FoV, and you have the position of the sun on the screen. (make sure its on the screen, might be outside) Also you might need to adjust it to get correct scale and origin...