fProjectionD.pas 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. unit fProjectionD;
  2. interface
  3. uses
  4. Winapi.OpenGL,
  5. Winapi.OpenGLext,
  6. System.SysUtils,
  7. System.Classes,
  8. System.Types,
  9. Vcl.Controls,
  10. Vcl.Forms,
  11. GLS.Scene,
  12. GLS.Objects,
  13. GLS.SceneViewer,
  14. GLS.Texture,
  15. GLS.VectorGeometry,
  16. GLS.VectorLists,
  17. GLS.GeomObjects,
  18. GLS.RenderContextInfo,
  19. GLS.State,
  20. GLS.VectorTypes,
  21. GLS.Graph,
  22. GLS.Coordinates,
  23. GLS.Color,
  24. GLS.BaseClasses;
  25. type
  26. TFormProjection = class(TForm)
  27. GLScene1: TGLScene;
  28. SceneViewer: TGLSceneViewer;
  29. GLCamera: TGLCamera;
  30. GLDummyCube: TGLDummyCube;
  31. GLPlane: TGLPlane;
  32. GLPoints: TGLPoints;
  33. DirectOpenGL: TGLDirectOpenGL;
  34. GLArrowLine1: TGLArrowLine;
  35. GLLightSource1: TGLLightSource;
  36. GLXYZGrid1: TGLXYZGrid;
  37. procedure FormCreate(Sender: TObject);
  38. procedure DirectOpenGLRender(Sender: TObject;
  39. var rci: TGLRenderContextInfo);
  40. procedure SceneViewerMouseDown(Sender: TObject; Button: TMouseButton;
  41. Shift: TShiftState; X, Y: Integer);
  42. procedure SceneViewerMouseMove(Sender: TObject; Shift: TShiftState;
  43. X, Y: Integer);
  44. procedure FormMouseWheel(Sender: TObject; Shift: TShiftState;
  45. WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
  46. private
  47. public
  48. mx, my: Integer;
  49. end;
  50. var
  51. FormProjection: TFormProjection;
  52. implementation
  53. {$R *.dfm}
  54. procedure TFormProjection.FormCreate(Sender: TObject);
  55. var
  56. i: Integer;
  57. begin
  58. // generate a bunch of random points
  59. for i := 1 to 1000 do
  60. begin
  61. GLPoints.Positions.Add((Random - 0.5) * 5, (Random - 0.5) * 5, (Random - 0.5) * 5);
  62. GLPoints.Colors.Add(Random, Random, Random, 0.8);
  63. end;
  64. end;
  65. procedure TFormProjection.DirectOpenGLRender(Sender: TObject; var rci: TGLRenderContextInfo);
  66. var
  67. i: Integer;
  68. mat: TGLMatrix;
  69. p, pProj: TGLVector;
  70. planePoint, planeNormal: TGLVector;
  71. plane: THmgPlane;
  72. begin
  73. // Here we recover our plane point and normal...
  74. planePoint := GLPlane.Position.AsVector;
  75. planeNormal := GLPlane.Direction.AsVector;
  76. // ...which we use to create a plane (equation)
  77. plane := PlaneMake(planePoint, planeNormal);
  78. // from that plane equation and our pojection direction
  79. // (which is here the plane normal)
  80. mat := MakeParallelProjectionMatrix(plane, planeNormal);
  81. // save state, turn off lighting
  82. rci.GLStates.Disable(stLighting);
  83. // and specify the lines color
  84. glColor3f(1, 1, 0);
  85. // we'll be drawing a bunch of lines, to specify a line in OpenGL,
  86. // you only need to specify the line start and end vertices
  87. glBegin(GL_LINES);
  88. for i := 0 to GLPoints.Positions.Count - 1 do
  89. begin
  90. // read the point coordinates, directly from the TGLPoints list
  91. MakePoint(p, GLPoints.Positions.List[i]);
  92. // project this point on the plane with the matrix
  93. pProj := VectorTransform(p, mat);
  94. // specify the two vertices for a line
  95. glVertex3fv(@p);
  96. glVertex3fv(@pProj);
  97. end;
  98. glEnd;
  99. end;
  100. procedure TFormProjection.SceneViewerMouseDown(Sender: TObject; Button: TMouseButton;
  101. Shift: TShiftState; X, Y: Integer);
  102. begin
  103. mx := X;
  104. my := Y;
  105. end;
  106. procedure TFormProjection.SceneViewerMouseMove(Sender: TObject; Shift: TShiftState;
  107. X, Y: Integer);
  108. begin
  109. if Shift = [ssLeft] then
  110. GLCamera.MoveAroundTarget(my - Y, mx - X)
  111. else if Shift = [ssRight] then
  112. GLCamera.RotateObject(GLPlane, my - Y, mx - X);
  113. mx := X;
  114. my := Y;
  115. end;
  116. procedure TFormProjection.FormMouseWheel(Sender: TObject; Shift: TShiftState;
  117. WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
  118. begin
  119. GLPlane.Position.Y := GLPlane.Position.Y + WheelDelta * 0.001;
  120. end;
  121. end.