fActorMS3dC.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. // ---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "fActorMS3dC.h"
  5. // ---------------------------------------------------------------------------
  6. #pragma package(smart_init)
  7. #pragma link "GLS.BaseClasses"
  8. #pragma link "GLS.Cadencer"
  9. #pragma link "GLS.Coordinates"
  10. #pragma link "GLSL.CustomShader"
  11. #pragma link "GLS.FBORenderer"
  12. #pragma link "GLS.Material"
  13. #pragma link "GLS.Objects"
  14. #pragma link "GLS.Utils"
  15. #pragma link "GLS.GeomObjects"
  16. #pragma link "GLS.Graphics"
  17. #pragma link "GLS.VectorTypes"
  18. #pragma link "GLS.RenderContextInfo"
  19. #pragma link "GLS.ShadowPlane"
  20. #pragma link "GLS.VectorGeometry"
  21. #pragma link "GLS.Mesh"
  22. #pragma link "GLS.Windows"
  23. #pragma link "GLS.State"
  24. #pragma link "GLS.Context"
  25. #pragma link "GLS.ArchiveManager"
  26. #pragma link "GLS.FileZLIB"
  27. #pragma link "GLS.TextureFormat"
  28. #pragma link "GLS.Scene"
  29. #pragma link "GLS.SimpleNavigation"
  30. #pragma link "GLS.VectorFileObjects"
  31. #pragma link "GLS.SceneViewer"
  32. #pragma link "GLS.CompositeImage"
  33. #pragma link "GLS.FileMS3D"
  34. #pragma link "GLS.FileJPEG"
  35. #pragma link "GLS.FilePNG"
  36. #pragma link "GLS.FileTGA"
  37. #pragma resource "*.dfm"
  38. TForm1* Form1;
  39. int mdx, mdy;
  40. TMatrix4f FBiasMatrix;
  41. TMatrix4f FLightModelViewMatrix;
  42. TMatrix4f FLightProjMatrix;
  43. TGLMatrix FInvCameraMatrix;
  44. TMatrix4f FEyeToLightMatrix;
  45. TMatrix4f FLightModelViewMatrix2;
  46. TMatrix4f FLightProjMatrix2;
  47. TMatrix4f FInvCameraMatrix2;
  48. TMatrix4f FEyeToLightMatrix2;
  49. // ---------------------------------------------------------------------------
  50. __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) {}
  51. // ---------------------------------------------------------------------------
  52. void __fastcall TForm1::Actor1EndFrameReached(TObject* Sender)
  53. {
  54. if (Actor1->AnimationMode == aamNone) {
  55. btnStartStop->Caption = "Start";
  56. Timer1->Enabled = false;
  57. aniPos->Enabled = true;
  58. }
  59. }
  60. // ---------------------------------------------------------------------------
  61. void __fastcall TForm1::aniBoxSelect(TObject* Sender)
  62. {
  63. Actor1->AnimationMode = aamNone;
  64. if (aniBox->ItemIndex != -1) {
  65. Chair1->Visible = aniBox->ItemIndex == 6;
  66. Timer1->Enabled = false;
  67. aniPos->Enabled = false;
  68. Actor1->SwitchToAnimation(aniBox->Text, false);
  69. aniPos->Min = 0;
  70. aniPos->Max = Actor1->EndFrame - Actor1->StartFrame;
  71. aniPos->Position = 0;
  72. aniPos->Enabled = true;
  73. btnStartStop->Caption = "Start";
  74. }
  75. }
  76. // ---------------------------------------------------------------------------
  77. void __fastcall TForm1::aniPosChange(TObject* Sender)
  78. {
  79. if (aniPos->Enabled) {
  80. Actor1->CurrentFrame = Actor1->StartFrame + aniPos->Position;
  81. }
  82. }
  83. // ---------------------------------------------------------------------------
  84. void __fastcall TForm1::btnStartStopClick(TObject* Sender)
  85. {
  86. if (Actor1->AnimationMode == aamNone) {
  87. if (Actor1->CurrentFrame == Actor1->EndFrame) {
  88. Actor1->CurrentFrame = Actor1->StartFrame;
  89. }
  90. Actor1->AnimationMode = aamPlayOnce;
  91. btnStartStop->Caption = "Stop";
  92. Timer1->Enabled = true;
  93. aniPos->Enabled = false;
  94. } else {
  95. Actor1->AnimationMode = aamNone;
  96. btnStartStop->Caption = "Start";
  97. Timer1->Enabled = false;
  98. aniPos->Enabled = true;
  99. }
  100. }
  101. // ---------------------------------------------------------------------------
  102. void __fastcall TForm1::Button2Click(TObject* Sender)
  103. {
  104. Actor1->NextFrame();
  105. }
  106. // ---------------------------------------------------------------------------
  107. void __fastcall TForm1::Button4Click(TObject* Sender)
  108. {
  109. Actor1->PrevFrame();
  110. }
  111. // ---------------------------------------------------------------------------
  112. void __fastcall TForm1::FormCloseQuery(TObject* Sender, bool &CanClose)
  113. {
  114. Actor1->AnimationMode = aamNone;
  115. GLCadencer1->Enabled = false;
  116. }
  117. // ---------------------------------------------------------------------------
  118. void _fastcall TForm1::LoadTexture(String AName, String ext)
  119. {
  120. TGLCompositeImage* img;
  121. TStream* strm = new TStream();
  122. img = (TGLCompositeImage*)MatLib->TextureByName(AName)->Image;
  123. strm = GLSArchiveManager1->Archives->Items[0]->GetContent(
  124. "Main/" + AName + "." + ext);
  125. img->LoadFromStream(strm);
  126. }
  127. // ---------------------------------------------------------------------------
  128. void __fastcall TForm1::FormCreate(TObject* Sender)
  129. {
  130. TFileName Path = GetCurrentAssetPath();
  131. // Loading an archive, to edit it you can use ..\utilities\ArchiveEdit
  132. SetCurrentDir(Path + "//modelext//");
  133. GLSArchiveManager1->Archives->Items[0]->LoadFromFile("ActorMS3D.zlib");
  134. Actor1->LoadFromStream("Woman4.ms3d", GLSArchiveManager1->Archives->Items[0]->GetContent("Main/Woman4.ms3d"));
  135. Chair1->LoadFromStream("Chair.ms3d", GLSArchiveManager1->Archives->Items[0]->GetContent("Main/Chair.ms3d"));
  136. // Loading textures from the archive as composite images and assigned to MatLib
  137. LoadTexture("Hair", "png");
  138. LoadTexture("Chair", "png");
  139. // Loading skins
  140. SetCurrentDir(Path + "//skin//");
  141. Actor1->Material->LibMaterialName = MatLib->Materials->Items[4]->Name; //"Woman4_skin";
  142. // Loading map for the earth
  143. SetCurrentDir(Path + "//map");
  144. Globus->Material->Texture->Image->LoadFromFile("earth.jpg");
  145. // Loading other textures as assets directly to objects
  146. SetCurrentDir(Path + "//texture//");
  147. GLPlane1->Material->Texture->Image->LoadFromFile("floor_parquet.jpg");
  148. Actor1->AnimationMode = aamNone;
  149. Actor1->Scale->SetVector(0.1, 0.1, 0.1, 0);
  150. Chair1->Scale->SetVector(0.35, 0.35, 0.35, 0);
  151. Actor1->Animations->Add();
  152. Actor1->Animations->Items[0]->Reference = aarSkeleton;
  153. Actor1->Animations->Items[0]->StartFrame = 2;
  154. Actor1->Animations->Items[0]->EndFrame = 855;
  155. Actor1->Animations->Items[0]->Name = "Dance";
  156. Actor1->Animations->Items[1]->Reference = aarSkeleton;
  157. Actor1->Animations->Items[1]->StartFrame = 856;
  158. Actor1->Animations->Items[1]->EndFrame = 1166;
  159. Actor1->Animations->Items[1]->Name = "Sexy Walk";
  160. Actor1->Animations->Add();
  161. Actor1->Animations->Items[2]->Reference = aarSkeleton;
  162. Actor1->Animations->Items[2]->StartFrame = 1168;
  163. Actor1->Animations->Items[2]->EndFrame = 1203;
  164. Actor1->Animations->Items[2]->Name = "Cartwheel";
  165. Actor1->Animations->Add();
  166. Actor1->Animations->Items[3]->Reference = aarSkeleton;
  167. Actor1->Animations->Items[3]->StartFrame = 1205;
  168. Actor1->Animations->Items[3]->EndFrame = 1306;
  169. Actor1->Animations->Items[3]->Name = "Hand Flip";
  170. Actor1->Animations->Add();
  171. Actor1->Animations->Items[4]->Reference = aarSkeleton;
  172. Actor1->Animations->Items[4]->StartFrame = 1308;
  173. Actor1->Animations->Items[4]->EndFrame = 1395;
  174. Actor1->Animations->Items[4]->Name = "Wave";
  175. Actor1->Animations->Add();
  176. Actor1->Animations->Items[5]->Reference = aarSkeleton;
  177. Actor1->Animations->Items[5]->StartFrame = 1397;
  178. Actor1->Animations->Items[5]->EndFrame = 2014;
  179. Actor1->Animations->Items[5]->Name = "Sun Salutation";
  180. Actor1->Animations->Add();
  181. Actor1->Animations->Items[6]->Reference = aarSkeleton;
  182. Actor1->Animations->Items[6]->StartFrame = 2016;
  183. Actor1->Animations->Items[6]->EndFrame = 2133;
  184. Actor1->Animations->Items[6]->Name = "Sit";
  185. Actor1->Animations->Add();
  186. FBiasMatrix = CreateScaleAndTranslationMatrix(
  187. VectorMake(0.5, 0.5, 0.5), VectorMake(0.5, 0.5, 0.5));
  188. }
  189. // ---------------------------------------------------------------------------
  190. void __fastcall TForm1::FormShow(TObject* Sender)
  191. {
  192. aniBox->ItemIndex = 0;
  193. aniBoxSelect(Sender);
  194. }
  195. // ---------------------------------------------------------------------------
  196. void __fastcall TForm1::GLCadencer1Progress(
  197. TObject* Sender, const double deltaTime, const double newTime)
  198. {
  199. TAffineVector af, af2, pv, pv2;
  200. GLCamera2->Position->Rotate(VectorMake(0, 1, 0), deltaTime * 0.1);
  201. af = Actor1->Skeleton->CurrentFrame->Position->Items[0];
  202. ScaleVector(af, Actor1->Scale->AsAffineVector);
  203. af2 = GLCamera2->Position->AsAffineVector;
  204. pv = VectorSubtract(af, af2);
  205. NormalizeVector(pv);
  206. GLCamera2->Direction->AsAffineVector = pv;
  207. }
  208. // ---------------------------------------------------------------------------
  209. void __fastcall TForm1::GLDirectOpenGL1Render(
  210. TObject* Sender, TGLRenderContextInfo &rci)
  211. {
  212. FInvCameraMatrix = *rci.PipelineTransformation->InvModelViewMatrix;
  213. FEyeToLightMatrix = MatrixMultiply(FInvCameraMatrix, FLightModelViewMatrix);
  214. FEyeToLightMatrix = MatrixMultiply(FEyeToLightMatrix, FLightProjMatrix);
  215. FEyeToLightMatrix = MatrixMultiply(FEyeToLightMatrix, FBiasMatrix);
  216. }
  217. // ---------------------------------------------------------------------------
  218. void __fastcall TForm1::GLFrameBufferAfterRender(
  219. TObject* Sender, TGLRenderContextInfo &rci)
  220. {
  221. CurrentGLContext()->GLStates->Disable(stPolygonOffsetFill);
  222. }
  223. // ---------------------------------------------------------------------------
  224. void __fastcall TForm1::GLFrameBufferBeforeRender(
  225. TObject* Sender, TGLRenderContextInfo &rci)
  226. {
  227. FLightModelViewMatrix =
  228. *CurrentGLContext()->PipelineTransformation->ModelViewMatrix;
  229. FLightProjMatrix =
  230. *CurrentGLContext()->PipelineTransformation->ProjectionMatrix;
  231. CurrentGLContext()->GLStates->Enable(stPolygonOffsetFill);
  232. CurrentGLContext()->GLStates->PolygonOffsetFactor = 2;
  233. CurrentGLContext()->GLStates->PolygonOffsetUnits = 2;
  234. }
  235. // ---------------------------------------------------------------------------
  236. void __fastcall TForm1::Timer1Timer(TObject* Sender)
  237. {
  238. aniPos->Position =
  239. Actor1->CurrentFrame -
  240. Actor1->Animations->Items[aniBox->ItemIndex + 1]->StartFrame;
  241. }
  242. // ---------------------------------------------------------------------------