fExPolygonD.pas 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. unit fExPolygonD;
  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. GLScene.VectorTypes,
  12. GLS.BaseClasses,
  13. GLScene.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. procedure MakeHole(Side:Integer; X,Y,Z,D,T:Double; Phi:Double=0; Rho:Double=0);
  56. property Plane[Side:Integer]:TGLMultiPolygon read GetPlane;
  57. property DX:Double read FDX write SetDX;
  58. property DY:Double read FDY write SetDY;
  59. property DZ:Double read FDZ write SetDZ;
  60. end;
  61. var
  62. FormExPolygon: TFormExPolygon;
  63. implementation
  64. {$R *.DFM}
  65. function Vektor(x,y,z:Double):TVektor;
  66. begin
  67. result.x := x;
  68. result.y := y;
  69. result.z := z;
  70. end;
  71. const
  72. cOpposite : array[0..5] of Integer = (5,3,4,1,2,0);
  73. procedure TFormExPolygon.MakeHole(Side: Integer; X, Y, Z, D, T, Phi, Rho: Double);
  74. var
  75. R : Double;
  76. Dum : TGLDummyCube;
  77. Cyl : TGLCylinder;
  78. through : Boolean;
  79. begin
  80. Dum := TGLDummyCube.Create(nil);
  81. Dum.Position.x := X;
  82. Dum.Position.y := Y;
  83. Dum.Position.z := Z;
  84. case Side of
  85. 0 : Dum.PitchAngle := -90;
  86. 1 : Dum.RollAngle := 90;
  87. 2 : Dum.RollAngle := 180;
  88. 3 : Dum.RollAngle := 270;
  89. 4 : Dum.RollAngle := 0;
  90. 5 : Dum.PitchAngle := 90;
  91. end;
  92. Dum.PitchAngle := Dum.PitchAngle + Rho;
  93. Dum.RollAngle := Dum.RollAngle + Phi;
  94. R := 0.5*D;
  95. through := true;
  96. case Side of
  97. 0 : if (Z-T)<=0 then T := Z else through := false;
  98. 1 : if (X+T)>=DX then T := DX-X else through := false;
  99. 2 : if (Y+T)>=DY then T := DY-Y else through := false;
  100. 3 : if (X-T)<=0 then T := X else through := false;
  101. 4 : if (Y-T)<=0 then T := Y else through := false;
  102. 5 : if (Z+T)>=DZ then T := DZ-Z else through := false;
  103. end;
  104. Cyl := TGLCylinder.Create(nil);
  105. AddMaterial(Cyl);
  106. Cyl.Position.x := 0;
  107. Cyl.Position.y := - 0.5*T;
  108. Cyl.Position.z := 0;
  109. Cyl.Height := T;
  110. Cyl.BottomRadius := R;
  111. Cyl.TopRadius := R;
  112. Cyl.NormalDirection := ndInside;
  113. if through then Cyl.Parts := [cySides]
  114. else Cyl.Parts := [cySides,cyBottom];
  115. Dum.AddChild(Cyl);
  116. Container.AddChild(Dum);
  117. Plane[Side].Contours.Add.Nodes.AddXYArc(R/cos(Phi*c180divPi),R,0,360,16, AffineVectorMake(X,Y,0));
  118. if through then
  119. Plane[cOpposite[Side]].Contours.Add.Nodes.AddXYArc(R/cos(Phi*c180divPi),R,0,360,16, AffineVectorMake(X,Y,0));
  120. end;
  121. procedure TFormExPolygon.CreatePanel;
  122. var
  123. I : Integer;
  124. function MakePlane(X,Y,Z,P,T,W,H:Double):TGLMultiPolygon;
  125. begin
  126. result := TGLMultiPolygon.Create(nil);
  127. result.Material.MaterialLibrary := GLMaterialLibrary1;
  128. result.Material.LibMaterialName := 'MatSurface';
  129. result.Parts := [ppTop];
  130. result.AddNode(0,0,0,0);
  131. result.AddNode(0,W,0,0);
  132. result.AddNode(0,W,H,0);
  133. result.AddNode(0,0,H,0);
  134. result.Position.x := X;
  135. result.Position.y := Y;
  136. result.Position.z := Z;
  137. result.PitchAngle := P;
  138. result.TurnAngle := T;
  139. end;
  140. begin
  141. Container.DeleteChildren;
  142. FPlane[2] := MakePlane( 0, 0, 0, -90, 0,DX,DZ);
  143. FPlane[3] := MakePlane(DX, 0, 0, -90, 90,DY,DZ);
  144. FPlane[4] := MakePlane(DX,DY, 0, -90,180,DX,DZ);
  145. FPlane[1] := MakePlane( 0,DY, 0, -90,270,DY,DZ);
  146. FPlane[5] := MakePlane( 0,DY, 0,-180, 0,DX,DY);
  147. FPlane[0] := MakePlane( 0, 0,DZ, 0, 0,DX,DY);
  148. for I:=0 to 5 do Container.AddChild(FPlane[I]);
  149. end;
  150. function TFormExPolygon.GetPlane(Side: Integer): TGLMultiPolygon;
  151. begin
  152. result := FPlane[Side];
  153. end;
  154. procedure TFormExPolygon.GLSceneViewer1MouseDown(Sender: TObject;
  155. Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  156. begin
  157. mx:=x; my:=y;
  158. end;
  159. procedure TFormExPolygon.GLSceneViewer1MouseMove(Sender: TObject;
  160. Shift: TShiftState; X, Y: Integer);
  161. begin
  162. if Shift<>[] then
  163. Camera.MoveAroundTarget(my-y, mx-x);
  164. mx:=x; my:=y;
  165. end;
  166. procedure TFormExPolygon.SetDX(const Value: Double);
  167. begin
  168. FDX := Value;
  169. Container.Position.X := -0.5*Value;
  170. end;
  171. procedure TFormExPolygon.SetDY(const Value: Double);
  172. begin
  173. FDY := Value;
  174. Container.Position.Y := -0.5*Value;
  175. end;
  176. procedure TFormExPolygon.SetDZ(const Value: Double);
  177. begin
  178. FDZ := Value;
  179. Container.Position.Z := -0.5*Value;
  180. end;
  181. procedure TFormExPolygon.AddMaterial(Obj: TGLSceneObject);
  182. begin
  183. Obj.Material.MaterialLibrary := GLMaterialLibrary1;
  184. Obj.Material.LibMaterialName := 'MatInner';
  185. end;
  186. procedure TFormExPolygon.ReDraw;
  187. begin
  188. DX := 600;
  189. DY := 400;
  190. DZ := 19;
  191. CreatePanel;
  192. MakeHole(0,0.5*DX,0.5*DY,DZ,50,DZ);
  193. end;
  194. procedure TFormExPolygon.FormShow(Sender: TObject);
  195. begin
  196. Redraw;
  197. end;
  198. function TFormExPolygon.TransformToPlane(Side: Integer; x, y, z: Double): TVektor;
  199. begin
  200. case Side of
  201. 0 : result := Vektor(x,y,z-DZ);
  202. 1 : result := Vektor(DY-y,z,x);
  203. 2 : result := Vektor(x,z,-y);
  204. 3 : result := Vektor(y,z,DX-x);
  205. 4 : result := Vektor(DX-x,z,DY-y);
  206. 5 : result := Vektor(x,DY-y,z);
  207. else result := Vektor(x,y,z);
  208. end;
  209. end;
  210. function TFormExPolygon.TransformToPlane(Side: Integer; v: TVektor): TVektor;
  211. begin
  212. result := TransformToPlane(Side,v.x,v.y,v.z);
  213. end;
  214. end.