I’m using OpenGL for Android to draw my 2D images.
Whenever I draw something using the code:
gl.glViewport(aspectRatioOffset, 0, screenWidth, screenHeight);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
GLU.gluOrtho2D(gl, aspectRatioOffset, screenWidth + aspectRatioOffset,screenHeight, 0);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glBindTexture(GL10.GL_TEXTURE_2D, myScene.neededGraphics.get(ID).get(animationID).get(animationIndex));
crop[0] = 0;
crop[1] = 0;
crop[2] = width;
crop[3] = height;
((GL11Ext)gl).glDrawTexfOES(x, y, z, width, height)
I get an upside down result. I’v seen people solve this through doing:
crop[0] = 0;
crop[1] = height;
crop[2] = width;
crop[3] = -height;
This does however hurt the logic in my application, so I would like the result to not be flipped upside down. Does anyone know why it happen, and any way of avoiding or solving it?
Edit: I found a solutions, though I don’t know if it is a good one:
int[] crop = new int[4];
crop[0] = 0;
crop[1] = imageDimension[ID][1];
crop[2] = imageDimension[ID][0];
crop[3] = -imageDimension[ID][1];
((GL11)gl).glTexParameteriv(GL10.GL_TEXTURE_2D, GL11Ext.GL_TEXTURE_CROP_RECT_OES, crop, 0);
((GL11Ext)gl).glDrawTexfOES(x, ScreenHeight - y - height, 0, width, height);
Define upside down. OpenGL defines (0, 0) to be in the lower left of the display with y going upward.
glDrawTexOESis explicitly defined to work in window coordinates so there’s no matrix stack in between even in ES 1.0. If you’ve set up your projection or modelview matrices to flip coordinates to whatever OpenGL considers upside down then that’s not going to have any effect during a call toglDrawTexOES.What generally happens is that people implicitly flip their graphics when loading them (because they ignore OpenGL’s placement of the origin), causing them to appear upside down when using
glDrawTexOES. The correct solution is that if you don’t want to have to flip coordinates manually later on then don’t implicitly flip them when loading your images and/or when setting up your matrix stacks.