Unit1.cpp 9.7 KB

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