nakayubi / zengl

Automatically exported from code.google.com/p/zengl
0 stars 0 forks source link

Feature request: add pr2d_Polygon #56

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
This is not a bug but a feature request. Would it be possible to add a polygon 
drawing routine to zgl_primitives_2d? 
I don't know much about OpenGL, but I tried the following version which seems 
to work:

procedure pr2d_Polygon(const Points: zglPPoint2D; const Count: Integer;
  const Closed: Boolean; const Color: Cardinal; const Alpha: Byte = 255;
  const FX: LongWord = 0);
begin
  { glVertexPointer cannot be called between glBegin and glEnd. }
  if (b2dStarted) then
    glEnd;
  glVertexPointer(2, GL_FLOAT, 0, Points);

  { Anti-aliasing (PR2D_SMOOTH) only seems to work for outlined polygons, not
    for filled polygons. }

  if (not b2dStarted) or batch2d_Check(GL_TRIANGLES, FX_BLEND or FX, nil) then
  begin
    if ((FX and PR2D_SMOOTH) > 0) then
    begin
      glEnable(GL_LINE_SMOOTH);
      {$IFNDEF USE_GLES}
      glEnable(GL_POLYGON_SMOOTH);
      {$ENDIF}
    end;
    glEnable(GL_BLEND);
//      glBegin(GL_TRIANGLES);
  end;

  glColor4ub((Color and $FF0000) shr 16, (Color and $FF00) shr 8, Color and $FF, Alpha);
  if ((FX and PR2D_FILL) <> 0) then
    glDrawArrays(GL_TRIANGLE_FAN, 0, Count)
  else if (Closed) then
    glDrawArrays(GL_LINE_LOOP, 0, Count)
  else
    glDrawArrays(GL_LINE_STRIP, 0, Count);

  if (not b2dStarted) then
  begin
//      glEnd;

    if ((FX and PR2D_SMOOTH) > 0) then
    begin
      glDisable(GL_LINE_SMOOTH);
      {$IFNDEF USE_GLES}
      glDisable(GL_POLYGON_SMOOTH);
      {$ENDIF}
    end;
    glDisable(GL_BLEND);
  end;
end;

This is probably not entirely correct (especially the call to batch2d_Check, 
and I don't know if you need glBegin and glEnd here). Also, because of 
GL_TRIANGLE_FAN, this code only works reliably with convex polygons. Maybe 
there is a better way to draw arbitrary polygons?

Also, you need to enable OpenGL polygon drawing somewhere during the 
initialization of OpenGL:

glEnableClientState(GL_VERTEX_ARRAY);

I don't know if there are any (cross-platform) issues with implementing this 
code in ZenGL.

Note that some constants and the glDrawArrays function are not part of 
zgl_opengl(es)_all yet.

Original issue reported on code.google.com by bil...@planet.nl on 26 Jan 2012 at 4:17

GoogleCodeExporter commented 9 years ago
>> this code only works reliably with convex polygons

Because of this I didn't add my own realization of rendering polygons to ZenGL. 
Polygons are not needed so often. And if you need to render arbitrary polygons 
you will face with triangles. For this ZenGL have pr2d_TriList function. So, if 
you want to render arbitrary polygons, you can use triangulation functions and 
pr2d_TriList :) For using triangulation you can look at code here: 
http://zengl.org/forum/index.php/topic,80.msg283.html#msg283

That demo a bit old, so maybe some modification is needed. After demo starts 
you can draw arbitrary textured polygon via left mouse button and draw in it 
holes via right mouse button.

Original comment by dr.andru@gmail.com on 26 Jan 2012 at 4:42

GoogleCodeExporter commented 9 years ago
Thanks for the info and demo. I will try it out.

Original comment by bil...@planet.nl on 26 Jan 2012 at 5:02