Home Ask Login Register

Developers Planet

Your answer is one click away!

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 enter image description here

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

enter image description here

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

enter image description here


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.

Post Status

Asked in February 2016
Viewed 3,750 times
Voted 6
Answered 1 times


Leave an answer

Quote of the day: live life