main.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #include <limits>
  2. #include "../../DFPSR/includeFramework.h"
  3. using namespace dsr;
  4. const String mediaPath = string_combine(U"media", file_separator());
  5. static BasicResourcePool pool(mediaPath);
  6. // Global variables
  7. std::shared_ptr<VisualComponent> mainPanel;
  8. float distance = 4.0f;
  9. bool running = true;
  10. int detailLevel = 2;
  11. bool useOrthogonalCamera = false;
  12. bool useDepthBuffer = true;
  13. // The window handle
  14. Window window;
  15. int createCubePart(Model model, const FVector3D &min, const FVector3D &max) {
  16. // Add positions
  17. model_addPoint(model, FVector3D(min.x, min.y, min.z)); // 0: Left-down-near
  18. model_addPoint(model, FVector3D(min.x, min.y, max.z)); // 1: Left-down-far
  19. model_addPoint(model, FVector3D(min.x, max.y, min.z)); // 2: Left-up-near
  20. model_addPoint(model, FVector3D(min.x, max.y, max.z)); // 3: Left-up-far
  21. model_addPoint(model, FVector3D(max.x, min.y, min.z)); // 4: Right-down-near
  22. model_addPoint(model, FVector3D(max.x, min.y, max.z)); // 5: Right-down-far
  23. model_addPoint(model, FVector3D(max.x, max.y, min.z)); // 6: Right-up-near
  24. model_addPoint(model, FVector3D(max.x, max.y, max.z)); // 7: Right-up-far
  25. // Create a part for the polygons
  26. int part = model_addEmptyPart(model, U"cube");
  27. // Polygons using default texture coordinates on the 4 corners of the texture
  28. model_addQuad(model, part, 3, 2, 0, 1); // Left quad
  29. model_addQuad(model, part, 6, 7, 5, 4); // Right quad
  30. model_addQuad(model, part, 2, 6, 4, 0); // Front quad
  31. model_addQuad(model, part, 7, 3, 1, 5); // Back quad
  32. model_addQuad(model, part, 3, 7, 6, 2); // Top quad
  33. model_addQuad(model, part, 0, 4, 5, 1); // Bottom quad
  34. return part;
  35. }
  36. Model createCubeModel(const FVector3D &min, const FVector3D &max) {
  37. Model result = model_create();
  38. createCubePart(result, min, max);
  39. return result;
  40. }
  41. int main(int argn, char **argv) {
  42. // Create a window
  43. window = window_create(U"David Piuva's Software Renderer - Cube example", 1600, 900);
  44. // Load an interface to the window
  45. window_loadInterfaceFromFile(window, mediaPath + U"interface.lof");
  46. // Tell the application to terminate when the window is closed
  47. window_setCloseEvent(window, []() {
  48. running = false;
  49. });
  50. // Get whole window key events
  51. window_setKeyboardEvent(window, [](const KeyboardEvent& event) {
  52. if (event.keyboardEventType == KeyboardEventType::KeyDown) {
  53. DsrKey key = event.dsrKey;
  54. if (key >= DsrKey_F1 && key <= DsrKey_F3) {
  55. detailLevel = key - DsrKey_F1;
  56. } else if (key >= DsrKey_1 && key <= DsrKey_9) {
  57. window_setPixelScale(window, key - DsrKey_0);
  58. } else if (key == DsrKey_F11) {
  59. window_setFullScreen(window, !window_isFullScreen(window));
  60. } else if (key == DsrKey_Escape) {
  61. running = false;
  62. }
  63. }
  64. });
  65. // Get component handles
  66. Component mainPanel = window_findComponentByName(window, U"mainPanel", true);
  67. Component buttonA = window_findComponentByName(window, U"buttonA", true);
  68. Component buttonB = window_findComponentByName(window, U"buttonB", true);
  69. // Connect components with actions
  70. component_setMouseMoveEvent(mainPanel, [](const MouseEvent& event) {
  71. distance = event.position.y / (float)window_getCanvasHeight(window) * 20.0f + 0.01f;
  72. });
  73. component_setPressedEvent(buttonA, []() {
  74. useOrthogonalCamera = !useOrthogonalCamera;
  75. });
  76. component_setPressedEvent(buttonB, []() {
  77. useDepthBuffer = !useDepthBuffer;
  78. });
  79. // Create a cube model
  80. Model cubeModel = createCubeModel(FVector3D(-0.5f), FVector3D(0.5f));
  81. model_setDiffuseMapByName(cubeModel, 0, pool, "RGB");
  82. model_setFilter(cubeModel, Filter::Alpha);
  83. // Import models
  84. // TODO: Load write protected models from a resource pool
  85. Model crateModel = importFromContent_DMF1(string_load(mediaPath + U"Model_Crate.dmf"), pool);
  86. Model barrelModel = importFromContent_DMF1(string_load(mediaPath + U"Model_Barrel.dmf"), pool);
  87. Model testModel = importFromContent_DMF1(string_load(mediaPath + U"Model_Test.dmf"), pool);
  88. // Create a renderer for multi-threading
  89. Renderer worker = renderer_create();
  90. while(running) {
  91. double startTime;
  92. window_executeEvents(window);
  93. // Request buffers after executing the events, to get newly allocated buffers after resize events
  94. auto colorBuffer = window_getCanvas(window);
  95. auto depthBuffer = window_getDepthBuffer(window);
  96. // Get target size
  97. int targetWidth = image_getWidth(colorBuffer);
  98. int targetHeight = image_getHeight(colorBuffer);
  99. // Paint the background color
  100. startTime = time_getSeconds();
  101. // TODO: Make a SIMD vectorized color fill for non-uniform bytes
  102. // Round the start location up to 16-bytes and the end location down to 16-bytes
  103. // Use regular assignments for the non-padding leftover pixels in sub-images
  104. image_fill(colorBuffer, ColorRgbaI32(160, 180, 200, 255));
  105. printText("Fill sky: ", (time_getSeconds() - startTime) * 1000.0, " ms\n");
  106. // Update the depth buffer
  107. startTime = time_getSeconds();
  108. // Clear the buffer
  109. if (useOrthogonalCamera) {
  110. image_fill(depthBuffer, std::numeric_limits<float>::infinity()); // Infinite depth
  111. } else {
  112. image_fill(depthBuffer, 0.0f); // Infinite reciprocal depth using zero
  113. }
  114. printText("Clear depth: ", (time_getSeconds() - startTime) * 1000.0, " ms\n");
  115. // Create a camera
  116. const double speed = 0.2f;
  117. double timer = time_getSeconds() * speed;
  118. FVector3D cameraPosition = FVector3D(sin(timer) * distance, 2, cos(timer) * distance);
  119. FMatrix3x3 cameraRotation = FMatrix3x3::makeAxisSystem(-cameraPosition, FVector3D(0.0f, 1.0f, 0.0f));
  120. Camera camera = useOrthogonalCamera ?
  121. Camera::createOrthogonal(Transform3D(cameraPosition, cameraRotation), targetWidth, targetHeight, 8.0f) :
  122. Camera::createPerspective(Transform3D(cameraPosition, cameraRotation), targetWidth, targetHeight);
  123. Transform3D testLocation(FVector3D(0.0f, -3.0f, 0.0f), FMatrix3x3(3.0f));
  124. Transform3D crateLocation(FVector3D(sin(timer * 0.36) * 0.21, sin(timer * 1.4) * 0.8, sin(timer * 0.43) * 0.17), FMatrix3x3(4.0f));
  125. Transform3D barrelLocation(FVector3D(sin(timer * 2.36) * 4.6, sin(timer * 3.45) * 4.6, sin(timer * 2.14 + 3.6) * 4.6), FMatrix3x3(4.0f));
  126. Transform3D cubeLocation(FVector3D(sin(timer * 4.37) * 2.6, sin(timer * 2.64) * 2.6, sin(timer * 3.34 + 2.7) * 2.6), FMatrix3x3());
  127. startTime = time_getSeconds();
  128. ImageF32 depth = useDepthBuffer ? depthBuffer : ImageF32();
  129. // Begin render batch
  130. renderer_begin(worker, colorBuffer, depth);
  131. // Solid
  132. renderer_giveTask(worker, crateModel, crateLocation, camera);
  133. renderer_giveTask(worker, barrelModel, barrelLocation, camera);
  134. renderer_giveTask(worker, testModel, testLocation, camera);
  135. // Filter
  136. renderer_giveTask(worker, cubeModel, cubeLocation, camera);
  137. // Complete render batch
  138. renderer_end(worker);
  139. printText("Draw world: ", (time_getSeconds() - startTime) * 1000.0, " ms\n");
  140. startTime = time_getSeconds();
  141. window_drawComponents(window);
  142. printText("Draw GUI: ", (time_getSeconds() - startTime) * 1000.0, " ms\n");
  143. startTime = time_getSeconds();
  144. window_showCanvas(window);
  145. printText("Show canvas: ", (time_getSeconds() - startTime) * 1000.0, " ms\n");
  146. }
  147. }