fActorMS3dC.cpp 9.4 KB

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