| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- //---------------------------------------------------------------------------
- #include <vcl.h>
- #pragma hdrstop
- #include "fWarpingC.h"
- #include "math.h"
- #include "GLS.Graphics.hpp"
- #include "GLS.Texture.hpp"
- #include "jpeg.hpp"
- //---------------------------------------------------------------------------
- #pragma package(smart_init)
- #pragma link "GLS.Graph"
- #pragma link "GLS.Scene"
- #pragma link "GLS.SceneViewer"
- #pragma link "GLS.Coordinates"
- #pragma resource "*.dfm"
- TForm1 *Form1;
- //---------------------------------------------------------------------------
- __fastcall TForm1::TForm1(TComponent* Owner)
- : TForm(Owner)
- {
- PathToTexture = GetCurrentAssetPath() + "\\texture";
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::FormCreate(TObject *Sender)
- {
- warpX = -1000;
- warpY = -1000;
- warpRadius = 20;
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::HeightFieldGetHeight(const float x, const float y,
- float &z, TVector4f &color, TTexPoint &texPoint)
- {
- // Here is the warping function
- // it basicly converts current pixel coords (x, y) to deformed coords (dx, dy)
- float d, dx, dy;
- TAffineVector vec;
- switch (warpEffect)
- {
- case 0 : { // the "zoom" effect
- d = 1.0-exp(-sqrt((x-warpX)*(x-warpX)+(y-warpY)*(y-warpY))/warpRadius);
- dx = x*d+warpX*(1-d);
- dy = y*d+warpY*(1-d);
- break;
- }
- case 1 : { // the "spin" effect
- vec.X = x-warpX;
- vec.Y = 0.0;
- vec.Z = y-warpY;
- d = VectorNorm(vec);
- RotateVectorAroundY(vec, (warpRadius*warpRadius)/(d+1.0));
- dx = warpX+vec.X;
- dy = warpY+vec.Z;
- break;
- }
- default :
- throw Exception("Unknown warp effect "+IntToStr(warpEffect));
- }
- // apply tex coord
- texPoint.S = dx/HeightField->XSamplingScale->Max;
- texPoint.T = dy/HeightField->YSamplingScale->Max;
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::MIOpenImageFileClick(TObject *Sender)
- {
- TPicture *picture;
- if (OpenPictureDialog->Execute())
- {
- picture = new TPicture();
- try
- {
- // load picture
- picture->LoadFromFile(OpenPictureDialog->FileName);
- // adjust HeightField
- HeightField->XSamplingScale->Max = picture->Width+0.1;
- HeightField->YSamplingScale->Max = picture->Height+0.1;
- HeightField->Material->Texture->Image->Assign(picture);
- // resize main window
- Width = Width-GLSceneViewer->Width+picture->Width;
- Height = Height-GLSceneViewer->Height+picture->Height;
- // adjust camera
- GLCamera->Position->X = picture->Width/2.0;
- GLCamera->Position->Y = picture->Height/2.0;
- GLCamera->FocalLength = 100.0/picture->Width;
- }
- __finally
- {
- delete picture;
- }
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::MISaveCurrentImageClick(TObject *Sender)
- {
- TGLBitmap32 *bmp32;
- TBitmap *bmp;
- bmp32 = GLSceneViewer->Buffer->CreateSnapShot();
- try
- {
- if (SaveDialog->Execute())
- {
- bmp = bmp32->Create32BitsBitmap();
- try
- {
- bmp->SaveToFile(SaveDialog->FileName);
- }
- __finally
- {
- delete bmp;
- }
- }
- }
- __finally
- {
- delete bmp32;
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::MIExitClick(TObject *Sender)
- {
- Close();
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::GLSceneViewerMouseDown(TObject *Sender,
- TMouseButton Button, TShiftState Shift, int X, int Y)
- {
- warpX = X;
- warpY = GLSceneViewer->Height-Y;
- HeightField->StructureChanged();
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::GLSceneViewerMouseMove(TObject *Sender,
- TShiftState Shift, int X, int Y)
- {
- if (Shift.Contains(ssLeft) || Shift.Contains(ssRight))
- {
- warpX = X;
- warpY = GLSceneViewer->Height-Y;
- HeightField->StructureChanged();
- GLSceneViewer->Refresh();
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::MIQualityOptionClick(TObject *Sender)
- {
- ((TMenuItem *) Sender)->Checked = true;
- HeightField->XSamplingScale->Step = ((TMenuItem *) Sender)->Tag;
- HeightField->YSamplingScale->Step = ((TMenuItem *) Sender)->Tag;
- HeightField->StructureChanged();
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::MIRadiusSettingClick(TObject *Sender)
- {
- ((TMenuItem *) Sender)->Checked = true;
- warpRadius = ((TMenuItem *) Sender)->Tag;
- HeightField->StructureChanged();
- }
- //---------------------------------------------------------------------------
- void __fastcall TForm1::MIZoomEffectClick(TObject *Sender)
- {
- ((TMenuItem *) Sender)->Checked = true;
- warpEffect = ((TMenuItem *) Sender)->Tag;
- HeightField->StructureChanged();
- }
- //---------------------------------------------------------------------------
|