/samples/3D/ColorSpheres/colorSpheres.ec

https://github.com/redj/ecere-sdk · C · 228 lines · 196 code · 32 blank · 0 comment · 22 complexity · 086209d9131b8086bc875dcc6a869290 MD5 · raw file

  1. #ifdef ECERE_STATIC
  2. import static "ecere"
  3. #else
  4. import "ecere"
  5. #endif
  6. define numSpheres = 4;
  7. define spacing = 0.75f;
  8. class ColorSpheresApp : GuiApplication
  9. {
  10. driver = "OpenGL";
  11. timerResolution = 60;
  12. bool Cycle(bool idle)
  13. {
  14. test3D.UpdateCube();
  15. return true;
  16. }
  17. };
  18. Camera camera
  19. {
  20. fixed,
  21. position = { 0, 0, -405 - 200/((numSpheres-1) * (1+spacing)) },
  22. orientation = Euler { 0, 0, 0 },
  23. fov = 53;
  24. };
  25. Light diffuseLight
  26. {
  27. multiplier = 0.5f;
  28. diffuse = white;
  29. orientation = Euler { 0,0 }
  30. };
  31. Object specularLight
  32. {
  33. transform = { scaling = { 1, 1, 1 } };
  34. flags = { light = true };
  35. light =
  36. {
  37. multiplier = 1;
  38. diffuse = white;
  39. specular = white;
  40. lightObject = specularLight;
  41. };
  42. };
  43. class ColorSpheres : Object
  44. {
  45. Sphere sphere { numLat = (int)(50 * 2 / (numSpheres-1)), numLon = (int)(50 * 2 / (numSpheres-1)) };
  46. transform.scaling = { 100, 100, 100 };
  47. void Create(DisplaySystem displaySystem)
  48. {
  49. float x, y, z;
  50. sphere.Create(displaySystem);
  51. for(x = -1; x <= 1; x += 2.0f / (numSpheres-1))
  52. for(y = -1; y <= 1; y += 2.0f / (numSpheres-1))
  53. for(z = -1; z <= 1; z += 2.0f / (numSpheres-1))
  54. {
  55. Object object { };
  56. float scaling = 1.0f / ((numSpheres-1) * (1+spacing));
  57. object.Duplicate(sphere);
  58. object.transform.position = { x, y, z };
  59. object.transform.scaling = { scaling, scaling, scaling };
  60. object.material = Material
  61. {
  62. opacity = 1,
  63. diffuse = ColorRGB { (x + 1) / 2.0f, (y + 1) / 2.0f, (z + 1) / 2.0f };
  64. ambient = ColorRGB { (x + 1) / 2.0f, (y + 1) / 2.0f, (z + 1) / 2.0f };
  65. specular = white;
  66. power = 32.0f;
  67. };
  68. displaySystem.AddMaterial(object.material);
  69. object.mesh.ApplyTranslucency(object);
  70. AddName(object, null);
  71. }
  72. UpdateTransform();
  73. }
  74. }
  75. class Test3D : Window
  76. {
  77. text = "Cube";
  78. borderStyle = none;
  79. moveable = true;
  80. stayOnTop = true;
  81. clientSize = { 400, 400 };
  82. background = 0;
  83. opacity = 0;
  84. alphaBlend = true;
  85. ColorSpheres cube { };
  86. Euler spin { };
  87. bool UpdateCube()
  88. {
  89. static Time lastTime = 0;
  90. Time time = GetTime(), diffTime = lastTime ? (time - lastTime) : 0;
  91. if(spin.yaw || spin.pitch)
  92. {
  93. int signYaw = 1, signPitch = 1;
  94. Radians yaw = spin.yaw, pitch = spin.pitch;
  95. Quaternion orientation = cube.transform.orientation;
  96. Euler tSpin { yaw * (double)diffTime, pitch * (double)diffTime, 0 };
  97. Quaternion thisSpin = tSpin, temp;
  98. if(yaw < 0) { yaw = -yaw; signYaw = -1; }
  99. if(pitch < 0) { pitch = -pitch; signPitch = -1; }
  100. yaw -= (double)diffTime / 3 * yaw;
  101. pitch -= (double)diffTime / 3 * pitch;
  102. if(yaw < 0.0001) yaw = 0;
  103. if(pitch < 0.0001) pitch = 0;
  104. spin.yaw = yaw * signYaw;
  105. spin.pitch = pitch * signPitch;
  106. temp.Multiply(orientation, thisSpin);
  107. orientation.Normalize(temp);
  108. cube.transform.orientation = orientation;
  109. cube.UpdateTransform();
  110. }
  111. Update(null);
  112. lastTime = time;
  113. return true;
  114. }
  115. bool OnLoadGraphics()
  116. {
  117. cube.Create(displaySystem);
  118. return true;
  119. }
  120. void OnResize(int w, int h)
  121. {
  122. camera.Setup(w, h, null);
  123. }
  124. void OnRedraw(Surface surface)
  125. {
  126. int x, y;
  127. surface.Clear(colorAndDepth);
  128. camera.Update();
  129. GetMousePosition(&x, &y);
  130. specularLight.transform.position =
  131. {
  132. (x - clientSize.w / 2) * 400 / clientSize.w,
  133. (y - clientSize.h / 2) * 400 / clientSize.h,
  134. - 2 * cube.transform.scaling.z * (1 + 1.0f / ((numSpheres - 1) * (1+spacing)))
  135. };
  136. specularLight.UpdateTransform();
  137. display.SetCamera(surface, camera);
  138. display.SetLight(0, diffuseLight);
  139. display.SetLight(1, specularLight.light);
  140. display.fogDensity = 0;
  141. display.DrawObject(cube);
  142. display.SetCamera(surface, null);
  143. }
  144. Point startClick;
  145. bool moving;
  146. Time clickTime;
  147. bool OnLeftButtonDown(int x, int y, Modifiers mods)
  148. {
  149. clickTime = GetTime();
  150. Capture();
  151. startClick = { x, y };
  152. moving = true;
  153. return true;
  154. }
  155. bool OnLeftButtonUp(int x, int y, Modifiers mods)
  156. {
  157. if(moving)
  158. {
  159. ReleaseCapture();
  160. moving = false;
  161. }
  162. return true;
  163. }
  164. bool OnMouseMove(int x, int y, Modifiers mods)
  165. {
  166. if(moving)
  167. {
  168. Time time = GetTime(), diffTime = Max(time - clickTime, 0.01);
  169. spin.yaw += Degrees { (x - startClick.x) / (25.0 * (double)diffTime) };
  170. spin.pitch += Degrees { (startClick.y - y) / (25.0 * (double)diffTime) };
  171. startClick = { x, y };
  172. clickTime = time;
  173. }
  174. return true;
  175. }
  176. bool OnKeyHit(Key key, unichar ch)
  177. {
  178. if(key == wheelDown || key == wheelUp)
  179. {
  180. int w = size.w;
  181. if(key == wheelDown)
  182. w /= 1.1;
  183. else if(key == wheelUp)
  184. w *= 1.1;
  185. Move(position.x + size.w / 2 - w / 2, position.y + size.h / 2 - w / 2, w, w);
  186. }
  187. Update(null);
  188. return true;
  189. }
  190. bool OnKeyDown(Key key, unichar ch)
  191. {
  192. if(key == escape) Destroy(0);
  193. return true;
  194. }
  195. }
  196. Test3D test3D {};