8

I am attempting to replace the Canvas-based rendering system that I already have with the faster opengl-es surface, however, I can't seem to get an openGL renderer to conform in such a way that it acts as 2d field, rather than a perspective view.

My current code for the renderer looks as follows:

     @Override
     public void onSurfaceChanged(GL10 gl, int width, int height) {
        gl.glViewport(0, 0, width, height);
        gl.glMatrixMode(GL10.GL_PROJECTION);
        gl.glLoadIdentity();
        gl.glOrthof(0.0f, width, 0.0f, height, 0.0f, 1.0f);

        gl.glShadeModel(GL10.GL_FLAT);
        gl.glEnable(GL10.GL_BLEND);
        gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
        gl.glColor4x(0x10000, 0x10000, 0x10000, 0x10000);
        gl.glEnable(GL10.GL_TEXTURE_2D);
    }

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);

        gl.glClearColor(0.5f, 0.5f, 0.5f, 1);
        gl.glShadeModel(GL10.GL_FLAT);
        gl.glDisable(GL10.GL_DEPTH_TEST);
        gl.glEnable(GL10.GL_TEXTURE_2D);

        gl.glDisable(GL10.GL_DITHER);
        gl.glDisable(GL10.GL_LIGHTING);

        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);    
    }

How would I setup the renderer so that the translate transformation would match up with the pixels on the screen? (so translating 5 to the right would move it 5 pixels)

genpfault
  • 51,148
  • 11
  • 85
  • 139
Kleptine
  • 5,089
  • 16
  • 58
  • 81
  • Why not just use pass a GL instance to the Canvas constructor. http://developer.android.com/reference/android/graphics/Canvas.html – James Black Aug 30 '10 at 01:32
  • Would that retain openGL's speed benefits over the basic Canvas in drawing Bitmaps? – Kleptine Aug 30 '10 at 01:45
  • Don't use Canvas for it. It doesn't work even though its documented. Also, according to talks in IRC, The developer of Canvas has already removed it from the API for future versions. – Moncader Sep 08 '10 at 09:03
  • Are they really depreciating it? That would certainly put my app out of the running. :/ – Kleptine Sep 15 '10 at 23:05

1 Answers1

9

Please note that in openGL that Y co-ordinate is inverted. Otherwise all is the same.
As for the correct flags, I recommend you check out the open source android game replica island: http://code.google.com/p/replicaisland/

Here's what I use in my own code:

public void onSurfaceChanged(GL10 gl, int width, int height) 
{
    mViewWidth = width;
    mViewHeight = height;

    gl.glViewport(0, 0, mViewWidth,  mViewHeight);
    gl.glLoadIdentity();
    GLU.gluOrtho2D(gl, 0, mViewWidth, mViewHeight, 0);
}

public void onSurfaceCreated(GL10 gl, EGLConfig config) 
{
    gl.glClearColor(0.5f, 0.5f, 0.5f, 1);
    gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
    gl.glShadeModel(GL10.GL_FLAT);
    gl.glDisable(GL10.GL_DEPTH_TEST);
    gl.glEnable(GL10.GL_BLEND);
    gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA); 

    gl.glViewport(0, 0, mViewWidth,  mViewHeight);
    gl.glMatrixMode(GL10.GL_PROJECTION);
    gl.glLoadIdentity();
    gl.glEnable(GL10.GL_BLEND);
    gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
    gl.glShadeModel(GL10.GL_FLAT);
    gl.glEnable(GL10.GL_TEXTURE_2D);

    GLU.gluOrtho2D(gl, 0, mViewWidth, mViewHeight, 0);
}

Where mViewWidth & mViewHeight are the size of the display.

Maxim Shoustin
  • 77,483
  • 27
  • 203
  • 225
Miguel Morales
  • 1,707
  • 10
  • 10
  • I have, however the code is so thick with optimizations and tile-based systems, that it's pretty much useless to find the simple code I need for rendering a few textures to the screen. – Kleptine Sep 03 '10 at 22:00
  • I had a similiar issue and had no problem finding the part where the author of replica islands setup the gl canvas. It's in the file called GameRenderer.java. I've edited my original response to include the setup code I use. – Miguel Morales Sep 03 '10 at 22:25
  • Alright, thanks. Though it's still not quite working out in terms of transitioning my overall code structure. Is there any way to store textures in non-base-2 sizes? If there isn't it's not really worth the trouble of using openGL anyways (I'm pushing the memory limits of Android already). – Kleptine Sep 15 '10 at 23:02
  • Not sure about the non-base-2. Don't use those myself, but I *believe* it is supported in OpenGL ES 2.0 which only Android 2.0 supports. There might be other tricks to make them work, perhaps you can post a new question. Good luck. – Miguel Morales Sep 16 '10 at 20:40