DGL
https://delphigl.com/forum/

polygon rendering use glutesselation or ...
https://delphigl.com/forum/viewtopic.php?f=19&t=7058
Seite 1 von 1

Autor:  noeska [ Mo Nov 12, 2007 21:47 ]
Betreff des Beitrags:  polygon rendering use glutesselation or ...

Currently i writing a 2D vector gui system. So far basic shapes like rectangles and circles are working fine even with radial gradients without using textures. Now i want to move to adding polygon shapes. This raises the following questions for me:
1) should i use glutesselation functions
or
2) should i write my own triangulation library
or
3) has this already been invented for delphi/pascal

As always thanks for your answers in advance.

Dateianhänge:
Dateikommentar: ScreenShot with some testing going on.
vg.jpg
vg.jpg [ 30.63 KiB | 4708-mal betrachtet ]

Autor:  MatReno [ Do Nov 15, 2007 17:37 ]
Betreff des Beitrags: 

Take a look at this post by Tak2004:
http://www.delphigl.com/forum/viewtopic.php?t=6919
Maybe that helps you to implement an own tesselation algorithm for your gui, because it's the same problem as yours. ;)

Greetings,
MatReno

Autor:  noeska [ Mo Jan 21, 2008 20:16 ]
Betreff des Beitrags: 

I tried to go for an simpler version first namely tesselate a triangle.

For that i found the following c code on the inet:
Code:
  1.  
  2. // p0, p1, p2: triangle tesselation
  3. void TriangleTesselation(CVector &p0, CVector &p1, CVector &p2, int nTesselation)
  4. {
  5.     CVector dv1 = (p2 - p0) / nTesselation;
  6.     CVector dv2 = (p2 - p1) / nTesselation;
  7.  
  8.     for (int j=0; j<nTesselation; j++)
  9.     {
  10.         CVector x1  = p0 + j*dv1;
  11.         CVector x2  = p0 + (j+1)*dv1;
  12.         CVector dx1 = ((p1-p0) + (dv2-dv1)*j);
  13.         CVector dx2 = ((p1-p0) + (dv2-dv1)*(j+1));
  14.  
  15.         glBegin(GL_TRIANGLE_STRIP);
  16.         for (int i=0; i<nTesselation-j; i++)
  17.         {
  18.             CVector u1 = x1 + ((float) i / (nTesselation-j))*dx1;
  19.             CVector u2 = x2;
  20.             if (nTesselation-j-1 != 0)
  21.                 u2 += ((float) i / (nTesselation-j-1))*dx2;
  22.  
  23.             glVertex3fv(u1.arr);
  24.             glVertex3fv(u2.arr);
  25.         }
  26.         glVertex3fv((x1+dx1).arr);
  27.  
  28.         glEnd();
  29.  
  30.     }
  31. }
  32.  


Now converted to pascal i think it should look like:
Code:
  1.  
  2. // p0, p1, p2: triangle tesselation
  3. procedure TriangleTesselation(p0: T3DPoint; p1: T3DPoint; p2: T3DPoint; nTesselation: integer);
  4. var
  5.   dv1: T3DPoint;
  6.   dv2: T3DPoint;
  7.   x1: T3DPoint;
  8.   x2: T3DPoint;
  9.   dx1: T3DPoint;
  10.   dx2: T3DPoint;
  11.   u1: T3DPoint;
  12.   u2: T3DPoint;
  13.   e1: T3DPoint;
  14.   j: integer;
  15.   i: integer;
  16. begin
  17.     dv1 := VectorDiv( VectorSubtract(p2,p0) , nTesselation);
  18.     dv2 := VectorDiv( VectorSubtract(p2,p1) , nTesselation);
  19.  
  20.     for j:=0 to nTesselation-1 do
  21.     begin
  22.         x1  := VectorAdd(p0 , VectorMul(dv1, j) );
  23.         x2  := VectorAdd(p0 , VectorMul(dv1, (j+1) ) );
  24.         dx1 := VectorAdd(VectorSubtract(p1,p0) , VectorMul( VectorSubtract(dv2,dv1) ,j));
  25.         dx2 := VectorAdd(VectorSubtract(p1,p0) , VectorMul( VectorSubtract(dv2,dv1) ,(j+1)));
  26.  
  27.         glBegin(GL_TRIANGLE_STRIP);
  28.         for i:=0 to nTesselation-j-1 do
  29.         begin
  30.             u1 := VectorAdd(x1 , VectorMul(dx1, ( i / (nTesselation-j) ) ) );
  31.             u2 := x2;
  32.             if (nTesselation-j-1 <> 0) then
  33.                 u2 := VectorAdd(u2 , VectorDiv(dx2, ( i / (nTesselation-j-1)) ) );
  34.  
  35.             glVertex2f(u1.x, u1.y);
  36.             glVertex2f(u2.x, u2.y);
  37.         end;
  38.                                 e1:=VectorAdd(x1,dx1);
  39.         glVertex2f(e1.x,e1.y);
  40.  
  41.         glEnd();
  42.  
  43.     end;
  44. end;
  45.  


This is how the Vector functions look like:
Code:
  1.  
  2. // returns the difference of two vectors
  3. function VectorSubtract(V1, V2: T3dPoint): T3dPoint; register;
  4. begin
  5.   Result.x := V1.x - V2.x;
  6.   Result.y := V1.y - V2.y;
  7.   Result.z := V1.z - V2.z;
  8. end;
  9.  
  10. // returns the addition of two vectors
  11. function VectorAdd(V1, V2: T3dPoint): T3dPoint; register;
  12. begin
  13.   Result.x := V1.x + V2.x;
  14.   Result.y := V1.y + V2.y;
  15.   Result.z := V1.z + V2.z;
  16. end;
  17.  
  18. // returns the multiplication of a vector with an single
  19. function VectorMul(V: T3DPoint; S: single): T3dPoint; register;
  20. begin
  21.   Result.x := V.x * S;
  22.   Result.y := V.y * S;
  23.   Result.z := V.z * S;
  24. end;
  25.  
  26. // returns the division of a vector with an single
  27. function VectorDiv(V: T3DPoint; S: single): T3dPoint; register;
  28. begin
  29.   Result.x := V.x / S;
  30.   Result.y := V.y / S;
  31.   Result.z := V.z / S;
  32. end;
  33.  


Unfortunately the end result is horrible. The first 2 inner triangles seem to go ok but after that coords go outside the original triangle. Also i am doing something not in the right way, but what?

Autor:  TAK2004 [ Di Jan 22, 2008 20:24 ]
Betreff des Beitrags: 

My final Solution you can find here Link .
The problem on vector based faces are the border. I designed a class TKar_Shape, where you can add vertice/points. Default there is no border and so the shape would draw without rounding but if i add a border and set it to rounding edge then the shape will generate rounded shapes. My solution look for concav and convex angles and decide to divide or not divide the current and their neighbors vertice via berzier. Because not every edge need a subdivide. For a good result you have to divide the shape first and then tesselate. The tesselation can be only correct if you search a convex triangle who is also a ear of the shape and then create a triangle with the neighbors. If you don't test the vertice on ears then you will get overdraw and wrong tesselated meshes.

Seite 1 von 1 Alle Zeiten sind UTC + 1 Stunde
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/