Never get full 480*800

Tutorials concerning the OpenGL® ES cross-platform API for full-function 2D and 3D graphics on the Google-Android platform.

Never get full 480*800

Postby luwen105 » Wed Feb 03, 2010 9:24 am

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:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. @Override
  3.  
  4.         public void sizeChanged(GL10 gl, int width, int height) {
  5.  
  6.                 /*
  7.  
  8.                  * Set our projection matrix. This doesn't have to be done
  9.  
  10.                  * each time we draw, but usually a new projection needs to
  11.  
  12.                  * be set when the viewport is resized.
  13.  
  14.                  */              
  15.  
  16.                 gl.glViewport( 0, 0, width, height);
  17.  
  18.                
  19.  
  20.                 gl.glMatrixMode(GL10.GL_PROJECTION);
  21.  
  22.                 gl.glLoadIdentity();           
  23.  
  24.                 GLU.gluOrtho2D(gl, 0, width, 0, height);                                       
  25.  
  26.         }
  27.  
  28.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


The coordinates for the triangle are:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. float[] coords = {
  3.  
  4.                 // X, Y, Z
  5.  
  6.                 0.0f, 0.0f, 0,
  7.  
  8.                 200.0f, 200.0f, 0,
  9.  
  10.                 100.0f, 0.0f, 0,
  11.  
  12.         };
  13.  
  14.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


And I get the result in the second attachment. The result is same on emulator 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 with the same setup and draw the exact same triangle. I get the result in the first attachment, which is the expected result. Anyone can help me figure out how to get this same result on the phone?
Attachments
glut_on_win.png
glut_on_win.png (35.56 KiB) Viewed 1591 times
device.png
device.png (5.56 KiB) Viewed 1595 times
luwen105
Freshman
Freshman
 
Posts: 3
Joined: Wed Feb 03, 2010 8:55 am

Top

Postby MichaelEGR » Wed Feb 03, 2010 3:58 pm

just a head check here, but you do have this in your AndroidManifest.xml to turn off automatic compatibility mode right?

<supports-screens
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:anyDensity="true"/>
Founder & Principal Architect; EGR Software LLC
http://www.typhonrt.org
http://www.egrsoftware.com
User avatar
MichaelEGR
Senior Developer
Senior Developer
 
Posts: 147
Joined: Thu Jan 21, 2010 5:30 am
Location: San Francisco, CA

Postby luwen105 » Wed Feb 03, 2010 7:52 pm

Yes, I have already put that 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.
luwen105
Freshman
Freshman
 
Posts: 3
Joined: Wed Feb 03, 2010 8:55 am

Postby MichaelEGR » Thu Feb 04, 2010 4:53 am

...another head check... Can you print out the width & height parameters in sizeChanged method and verify that they are what you think they are.

Also there is the classic GL mistake of not having the right matrix mode selected when doing the projection setup (in your case ortho projection) or forgetting to switch the matrix mode after the ortho call to GL_MODELVIEW can subtely mess things up even though you seemingly get "reasonable" results.

So try dropping in this after the GLU ortho method. You can likely omit the glPushMatrix I do that cause I push / pop into and out of ortho mode from a 3D projection state:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.       gl.glMatrixMode(GL_MODELVIEW);                                       // Select Modelview Matrix
  3.  
  4. //      gl.glPushMatrix();                                                               // Push The Matrix
  5.  
  6.       gl.glLoadIdentity();                                                          // Reset The Matrix
  7.  
  8.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4



If that doesn't work try this method instead of the GLU ortho method:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. gl.glOrthof( 0, width, height, 0, -1, 1 );              // Select Ortho Mode
  3.  
  4.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


Also here is the sequence I use to enter ortho mode (again pushing the projection/modelview matrix so that I can pop back off to whatever 3D projection was set before):

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.       gl.glMatrixMode(GL_PROJECTION);                                      // Select Projection
  3.  
  4.       gl.glPushMatrix();                                                                 // Push The Matrix
  5.  
  6.       gl.glLoadIdentity();                                                          // Reset The Matrix
  7.  
  8.       gl.glOrthof( 0, width, height, 0, -1, 1 );                // Select Ortho Mode
  9.  
  10.       gl.glMatrixMode(GL_MODELVIEW);                                       // Select Modelview Matrix
  11.  
  12.       gl.glPushMatrix();                                                                 // Push The Matrix
  13.  
  14.       gl.glLoadIdentity();                                                          // Reset The Matrix
  15.  
  16.  
Parsed in 0.040 seconds, using GeSHi 1.0.8.4


------------------------------

This should give a few things to check/verify before continuing.
Founder & Principal Architect; EGR Software LLC
http://www.typhonrt.org
http://www.egrsoftware.com
User avatar
MichaelEGR
Senior Developer
Senior Developer
 
Posts: 147
Joined: Thu Jan 21, 2010 5:30 am
Location: San Francisco, CA

Postby luwen105 » Fri Feb 05, 2010 5:12 am

Thanks MichaelEGR for the 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-andro ... derer.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.
luwen105
Freshman
Freshman
 
Posts: 3
Joined: Wed Feb 03, 2010 8:55 am

Top

Return to Android 2D/3D Graphics - OpenGL Tutorials

Who is online

Users browsing this forum: No registered users and 2 guests