Can I draw 2Dshapes (lines,rect. etc) with open GL.

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

Can I draw 2Dshapes (lines,rect. etc) with open GL.

Postby qamerfarooq » Fri Jan 25, 2008 11:21 am

Hi,

Can I draw different 2D shapes(lines,rectangles etc) with openGL API provided by the Android, in case YES then please provide some example and tell the level of difficulty.

If we cannot draw 2D shapes(lines,rectangles etc) with openGL API, then what other APIs are provided by Andoird.
Please list all available options for 2D shapes drawing.

Would be easy for me, If package names for the suggested API are also mentioned.
Qamer Farooq
End 2 End Solution Providers
http://www.e2esp.com
qamerfarooq
Junior Developer
Junior Developer
 
Posts: 13
Joined: Fri Jan 11, 2008 6:07 am
Location: Pakistan

Top

Postby plusminus » Fri Jan 25, 2008 3:23 pm

Hello gamerfarooq,

you could have a look at the Source of :src: Monolith Android , somewhere in teacake's code you'll find the solution.

Would be too kind if you'd finally share your solution.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby shredder » Fri Apr 11, 2008 10:45 am

Hi,

I am afraid you can only draw lines ( and from lines you can draw almost shape :lol: ).

Try glDrawElements with GL_LINES instead of TRIANGLES.

If you want to make the line "thick", then it is not line, but "beam". In that case, you'll have to implement a "beam" drawing by yourself. I remember reading about "beam" in Game Programming Gems Volume 1 ( or 2 ).

About filled shapes, I am afraid you'll have to build the shape by triangles.
shredder
Junior Developer
Junior Developer
 
Posts: 12
Joined: Mon Mar 03, 2008 2:34 pm

Draw Thick line with TRIANGLE_STRIP

Postby cg0601 » Wed Mar 17, 2010 9:27 pm

A bit late, but might be useful to others too:

One way to simulate a thick line is by creating a rectangle from 2 triangles and make then wider then high.
Here is some code that does exactly this:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.    
  2.  
  3.     public static final int ONE = 0x10000;
  4.  
  5.     private IntBuffer lineCoordsBuffer;
  6.  
  7.  
  8.  
  9.     private void initVertices()
  10.  
  11.     {
  12.  
  13.         int[] lineCoords = {
  14.  
  15.                 -ONE * 3, ONE/10, 0,
  16.  
  17.                 -ONE * 3, -ONE/10, 0,
  18.  
  19.                 ONE * 3, ONE/10, 0,
  20.  
  21.                 ONE * 3, -ONE/10, 0
  22.  
  23.         };
  24.  
  25.        
  26.  
  27.         lineCoordsBuffer = makeIntBuffer(lineCoords);
  28.  
  29.     }
  30.  
  31.  
  32.  
  33.     @Override
  34.  
  35.     public void onDrawFrame(GL10 gl)
  36.  
  37.     {
  38.  
  39.         gl.glEnable(GL10.GL_DITHER);
  40.  
  41.         gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
  42.  
  43.         gl.glMatrixMode(GL10.GL_MODELVIEW);
  44.  
  45.         gl.glLoadIdentity();
  46.  
  47.         drawBeam(gl);
  48.  
  49.     }
  50.  
  51.  
  52.  
  53.     private void drawBeam(GL10 gl)
  54.  
  55.     {
  56.  
  57.         gl.glColor4f(0.5f, 0.0f, 0.0f, 1.0f);
  58.  
  59.         gl.glVertexPointer(3, GL10.GL_FIXED, 0, lineCoordsBuffer);
  60.  
  61.  
  62.  
  63.         gl.glPushMatrix();
  64.  
  65.  
  66.  
  67.         gl.glTranslatef(0, 0, -20);
  68.  
  69.         gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
  70.  
  71.  
  72.  
  73.         gl.glPopMatrix();
  74.  
  75.     }
  76.  
  77.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4

The beam will be drawn in the current color, which in this case was set to darkish red.

If using integers scares you here is the same example, using floats.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.     private FloatBuffer lineCoordsFBuffer;
  2.  
  3.  
  4.  
  5.     private void initVertices()
  6.  
  7.     {
  8.  
  9.        
  10.  
  11.         float[] lineCoordsF =
  12.  
  13.         {
  14.  
  15.                 -3.0f, 4.1f, 0,
  16.  
  17.                 -3.0f, 3.9f, 0,
  18.  
  19.                 3, 0.1f, 0,
  20.  
  21.                 3, -0.1f, 0
  22.  
  23.         };
  24.  
  25.         lineCoordsFBuffer = makeFloatBuffer(lineCoordsF);
  26.  
  27.     }
  28.  
  29.  
  30.  
  31.     @Override
  32.  
  33.     public void onDrawFrame(GL10 gl)
  34.  
  35.     {
  36.  
  37.         gl.glEnable(GL10.GL_DITHER);
  38.  
  39.         gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
  40.  
  41.         gl.glMatrixMode(GL10.GL_MODELVIEW);
  42.  
  43.         gl.glLoadIdentity();
  44.  
  45.         drawBeam(gl);
  46.  
  47.     }
  48.  
  49.  
  50.  
  51.     private void drawBeam(GL10 gl)
  52.  
  53.     {
  54.  
  55.         gl.glColor4f(0.5f, 0.0f, 0.0f, 1.0f);
  56.  
  57.         gl.glVertexPointer(3, GL10.GL_FLOAT, 0, lineCoordsFBuffer);
  58.  
  59.  
  60.  
  61.         gl.glPushMatrix();
  62.  
  63.  
  64.  
  65.         gl.glTranslatef(0, 0, -20);
  66.  
  67.         gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
  68.  
  69.  
  70.  
  71.         gl.glPopMatrix();
  72.  
  73.     }
  74.  
  75.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


Helper code:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.     protected static FloatBuffer makeFloatBuffer(float[] arr)
  3.  
  4.     {
  5.  
  6.         ByteBuffer bb = ByteBuffer.allocateDirect(arr.length * 4);
  7.  
  8.         bb.order(ByteOrder.nativeOrder());
  9.  
  10.         FloatBuffer fb = bb.asFloatBuffer();
  11.  
  12.         fb.put(arr);
  13.  
  14.         fb.position(0);
  15.  
  16.         return fb;
  17.  
  18.     }
  19.  
  20.  
  21.  
  22.     protected static IntBuffer makeIntBuffer(int[] arr)
  23.  
  24.     {
  25.  
  26.         ByteBuffer vbb = ByteBuffer.allocateDirect(arr.length * 4);
  27.  
  28.         vbb.order(ByteOrder.nativeOrder());
  29.  
  30.         IntBuffer texBuffer = vbb.asIntBuffer();
  31.  
  32.         texBuffer.put(arr);
  33.  
  34.         texBuffer.position(0);
  35.  
  36.         return texBuffer;
  37.  
  38.     }
  39.  
  40.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4
cg0601
Junior Developer
Junior Developer
 
Posts: 16
Joined: Tue Aug 04, 2009 8:53 am

Postby Mike Nelson » Fri Apr 02, 2010 4:43 pm

wouldn't it not work at all using integers, because if you divide 1 by 10 you get 0 if your still using integers??
Mike Nelson
Freshman
Freshman
 
Posts: 9
Joined: Fri Apr 02, 2010 4:41 pm

Postby cg0601 » Sat Apr 03, 2010 7:59 am

Mike Nelson wrote:wouldn't it not work at all using integers, because if you divide 1 by 10 you get 0 if your still using integers??

It would work also using integers, but OpenGL integers in Android are not the standard 0, 1, 2, etc
For example 1 = 0x10000 and 0.1 would be 0x1000, so you have to do some conversion !
cg0601
Junior Developer
Junior Developer
 
Posts: 16
Joined: Tue Aug 04, 2009 8:53 am

Top

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

Who is online

Users browsing this forum: No registered users and 1 guest