fWarpingC.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "fWarpingC.h"
  5. #include "math.h"
  6. #include "GLS.Graphics.hpp"
  7. #include "GLS.Texture.hpp"
  8. #include "jpeg.hpp"
  9. //---------------------------------------------------------------------------
  10. #pragma package(smart_init)
  11. #pragma link "GLS.Graph"
  12. #pragma link "GLS.Scene"
  13. #pragma link "GLS.SceneViewer"
  14. #pragma link "GLS.Coordinates"
  15. #pragma resource "*.dfm"
  16. TForm1 *Form1;
  17. //---------------------------------------------------------------------------
  18. __fastcall TForm1::TForm1(TComponent* Owner)
  19. : TForm(Owner)
  20. {
  21. PathToTexture = GetCurrentAssetPath() + "\\texture";
  22. }
  23. //---------------------------------------------------------------------------
  24. void __fastcall TForm1::FormCreate(TObject *Sender)
  25. {
  26. warpX = -1000;
  27. warpY = -1000;
  28. warpRadius = 20;
  29. }
  30. //---------------------------------------------------------------------------
  31. void __fastcall TForm1::HeightFieldGetHeight(const float x, const float y,
  32. float &z, TVector4f &color, TTexPoint &texPoint)
  33. {
  34. // Here is the warping function
  35. // it basicly converts current pixel coords (x, y) to deformed coords (dx, dy)
  36. float d, dx, dy;
  37. TAffineVector vec;
  38. switch (warpEffect)
  39. {
  40. case 0 : { // the "zoom" effect
  41. d = 1.0-exp(-sqrt((x-warpX)*(x-warpX)+(y-warpY)*(y-warpY))/warpRadius);
  42. dx = x*d+warpX*(1-d);
  43. dy = y*d+warpY*(1-d);
  44. break;
  45. }
  46. case 1 : { // the "spin" effect
  47. vec.X = x-warpX;
  48. vec.Y = 0.0;
  49. vec.Z = y-warpY;
  50. d = VectorNorm(vec);
  51. RotateVectorAroundY(vec, (warpRadius*warpRadius)/(d+1.0));
  52. dx = warpX+vec.X;
  53. dy = warpY+vec.Z;
  54. break;
  55. }
  56. default :
  57. throw Exception("Unknown warp effect "+IntToStr(warpEffect));
  58. }
  59. // apply tex coord
  60. texPoint.S = dx/HeightField->XSamplingScale->Max;
  61. texPoint.T = dy/HeightField->YSamplingScale->Max;
  62. }
  63. //---------------------------------------------------------------------------
  64. void __fastcall TForm1::MIOpenImageFileClick(TObject *Sender)
  65. {
  66. TPicture *picture;
  67. if (OpenPictureDialog->Execute())
  68. {
  69. picture = new TPicture();
  70. try
  71. {
  72. // load picture
  73. picture->LoadFromFile(OpenPictureDialog->FileName);
  74. // adjust HeightField
  75. HeightField->XSamplingScale->Max = picture->Width+0.1;
  76. HeightField->YSamplingScale->Max = picture->Height+0.1;
  77. HeightField->Material->Texture->Image->Assign(picture);
  78. // resize main window
  79. Width = Width-GLSceneViewer->Width+picture->Width;
  80. Height = Height-GLSceneViewer->Height+picture->Height;
  81. // adjust camera
  82. GLCamera->Position->X = picture->Width/2.0;
  83. GLCamera->Position->Y = picture->Height/2.0;
  84. GLCamera->FocalLength = 100.0/picture->Width;
  85. }
  86. __finally
  87. {
  88. delete picture;
  89. }
  90. }
  91. }
  92. //---------------------------------------------------------------------------
  93. void __fastcall TForm1::MISaveCurrentImageClick(TObject *Sender)
  94. {
  95. TGLBitmap32 *bmp32;
  96. TBitmap *bmp;
  97. bmp32 = GLSceneViewer->Buffer->CreateSnapShot();
  98. try
  99. {
  100. if (SaveDialog->Execute())
  101. {
  102. bmp = bmp32->Create32BitsBitmap();
  103. try
  104. {
  105. bmp->SaveToFile(SaveDialog->FileName);
  106. }
  107. __finally
  108. {
  109. delete bmp;
  110. }
  111. }
  112. }
  113. __finally
  114. {
  115. delete bmp32;
  116. }
  117. }
  118. //---------------------------------------------------------------------------
  119. void __fastcall TForm1::MIExitClick(TObject *Sender)
  120. {
  121. Close();
  122. }
  123. //---------------------------------------------------------------------------
  124. void __fastcall TForm1::GLSceneViewerMouseDown(TObject *Sender,
  125. TMouseButton Button, TShiftState Shift, int X, int Y)
  126. {
  127. warpX = X;
  128. warpY = GLSceneViewer->Height-Y;
  129. HeightField->StructureChanged();
  130. }
  131. //---------------------------------------------------------------------------
  132. void __fastcall TForm1::GLSceneViewerMouseMove(TObject *Sender,
  133. TShiftState Shift, int X, int Y)
  134. {
  135. if (Shift.Contains(ssLeft) || Shift.Contains(ssRight))
  136. {
  137. warpX = X;
  138. warpY = GLSceneViewer->Height-Y;
  139. HeightField->StructureChanged();
  140. GLSceneViewer->Refresh();
  141. }
  142. }
  143. //---------------------------------------------------------------------------
  144. void __fastcall TForm1::MIQualityOptionClick(TObject *Sender)
  145. {
  146. ((TMenuItem *) Sender)->Checked = true;
  147. HeightField->XSamplingScale->Step = ((TMenuItem *) Sender)->Tag;
  148. HeightField->YSamplingScale->Step = ((TMenuItem *) Sender)->Tag;
  149. HeightField->StructureChanged();
  150. }
  151. //---------------------------------------------------------------------------
  152. void __fastcall TForm1::MIRadiusSettingClick(TObject *Sender)
  153. {
  154. ((TMenuItem *) Sender)->Checked = true;
  155. warpRadius = ((TMenuItem *) Sender)->Tag;
  156. HeightField->StructureChanged();
  157. }
  158. //---------------------------------------------------------------------------
  159. void __fastcall TForm1::MIZoomEffectClick(TObject *Sender)
  160. {
  161. ((TMenuItem *) Sender)->Checked = true;
  162. warpEffect = ((TMenuItem *) Sender)->Tag;
  163. HeightField->StructureChanged();
  164. }
  165. //---------------------------------------------------------------------------