Unit1.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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.Cadencer"
  10. #pragma link "GLS.Coordinates"
  11. #pragma link "GLS.GeomObjects"
  12. #pragma link "GLS.Objects"
  13. #pragma link "GLS.Scene"
  14. #pragma link "GLS.SceneViewer"
  15. #pragma resource "*.dfm"
  16. TForm1 *Form1;
  17. //---------------------------------------------------------------------------
  18. __fastcall TForm1::TForm1(TComponent* Owner)
  19. : TForm(Owner)
  20. {
  21. }
  22. //---------------------------------------------------------------------------
  23. void __fastcall TForm1::BUCastClick(TObject *Sender)
  24. {
  25. Gls::Vectorgeometry::TVector o, v, vLight, light, iPoint, iNormal;
  26. Gls::Vectorgeometry::TVector up, right, dir;
  27. int x, y, dx, dy;
  28. float f, d;
  29. TColor color;
  30. TGLBaseSceneObject *iObj;
  31. __int64 t;
  32. Screen->Cursor = crHourGlass;
  33. t = StartPrecisionTimer();
  34. // First we extract/prepare the vector we will use during our raycasting
  35. // the origin is the camera position, and factor was grossly adjusted so
  36. // that both view look grossly similar
  37. MakePoint (o, GLCamera1->AbsolutePosition);
  38. MakeVector(dir, GLCamera1->AbsoluteDirection);
  39. MakeVector(up, GLCamera1->AbsoluteUp);
  40. MakePoint(light, GLLightSource1->AbsolutePosition);
  41. right = VectorCrossProduct(dir, up);
  42. f = (float)1/300;
  43. dx = (PaintBox1->Width / 2);
  44. dy = (PaintBox1->Height / 2);
  45. // Cover a square area
  46. for (y=0; y < PaintBox1->Height-1; y++)
  47. for (x=0; x < PaintBox1->Width-1; x++)
  48. {
  49. // Calculate our ray vector for current pixel
  50. v = VectorCombine3(dir, right, up, 1, (x-dx)*f, (dy-y)*f);
  51. // ray vectors must be of unit length!
  52. NormalizeVector(v);
  53. // ray cast
  54. iObj = GLScene1->RayCastIntersect(o, v, &iPoint, &iNormal);
  55. if (iObj)
  56. {
  57. // if something found, calculate vector to light source
  58. vLight = VectorSubtract(light, iPoint);
  59. NormalizeVector(vLight);
  60. // color is given by the normal/lightsource vectors dot-product
  61. // and this intensity is composited with the object's diffuse color
  62. NormalizeVector(iNormal);
  63. d = VectorDotProduct(iNormal, vLight);
  64. if (d<0) d=0;
  65. color =
  66. ConvertColorVector(((TGLCustomSceneObject *) iObj)->Material->FrontProperties->Diffuse->Color, d);
  67. }
  68. else
  69. color = clGray;
  70. // plot our point
  71. PaintBox1->Canvas->Pixels[x][y] = color;
  72. }
  73. Caption = Format("RayCast in %.1f ms",
  74. ARRAYOFCONST ((StopPrecisionTimer(t)*1000)));
  75. Screen->Cursor = crDefault;
  76. }
  77. //---------------------------------------------------------------------------
  78. void __fastcall TForm1::GLCadencer1Progress(TObject *Sender, const double deltaTime,
  79. const double newTime)
  80. {
  81. DummyCube1->TurnAngle = newTime*50;
  82. }
  83. //---------------------------------------------------------------------------