Using multiple viewports

Problems with Canvas, OpenGL, etc...

Using multiple viewports

Postby souza » Fri Aug 20, 2010 9:59 pm

Hi,

I'm trying to get two viewports setup on the screen of the Android Emulator. The FIRST VIEWPORT (orthographic projection) fills the emulators screen and displays a white, screen filling polygon. The SECOND VIEWPORT (perspective projection) is only a square inside the screen and should appear above the other viewport showing a cube. Now, when i have both viewports active I cannot see the white polygon but the smaller viewport with the cube. In case I have the second viewports code in block comments it again apears. I don't get where I have to modify the code that both viewport appear correctly on the emulator. Here are some code snippets:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public void onSurfaceCreated(GL10 gl, EGLConfig config) {
  2.                 // Set the background color to red ( rgba ).
  3.                 gl.glClearColor(1.0f, 0.0f, 0.0f, 0.5f);
  4.                 // Enable Smooth Shading, default not really needed.
  5.                 gl.glShadeModel(GL10.GL_SMOOTH);
  6.                 // Depth buffer setup.
  7.                 gl.glClearDepthf(1.0f);
  8.                 // Enables depth testing.
  9.                 gl.glEnable(GL10.GL_DEPTH_TEST);
  10.                 // The type of depth testing to do.
  11.                 gl.glDepthFunc(GL10.GL_LEQUAL);
  12.                 // Really nice perspective calculations.
  13.                 gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
  14. }
  15.  
  16. public void onDrawFrame(GL10 gl) {             
  17.                
  18.                 // Clears the screen and depth buffer.
  19.                 gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
  20.                 // Modelview Matrix selektieren
  21.                 gl.glMatrixMode(GL10.GL_MODELVIEW);
  22.                 // Replace the current Matrix with the identity matrix
  23.                 gl.glLoadIdentity();
  24.                 // Translates 6 units into the screen
  25.                 gl.glTranslatef(0, 0, -10);
  26.                
  27.                 // Draw RECTANGLE
  28.                 square.draw(gl);
  29.        
  30.                 // CUBE
  31.                 // Save the current matrix
  32.                 gl.glPushMatrix();
  33.                         gl.glTranslatef(0, 0, -5);
  34.                         // Draw the cube
  35.                         root.draw(gl);
  36.                 // Restore the last matrix
  37.                 gl.glPopMatrix();
  38. }
  39.  
  40. public void onSurfaceChanged(GL10 gl, int width, int height) {
  41.                
  42.                 for(int i = 0; i < 2; i++) {
  43.                        
  44.                         if(i==0) {
  45.                                 // Main Viewport
  46.                                 gl.glViewport(0, 0, width, height);
  47.                                 // Select the projection matrix
  48.                                 gl.glMatrixMode(GL10.GL_PROJECTION);
  49.                                 // Reset the projection matrix
  50.                                 gl.glLoadIdentity();
  51.                                 // Setup orthographic projection. Consider aspect ratio to avoid distortion.
  52.                                 if(width < height) {
  53.                                         gl.glOrthof(-1.0f, 1.0f, -1.0f * height / width, 1.0f * height / width, -2.0f, 10.0f);
  54.                                 } else {
  55.                                         gl.glOrthof(-1.0f * width / height, 1.0f * width / height, -1.0f, 1.0f, -2.0f, 10.0f);
  56.                                 }
  57.                                
  58.                                 GLU.gluLookAt(gl, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f);                              
  59.                         }
  60.                        
  61.                         if(i==1) {
  62.                                 // Sets the cube's viewport.
  63.                                 gl.glViewport(width/2, height/3, width/2, height/3);
  64.                                 // Select the projection matrix
  65.                                 gl.glMatrixMode(GL10.GL_PROJECTION);
  66.                                 // Reset the projection matrix
  67.                                 gl.glLoadIdentity();
  68.                                 // Calculate the aspect ratio of the window
  69.                                 GLU.gluPerspective(gl, 45.0f, (float) width / (float) height, -2.0f, 10.0f);
  70.                                
  71.                                 GLU.gluLookAt(gl, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f);
  72.                         }
  73.                         // Select the modelview matrix
  74.                         gl.glMatrixMode(GL10.GL_MODELVIEW);
  75.                         // Reset the modelview matrix
  76.                         gl.glLoadIdentity();
  77.                         // Clear Depth Buffer
  78.                         gl.glClear(GL10.GL_DEPTH_BUFFER_BIT);
  79.                 }
  80. }
  81.  
Parsed in 0.041 seconds, using GeSHi 1.0.8.4


That's how it looks like without the FIRST VIEWPORT:
Image
souza
Freshman
Freshman
 
Posts: 3
Joined: Fri Aug 06, 2010 3:14 pm

Top

Re: Using multiple viewports

Postby MichaelEGR » Sun Aug 22, 2010 3:18 pm

As things go I don't have time to look at your code, but just offer this word of advice.. Don't try this kind of thing in the emulator as you'll just run into problems if not with this particular use case then something you try soon enough just simply will not work in the emulator because it is of very poor quality for OpenGL ES support. It's a rule of thumb that you just got to try OpenGL ES efforts out on a real device. So move to a real device as soon as possible.
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

Re: Using multiple viewports

Postby souza » Sun Aug 22, 2010 3:51 pm

My problem is that I am working on a tablet PC with Windows 7 and didn't get a real android device from my university :S So I'm more or less forced to keep going with the emulator!
souza
Freshman
Freshman
 
Posts: 3
Joined: Fri Aug 06, 2010 3:14 pm

Top

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

Who is online

Users browsing this forum: No registered users and 3 guests