I'm trying to set up my GLSurfaceView to use traditional 2D coordinate system. Basically, I want to put my first tile at 0x0 which would be at the top left. However, when I draw my texture at 0x0 it draws it at the bottom left.
I want it to start at 0x0 and go down as the Y co-ordinate increases.
Here's how I'm initializing my renderer:
gl.glClearColor(0f, 0f, 0f, 1);
gl.glViewport(0, 0, mViewWidth,mViewHeight);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
GLU.gluOrtho2D(gl, 0, mViewWidth, mViewHeight, 0);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
gl.glEnable(GL10.GL_BLEND);
gl.glDisable(GL10.GL_DEPTH_TEST);
gl.glBlendFunc(G开发者_如何学CL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
gl.glShadeModel(GL10.GL_FLAT);
gl.glEnable(GL10.GL_TEXTURE_2D);
And I draw my texture using:
gl.glBindTexture(GL10.GL_TEXTURE_2D, mytexture);
((GL11Ext) gl).glDrawTexfOES(0, 32, 0, tile_width, tile_height);
Any help would be appreciated, thanks!
It draws at the bottom left, because that's where OpenGL defines the viewport origin to be. To overcome this you must mirror the Y axis in the projection. Easy enough, just swap the bottom and top parameters of glOrtho.
GL.glOrtho(gl, 0, mViewWidth, mViewHeight, 0, -1, 1); // using GL. instead of GLU
I was able to solve this by @datenwolf's comment.
The basic initialization was correct, however using glDrawTexOES ignores the ortho camera. To avoid using glDrawTex I loaded the textures as usual but used a vertex buffer to draw them.
I used the SpriteMethodTest android project which contains code on how to load them properly. I simply had to play with the camera and everything works ok now.
精彩评论