PageRenderTime 48ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/samples/3D/terrainCameraDemo/demo.ec

https://github.com/thexa4/sdk
C | 432 lines | 347 code | 57 blank | 28 comment | 37 complexity | 8ac985ffc8a151cfbd43b01359ff356b MD5 | raw file
  1. /****************************************************************************
  2. Ecere 3D Engine Demonstration
  3. Copyright (c) 2001-2010 Jerome Jacovella-St-Louis
  4. All Rights Reserved.
  5. demo.ec - Main Module
  6. ****************************************************************************/
  7. import "ecere"
  8. import "engineSettings"
  9. import "dted"
  10. import "dna"
  11. define BACKGROUND = white;
  12. define speed = 200;
  13. class Scene : Window
  14. {
  15. bool filled;
  16. Camera camera
  17. {
  18. //type = attached;
  19. type = attachedQuaternion;
  20. position = { 0, 0, -10 };
  21. orientation = Euler { 180, 20, 0 };
  22. zMax = 340000;
  23. };
  24. Light light;
  25. Terrain terrain { };
  26. Bitmap textures[16];
  27. Bitmap detail { };
  28. Object player { };
  29. SkyBox sky { size = { 100, 100, 100 }, folder = ":skycube", extension = "pcx" };
  30. DNAModel dna
  31. {
  32. numBases = 24;
  33. space = 60;
  34. rotation = 22.5; //Pi/8;
  35. helixCurveSegments = 48;
  36. helixWidth = 50;
  37. hydrogenWidth = 40;
  38. baseWidth = 180;
  39. baseHeight = 40;
  40. baseDepth = 40;
  41. desoxyriboseWidth = 40;
  42. textureFile = ":texture1.pcx";
  43. };
  44. /*
  45. Cube dna
  46. {
  47. size = { 100, 100, 100 };
  48. };*/
  49. Object water { };
  50. Material groundMaterial;
  51. TerrainMesh terrainMesh { terrain = terrain };
  52. float fogDensity;
  53. float detailBias;
  54. float zMax;
  55. double lastTime;
  56. FPS frameRate;
  57. int frame;
  58. Point lastMousePosition, newMousePosition;
  59. bool leftDown, rightDown;
  60. bool acquiredInput;
  61. borderStyle = sizable;
  62. hasMaximize = true;
  63. hasMinimize = true;
  64. hasClose = true;
  65. text = "3D Camera & Terrain Demo";
  66. size = { 640, 480 };
  67. Scene()
  68. {
  69. LoadDTEDTerrain(terrain, ":N19.DT1");
  70. frame = 0;
  71. fogDensity = 0.0001f;
  72. detailBias = 400;//1000;
  73. //detailBias = 0;
  74. //zMax = 100000; //340000;
  75. zMax = 340000;
  76. filled = true;
  77. newMousePosition = lastMousePosition = { MAXINT, MAXINT };
  78. }
  79. Timer timer
  80. {
  81. this, delay = 0.01;
  82. bool DelayExpired()
  83. {
  84. Elevation height;
  85. int x, y;
  86. MouseButtons b;
  87. static bool firstTime = true;
  88. double currentTime = GetTime();
  89. double diffTime = currentTime - lastTime;
  90. lastTime = currentTime;
  91. if(dna)
  92. {
  93. Quaternion orientation = dna.transform.orientation;
  94. orientation.RotateYawPitch(Degrees { (float)30*diffTime }, 0);
  95. dna.transform.orientation = orientation;
  96. dna.UpdateTransform();
  97. }
  98. //light.orientation.RotatePitch(Radians { 0.02f });
  99. if(acquiredInput)
  100. ((GuiApplication)__thisModule).GetMouseState(&b, &x, &y);
  101. else
  102. {
  103. if(lastMousePosition.x != MAXINT)
  104. {
  105. x = newMousePosition.x - lastMousePosition.x;
  106. y = newMousePosition.y - lastMousePosition.y;
  107. lastMousePosition = newMousePosition;
  108. }
  109. else
  110. {
  111. x = y = 0;
  112. }
  113. b = 0;
  114. if(leftDown) b.left = true;
  115. if(rightDown) b.right = true;
  116. }
  117. if(player)
  118. {
  119. player.Animate(frame);
  120. frame ++;
  121. if(frame == 100) frame = 0;
  122. //player.RotateEuler({ Radians { -x / 60.0f }, Radians { -y / 60.0f } }, { pitch = -90 }, { pitch = 90 });
  123. //player.RotateEuler({ roll = Radians { x / 60.0f }, pitch = Radians { y / 60.0f } }, null, null);
  124. //player.Rotate({ roll = Radians { x / 60.0f }, pitch = Radians { y / 60.0f } });
  125. {
  126. Quaternion a = player.transform.orientation;
  127. a.RotateRoll(Radians { x / 60.0f });
  128. a.RotatePitch(Radians { y / 60.0f });
  129. player.transform.orientation = a;
  130. player.UpdateTransform();
  131. }
  132. if(b.right)
  133. player.Move({ 0, 0,-(float)diffTime * (camera.type == attachedQuaternion ? 10 : 1) * speed });
  134. else if(b.left)
  135. player.Move({ 0, 0, (float)diffTime * (camera.type == attachedQuaternion ? 10 : 1) * speed });
  136. height = (int)-terrain.GetElevation(
  137. (float)player.transform.position.x,
  138. (float)player.transform.position.z);
  139. if((float)player.transform.position.y > height - 100)
  140. {
  141. player.transform.position.y = height - 100.0f;
  142. player.UpdateTransform();
  143. }
  144. }
  145. if(firstTime || x || y || b)
  146. {
  147. firstTime = false;
  148. camera.Slerp(0.3f);
  149. }
  150. if(camera.Update())
  151. {
  152. display.Lock(false);
  153. terrainMesh.OptimizeMesh(camera, detailBias, zMax);
  154. display.Unlock();
  155. }
  156. Update(null);
  157. return true;
  158. }
  159. };
  160. void OnPosition(int x, int y, int w, int h)
  161. {
  162. camera.Setup(w, h, null);
  163. camera.Update(); // This wasn't necessary before!
  164. display.Lock(false);
  165. terrainMesh.OptimizeMesh(camera, detailBias, zMax);
  166. display.Unlock();
  167. }
  168. bool OnPostCreate()
  169. {
  170. timer.Start();
  171. acquiredInput = AcquireInput(true);
  172. return true;
  173. }
  174. bool OnMouseOver(int x, int y, Modifiers mods)
  175. {
  176. lastMousePosition = { x, y };
  177. newMousePosition = { x, y };
  178. return true;
  179. }
  180. bool OnMouseLeave(Modifiers mods)
  181. {
  182. lastMousePosition = { MAXINT, MAXINT };
  183. newMousePosition = { MAXINT, MAXINT };
  184. return true;
  185. }
  186. bool OnMouseMove(int x, int y, Modifiers mods)
  187. {
  188. newMousePosition = { x, y };
  189. return true;
  190. }
  191. bool OnLeftButtonDown(int x, int y, Modifiers mods)
  192. {
  193. leftDown = true;
  194. Capture();
  195. return true;
  196. }
  197. bool OnLeftButtonUp(int x, int y, Modifiers mods)
  198. {
  199. if(leftDown && !rightDown)
  200. ReleaseCapture();
  201. leftDown = false;
  202. return true;
  203. }
  204. bool OnRightButtonDown(int x, int y, Modifiers mods)
  205. {
  206. rightDown = true;
  207. Capture();
  208. return true;
  209. }
  210. bool OnRightButtonUp(int x, int y, Modifiers mods)
  211. {
  212. if(rightDown && !leftDown)
  213. ReleaseCapture();
  214. rightDown = false;
  215. return true;
  216. }
  217. bool OnLoadGraphics()
  218. {
  219. Bitmap map { };
  220. light.ambient = { 0.2f, 0.2f, 0.2f };
  221. light.diffuse = light.specular = white;
  222. light.orientation = { 1,0,0,0 };
  223. light.orientation.RotatePitch(30);
  224. if(map.Load(":dted.pcx", null, null))
  225. {
  226. int c;
  227. int across = 4;
  228. map.SmoothEdges(256);
  229. for(c = 0; c < across * across; c++)
  230. {
  231. textures[c] = { };
  232. textures[c].Allocate(null, 256, 256, 0, map.pixelFormat, true);
  233. if(map.pixelFormat == pixelFormat8)
  234. memcpy(textures[c].palette, map.palette, 256*4);
  235. textures[c].Grab(map,
  236. (c % across) * textures[c].width,
  237. (c / across) * textures[c].height);
  238. textures[c].MakeMipMaps(displaySystem);
  239. }
  240. }
  241. delete map;
  242. //player.Load(":beholder/beholder.3ds", null, displaySystem);
  243. player.Load(":cesna.3ds", null, displaySystem);
  244. camera.target = player;
  245. dna.Create(displaySystem);
  246. if(dna)
  247. dna.transform.position.y = -3000;
  248. dna.UpdateTransform();
  249. if(player)
  250. {
  251. player.transform.position.y = -3000;
  252. player.transform.position.x = 1000;
  253. player.transform.orientation = Euler { yaw = 230 };
  254. }
  255. detail.Load(":detail.pcx", null, displaySystem);
  256. displaySystem.AddTexture("DetailTexture", detail);
  257. sky.InitializeMesh(displaySystem);
  258. sky.Create(displaySystem);
  259. terrainMesh.CreateMesh(terrain, 8192*16, 1024, 1024, false, false, displaySystem);
  260. lastTime = GetTime();
  261. frameRate.Start();
  262. return true;
  263. }
  264. void OnUnloadGraphics()
  265. {
  266. int c;
  267. for(c = 0; c<16; c++)
  268. delete textures[c];
  269. terrainMesh.FreeMesh();
  270. }
  271. void OnRedraw(Surface surface)
  272. {
  273. surface.SetBackground(BACKGROUND);
  274. surface.Clear(depthBuffer);
  275. display.SetCamera(surface, camera);
  276. display.SetLight(0, light);
  277. display.fogColor = BACKGROUND;
  278. display.fogDensity = fogDensity;
  279. //display.fogDensity = 0.0000000001f;
  280. sky.Render(camera, display);
  281. if(!filled) display.fillMode = wireframe;
  282. terrainMesh.RenderMesh(display, null, textures);
  283. if(!filled) display.fillMode = solid;
  284. display.DrawObject(dna);
  285. display.DrawObject(player);
  286. display.SetCamera(surface, null);
  287. surface.SetForeground(black);
  288. //surface.WriteTextf(10,10, "Detail: %.0f, ZMax: %.02f, FPS: %.02f", detailBias, zMax, frameRate.fps);
  289. frameRate.Step();
  290. }
  291. bool OnKeyDown(Key key, unichar ch)
  292. {
  293. switch(key)
  294. {
  295. case escape:
  296. Destroy(0);
  297. return false;
  298. case w: filled = false; break;
  299. case f: filled = true; break;
  300. case u:
  301. AcquireInput(false);
  302. acquiredInput = false;
  303. GetMousePosition(&lastMousePosition.x, &lastMousePosition.y);
  304. break;
  305. case a:
  306. acquiredInput = AcquireInput(true);
  307. break;
  308. case k0:
  309. camera.type = fixed;
  310. camera.position = camera.cPosition;
  311. camera.orientation = camera.cOrientation;
  312. break;
  313. case k1:
  314. /*camera.type = attached;
  315. camera.position = { 0, 0, -30 };
  316. camera.Slerp(0.1f);
  317. */
  318. camera.type = attachedQuaternion;
  319. camera.position = { 0, 0, -10 };
  320. camera.orientation = Euler { 180, 20, 0 };
  321. camera.Slerp(0.3f);
  322. break;
  323. case k2:
  324. camera.type = lookAt;
  325. camera.position = camera.cPosition; //{ 1000, -3000, 0 };
  326. camera.position.z -= 50;
  327. camera.orientation = { 1,0,0,0 };
  328. camera.Slerp(0.1f);
  329. break;
  330. }
  331. return true;
  332. }
  333. bool OnKeyHit(Key key, unichar ch)
  334. {
  335. float detailBias = this.detailBias;
  336. float zMax = this.zMax;
  337. switch(key)
  338. {
  339. case pageUp: detailBias++; break;
  340. case pageDown: detailBias--; break;
  341. case home: zMax *= 0.9f; break;
  342. case end: zMax /= 0.9f; break;
  343. case keyPadPlus: camera.position.z /= 0.9f; break;
  344. case keyPadMinus: camera.position.z *= 0.9f; break;
  345. case f7: camera.RotateYaw(-0.02f, 0, 0); break;
  346. case f8: camera.RotateYaw(0.02f, 0, 0); break;
  347. case f9: camera.RotatePitch(-0.02f, 0, 0); break;
  348. case f10:camera.RotatePitch( 0.02f, 0, 0); break;
  349. }
  350. if(this.detailBias != detailBias || this.zMax != zMax)
  351. {
  352. this.detailBias = Max(0, detailBias);
  353. this.zMax = Min(zMax, 340000);
  354. display.Lock(false);
  355. terrainMesh.OptimizeMesh(camera, this.detailBias, this.zMax);
  356. display.Unlock();
  357. }
  358. return true;
  359. }
  360. }
  361. class CameraDemoApp : GuiApplication
  362. {
  363. driver = "OpenGL";
  364. timerResolution = 60;
  365. void Main()
  366. {
  367. //if(EngineSettings { }.Modal())
  368. {
  369. scene.Modal();
  370. }
  371. }
  372. }
  373. Scene scene { autoCreate = false };