/samples/3D/TransCube/transCube.ec

https://github.com/redj/ecere-sdk · C · 215 lines · 181 code · 31 blank · 3 comment · 18 complexity · ebb3abd0e26a2174327bce99d6d133f9 MD5 · raw file

  1. import "ecere"
  2. ModelViewer modelViewer { };
  3. class eModelApp : GuiApplication
  4. {
  5. driver = "OpenGL";
  6. //driver = "Direct3D8";
  7. //driver = "Direct3D";
  8. }
  9. class ModelViewer : Window
  10. {
  11. text = "Transparent Cube Demo";
  12. background = black;
  13. borderStyle = sizable;
  14. hasMaximize = true;
  15. hasMinimize = true;
  16. hasClose = true;
  17. anchor = { left = 0, top = 0, right = 0, bottom = 0 };
  18. Camera camera
  19. {
  20. attachedQuaternion,
  21. position = Vector3D { 0, 0, -250 },
  22. orientation = Euler { 120, 30, 0 },
  23. zMin = 0.01f;
  24. fov = 53;
  25. };
  26. Cube model { size = { 100, 100, 100 } };
  27. bool moving, lightMoving;
  28. Point startPosition;
  29. Euler startOrientation;
  30. Light light
  31. {
  32. diffuse = white;
  33. specular = white;
  34. // orientation = Euler { pitch = 50, yaw = 45 };
  35. orientation = Euler { pitch = 10, yaw = 30 };
  36. };
  37. void OnUnloadGraphics()
  38. {
  39. displaySystem.ClearMaterials();
  40. displaySystem.ClearTextures();
  41. displaySystem.ClearMeshes();
  42. }
  43. void OnResize(int w, int h)
  44. {
  45. camera.Setup(w, h, null);
  46. Update(null);
  47. }
  48. bool OnLeftButtonDown(int x, int y, Modifiers mods)
  49. {
  50. if(!moving && !lightMoving)
  51. {
  52. startPosition.x = x;
  53. startPosition.y = y;
  54. startOrientation = camera.orientation;
  55. Capture();
  56. moving = true;
  57. }
  58. return true;
  59. }
  60. bool OnLeftButtonUp(int x, int y, Modifiers mods)
  61. {
  62. if(moving)
  63. {
  64. ReleaseCapture();
  65. moving = false;
  66. }
  67. return true;
  68. }
  69. bool OnRightButtonDown(int x, int y, Modifiers mods)
  70. {
  71. if(!moving && !lightMoving)
  72. {
  73. startPosition.x = x;
  74. startPosition.y = y;
  75. startOrientation = light.orientation;
  76. Capture();
  77. lightMoving = true;
  78. }
  79. return true;
  80. }
  81. bool OnRightButtonUp(int x, int y, Modifiers mods)
  82. {
  83. if(lightMoving)
  84. {
  85. ReleaseCapture();
  86. lightMoving = false;
  87. }
  88. return true;
  89. }
  90. bool OnMouseMove(int x, int y, Modifiers mods)
  91. {
  92. if(moving)
  93. {
  94. Euler euler
  95. {
  96. startOrientation.yaw - (x - startPosition.x),
  97. startOrientation.pitch + (y - startPosition.y),
  98. startOrientation.roll
  99. };
  100. camera.orientation = euler;
  101. Update(null);
  102. }
  103. else if(lightMoving)
  104. {
  105. light.orientation = Euler
  106. {
  107. startOrientation.yaw - (x - startPosition.x),
  108. startOrientation.pitch + (y - startPosition.y),
  109. startOrientation.roll
  110. };
  111. Update(null);
  112. }
  113. return true;
  114. }
  115. bool OnKeyDown(Key key, unichar ch)
  116. {
  117. switch(key)
  118. {
  119. case escape: Destroy(0); return false;
  120. }
  121. return true;
  122. }
  123. bool OnKeyHit(Key key, unichar ch)
  124. {
  125. switch((SmartKey) key)
  126. {
  127. case wheelDown: case minus: camera.position.z *= 1.1111111f; Update(null); break;
  128. case wheelUp: case plus: camera.position.z *= 0.9f; Update(null); break;
  129. }
  130. return true;
  131. }
  132. bool OnLoadGraphics()
  133. {
  134. if(model.Create(displaySystem))
  135. {
  136. if(model)
  137. {
  138. int c;
  139. for(c = 0; c<6; c++)
  140. {
  141. PrimitiveGroup group;
  142. Material material;
  143. char name[20];
  144. sprintf(name, "Cube Face %d", c+1);
  145. material = displaySystem.GetMaterial(name);
  146. if(material)
  147. {
  148. material.flags = { noFog = true, doubleSided = true, translucent = true };
  149. material.opacity = 0.5f;
  150. material.diffuse.r = material.diffuse.g = material.diffuse.b = 1;
  151. material.ambient = material.diffuse;
  152. material.baseMap = displaySystem.GetTexture(name);
  153. if(!material.baseMap)
  154. {
  155. material.baseMap = Bitmap { };
  156. sprintf(name, "tex%d.bmp", c+1);
  157. if(c == 3) strcpy(name, "glass.bmp");
  158. material.baseMap.LoadMipMaps(name, null, displaySystem);
  159. displaySystem.AddTexture(name, material.baseMap);
  160. }
  161. }
  162. }
  163. model.mesh.ApplyTranslucency(model);
  164. camera.target = model;
  165. }
  166. return true;
  167. }
  168. else
  169. return false;
  170. }
  171. void OnRedraw(Surface surface)
  172. {
  173. display.fogColor = blue;
  174. display.fogDensity = 0.000001f;
  175. surface.Clear(depthBuffer);
  176. camera.Update();
  177. display.antiAlias = true;
  178. display.SetLight(0, light);
  179. display.SetCamera(surface, camera);
  180. display.ambient = ColorRGB { 0.2f, 0.2f, 0.2f };
  181. display.DrawObject(model);
  182. display.SetCamera(surface, null);
  183. }
  184. }