Advertisement

2D OrthoM Matrix Issue

Started by April 05, 2018 05:02 AM
2 comments, last by Psychopathetica 6 years, 10 months ago

Hey guys. Wow it's been super long since I been here.

 

Anyways, I'm having trouble with my 2D OrthoM matrix setup for phones / tablets. Basically I wan't my coordinates to start at the top left of the screen. I also want my polygons to remain squared regardless if you have it on portrait or landscape orientation. At the same time, if I translate the polygon to the middle of the screen, I want it to come to the middle regardless if I have it in portrait or landscape mode. So far I'm pretty close with this setup:


private float aspectRatio;

@Override
public void onSurfaceChanged(GL10 glUnused, int width, int height) {
    Log.d("Result", "onSurfacedChanged()");

    glViewport(0, 0, width, height);
    
    if (MainActivity.orientation == Configuration.ORIENTATION_PORTRAIT) {
        Log.d("Result", "onSurfacedChanged(PORTRAIT)");
        aspectRatio = ((float) height / (float) width);
        orthoM(projectionMatrix, 0, 0f, 1f, aspectRatio, 0f, -1f, 1f);
    }
    else{
        Log.d("Result", "onSurfacedChanged(LANDSCAPE)");
        aspectRatio = ((float) width / (float) height);
        orthoM(projectionMatrix, 0, 0f, aspectRatio, 1f, 0f, -1f, 1f);
    }
}

When I translate the polygon using TranslateM( ) however, goes to the middle in portrait mode but in landscape, it only moved partially to the right, as though portrait mode was on some of the left of the screen. The only time I can get the translation to match is if in Landscape I move the aspectRatio variable in OrthoM( ) from the right arguement to the bottom arguement, and make right be 1f. Works but now the polygon is stretched after doing this. Do I just simply multiply the aspectRatio to the translation values only when its in Landscape mode to fix this or is there a better way?


if (MainActivity.orientation == Configuration.ORIENTATION_PORTRAIT) {
    Matrix.translateM(modelMatrix, 0, 0.5f, 0.5f * aspectRatio, 0f);
}
else {
    Matrix.translateM(modelMatrix, 0, 0.5f * aspectRatio, 0.5f, 0f);
}

Thanks in advance.

 

Cant you just use vertex shader to swap y coord and stop dealing with 'orthomatrix useless purpose'

Advertisement

I don't think there is a point considering that it's doing what I want. Anyways I fixed it. Had a flaw in my code that was stretching the image when in landscape mode.

This topic is closed to new replies.

Advertisement