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