Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Never got full 480*800 when using 2D orthogonal projection in opengl-es

I am doing a little experiment with OpenGL ES on Nexus One. Got a problem about the full screen resolution. It seems like I can never get the real full resolution of Nexus One, which is 480*800. I am using an orthogonal projection and just want to draw a simple triangle with identity model view matrix:

@Override
 public void sizeChanged(GL10 gl, int width, int height) {
      /*
       * Set our projection matrix. This doesn't have to be done
       * each time we draw, but usually a new projection needs to
       * be set when the viewport is resized.
       */         
      gl.glViewport( 0, 0, width, height);

      gl.glMatrixMode(GL10.GL_PROJECTION);
      gl.glLoadIdentity();          
      GLU.gluOrtho2D(gl, 0, width, 0, height);                         
 } 

The coordinates for the triangle are:

float[] coords = {
            // X, Y, Z
            0.0f, 0.0f, 0,
            200.0f, 200.0f, 0,
            100.0f, 0.0f, 0,
    }; 

And I get the result below:

alt text http://www.anddev.org/files/device_184.png

This result is same on emulator (480*800 res) and Nexus 1. Obviously I didn’t get the full resolution (because the top vertex of the triangle is already close to the right edge, if width is real 480, this should be on the left of the half width). Another strangeness is that in sizeChanged (I am using GLSurfaceView, this is the override method in the renderer required by GLSurfaceView), I always get width = 480 and height = 800 no matter whether I start the app in full screen mode. I was expecting that with app title bar and status bar, the size passed to sizeChanged should be smaller right? I mean if the size passed to this method isn’t the real size I am getting, then how can I setup a correct viewport?

I also did a quick implementation using GLUT on Windows with the same setup and draw the exact same triangle. I get the result below, which is the expected result. Anyone can help me figure out how to get this same result on the phone?

alt text http://www.anddev.org/files/glut_on_win_137.png

I have already put

<supports-screens
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:anyDensity="true"/>

tag in AndroidManifest.xml. In particular, I found that it seems like only android:anyDensity="true"|”false” matters, as this does change the rendered result. Others don’t change the result at all. Also my project is on SDK 1.6, which is recommended for developing apps that support larger phone screen.

The original question is also asked here: http://www.anddev.org/viewtopic.php?p=34832#34832

like image 305
Echo Lu Avatar asked Feb 03 '10 19:02

Echo Lu


1 Answers

Thanks for all your answers, they helped me a lot. I found myself extremely embarrassing to tell you the real cause of this problem, which I solved just now. The triangle class that I copied from one of the samples actually scale on the coordinates that you define in the position array by the factor of two! I just modified the coordinates in the array and I didn’t notice when they are loaded into the vertex buffer, they get scaled!

I am guilty… But let’s just be careful with something copied over as you trust it does something in the way you expected, but it doesn’t, this is the triangle class I copied the code from http://code.google.com/p/apps-for-android/source/browse/trunk/Samples/OpenGLES/Triangle/src/com/google/android/opengles/triangle/TriangleRenderer.java, and notice that on line number 215, the positions get scaled by 2! Really evil as a sample…

Also, when using GLSurfaceView, use android.opengl.GLSurfaceView instead of the GLSurfaceView.java in your sample folder sdk_root\platforms\android-1.1\samples\ApiDemos\src\com\example\android\apis\graphics. As the latter one is an early version and has some strange behaviors.

like image 134
Echo Lu Avatar answered Nov 15 '22 04:11

Echo Lu