I am writing my first 2D Android app using OpenGL. I write this on my Desire, so my screen coordinates should be from 0.0 to 799,479 in landscape mode. I am trying to get OpenGL to use this range in world coordinates.
The application, for example, still works, but I had to adjust the numbers so that something appeared on the screen, and I was disappointed by my inability to understand the relationship between the projection matrix, and texture rendering in this regard.
Setting the projection matrix:
gl.glViewport(0, 0, width, height); float ratio = (float) width / height; float size = .01f * (float) Math.tan(Math.toRadians(45.0) / 2); gl.glMatrixMode(GL10.GL_PROJECTION); gl.glLoadIdentity(); gl.glFrustumf(-size, size, -size / ratio, size / ratio, 0.01f, 100.0f);
I want to understand 0.01f and 100.0f here. What do I use to describe the 2D world 0,0 → 799,479 with z value of zero?
Also, I'm not sure what is “best” - using glFrustumF or GLU.gluOrtho2D. The latter has simpler parameters - only the size of the viewport, but I have not found this anywhere. (Some sites have a height and 0 vice versa, but that doesn't matter.) But shouldn't that be a natural choice for using OpenGL 2D? Do I have to say something somewhere to say OpenGL "I do it in 2D - please ignore the third dimension everywhere, in the interest of speed"?
Drawing my textures:
I draw material using 2 textured triangles. Relevant parts of my init (let me know if I need to edit my question in more detail):
gl.glMatrixMode(GL10.GL_MODELVIEW); gl.glLoadIdentity(); gl.glTranslatex(nXpos, nYpos, nZoomin); gl.glRotatef(nRotZ, 0, 0, 1); gl.glScalef((float)nScaleup,(float)nScaleup, 0.0f); ... ... gl.glVertexPointer(2, GL10.GL_FIXED, 0, mVertexBuffer); gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, mTextureBuffer); gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4); mVertexBuffer is an IntBuffer and contains: int vertices[] = { -1, -1, 1, -1, -1, 1, 1, 1 };
I do not intend to ultimately go to nZoomin - I did it this way, because that’s how I found the “magic numbers” necessary to see something! Currently, I need to use -1000, with smaller numbers resulting in smaller images. Do I think that there must be some way to have a value of zero for nZoomin when the projection matrix is set correctly?
My textures are currently 128x128 (but may have different sizes, possibly always square). I do not know when they will be displayed in real size. I would like to be able to pass a value of, say, 128 to nScaleup so that it is built to the actual size. Is this related to the projection matrix, or do I have two separate problems?