GlDrawElements and drawing a quad

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

GlDrawElements and drawing a quad

Postby Shaka » Sun Nov 08, 2009 5:18 pm

Hi!

I've been trying to draw a simple quad with OpenGL ES the entire day today without success. I'm using this for my 2D "engine" and want to draw it in orthographic projection. I've managed to do it with glDrawArrays but not with glDrawElements. Maybe some of you guys can see what I'm doing wrong.

Here's the code for my quad.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class OpenGLSprite {            
  2.  
  3.         public IntBuffer VertexBuffer;
  4.  
  5.         public IntBuffer ColorBuffer;
  6.  
  7.         public ByteBuffer IndexBuffer; 
  8.  
  9.        
  10.  
  11.         private int one = 0x10000;
  12.  
  13.  
  14.  
  15.     int vertices[] = {
  16.  
  17.             -one, -one, one,
  18.  
  19.             one, -one, one,
  20.  
  21.             -one, one, one,
  22.  
  23.             one, one, one
  24.  
  25.     };
  26.  
  27.  
  28.  
  29.     int colors[] = {
  30.  
  31.             one,    one,    one,  one,
  32.  
  33.             one,    one,    one,  one,
  34.  
  35.             one,    one,    one,  one,
  36.  
  37.             one,    one,    one,  one,      
  38.  
  39.     };
  40.  
  41.  
  42.  
  43.     byte indices[] = {
  44.  
  45.                 0, 1, 2, 1, 2, 3
  46.  
  47.     };
  48.  
  49.  
  50.  
  51.     public OpenGLSprite(){       
  52.  
  53.                 ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length*4);
  54.  
  55.                 vbb.order(ByteOrder.nativeOrder());
  56.  
  57.                 VertexBuffer = vbb.asIntBuffer();
  58.  
  59.                 VertexBuffer.put(vertices);
  60.  
  61.                 VertexBuffer.position(0);
  62.  
  63.                
  64.  
  65.                 ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length*4);
  66.  
  67.                 cbb.order(ByteOrder.nativeOrder());
  68.  
  69.                 ColorBuffer = cbb.asIntBuffer();
  70.  
  71.                 ColorBuffer.put(colors);
  72.  
  73.                 ColorBuffer.position(0);
  74.  
  75.                
  76.  
  77.                 IndexBuffer = ByteBuffer.allocateDirect(indices.length);
  78.  
  79.                 IndexBuffer.put(indices);
  80.  
  81.                 IndexBuffer.position(0);                       
  82.  
  83.     }
  84.  
  85. }
  86.  
  87.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


And here's the drawing code: oglSprite is a OpenGLSprite instance.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.                
  3.  
  4. gl.glClear(GL10.GL_COLOR_BUFFER_BIT|GL10.GL_DEPTH_BUFFER_BIT);
  5.  
  6. gl.glMatrixMode(GL10.GL_PROJECTION);
  7.  
  8. gl.glLoadIdentity();   
  9.  
  10. GLU.gluOrtho2D(gl, 0.0f, _width, 0.0f, _height);
  11.  
  12.                
  13.  
  14. gl.glMatrixMode(GL10.GL_MODELVIEW);        
  15.  
  16. gl.glLoadIdentity();
  17.  
  18. GLU.gluLookAt(gl, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
  19.  
  20. gl.glVertexPointer(3, GL10.GL_FIXED, 0, oglSprite.VertexBuffer);
  21.  
  22. gl.glColorPointer(4, GL10.GL_FIXED, 0, oglSprite.ColorBuffer);            
  23.  
  24. gl.glDrawElements(GL10.GL_TRIANGLES, 6, GL10.GL_UNSIGNED_BYTE, oglSprite.IndexBuffer);
  25.  
  26.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


I've initialized with this code below:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                 gl.glClearColor(0.0f, 0.0f, 1.0f, 0.0f);
  2.  
  3.         gl.glEnable(GL10.GL_TEXTURE_2D);
  4.  
  5.         gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
  6.  
  7.         gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
  8.  
  9.         gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);
Parsed in 0.034 seconds, using GeSHi 1.0.8.4



My brain is really hurting at this point so any suggestions are appreciated.

Regards/Per
Shaka
Developer
Developer
 
Posts: 44
Joined: Fri Aug 28, 2009 3:01 pm

Top

Postby Shaka » Fri Nov 13, 2009 10:26 am

Never mind, seems to be a device problem, need to investigate it further.
Shaka
Developer
Developer
 
Posts: 44
Joined: Fri Aug 28, 2009 3:01 pm

Top

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

Who is online

Users browsing this forum: No registered users and 3 guests