Unit1.cpp 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #include <tchar.h>
  4. #pragma hdrstop
  5. #include "Unit1.h"
  6. //---------------------------------------------------------------------------
  7. #pragma package(smart_init)
  8. #pragma link "GLS.BaseClasses"
  9. #pragma link "GLS.Coordinates"
  10. #pragma link "GLS.GeomObjects"
  11. #pragma link "GLS.Graph"
  12. #pragma link "GLS.Objects"
  13. #pragma link "GLS.Scene"
  14. #pragma link "GLS.SceneViewer"
  15. #pragma link "GLS.BaseClasses"
  16. #pragma resource "*.dfm"
  17. TForm1 *Form1;
  18. //---------------------------------------------------------------------------
  19. __fastcall TForm1::TForm1(TComponent* Owner)
  20. : TForm(Owner)
  21. {
  22. }
  23. //---------------------------------------------------------------------------
  24. void __fastcall TForm1::FormCreate(TObject *Sender)
  25. {
  26. int i;
  27. // generate a bunch of random points
  28. for (i=1; i < 1000; i++)
  29. GLPoints->Positions->Add((float)(Random()-0.5)*5,
  30. (float)(Random()-0.5)*5, (float)(Random()-0.5)*5);
  31. }
  32. //---------------------------------------------------------------------------
  33. void __fastcall TForm1::DirectOpenGLRender(TObject *Sender, TGLRenderContextInfo &rci)
  34. {
  35. int i;
  36. Gls::Vectorgeometry::TGLMatrix mat;
  37. TGLVector p, pProj;
  38. TGLVector planePoint, planeNormal;
  39. THmgPlane plane;
  40. // Here we recover our plane point and normal...
  41. planePoint = GLPlane->Position->AsVector;
  42. planeNormal = GLPlane->Direction->AsVector;
  43. // ...which we use to create a plane (equation)
  44. plane = PlaneMake(planePoint, planeNormal);
  45. // from that plane equation and our pojection direction
  46. // (which is here the plane normal)
  47. mat = MakeParallelProjectionMatrix(plane, planeNormal);
  48. // save state, turn off lighting and specify the lines color
  49. rci.GLStates->Disable(stLighting);
  50. glColor3f(1, 1, 0);
  51. // we'll be drawing a bunch of lines, to specify a line in OpenGL,
  52. // you only need to specify the line start and end vertices
  53. glBegin(GL_LINES);
  54. for (i=0; i < GLPoints->Positions->Count-1; i++) {
  55. // read the point coordinates, directly from the TGLPoints list
  56. MakePoint(p, GLPoints->Positions->Items[i]);
  57. // project this point on the plane with the matrix
  58. pProj = VectorTransform(p, mat);
  59. // specify the two vertices
  60. glVertex3fv(p.V);
  61. glVertex3fv(pProj.V);
  62. }
  63. glEnd();
  64. }
  65. //---------------------------------------------------------------------------
  66. void __fastcall TForm1::SceneViewerMouseDown(TObject *Sender, TMouseButton Button,
  67. TShiftState Shift, int X, int Y)
  68. {
  69. mx = X; my = Y;
  70. }
  71. //---------------------------------------------------------------------------
  72. void __fastcall TForm1::SceneViewerMouseMove(TObject *Sender, TShiftState Shift, int X,
  73. int Y)
  74. {
  75. if (Shift.Contains(ssLeft))
  76. GLCamera->MoveAroundTarget(my-Y, mx-X);
  77. else if (Shift.Contains(ssRight))
  78. GLCamera->RotateObject(GLPlane, my-Y, mx-X);
  79. mx = X; my = Y;
  80. }
  81. //---------------------------------------------------------------------------
  82. void __fastcall TForm1::FormMouseWheel(TObject *Sender, TShiftState Shift, int WheelDelta,
  83. TPoint &MousePos, bool &Handled)
  84. {
  85. GLPlane->Position->Y = GLPlane->Position->Y+WheelDelta*0.001;
  86. }
  87. //---------------------------------------------------------------------------