fcProjection.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #include <tchar.h>
  4. #pragma hdrstop
  5. #include "fcProjection.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. TFormProjection *FormProjection;
  18. //---------------------------------------------------------------------------
  19. __fastcall TFormProjection::TFormProjection(TComponent* Owner)
  20. : TForm(Owner)
  21. {
  22. }
  23. //---------------------------------------------------------------------------
  24. void __fastcall TFormProjection::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. GLPoints->Colors->Add(Random(), Random(), Random(), 0.8);
  32. }
  33. }
  34. //---------------------------------------------------------------------------
  35. void __fastcall TFormProjection::DirectOpenGLRender(TObject *Sender, TGLRenderContextInfo &rci)
  36. {
  37. int i;
  38. TGLMatrix mat;
  39. TGLVector p, pProj;
  40. TGLVector planePoint, planeNormal;
  41. THmgPlane plane;
  42. // Here we recover our plane point and normal...
  43. planePoint = GLPlane->Position->AsVector;
  44. planeNormal = GLPlane->Direction->AsVector;
  45. // ...which we use to create a plane (equation)
  46. plane = PlaneMake(planePoint, planeNormal);
  47. // from that plane equation and our pojection direction
  48. // (which is here the plane normal)
  49. mat = MakeParallelProjectionMatrix(plane, planeNormal);
  50. // save state, turn off lighting and specify the lines color
  51. rci.GLStates->Disable(stLighting);
  52. glColor3f(1, 1, 0);
  53. // we'll be drawing a bunch of lines, to specify a line in OpenGL,
  54. // you only need to specify the line start and end vertices
  55. glBegin(GL_LINES);
  56. for (i=0; i < GLPoints->Positions->Count-1; i++) {
  57. // read the point coordinates, directly from the TGLPoints list
  58. MakePoint(p, GLPoints->Positions->Items[i]);
  59. // project this point on the plane with the matrix
  60. pProj = VectorTransform(p, mat);
  61. // specify the two vertices
  62. glVertex3fv(p.V);
  63. glVertex3fv(pProj.V);
  64. }
  65. glEnd();
  66. }
  67. //---------------------------------------------------------------------------
  68. void __fastcall TFormProjection::SceneViewerMouseDown(TObject *Sender, TMouseButton Button,
  69. TShiftState Shift, int X, int Y)
  70. {
  71. mx = X; my = Y;
  72. }
  73. //---------------------------------------------------------------------------
  74. void __fastcall TFormProjection::SceneViewerMouseMove(TObject *Sender, TShiftState Shift, int X,
  75. int Y)
  76. {
  77. if (Shift.Contains(ssLeft))
  78. GLCamera->MoveAroundTarget(my-Y, mx-X);
  79. else if (Shift.Contains(ssRight))
  80. GLCamera->RotateObject(GLPlane, my-Y, mx-X);
  81. mx = X; my = Y;
  82. }
  83. //---------------------------------------------------------------------------
  84. void __fastcall TFormProjection::FormMouseWheel(TObject *Sender, TShiftState Shift, int WheelDelta,
  85. TPoint &MousePos, bool &Handled)
  86. {
  87. GLPlane->Position->Y = GLPlane->Position->Y+WheelDelta*0.001;
  88. }
  89. //---------------------------------------------------------------------------