fDceDemoC.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "fDceDemoC.h"
  5. //---------------------------------------------------------------------------
  6. #pragma package(smart_init)
  7. #pragma link "GLS.Scene"
  8. #pragma link "GLS.File3DS"
  9. #pragma link "GLS.FileMD2"
  10. #pragma link "GLS.ProxyObjects"
  11. #pragma link "GLS.BaseClasses"
  12. #pragma link "GLS.BitmapFont"
  13. #pragma link "GLS.Cadencer"
  14. #pragma link "GLS.Coordinates"
  15. #pragma link "GLS.HeightData"
  16. #pragma link "GLS.HUDObjects"
  17. #pragma link "GLS.Material"
  18. #pragma link "GLS.Objects"
  19. #pragma link "GLS.TerrainRenderer"
  20. #pragma link "GLS.VectorFileObjects"
  21. #pragma link "GLS.SceneViewer"
  22. #pragma link "GLS.WindowsFont"
  23. #pragma link "GLS.SimpleNavigation"
  24. #pragma resource "*.dfm"
  25. TForm1 *Form1;
  26. const
  27. float cForce = 250.0;
  28. const
  29. int cSpread = 200;
  30. const
  31. int cNbMushrooms = 20;
  32. float random(void)
  33. {
  34. return (float)(rand() & 0x1FFF) / (float)0x1FFF;
  35. }
  36. //---------------------------------------------------------------------------
  37. __fastcall TForm1::TForm1(TComponent* Owner)
  38. : TForm(Owner)
  39. {
  40. }
  41. //---------------------------------------------------------------------------
  42. void TForm1::Load()
  43. {
  44. TFileName Path = GetCurrentAssetPath();
  45. // Load texture material for terrain
  46. SetCurrentDir(Path + "\\texture");
  47. GLMatLib->AddTextureMaterial("Terrain", "snow512.jpg");
  48. //Load Terrain
  49. GLBitmapHDS1->MaxPoolSize = 8 * 1024 * 1024;
  50. GLBitmapHDS1->Picture->LoadFromFile("terrain.bmp");
  51. Terrain->Direction->SetVector(0.0, 1.0, 0.0);
  52. Terrain->Material->LibMaterialName = "Terrain";
  53. Terrain->TilesPerTexture = 256 / Terrain->TileSize;
  54. Terrain->Scale->SetVector(1.0, 1.0, 0.02);
  55. Ground->Material->LibMaterialName = "Terrain";
  56. // Load static mushroom mesh
  57. SetCurrentDir(Path + "\\model");
  58. //Always use AutoScaling property or you may get some problems
  59. moMushroom->AutoScaling->SetPoint(0.1, 0.1, 0.1);
  60. moMushroom->LoadFromFile("Mushroom.3ds");
  61. moMushroom->Direction->SetVector(0.0, 1.0, 0.0);
  62. moMushroom->BuildOctree();
  63. //Load player
  64. Player->Position->SetPoint(0.0, 3.0, 0.0);
  65. // Load dynamic Actor modelext with animation and texture
  66. SetCurrentDir(Path + "\\modelext");
  67. GLMatLib->AddTextureMaterial("Actor", "waste.jpg");
  68. GLActor1->LoadFromFile("Waste.md2");
  69. GLActor1->Direction->SetVector(0.0, 1.0, 0.0);
  70. GLActor1->Up->SetVector(1.0, 0.0, 0.0);
  71. GLActor1->Scale->SetVector(0.05, 0.05, 0.05);
  72. GLActor1->Material->LibMaterialName = "Actor";
  73. GLActor1->Animations->LoadFromFile("Quake2Animations.aaf");
  74. // Define animation properties
  75. GLActor1->AnimationMode = aamLoop;
  76. GLActor1->SwitchToAnimation("stand");
  77. GLActor1->FrameInterpolation = afpLinear;
  78. //DCE Behaviour
  79. GLSphere1->Scale->Assign(GetOrCreateDCEDynamic(Player)->Size);
  80. // GetOrCreateDCEDynamic(Player)->OnCollision = PlayerBehaviours0Collision;
  81. }
  82. //---------------------------------------------------------------------------
  83. void __fastcall TForm1::FormShow(TObject *Sender)
  84. {
  85. Load();
  86. GLCadencer1->Enabled = true;
  87. Help->Text =
  88. "Mouse Drag - Look\n\r\
  89. A,W,S,D - movement\n\r\
  90. SPACE - Jump\n\r\
  91. F1 - Add one ball\n\r\
  92. F2 - Add 10 balls\n\r\
  93. F3 - Add 20 mushrooms\n\r\
  94. F4 - Change ground to box\n\r\
  95. F5 - Toggle step mode\n\r\
  96. RETURN - Reset";
  97. }
  98. //---------------------------------------------------------------------------
  99. void TForm1::AddBall()
  100. {
  101. TGLSphere *Ball;
  102. float S;
  103. float pX,pY,pZ;
  104. float cR,cG,cB;
  105. Ball = (TGLSphere *)(Balls->AddNewChild(__classid(TGLSphere)));
  106. //with Ball do
  107. Ball->Tag = 1; //set the identifier of a ball
  108. Ball->Radius = 1.0;
  109. S = (float)(100.0 + random(900.0))/ 500.0;
  110. Ball->Scale->SetVector(S, S, S);
  111. pX = random(40.0) - random(40.0);
  112. pY = 4.0 + random(10.0);
  113. pZ = random(40.0) - random(40.0);
  114. Ball->Position->SetPoint(pX, pY, pZ);
  115. cR = (float)(100.0 + random(900.0))/1000;
  116. cG = (float)(100.0 + random(900.0))/1000;
  117. cB = (float)(100.0 + random(900.0))/1000;
  118. Ball->Material->FrontProperties->Diffuse->SetColor(cR, cG, cB);
  119. GetOrCreateDCEDynamic(Ball)->Manager = GLDCEManager1;
  120. GetOrCreateDCEDynamic(Ball)->BounceFactor = 0.75;
  121. GetOrCreateDCEDynamic(Ball)->Friction = 0.1;
  122. GetOrCreateDCEDynamic(Ball)->SlideOrBounce = csbBounce;
  123. GetOrCreateDCEDynamic(Ball)->Size->Assign(Ball->Scale);
  124. }
  125. //---------------------------------------------------------------------------
  126. void TForm1::AddMushrooms()
  127. {
  128. int i;
  129. TGLFreeFormProxy *Proxy;
  130. TVector4f s;
  131. float f;
  132. // spawn some more mushrooms using proxy objects
  133. for (i = 0; i < cNbMushrooms - 1; i++) {
  134. // create a new Proxy and set its MasterObject property
  135. Proxy = (TGLFreeFormProxy *)(Mushrooms->AddNewChild(__classid(TGLFreeFormProxy)));
  136. Proxy->ProxyOptions = Proxy->ProxyOptions << pooObjects;
  137. Proxy->MasterObject = moMushroom;
  138. // retrieve reference attitude
  139. Proxy->Direction = moMushroom->Direction;
  140. Proxy->Up = moMushroom->Up;
  141. // randomize scale
  142. s = moMushroom->Scale->AsVector;
  143. f = 2.0 * random() + 1.0;
  144. ScaleVector(s, f);
  145. Proxy->Scale->AsVector = s;
  146. // randomize position
  147. Proxy->Position->SetPoint(
  148. (random(cSpread) - (float)(cSpread / 2.0)),
  149. (moMushroom->Position->Z + 1.5 * f),
  150. (random(cSpread) - (float)(cSpread / 2.0)));
  151. // randomize orientation
  152. Proxy->RollAngle = random(360.0);
  153. Proxy->TransformationChanged();
  154. GetOrCreateDCEStatic(Proxy)->Manager = GLDCEManager1;
  155. GetOrCreateDCEStatic(Proxy)->BounceFactor = 0.75;
  156. GetOrCreateDCEStatic(Proxy)->Friction = 10.0;
  157. GetOrCreateDCEStatic(Proxy)->Shape = csFreeform;
  158. }
  159. }
  160. //---------------------------------------------------------------------------
  161. void TForm1::HandleAnimation()
  162. {
  163. String anim;
  164. if (VectorNorm(GetOrCreateDCEDynamic(Player)->Speed) > 0.1)
  165. anim = "run";
  166. else
  167. anim = "stand";
  168. if (Jumped==true)
  169. {
  170. if (!GetOrCreateDCEDynamic(Player)->InGround)
  171. anim = "jump";
  172. else
  173. Jumped = false;
  174. }
  175. if (anim == "jump")
  176. GLActor1->Interval = 500;
  177. else
  178. GLActor1->Interval = 100;
  179. if (GLActor1->CurrentAnimation() != anim)
  180. GLActor1->SwitchToAnimation(anim);
  181. }
  182. //---------------------------------------------------------------------------
  183. void TForm1::HandleKeys()
  184. {
  185. TAffineVector Force;
  186. Force = NullVector;
  187. if (IsKeyDown(VK_ESCAPE)) Close();
  188. if (IsKeyDown('w') || IsKeyDown('W') || IsKeyDown(VK_UP))
  189. Force.Z = cForce;
  190. if (IsKeyDown('s') || IsKeyDown('S') || IsKeyDown(VK_DOWN))
  191. Force.Z = -cForce;
  192. if (IsKeyDown('a') || IsKeyDown('A') || IsKeyDown(VK_LEFT))
  193. Force.X = cForce;
  194. if (IsKeyDown('d') || IsKeyDown('D') || IsKeyDown(VK_RIGHT))
  195. Force.X = -cForce;
  196. GetOrCreateDCEDynamic(Player)->ApplyAccel(Force);
  197. }
  198. //---------------------------------------------------------------------------
  199. void __fastcall TForm1::FormKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
  200. {
  201. int i;
  202. if (Key == VK_F1)
  203. AddBall();
  204. if (Key == VK_F2)
  205. for (i = 1; i <= 10; i++)
  206. AddBall();
  207. if (Key == VK_F3)
  208. AddMushrooms();
  209. if (Key == VK_SPACE)
  210. {
  211. GetOrCreateDCEDynamic(Player)->Jump(1.0, 20.0);
  212. Jumped = true;
  213. }
  214. if (Key == VK_F4)
  215. {
  216. Terrain->Visible = false;
  217. Ground->Visible = true;
  218. GetOrCreateDCEStatic(Terrain)->Active = false;
  219. GetOrCreateDCEStatic(Ground)->Active = true;
  220. }
  221. if (Key == VK_F5)
  222. GLDCEManager1->ManualStep = !GLDCEManager1->ManualStep;
  223. if (Key = VK_RETURN) {
  224. Player->Position->SetPoint(0.0, 3.0, 0.0);
  225. Balls->DeleteChildren();
  226. Mushrooms->DeleteChildren();
  227. Help->ModulateColor->Alpha = 1.0;
  228. Terrain->Visible = true;
  229. Ground->Visible = false;
  230. GetOrCreateDCEStatic(Terrain)->Active = true;
  231. GetOrCreateDCEStatic(Ground)->Active = false;
  232. }
  233. }
  234. //---------------------------------------------------------------------------
  235. void __fastcall TForm1::GLSceneViewer1MouseMove(TObject *Sender, TShiftState Shift,
  236. int X, int Y)
  237. {
  238. //Mouse look
  239. if (Shift.Contains(ssLeft))
  240. {
  241. GLCamera1->MoveAroundTarget((my - Y), 0);
  242. Player->Turn(-(mx - X));
  243. }
  244. mx = X;
  245. my = Y;
  246. }
  247. //---------------------------------------------------------------------------
  248. void __fastcall TForm1::GLCadencer1Progress(TObject *Sender, const double deltaTime,
  249. const double newTime)
  250. {
  251. HandleKeys();
  252. HandleAnimation();
  253. //This shows the manual progress, don't need this if you use the automatic mode
  254. if (GLDCEManager1->ManualStep)
  255. GLDCEManager1->Step(deltaTime);
  256. Help->ModulateColor->Alpha = Help->ModulateColor->Alpha - (deltaTime * 0.05);
  257. if (Help->ModulateColor->Alpha < 0.25)
  258. Help->ModulateColor->Alpha = 0.25;
  259. HelpShadow->ModulateColor->Alpha = Help->ModulateColor->Alpha;
  260. HelpShadow->Text = Help->Text;
  261. }
  262. //---------------------------------------------------------------------------
  263. void __fastcall TForm1::Timer1Timer(TObject *Sender)
  264. {
  265. String s;
  266. if (GLDCEManager1->ManualStep)
  267. s = "Manual";
  268. else
  269. s = "Automatic";
  270. GLHUDText1->Text = Format("FPS: %.1f - Dynamics: %d - Statics: %d - Step mode: %s",
  271. ARRAYOFCONST((GLSceneViewer1->FramesPerSecond(),GLDCEManager1->DynamicCount,GLDCEManager1->StaticCount,s)));
  272. GLSceneViewer1->ResetPerformanceMonitor();
  273. }
  274. //---------------------------------------------------------------------------
  275. void __fastcall TForm1::PlayerBehaviours0Collision(TObject *Sender,
  276. TGLBaseSceneObject *ObjectCollided, TDCECollision &CollisionInfo)
  277. {
  278. //Use some kind of identifier to know what object you are colliding
  279. //You can use the Tag, TagFloat, Name, Class
  280. TAffineVector v;
  281. if (ObjectCollided->Tag == 1)
  282. {
  283. v = AffineVectorMake(VectorSubtract(ObjectCollided->AbsolutePosition, Player->AbsolutePosition));
  284. NormalizeVector(v);
  285. ScaleVector(v, 400.0);
  286. GetOrCreateDCEDynamic(ObjectCollided)->StopAbsAccel();
  287. GetOrCreateDCEDynamic(ObjectCollided)->ApplyAbsAccel(v);
  288. }
  289. }
  290. //---------------------------------------------------------------------------
  291. void __fastcall TForm1::GLDirectOpenGL1Render(TObject *Sender, TGLRenderContextInfo &rci)
  292. {
  293. int i;
  294. TAffineVector p, n;
  295. //To use this you will need to enable the debug define in the
  296. //GLEllipseCollision.pas, if you do, don't forget to clear the
  297. //triangle list! -> SetLength(Debug_Tri,0);
  298. rci.GLStates->PointSize = 5.0;
  299. glColor3f(0.0, 1.0, 0.0);
  300. for (i = 0; i < Debug_Tri.High; i++) {
  301. glColor3f(0.0, 0.0, 0.0);
  302. glBegin(GL_LINE_STRIP);
  303. glVertex3f(Debug_Tri[i].p1.X, Debug_Tri[i].p1.Y, Debug_Tri[i].p1.Z);
  304. glVertex3f(Debug_Tri[i].p2.X, Debug_Tri[i].p2.Y, Debug_Tri[i].p2.Z);
  305. glVertex3f(Debug_Tri[i].p3.X, Debug_Tri[i].p3.Y, Debug_Tri[i].p3.Z);
  306. glEnd;
  307. CalcPlaneNormal(Debug_Tri[i].p1, Debug_Tri[i].p2, Debug_Tri[i].p3, n);
  308. ScaleVector(n, 0.25);
  309. p.X = (Debug_Tri[i].p1.X + Debug_Tri[i].p2.X + Debug_Tri[i].p3.X) / 3.0;
  310. p.Y = (Debug_Tri[i].p1.Y + Debug_Tri[i].p2.Y + Debug_Tri[i].p3.Y) / 3.0;
  311. p.Z = (Debug_Tri[i].p1.Z + Debug_Tri[i].p2.Z + Debug_Tri[i].p3.Z) / 3.0;
  312. glColor3f(0.0, 0.0, 1.0);
  313. glBegin(GL_LINE_STRIP);
  314. glVertex3f(p.X, p.Y, p.Z);
  315. glVertex3f(p.X + n.X, p.Y + n.Y, p.Z + n.Z);
  316. glEnd;
  317. glBegin(GL_POINTS);
  318. glVertex3f(p.X + n.X, p.Y + n.Y, p.Z + n.Z);
  319. glEnd;
  320. }
  321. Debug_Tri.Length = 0.0;
  322. }
  323. //---------------------------------------------------------------------------