ShapeRenderer is not doing job well...

Any community contributions to libgdx go here! Some may get included in the core API when permission is granted.

ShapeRenderer is not doing job well...

Postby Kerai » Sat Dec 17, 2011 3:56 am

So... I found something wrong about it...
I wanted to draw some transparent shapes for debugging and I spotted this:
Code: Select all
      Gdx.gl11.glEnable(GL10.GL_BLEND);
      Gdx.gl11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
      renderer.begin(ShapeRenderer.ShapeType.FilledCircle);
      renderer.setColor(1, 0, 0, 0.4f);
      renderer.filledCircle(0, 0, 100, 30);
      renderer.end();

Image
Yeah... it draws one additional triangle where there was one already, so they overlap ;)
A little fix for ShapeRenderer code here:
Code: Select all
      int inc = 360 / segments;
      float angle = 2 * 3.1415926f / segments;
      float cos = MathUtils.cos(angle);
      float sin = MathUtils.sin(angle);
      float cx = radius, cy = 0;
      for (int i = 0; i < segments-1; i++) {
         renderer.color(color.r, color.g, color.b, color.a);
         renderer.vertex(x, y, 0);
         renderer.color(color.r, color.g, color.b, color.a);
         renderer.vertex(x + cx, y + cy, 0);
         float temp = cx;
         cx = cos * cx - sin * cy;
         cy = sin * temp + cos * cy;
         renderer.color(color.r, color.g, color.b, color.a);
         renderer.vertex(x + cx, y + cy, 0);
      }
      //drawing last segment
      renderer.color(color.r, color.g, color.b, color.a);
      renderer.vertex(x, y, 0);
      renderer.color(color.r, color.g, color.b, color.a);
      renderer.vertex(x + cx, y + cy, 0);
      renderer.color(color.r, color.g, color.b, color.a);
      renderer.vertex(x + radius, y, 0);

I changed "<=segments" to "<segments-1" and I draw last segment out of loop, so the last vertice c is the same as b in first triangle

Also, shaperenderer doesnt let you draw polygond/triangles...
It should!
If you let me draw at least triangles, then I can draw polygons pretty easily.
So I suggest adding Triangle and FilledTriagle ShapeType ;)

Some code, so you can just copy&paste and dont have to waste time writing it:
Code: Select all
   public void triangle(float x1, float y1, float x2, float y2, float x3, float y3)
   {
      if (currType != ShapeType.Triangle) throw new GdxRuntimeException("Must call begin(ShapeType.Triangle)");
      checkDirty();
      checkFlush(6);
      renderer.color(color.r, color.g, color.b, color.a);
      renderer.vertex(x1, y1, 0);
      renderer.color(color.r, color.g, color.b, color.a);
      renderer.vertex(x2, y2, 0);

      renderer.color(color.r, color.g, color.b, color.a);
      renderer.vertex(x2, y2, 0);
      renderer.color(color.r, color.g, color.b, color.a);
      renderer.vertex(x3, y3, 0);

      renderer.color(color.r, color.g, color.b, color.a);
      renderer.vertex(x3, y3, 0);
      renderer.color(color.r, color.g, color.b, color.a);
      renderer.vertex(x1, y1, 0);
   }
   

   public void filledTriangle(float x1, float y1, float x2, float y2, float x3, float y3)
   {
      if (currType != ShapeType.FilledTriangle) throw new GdxRuntimeException("Must call begin(ShapeType.FilledTriangle)");
      checkDirty();
      checkFlush(3);
      renderer.color(color.r, color.g, color.b, color.a);
      renderer.vertex(x1, y1, 0);
      renderer.color(color.r, color.g, color.b, color.a);
      renderer.vertex(x2, y2, 0);
      renderer.color(color.r, color.g, color.b, color.a);
      renderer.vertex(x3, y3, 0);
   }
Kerai
 
Posts: 63
Joined: Thu Dec 08, 2011 12:51 pm

Re: ShapeRenderer is not doing job well...

Postby NateS » Sat Dec 17, 2011 5:13 pm

Added your stuff, thanks! <= segments should be < segments though. :)
NateS
 
Posts: 1980
Joined: Fri Nov 12, 2010 11:08 am

Re: ShapeRenderer is not doing job well...

Postby Kerai » Sun Dec 18, 2011 6:34 am

I just realized that Triangle is not necessary, as lines are just enough to draw polygons, while outlined triangles make it look weird... only FilledTriangle makes sense.

Nates wrote:Added your stuff, thanks!

Now, could you, in return, answer my question there? :roll:
viewtopic.php?p=14385#p14385
Kerai
 
Posts: 63
Joined: Thu Dec 08, 2011 12:51 pm

Re: ShapeRenderer is not doing job well...

Postby NateS » Sun Dec 18, 2011 4:20 pm

By that logic box isn't needed either, but it is convenient. Might as well leave triangle.
NateS
 
Posts: 1980
Joined: Fri Nov 12, 2010 11:08 am


Return to Libgdx Contributions

Who is online

Users browsing this forum: No registered users and 0 guests