Problem Drawing with OpenGL

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

Problem Drawing with OpenGL

Postby trowgundam » Sun May 30, 2010 2:54 am

I am trying to create a game for Android. I have a large number of sprites on the screen, and while using a Canvas to do all my drawing with a custom view, I would like to get some smoother animation in the game. Also I plan to make a 3D version of the game and this will make converting the game much easier. I have tried 2 different methods for drawing my sprites on the screen. The first is trying to draw a quad with a orthogonal projection matrix. However it just doesn't seem to work. I have tried it on my G1 and in the emulator. Below is the code for my Renderer that I have attached to my GLSurfaceView.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.testing.GLSpriteTest;
  2.  
  3. import java.nio.ByteBuffer;
  4. import java.nio.ByteOrder;
  5. import java.nio.FloatBuffer;
  6. import java.nio.IntBuffer;
  7.  
  8. import javax.microedition.khronos.egl.EGLConfig;
  9. import javax.microedition.khronos.opengles.GL10;
  10. import javax.microedition.khronos.opengles.GL11Ext;
  11.  
  12. import android.content.Context;
  13. import android.graphics.Bitmap;
  14. import android.graphics.BitmapFactory;
  15. import android.opengl.GLU;
  16. import android.opengl.GLUtils;
  17. import android.opengl.GLSurfaceView.Renderer;
  18. import android.util.Log;
  19.  
  20. public class SpriteRenderer implements Renderer {
  21.      private static FloatBuffer spriteVertices;
  22.      private static IntBuffer texturesBuffer;
  23.        
  24.      Context context;
  25.      private int h, w;
  26.        
  27.      /* (non-Javadoc)
  28.      * @see android.opengl.GLSurfaceView.Renderer#onDrawFrame(javax.microedition.khronos.opengles.GL10)
  29.      */
  30.      public void onDrawFrame(GL10 gl) {
  31.           gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
  32.                
  33.           // Setup the MODELVIEW matrix
  34.           gl.glMatrixMode(GL10.GL_MODELVIEW);
  35.           gl.glLoadIdentity();
  36.           gl.glTranslatef(w/2, h/2, 0);
  37.  
  38.           // Draw Square
  39.           gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
  40.           gl.glVertexPointer(2, GL10.GL_FLOAT, 0, spriteVertices);
  41.           gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
  42.           gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
  43.      }
  44.  
  45.      /* (non-Javadoc)
  46.      * @see android.opengl.GLSurfaceView.Renderer#onSurfaceChanged(javax.microedition.khronos.opengles.GL10, int, int)
  47.      */
  48.      public void onSurfaceChanged(GL10 gl, int width, int height) {
  49.           // avoid division by zero
  50.           if (height == 0) height = 1;
  51.           // draw on the entire screen
  52.           gl.glViewport(0, 0, width, height);
  53.           // setup projection matrix
  54.           gl.glMatrixMode(GL10.GL_PROJECTION);
  55.           gl.glLoadIdentity();
  56.           Log.d("SpriteRenderer", width + "x" + height);
  57.           GLU.gluOrtho2D(gl, 0, width, height, 0);
  58.        
  59.           float qWidth = width/8;
  60.        
  61.           float[] quadCoords = new float[] {
  62.                  -qWidth, -qWidth,
  63.                   qWidth, -qWidth,
  64.                  -qWidth,  qWidth,
  65.                   qWidth,  qWidth,
  66.           };
  67.        
  68.           ByteBuffer bb = ByteBuffer.allocateDirect(quadCoords.length*4);
  69.           bb.order(ByteOrder.nativeOrder());
  70.           spriteVertices = bb.asFloatBuffer();
  71.           spriteVertices.put(quadCoords);
  72.                
  73.           w = width;
  74.           h = height;
  75.      }
  76.  
  77.      /* (non-Javadoc)
  78.       * @see android.opengl.GLSurfaceView.Renderer#onSurfaceCreated(javax.microedition.khronos.opengles.GL10, javax.microedition.khronos.egl.EGLConfig)
  79.       */
  80.      @Override
  81.      public void onSurfaceCreated(GL10 gl, EGLConfig config) {
  82.           gl.glClearColor(0, 0, 0, 0);
  83.        
  84.           gl.glDisable(GL10.GL_LIGHTING);
  85.           gl.glDisable(GL10.GL_DEPTH_TEST);
  86.           gl.glDisable(GL10.GL_CULL_FACE);
  87.        
  88.           gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
  89.      }
  90. }
  91.  
Parsed in 0.040 seconds, using GeSHi 1.0.8.4


When that didn't work I looked around a bit and decided to use the draw texture functions in GL11Ext. And that still didn't work. Here is my code for that:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.testing.GLSpriteTest;
  2.  
  3. import java.nio.ByteBuffer;
  4. import java.nio.ByteOrder;
  5. import java.nio.FloatBuffer;
  6. import java.nio.IntBuffer;
  7.  
  8. import javax.microedition.khronos.egl.EGLConfig;
  9. import javax.microedition.khronos.opengles.GL10;
  10. import javax.microedition.khronos.opengles.GL11Ext;
  11.  
  12. import android.content.Context;
  13. import android.graphics.Bitmap;
  14. import android.graphics.BitmapFactory;
  15. import android.opengl.GLU;
  16. import android.opengl.GLUtils;
  17. import android.opengl.GLSurfaceView.Renderer;
  18. import android.util.Log;
  19.  
  20. public class SpriteRenderer implements Renderer {
  21.         private static IntBuffer texturesBuffer;
  22.        
  23.         Context context;
  24.        
  25.         public SpriteRenderer(Context c)
  26.         {
  27.                 context = c;
  28.         }
  29.        
  30.         /* (non-Javadoc)
  31.          * @see android.opengl.GLSurfaceView.Renderer#onDrawFrame(javax.microedition.khronos.opengles.GL10)
  32.          */
  33.         public void onDrawFrame(GL10 gl) {
  34.                 gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
  35.  
  36.                 gl.glBindTexture(GL10.GL_TEXTURE_2D, texturesBuffer.get(0));
  37.                 ((GL11Ext) gl).glDrawTexiOES(1, 1, 0, 128, 128);
  38.         }
  39.  
  40.         /* (non-Javadoc)
  41.          * @see android.opengl.GLSurfaceView.Renderer#onSurfaceChanged(javax.microedition.khronos.opengles.GL10, int, int)
  42.          */
  43.         public void onSurfaceChanged(GL10 gl, int width, int height) {
  44.                 // avoid division by zero
  45.                 if (height == 0) height = 1;
  46.                 // draw on the entire screen
  47.                 gl.glViewport(0, 0, width, height);
  48.                 // setup projection matrix
  49.                 gl.glMatrixMode(GL10.GL_PROJECTION);
  50.                 gl.glLoadIdentity();
  51.                 Log.d("SpriteRenderer", width + "x" + height);
  52.                 GLU.gluOrtho2D(gl, 0, width, height, 0);
  53.         }
  54.  
  55.         /* (non-Javadoc)
  56.          * @see android.opengl.GLSurfaceView.Renderer#onSurfaceCreated(javax.microedition.khronos.opengles.GL10, javax.microedition.khronos.egl.EGLConfig)
  57.          */
  58.         @Override
  59.         public void onSurfaceCreated(GL10 gl, EGLConfig config) {
  60.                 gl.glClearColor(0, 0, 0, 0);
  61.        
  62.                 gl.glDisable(GL10.GL_LIGHTING);
  63.                 gl.glDisable(GL10.GL_DEPTH_TEST);
  64.                 gl.glDisable(GL10.GL_CULL_FACE);
  65.        
  66.                 // create texture
  67.                 gl.glEnable(GL10.GL_TEXTURE_2D);
  68.                 texturesBuffer = IntBuffer.allocate(1);
  69.                 gl.glGenTextures(1, texturesBuffer);
  70.                 gl.glBindTexture(GL10.GL_TEXTURE_2D, texturesBuffer.get(0));
  71.        
  72.                 // setup texture parameters
  73.                 gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
  74.                 gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
  75.                 gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE);
  76.                 gl.glTexParameterx(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE);
  77.        
  78.                 // set the texture
  79.                 Bitmap texture = BitmapFactory.decodeResource(context.getResources(), R.drawable.sphere);
  80.                 GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, texture, 0);
  81.                 texture.recycle();
  82.        
  83.                 gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
  84.         }
  85. }
  86.  
Parsed in 0.040 seconds, using GeSHi 1.0.8.4


So if someone could take a look at my code and tell me what I am missing that would be great. I've been trying to get this to work almost all day and I am done with it. I'm about to just take the slightly jerky animation in my Canvas, but I figured I'd give here a shot before I gave up for good.
trowgundam
Freshman
Freshman
 
Posts: 4
Joined: Wed Nov 05, 2008 12:32 pm
Location: Arizona

Top

Re: Problem Drawing with OpenGL

Postby IronChef253 » Sun Jun 13, 2010 7:02 am

When you say "just didn't work" do you mean the textures arent being drawn or do you mean that the textures are being drawn but they are still too slow?

*edit*

I did a little research. I recently watched a video on this topic by Chris Pruett and he posted the source code to a test he did to compare the rendering speeds of different android sprite animation methods.

http://code.google.com/p/apps-for-andro ... owse/trunk

If you follow that link, look at the project called "sprite method test." it contains a renderer for open gl very similar tom yours...except it probably works(haven't been near my laptop so I can't verify). I believe the class is com.android.spritemethodtest.SimpleGLRenderer.

My best guess for your solution is go download and try that project and see what he is doing there...
IronChef253
Junior Developer
Junior Developer
 
Posts: 11
Joined: Sun Jun 13, 2010 6:51 am

Top

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

Who is online

Users browsing this forum: Google Feedfetcher and 3 guests