Developers Planet

Student123 February 2016

Keeping the sun in the same position

I am drawing a circle in OpenGL using a simple circle drawing shader that draws a circle around a point passed in. The point needs to be in screen space coordinates i,e, 1 to -1 in the x and y.

The problem is that the circle drawn doesn't stay in the same position like a real sun would, it seems to over rotate compared to other objects in the scene. This is due to it moving out of sync to the SkyBox.

The matrix passed into the SkyBox is:

``````    m_activeCamera.m_projectionMatrix *
m_activeCamera.m_viewMatrix *
glm::translate(mat4(1.0), m_activeCamera.m_position));
``````

The calculation for the light sun position so far:

``````vec4 returnPos = pos;
returnPos.w = 1.0f;
returnPos = projection * (view * returnPos);
return vec2(returnPos.x / returnPos.w, returnPos.y / returnPos.w);
``````

The position passed in to this function (pos) is:

``````SUN.position + m_activeCamera.position
``````

The view and projection matrix belong to the m_activeCamera.

Pictures to help explain the issue:

Starting position

If I move the camera from left to the right the sun stays in place, relative to the sky box.

If I rotate the camera the sun moves out of place with the skybox.

Student123 February 2016

The problem was that the "GetNDCCoords" function was returning a point between -1 and 1 in the X and Y if the sun was on screen of course. The function that draws the circle needs a position in screen space 0 to 1. This was solved with a simple formula:

``````screenPos.x = NDC.x * 0.5 + 0.5;
screenPos.y = NDC.y * 0.5 + 0.5;
``````

Now the sun stays fixed in place.