fActorMS3dC.cpp 9.5 KB

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