main.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. 
  2. // TODO:
  3. // Test with multiple displays and high DPI mode.
  4. #include "../../DFPSR/includeFramework.h"
  5. using namespace dsr;
  6. // Get the application folder when possible, falling back on current directory on systems not offering the feature.
  7. static const String applicationFolder = file_getApplicationFolder();
  8. static const String mediaFolder = file_combinePaths(applicationFolder, U"media");
  9. static BasicResourcePool pool(mediaFolder);
  10. // Global variables
  11. static bool running = true;
  12. static double cameraYaw = 0.0f;
  13. static double cameraPitch = 0.0f;
  14. static FVector3D cameraPosition = FVector3D(0.0f, 0.0f, 0.0f);
  15. static bool showCursor = false;
  16. // The window handle
  17. static Window window;
  18. static int createRoomPart(Model model, const FVector3D &min, const FVector3D &max) {
  19. // Add positions
  20. model_addPoint(model, FVector3D(min.x, min.y, min.z)); // 0: Left-down-near
  21. model_addPoint(model, FVector3D(min.x, min.y, max.z)); // 1: Left-down-far
  22. model_addPoint(model, FVector3D(min.x, max.y, min.z)); // 2: Left-up-near
  23. model_addPoint(model, FVector3D(min.x, max.y, max.z)); // 3: Left-up-far
  24. model_addPoint(model, FVector3D(max.x, min.y, min.z)); // 4: Right-down-near
  25. model_addPoint(model, FVector3D(max.x, min.y, max.z)); // 5: Right-down-far
  26. model_addPoint(model, FVector3D(max.x, max.y, min.z)); // 6: Right-up-near
  27. model_addPoint(model, FVector3D(max.x, max.y, max.z)); // 7: Right-up-far
  28. // Create a part for the polygons
  29. int part = model_addEmptyPart(model, U"cube");
  30. // Polygons using default texture coordinates on the 4 corners of the texture
  31. model_addQuad(model, part, 1, 0, 2, 3); // Left quad
  32. model_addQuad(model, part, 4, 5, 7, 6); // Right quad
  33. model_addQuad(model, part, 0, 4, 6, 2); // Front quad
  34. model_addQuad(model, part, 5, 1, 3, 7); // Back quad
  35. model_addQuad(model, part, 2, 6, 7, 3); // Top quad
  36. model_addQuad(model, part, 1, 5, 4, 0); // Bottom quad
  37. return part;
  38. }
  39. static Model createRoomModel(const FVector3D &min, const FVector3D &max) {
  40. Model result = model_create();
  41. createRoomPart(result, min, max);
  42. return result;
  43. }
  44. static IVector2D cursorOrigin;
  45. static int32_t cursorLimitX = 10;
  46. static int32_t cursorLimitY = 10;
  47. static IVector2D previousCursorPosition;
  48. static bool cursorWasReset = false;
  49. static bool firstMouseEvent = true;
  50. static bool moveForward = false;
  51. static bool moveBackward = false;
  52. static bool moveUp = false;
  53. static bool moveDown = false;
  54. static bool moveLeft = false;
  55. static bool moveRight = false;
  56. static bool moveFaster = false;
  57. // The maximum camera pitch in both positive and negative direction.
  58. // Goes a bit outside of 90 degrees (1.57079633 radians) just to show that it is possible when calculating the up vector instead of hardcoding it to a constant.
  59. static const double maxPitch = 1.8;
  60. // Room coordinates.
  61. static const FVector3D roomMinimum = FVector3D(-10.0f);
  62. static const FVector3D roomMaximum = FVector3D(10.0f);
  63. static const float cameraCollisionRadius = 1.0f;
  64. static const FVector3D cameraMinimum = roomMinimum + cameraCollisionRadius;
  65. static const FVector3D cameraMaximum = roomMaximum - cameraCollisionRadius;
  66. DSR_MAIN_CALLER(dsrMain)
  67. void dsrMain(List<String> args) {
  68. // Create a full-screen window
  69. window = window_create_fullscreen(U"David Piuva's Software Renderer - Camera example");
  70. // Hide the cursor
  71. window_setCursorVisibility(window, false);
  72. // Tell the application to terminate when the window is closed
  73. window_setCloseEvent(window, []() {
  74. running = false;
  75. });
  76. // Get whole window key events
  77. window_setKeyboardEvent(window, [](const KeyboardEvent& event) {
  78. DsrKey key = event.dsrKey;
  79. if (event.keyboardEventType == KeyboardEventType::KeyDown) {
  80. if (key >= DsrKey_1 && key <= DsrKey_9) {
  81. // TODO: Create a smooth transition between resolutions.
  82. window_setPixelScale(window, key - DsrKey_0);
  83. } else if (key == DsrKey_F11) {
  84. window_setFullScreen(window, !window_isFullScreen(window));
  85. } else if (key == DsrKey_Escape) {
  86. running = false;
  87. } else if (key == DsrKey_C) {
  88. // Press C to toggle visibility the cursor and debug drawing.
  89. showCursor = !showCursor;
  90. window_setCursorVisibility(window, showCursor);
  91. } else if (key == DsrKey_W) {
  92. moveForward = true;
  93. } else if (key == DsrKey_S) {
  94. moveBackward = true;
  95. } else if (key == DsrKey_E) {
  96. moveUp = true;
  97. } else if (key == DsrKey_Q) {
  98. moveDown = true;
  99. } else if (key == DsrKey_A) {
  100. moveLeft = true;
  101. } else if (key == DsrKey_D) {
  102. moveRight = true;
  103. } else if (key == DsrKey_Shift) {
  104. moveFaster = true;
  105. }
  106. } else if (event.keyboardEventType == KeyboardEventType::KeyUp) {
  107. if (key == DsrKey_W) {
  108. moveForward = false;
  109. } else if (key == DsrKey_S) {
  110. moveBackward = false;
  111. } else if (key == DsrKey_E) {
  112. moveUp = false;
  113. } else if (key == DsrKey_Q) {
  114. moveDown = false;
  115. } else if (key == DsrKey_A) {
  116. moveLeft = false;
  117. } else if (key == DsrKey_D) {
  118. moveRight = false;
  119. } else if (key == DsrKey_Shift) {
  120. moveFaster = false;
  121. }
  122. }
  123. });
  124. // Get whole window mouse events
  125. window_setMouseEvent(window, [](const MouseEvent& event) {
  126. if (firstMouseEvent) {
  127. // Ignore motion from the first mouse event, because it has no previous cursor position to compare against.
  128. firstMouseEvent = false;
  129. } else {
  130. IVector2D movement = event.position - previousCursorPosition;
  131. IVector2D offset = event.position - cursorOrigin;
  132. if (cursorWasReset && offset.x == 0 && offset.y == 0) {
  133. // The first cursor at the image center after a reset is ignored.
  134. cursorWasReset = false;
  135. } else {
  136. // TODO: Adjust mouse sensitivity somehow.
  137. double radiansPerCanvasPixel = 0.005 * double(window_getPixelScale(window));
  138. cameraYaw += double(movement.x) * radiansPerCanvasPixel;
  139. cameraPitch -= double(movement.y) * radiansPerCanvasPixel;
  140. if (cameraPitch > maxPitch) cameraPitch = maxPitch;
  141. if (cameraPitch < -maxPitch) cameraPitch = -maxPitch;
  142. if (offset.x < -cursorLimitX || offset.y < -cursorLimitY || offset.x > cursorLimitX || offset.y > cursorLimitY) {
  143. // The cursor traveled outside of the box, so it is moved to the center.
  144. if (window_setCursorPosition(window, cursorOrigin.x, cursorOrigin.y)) {
  145. // If successful, remember that the cursor was reset, so that the next mouse move event going to the center can be ignored.
  146. cursorWasReset = true;
  147. }
  148. }
  149. }
  150. }
  151. previousCursorPosition = event.position;
  152. });
  153. // Create a room model
  154. Model roomModel = createRoomModel(roomMinimum, roomMaximum);
  155. model_setDiffuseMapByName(roomModel, 0, pool, "Grid");
  156. // Create a renderer for multi-threading
  157. Renderer worker = renderer_create();
  158. double lastTime = 0.0;
  159. while(running) {
  160. // Measure time
  161. double time = time_getSeconds();
  162. double timePerFrame = time - lastTime;
  163. // Fetch mouse and keyboard events from the window.
  164. window_executeEvents(window);
  165. // Request buffers after executing the events, to get newly allocated buffers after resize events
  166. ImageRgbaU8 colorBuffer = window_getCanvas(window);
  167. // For a sky box, the depth buffer can be replaced with ImageF32() as an empty image handle.
  168. //ImageF32 depthBuffer;
  169. // But for demonstration purposes, we render the room using a depth buffer.
  170. ImageF32 depthBuffer = window_getDepthBuffer(window);
  171. // Get target size
  172. int targetWidth = image_getWidth(colorBuffer);
  173. int targetHeight = image_getHeight(colorBuffer);
  174. // Reset the mouse to the center of the canvas when getting too far out.
  175. cursorOrigin = IVector2D(targetWidth / 2, targetHeight / 2);
  176. cursorLimitX = targetWidth / 4;
  177. cursorLimitY = targetHeight / 4;
  178. // No need to paint the background for indoor scenes, because everything will be covered by geometry.
  179. // Any gap will be undefined behavior because some window backends use double buffering while others do not.
  180. // One way to make sure that this does not happen, is to let the debug version clear the color with a flashing color while looking for holes.
  181. //image_fill(colorBuffer, ColorRgbaI32(0, 0, 0, 0));
  182. // Clear the depth buffer
  183. image_fill(depthBuffer, 0.0f); // Infinite reciprocal depth using zero
  184. // Calculate camera orientation from pitch and yaw in radians.
  185. FVector3D cameraForwardDirection = FVector3D(sin(cameraYaw) * cos(cameraPitch), sin(cameraPitch), cos(cameraYaw) * cos(cameraPitch));
  186. FVector3D cameraUpDirection = FVector3D(-sin(cameraYaw) * sin(cameraPitch), cos(cameraPitch), -cos(cameraYaw) * sin(cameraPitch));
  187. FMatrix3x3 cameraRotation = FMatrix3x3::makeAxisSystem(cameraForwardDirection, cameraUpDirection);
  188. Camera camera = Camera::createPerspective(Transform3D(cameraPosition, cameraRotation), targetWidth, targetHeight);
  189. // Move the camera.
  190. double speed = moveFaster ? 40.0 : 10.0;
  191. double moveOffset = speed * timePerFrame;
  192. if (moveForward) cameraPosition = cameraPosition + cameraForwardDirection * moveOffset;
  193. if (moveBackward) cameraPosition = cameraPosition - cameraForwardDirection * moveOffset;
  194. if (moveUp) cameraPosition = cameraPosition + cameraUpDirection * moveOffset;
  195. if (moveDown) cameraPosition = cameraPosition - cameraUpDirection * moveOffset;
  196. if (moveLeft) cameraPosition = cameraPosition - cameraRotation.xAxis * moveOffset;
  197. if (moveRight) cameraPosition = cameraPosition + cameraRotation.xAxis * moveOffset;
  198. // Collide against walls.
  199. if (cameraPosition.x < cameraMinimum.x) cameraPosition.x = cameraMinimum.x;
  200. if (cameraPosition.y < cameraMinimum.y) cameraPosition.y = cameraMinimum.y;
  201. if (cameraPosition.z < cameraMinimum.z) cameraPosition.z = cameraMinimum.z;
  202. if (cameraPosition.x > cameraMaximum.x) cameraPosition.x = cameraMaximum.x;
  203. if (cameraPosition.y > cameraMaximum.y) cameraPosition.y = cameraMaximum.y;
  204. if (cameraPosition.z > cameraMaximum.z) cameraPosition.z = cameraMaximum.z;
  205. // Begin render batch
  206. renderer_begin(worker, colorBuffer, depthBuffer);
  207. // Projected triangles from the room's model
  208. renderer_giveTask(worker, roomModel, Transform3D(), camera);
  209. // Render the projected triangles
  210. renderer_end(worker);
  211. // Debug draw the camera rotation system, which is toggled using the C button.
  212. if (showCursor) {
  213. IVector2D writer = IVector2D(10, 10);
  214. font_printLine(colorBuffer, font_getDefault(), string_combine(U"cameraYaw = ", cameraYaw, U" radians"), writer, ColorRgbaI32(255, 255, 255, 255)); writer.y += 20;
  215. font_printLine(colorBuffer, font_getDefault(), string_combine(U"cameraPitch = ", cameraPitch, U" radians"), writer, ColorRgbaI32(255, 255, 255, 255)); writer.y += 20;
  216. font_printLine(colorBuffer, font_getDefault(), string_combine(U"forward = ", cameraForwardDirection), writer, ColorRgbaI32(255, 255, 255, 255)); writer.y += 20;
  217. font_printLine(colorBuffer, font_getDefault(), string_combine(U"up = ", cameraUpDirection), writer, ColorRgbaI32(255, 255, 255, 255)); writer.y += 20;
  218. // Draw the region that the cursor can move within without jumping to the center.
  219. int32_t left = cursorOrigin.x - cursorLimitX;
  220. int32_t right = cursorOrigin.x + cursorLimitX;
  221. int32_t top = cursorOrigin.y - cursorLimitY;
  222. int32_t bottom = cursorOrigin.y + cursorLimitY;
  223. draw_line(colorBuffer, left, top, right, top, ColorRgbaI32(255, 255, 255, 255));
  224. draw_line(colorBuffer, left, bottom, right, bottom, ColorRgbaI32(255, 255, 255, 255));
  225. draw_line(colorBuffer, left, top, left, bottom, ColorRgbaI32(255, 255, 255, 255));
  226. draw_line(colorBuffer, right, top, right, bottom, ColorRgbaI32(255, 255, 255, 255));
  227. } else {
  228. int32_t crosshairRadius = cursorLimitY / 16;
  229. int32_t left = cursorOrigin.x - crosshairRadius;
  230. int32_t right = cursorOrigin.x + crosshairRadius;
  231. int32_t top = cursorOrigin.y - crosshairRadius;
  232. int32_t bottom = cursorOrigin.y + crosshairRadius;
  233. draw_line(colorBuffer, left, cursorOrigin.y, right, cursorOrigin.y, ColorRgbaI32(255, 255, 255, 255));
  234. draw_line(colorBuffer, cursorOrigin.x, top, cursorOrigin.x, bottom, ColorRgbaI32(255, 255, 255, 255));
  235. }
  236. // Upload canvas to window.
  237. window_showCanvas(window);
  238. lastTime = time;
  239. }
  240. }