fProjectionD.pas 3.5 KB

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