Unit1.cpp 11 KB

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