main.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. 
  2. #include <limits>
  3. #include "../../DFPSR/includeFramework.h"
  4. using namespace dsr;
  5. // Global variables
  6. bool running = true;
  7. // The window handle
  8. Window window;
  9. // Textures for 3D models must use power-of-two dimensions
  10. AlignedImageU8 darkEdge = image_fromAscii(
  11. "< .-x>"
  12. "<xxxxxxxxxxxxxxxx>"
  13. "<x--------------x>"
  14. "<x-............-x>"
  15. "<x-. .-x>"
  16. "<x-. .-x>"
  17. "<x-. .-x>"
  18. "<x-. .-x>"
  19. "<x-. .-x>"
  20. "<x-. .-x>"
  21. "<x-. .-x>"
  22. "<x-. .-x>"
  23. "<x-. .-x>"
  24. "<x-. .-x>"
  25. "<x-............-x>"
  26. "<x--------------x>"
  27. "<xxxxxxxxxxxxxxxx>"
  28. );
  29. TextureRgbaU8 myTexture = texture_create_RgbaU8(image_pack(darkEdge, darkEdge, 0, 255), 1);
  30. int createCubePart(Model model, const FVector3D &min, const FVector3D &max) {
  31. // Add positions
  32. model_addPoint(model, FVector3D(min.x, min.y, min.z)); // 0: Left-down-near
  33. model_addPoint(model, FVector3D(min.x, min.y, max.z)); // 1: Left-down-far
  34. model_addPoint(model, FVector3D(min.x, max.y, min.z)); // 2: Left-up-near
  35. model_addPoint(model, FVector3D(min.x, max.y, max.z)); // 3: Left-up-far
  36. model_addPoint(model, FVector3D(max.x, min.y, min.z)); // 4: Right-down-near
  37. model_addPoint(model, FVector3D(max.x, min.y, max.z)); // 5: Right-down-far
  38. model_addPoint(model, FVector3D(max.x, max.y, min.z)); // 6: Right-up-near
  39. model_addPoint(model, FVector3D(max.x, max.y, max.z)); // 7: Right-up-far
  40. // Create a part for the polygons
  41. int part = model_addEmptyPart(model, U"cube");
  42. // Polygons using default texture coordinates on the 4 corners of the texture
  43. model_addQuad(model, part, 3, 2, 0, 1); // Left quad
  44. model_addQuad(model, part, 6, 7, 5, 4); // Right quad
  45. model_addQuad(model, part, 2, 6, 4, 0); // Front quad
  46. model_addQuad(model, part, 7, 3, 1, 5); // Back quad
  47. model_addQuad(model, part, 3, 7, 6, 2); // Top quad
  48. model_addQuad(model, part, 0, 4, 5, 1); // Bottom quad
  49. return part;
  50. }
  51. Model createCubeModel(const FVector3D &min, const FVector3D &max) {
  52. Model result = model_create();
  53. createCubePart(result, min, max);
  54. return result;
  55. }
  56. DSR_MAIN_CALLER(dsrMain)
  57. void dsrMain(List<String> args) {
  58. // Create a window
  59. window = window_create_fullscreen(U"Basic 3D template");
  60. // Hide the cursor
  61. window_setCursorVisibility(window, false);
  62. // Tell the application to terminate when the window is closed
  63. window_setCloseEvent(window, []() {
  64. running = false;
  65. });
  66. // Get whole window key events
  67. window_setKeyboardEvent(window, [](const KeyboardEvent& event) {
  68. if (event.keyboardEventType == KeyboardEventType::KeyDown) {
  69. DsrKey key = event.dsrKey;
  70. if (key >= DsrKey_1 && key <= DsrKey_9) {
  71. window_setPixelScale(window, key - DsrKey_0);
  72. } else if (key == DsrKey_F11) {
  73. bool makeWindowed = window_isFullScreen(window);
  74. window_setFullScreen(window, !makeWindowed);
  75. window_setCursorVisibility(window, makeWindowed);
  76. } else if (key == DsrKey_Escape) {
  77. running = false;
  78. }
  79. }
  80. });
  81. // Genrate mip-maps for the texture
  82. texture_generatePyramid(myTexture);
  83. // Create a cube model
  84. Model cubeModel = createCubeModel(FVector3D(-0.5f), FVector3D(0.5f));
  85. // Assign the texture to part 0
  86. model_setDiffuseMap(cubeModel, 0, myTexture);
  87. // Create a renderer for multi-threading
  88. Renderer worker = renderer_create();
  89. while(running) {
  90. window_executeEvents(window);
  91. auto colorBuffer = window_getCanvas(window);
  92. auto depthBuffer = window_getDepthBuffer(window);
  93. int targetWidth = image_getWidth(colorBuffer);
  94. int targetHeight = image_getHeight(colorBuffer);
  95. // Paint the background color
  96. image_fill(colorBuffer, ColorRgbaI32(0, 0, 0, 0));
  97. image_fill(depthBuffer, 0.0f); // Infinite reciprocal depth using zero
  98. // Create a camera
  99. const float distance = 1.3f;
  100. const float height = 1.0f;
  101. const double speed = 0.2f;
  102. double timer = time_getSeconds() * speed;
  103. FVector3D cameraPosition = FVector3D(sin(timer) * distance, height, cos(timer) * distance);
  104. FMatrix3x3 cameraRotation = FMatrix3x3::makeAxisSystem(-cameraPosition, FVector3D(0.0f, 1.0f, 0.0f));
  105. Camera camera = Camera::createPerspective(Transform3D(cameraPosition, cameraRotation), targetWidth, targetHeight);
  106. // Render
  107. renderer_begin(worker, colorBuffer, depthBuffer);
  108. renderer_giveTask(worker, cubeModel, Transform3D(), camera);
  109. renderer_end(worker);
  110. window_showCanvas(window);
  111. }
  112. }