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