I’m getting some strange values for the width/height in the following code, resulting in either a stretched picture, or no picture at all.
public void onSurfaceChanged(GL10 gl, int w, int h) {
screenHeight = h; // actual height in pixels
screenWidth = w; // actual width in pixels
worldWidth = 20; // width of the projection (20 units)
worldHeight = (int) (worldWidth * (screenHeight / screenWidth));
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glViewport(0, 0, screenWidth, screenHeight); //new viewport
gl.glLoadIdentity();
GLU.gluOrtho2D(gl, 0, worldWidth, 0, worldHeight); set the 2D projection
So I logged the variables, these are from portrait mode:
screenHeight = 455
screenWidth = 320
worldHeight = 20
worldWidth = 20
while (worldWidth * (screenHeight / screenWidth) should give 20*455/320=28 for worldHeight.
It gets even stranger in landscape mode, where worldHeight suddenly equals 0.
What am I doing wrong?
Let me guess,
screenHeightandscreenWidthare bothints? In this case the division will be an integer division, resulting in a rounded/truncated integer and therefore being 0 if the ratio is <1. Cast at least one of the operands of the division to a floating point number to perform a real floating point division: