• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Sun position in "texcoord space"

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

4 replies to this topic

### #1belfegor  Members

Posted 18 November 2012 - 07:41 AM

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"
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));
...


Then i render fullscreen quad and use this pixel shader to be able to see where the sun is:
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);


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?

Edited by belfegor, 18 November 2012 - 07:41 AM.

### #2Waterlimon  Members

Posted 18 November 2012 - 07:54 AM

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.

o3o

### #3belfegor  Members

Posted 18 November 2012 - 08:00 AM

I just tried something:
sunPos = sunDir * 100000.0f;// move it far away


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?
D3DXVec4Transform(&out, &D3DXVECTOR4(sunDir.x, sunDir.y, sunDir.z, 0.0f, &view);


### #4Waterlimon  Members

Posted 18 November 2012 - 10:40 AM

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...

o3o

### #5EsorU  Members

Posted 19 November 2012 - 03:10 PM

Maybe this >Math for computing relative sun direction< could help.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.