fExPolygon.pas 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. unit fExPolygon;
  2. interface
  3. uses
  4. Winapi.OpenGL,
  5. System.SysUtils,
  6. System.Classes,
  7. Vcl.Graphics,
  8. Vcl.Controls,
  9. Vcl.Forms,
  10. Vcl.Dialogs,
  11. GLS.VectorTypes,
  12. GLS.BaseClasses,
  13. GLS.VectorGeometry,
  14. GLS.Scene, GLS.Objects,
  15. GLS.GeomObjects,
  16. GLS.Texture,
  17. GLS.MultiPolygon,
  18. GLS.SceneViewer,
  19. GLS.Material,
  20. GLS.Coordinates;
  21. type
  22. TVektor = record
  23. x,y,z : Double;
  24. end;
  25. TFormExPolygon = class(TForm)
  26. GLSceneViewer1: TGLSceneViewer;
  27. GLScene1: TGLScene;
  28. GLLightSource1: TGLLightSource;
  29. GLLightSource2: TGLLightSource;
  30. Container: TGLDummyCube;
  31. CameraTarget: TGLDummyCube;
  32. Camera: TGLCamera;
  33. GLMaterialLibrary1: TGLMaterialLibrary;
  34. procedure GLSceneViewer1MouseDown(Sender: TObject;
  35. Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  36. procedure GLSceneViewer1MouseMove(Sender: TObject; Shift: TShiftState;
  37. X, Y: Integer);
  38. procedure FormShow(Sender: TObject);
  39. private
  40. mx,my : Integer;
  41. FPlane : array[0..5] of TGLMultiPolygon;
  42. FDY: Double;
  43. FDX: Double;
  44. FDZ: Double;
  45. function GetPlane(Side: Integer): TGLMultiPolygon;
  46. procedure SetDX(const Value: Double);
  47. procedure SetDY(const Value: Double);
  48. procedure SetDZ(const Value: Double);
  49. procedure CreatePanel;
  50. procedure AddMaterial(Obj:TGLSceneObject);
  51. procedure ReDraw;
  52. function TransformToPlane(Side:Integer; x,y,z:Double):TVektor; overload;
  53. function TransformToPlane(Side:Integer; v:TVektor):TVektor; overload;
  54. public
  55. { Public-Deklarationen }
  56. procedure MakeHole(Side:Integer; X,Y,Z,D,T:Double; Phi:Double=0; Rho:Double=0);
  57. property Plane[Side:Integer]:TGLMultiPolygon read GetPlane;
  58. property DX:Double read FDX write SetDX;
  59. property DY:Double read FDY write SetDY;
  60. property DZ:Double read FDZ write SetDZ;
  61. end;
  62. var
  63. FormExPolygon: TFormExPolygon;
  64. implementation
  65. {$R *.DFM}
  66. function Vektor(x,y,z:Double):TVektor;
  67. begin
  68. result.x := x;
  69. result.y := y;
  70. result.z := z;
  71. end;
  72. const
  73. cOpposite : array[0..5] of Integer = (5,3,4,1,2,0);
  74. procedure TFormExPolygon.MakeHole(Side: Integer; X, Y, Z, D, T, Phi, Rho: Double);
  75. var
  76. R : Double;
  77. Dum : TGLDummyCube;
  78. Cyl : TGLCylinder;
  79. through : Boolean;
  80. begin
  81. Dum := TGLDummyCube.Create(nil);
  82. Dum.Position.x := X;
  83. Dum.Position.y := Y;
  84. Dum.Position.z := Z;
  85. case Side of
  86. 0 : Dum.PitchAngle := -90;
  87. 1 : Dum.RollAngle := 90;
  88. 2 : Dum.RollAngle := 180;
  89. 3 : Dum.RollAngle := 270;
  90. 4 : Dum.RollAngle := 0;
  91. 5 : Dum.PitchAngle := 90;
  92. end;
  93. Dum.PitchAngle := Dum.PitchAngle + Rho;
  94. Dum.RollAngle := Dum.RollAngle + Phi;
  95. R := 0.5*D;
  96. through := true;
  97. case Side of
  98. 0 : if (Z-T)<=0 then T := Z else through := false;
  99. 1 : if (X+T)>=DX then T := DX-X else through := false;
  100. 2 : if (Y+T)>=DY then T := DY-Y else through := false;
  101. 3 : if (X-T)<=0 then T := X else through := false;
  102. 4 : if (Y-T)<=0 then T := Y else through := false;
  103. 5 : if (Z+T)>=DZ then T := DZ-Z else through := false;
  104. end;
  105. Cyl := TGLCylinder.Create(nil);
  106. AddMaterial(Cyl);
  107. Cyl.Position.x := 0;
  108. Cyl.Position.y := - 0.5*T;
  109. Cyl.Position.z := 0;
  110. Cyl.Height := T;
  111. Cyl.BottomRadius := R;
  112. Cyl.TopRadius := R;
  113. Cyl.NormalDirection := ndInside;
  114. if through then Cyl.Parts := [cySides]
  115. else Cyl.Parts := [cySides,cyBottom];
  116. Dum.AddChild(Cyl);
  117. Container.AddChild(Dum);
  118. Plane[Side].Contours.Add.Nodes.AddXYArc(R/cos(Phi*c180divPi),R,0,360,16, AffineVectorMake(X,Y,0));
  119. if through then
  120. Plane[cOpposite[Side]].Contours.Add.Nodes.AddXYArc(R/cos(Phi*c180divPi),R,0,360,16, AffineVectorMake(X,Y,0));
  121. end;
  122. procedure TFormExPolygon.CreatePanel;
  123. var
  124. I : Integer;
  125. function MakePlane(X,Y,Z,P,T,W,H:Double):TGLMultiPolygon;
  126. begin
  127. result := TGLMultiPolygon.Create(nil);
  128. result.Material.MaterialLibrary := GLMaterialLibrary1;
  129. result.Material.LibMaterialName := 'MatSurface';
  130. result.Parts := [ppTop];
  131. result.AddNode(0,0,0,0);
  132. result.AddNode(0,W,0,0);
  133. result.AddNode(0,W,H,0);
  134. result.AddNode(0,0,H,0);
  135. result.Position.x := X;
  136. result.Position.y := Y;
  137. result.Position.z := Z;
  138. result.PitchAngle := P;
  139. result.TurnAngle := T;
  140. end;
  141. begin
  142. Container.DeleteChildren;
  143. FPlane[2] := MakePlane( 0, 0, 0, -90, 0,DX,DZ);
  144. FPlane[3] := MakePlane(DX, 0, 0, -90, 90,DY,DZ);
  145. FPlane[4] := MakePlane(DX,DY, 0, -90,180,DX,DZ);
  146. FPlane[1] := MakePlane( 0,DY, 0, -90,270,DY,DZ);
  147. FPlane[5] := MakePlane( 0,DY, 0,-180, 0,DX,DY);
  148. FPlane[0] := MakePlane( 0, 0,DZ, 0, 0,DX,DY);
  149. for I:=0 to 5 do Container.AddChild(FPlane[I]);
  150. end;
  151. function TFormExPolygon.GetPlane(Side: Integer): TGLMultiPolygon;
  152. begin
  153. result := FPlane[Side];
  154. end;
  155. procedure TFormExPolygon.GLSceneViewer1MouseDown(Sender: TObject;
  156. Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  157. begin
  158. mx:=x; my:=y;
  159. end;
  160. procedure TFormExPolygon.GLSceneViewer1MouseMove(Sender: TObject;
  161. Shift: TShiftState; X, Y: Integer);
  162. begin
  163. if Shift<>[] then
  164. Camera.MoveAroundTarget(my-y, mx-x);
  165. mx:=x; my:=y;
  166. end;
  167. procedure TFormExPolygon.SetDX(const Value: Double);
  168. begin
  169. FDX := Value;
  170. Container.Position.X := -0.5*Value;
  171. end;
  172. procedure TFormExPolygon.SetDY(const Value: Double);
  173. begin
  174. FDY := Value;
  175. Container.Position.Y := -0.5*Value;
  176. end;
  177. procedure TFormExPolygon.SetDZ(const Value: Double);
  178. begin
  179. FDZ := Value;
  180. Container.Position.Z := -0.5*Value;
  181. end;
  182. procedure TFormExPolygon.AddMaterial(Obj: TGLSceneObject);
  183. begin
  184. Obj.Material.MaterialLibrary := GLMaterialLibrary1;
  185. Obj.Material.LibMaterialName := 'MatInner';
  186. end;
  187. procedure TFormExPolygon.ReDraw;
  188. begin
  189. DX := 600;
  190. DY := 400;
  191. DZ := 19;
  192. CreatePanel;
  193. MakeHole(0,0.5*DX,0.5*DY,DZ,50,DZ);
  194. end;
  195. procedure TFormExPolygon.FormShow(Sender: TObject);
  196. begin
  197. Redraw;
  198. end;
  199. function TFormExPolygon.TransformToPlane(Side: Integer; x, y, z: Double): TVektor;
  200. begin
  201. case Side of
  202. 0 : result := Vektor(x,y,z-DZ);
  203. 1 : result := Vektor(DY-y,z,x);
  204. 2 : result := Vektor(x,z,-y);
  205. 3 : result := Vektor(y,z,DX-x);
  206. 4 : result := Vektor(DX-x,z,DY-y);
  207. 5 : result := Vektor(x,DY-y,z);
  208. else result := Vektor(x,y,z);
  209. end;
  210. end;
  211. function TFormExPolygon.TransformToPlane(Side: Integer; v: TVektor): TVektor;
  212. begin
  213. result := TransformToPlane(Side,v.x,v.y,v.z);
  214. end;
  215. end.