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: // p0, p1, p2: triangle tesselation void TriangleTesselation(CVector &p0, CVector &p1, CVector &p2, int nTesselation) { CVector dv1 = (p2 - p0) / nTesselation; CVector dv2 = (p2 - p1) / nTesselation; for (int j=0; j<nTesselation; j++) { CVector x1 = p0 + j*dv1; CVector x2 = p0 + (j+1)*dv1; CVector dx1 = ((p1-p0) + (dv2-dv1)*j); CVector dx2 = ((p1-p0) + (dv2-dv1)*(j+1)); glBegin(GL_TRIANGLE_STRIP); for (int i=0; i<nTesselation-j; i++) { CVector u1 = x1 + ((float) i / (nTesselation-j))*dx1; CVector u2 = x2; if (nTesselation-j-1 != 0) u2 += ((float) i / (nTesselation-j-1))*dx2; glVertex3fv(u1.arr); glVertex3fv(u2.arr); } glVertex3fv((x1+dx1).arr); glEnd(); } }
Now converted to pascal i think it should look like:
Code: // p0, p1, p2: triangle tesselation procedure TriangleTesselation(p0: T3DPoint; p1: T3DPoint; p2: T3DPoint; nTesselation: integer); var dv1: T3DPoint; dv2: T3DPoint; x1: T3DPoint; x2: T3DPoint; dx1: T3DPoint; dx2: T3DPoint; u1: T3DPoint; u2: T3DPoint; e1: T3DPoint; j: integer; i: integer; begin dv1 := VectorDiv( VectorSubtract(p2,p0) , nTesselation); dv2 := VectorDiv( VectorSubtract(p2,p1) , nTesselation); for j:=0 to nTesselation-1 do begin x1 := VectorAdd(p0 , VectorMul(dv1, j) ); x2 := VectorAdd(p0 , VectorMul(dv1, (j+1) ) ); dx1 := VectorAdd(VectorSubtract(p1,p0) , VectorMul( VectorSubtract(dv2,dv1) ,j)); dx2 := VectorAdd(VectorSubtract(p1,p0) , VectorMul( VectorSubtract(dv2,dv1) ,(j+1))); glBegin(GL_TRIANGLE_STRIP); for i:=0 to nTesselation-j-1 do begin u1 := VectorAdd(x1 , VectorMul(dx1, ( i / (nTesselation-j) ) ) ); u2 := x2; if (nTesselation-j-1 <> 0) then u2 := VectorAdd(u2 , VectorDiv(dx2, ( i / (nTesselation-j-1)) ) ); glVertex2f(u1.x, u1.y); glVertex2f(u2.x, u2.y); end; e1:=VectorAdd(x1,dx1); glVertex2f(e1.x,e1.y); glEnd(); end; end;
This is how the Vector functions look like:
Code: // returns the difference of two vectors function VectorSubtract(V1, V2: T3dPoint): T3dPoint; register; begin Result.x := V1.x - V2.x; Result.y := V1.y - V2.y; Result.z := V1.z - V2.z; end; // returns the addition of two vectors function VectorAdd(V1, V2: T3dPoint): T3dPoint; register; begin Result.x := V1.x + V2.x; Result.y := V1.y + V2.y; Result.z := V1.z + V2.z; end; // returns the multiplication of a vector with an single function VectorMul(V: T3DPoint; S: single): T3dPoint; register; begin Result.x := V.x * S; Result.y := V.y * S; Result.z := V.z * S; end; // returns the division of a vector with an single function VectorDiv(V: T3DPoint; S: single): T3dPoint; register; begin Result.x := V.x / S; Result.y := V.y / S; Result.z := V.z / S; end;
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?
|