In all tutorials I found about creating projection matrix based on viewport size all of them assumed that left bottom coordinates of viewport will be (0,0).
Now I want to draw to the different parts of the screen and for that purpose I want to switch viewports accordingly:
glViewport(0,0,windowWidth/2, windowHeight/2); //left bottom
glViewport(0,windowHeight/2,windowWidth/2, windowHeight/2);//left top
glViewport(windowWidth/2,0,windowWidth/2, windowHeight/2);//right bottom
glViewport(windowWidth/2, windowHeight/2,windowWidth/2, windowHeight/2);//right top
Now I have a problem with defining my projection matrix. Without having any (x,y) offest I was using this code for calculating my ortho projection matrix:
float m_fWindowSize = 15.0f;
if (m_WindowWidth > m_WindowHeight)
{
auto viewportAspectRatio = (float)m_WindowWidth / (float)m_WindowHeight;
m_ProjectionMatrix.m_fLeft = (-1.0f) * m_fWindowSize * viewportAspectRatio;
m_ProjectionMatrix.m_fRight = m_fWindowSize * viewportAspectRatio;
m_ProjectionMatrix.m_fBottom = (-1.0f)*m_fWindowSize;
m_ProjectionMatrix.m_fTop = m_fWindowSize;
m_ProjectionMatrix.m_fNear = -(10.0f)*m_fWindowSize;
m_ProjectionMatrix.m_fFar = (10.0f)*m_fWindowSize;
m_fMoveSpeed = static_cast<GLfloat>(m_fWindowSize * 2 / static_cast<float>(m_WindowHeight));
}
else
{
auto viewportAspectRatio = (float)m_WindowHeight / (float)m_WindowWidth;
m_ProjectionMatrix.m_fLeft = (-1.0f)*m_fWindowSize;
m_ProjectionMatrix.m_fRight = m_fWindowSize;
m_ProjectionMatrix.m_fBottom = (-1.0f)*m_fWindowSize * viewportAspectRatio;
m_ProjectionMatrix.m_fTop = m_fWindowSize * viewportAspectRatio;
m_ProjectionMatrix.m_fNear = -(10.0f)*m_fWindowSize;
m_ProjectionMatrix.m_fFar = (10.0f)*m_fWindowSize;
m_fMoveSpeed = static_cast<GLfloat>(m_fWindowSize * 2 / static_cast<float>(m_WindowWidth));
}
And this works fine UNTIL I will add any (x,y) offset to my viewport. The effect is following when using glViewport(0, m_WindowHeight/2, m_WindowWidth/2, m_WindowHeight/2)
:
And with glViewport(0, 0, m_WindowWidth/2, m_WindowHeight/2)
:
So how I can change projection matrix accordingly to the viewport offset?