OpenGL "Free Aspect Ratio" like in Unity

221 views Asked by At

I've made my own simple Level Editor in Visual Studio using OpenGL and C++ to create games for android phone. But I've stumbled across a problem where some objects that I have put in my editor do not appear correctly on android nor and sometimes they don't even appear. I have been searching around and could not find a solution. So here is my lLevel Editor's orthographic camera and resolution of the screen:

Camera objects Ortho Matrix:

GoldState::aspectRatio = m_AspectRatio = (float)width / (float)height;
    float orthoLeft = 0.0f; //* m_AspectRatio;
    float orthoRight = 100.0f; //* m_AspectRatio;
    float orthoUp = 100.0f;
    float orthoDown = 0.0f;
    m_OrthoMatrix = glm::ortho(orthoLeft, orthoRight, orthoDown, orthoUp);

And its exactly the same for android too(using ndk).

The aspect ratios are commented out because I was experimenting and without multiplying by aspect ratio entire world will get stretched.

Now to unity's Free Aspect Ratio which I have a feeling is just like my ortho camera without multiplying aspect ratio. What exactly is that and how does every object that I place on unity when I select Free Aspect Ratio stays 1:1 and it is not stretched or anything? If someone can explain this to me that would really help me a lot.

Just one more thing. I have made several games on android using opengl but I specificly made background image to be exact width and height because I was multiplying aspect ratio in my ortho matrix all the time. But this time I just want to extract background and objects positions and sizes and have them keep their aspect without stretching on different resolutions.

If there is any other general solution for this that would be nice if there is not I guess I should use some fixed resolution on level editor and use that same resolution for android?

1

There are 1 answers

0
PunkSnips On

It should do the trick:

GoldState::aspectRatio = m_AspectRatio = (float)width / (float)height;
GoldState::orthoSize = m_OrthoSize = ((float)width / (float)height < 16f / 9f ? (float)width / (float)height : 16f / 9f) / (16f / 9f);
float srcAspectRatio = 1f / 1f; //* Original aspect, default: 1.0;
float orthoLeft = 0.0f;
float orthoRight = 100.0f;
float orthoUp = 100.0f;
float orthoDown = 0.0f;
m_OrthoMatrix = glm::ortho(((0.5f*(orthoright - orthoLeft) + ortholeft) * srcAspectRatio) - (0.5f*(orthoright - orthoLeft) * m_AspectRatio * m_OrthoSize), ((0.5f*(orthoright - orthoLeft) + ortholeft) * srcAspectRatio) + (0.5f*(orthoright - orthoLeft) * m_AspectRatio * m_OrthoSize),(0.5f*(orthoDown - orthoUp) + orthoUp) - (0.5f*(orthoDown - orthoUp) * m_OrthoSize),(0.5f*(orthoDown - orthoUp) + orthoUp) + (0.5f*(orthoDown - orthoUp) * m_OrthoSize));