main.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. 
  2. #include <limits>
  3. #include <cassert>
  4. #include "../../DFPSR/includeFramework.h"
  5. /*
  6. To do:
  7. * Make a class for the heightmap.
  8. This will make it reusable and simplify lazy orthogonal rendering.
  9. 2D rendering allow very high resolutions to be used.
  10. When the camera moves, background patches are drawn over the screen.
  11. The camera may only translate in whole pixels along a world center camera location.
  12. When the camera is still, dirty rectangles clears the background before drawing new dynamic items.
  13. Items that are currently not changing can be kept if their dirty rectangles aren't colliding with anything animated.
  14. A parked car or barrel doesn't have to be redrawn until something passes nearby and cuts into it by clearing itself.
  15. Static buildings are drawn together with the heightmap as the background.
  16. * A depth buffer based on the world Y coordinate is used for all rendering so that drawing 2D sprites is easy.
  17. When requesting an orthogonal rendering to be done, the request is done in a 2D coordinates system.
  18. The coordinates can be calculated from the tile or world space using the orthogonal camera.
  19. * How can larger maps be made if the whole bump map cannot be stored in memory at once?
  20. Mostly relevant on the Raspberry PI.
  21. Changing camera angle or switching to another zoom level will clear all temporary buffers.
  22. A set of 512² patches are rendered when made visible and deleted when running out of buffers.
  23. A minimum set of buffers will be calculated from the window size.
  24. Static light sources can cast shadows on the bump map.
  25. A double intensity RGBA lightmap might be able to express color light without taking too much cache.
  26. Can deferred light be used in screen space?
  27. * A template layer class that updates one image from a set of other images.
  28. The function being applied as a filter should be virtual.
  29. The input arguments should hold shared pointers to the inputs.
  30. Updating is done on demand when the output is requested to be updated for a used region of the image.
  31. The update will recursively order updates of inputs on the relevant sections.
  32. Any extra size needed for sampling around the center is added to the bounds.
  33. Dirty rectangles are sent to order updates.
  34. The renderer should provide a collection type for dirty rectangles with built in merging of overlapping regions.
  35. Extend image pyramid generation to allow updating after partial changes to the image.
  36. This might require refactoring with buffer views so that pyramid layers are actually images.
  37. Alternatively, just allow giving separate images to the pyramid and give full control.
  38. * Start to controll the camera and terraform the ground in realtime.
  39. * Respond to mouse move event at screen borders to move around.
  40. * Hold a key or something to rotate the camera.
  41. * Line intersection with triangles in object space transformed using the instance's inverse matrix.
  42. * Partial update of diffuse map and the final color map.
  43. The outmost edges of the height map must be at height zero to avoid seeing the end of we world as a seam.
  44. * Place the camera around a non-zero location so that the map's upper left corner is at zero in world space.
  45. One tile should be one length unit in 3D.
  46. * Draw a 3D model where the tool is placed in 3D.
  47. * Allow spraying decals on the map.
  48. Both bump and diffuse maps should be affected by decal layers.
  49. Decals should be possible to serialize for efficiently compressed level design.
  50. * Make some kind of vehicle that a camera can follow when playing in third person mode.
  51. Maybe a helicopter that can shoot rockets on turrets and enemy cars.
  52. * Try to divide the ground model into multiple sections so that model culling can be implemented for a known 0..255 height bound.
  53. When actually generating models, the framework can generate the bounds automatically.
  54. * Make it fast and simple to convert between world space coordinates (x, y, z) and map indices (x, -z) for multiple resolutions.
  55. One unit in world space should be one square in the grid.
  56. Buildings are placed in a tile system where the height map has to be made flat before they can be placed adjacent to each other.
  57. Some buildings like defensive walls does not require a completely flat ground.
  58. * Try to make a reusable class from the terrain that knows its bounding box for culling from the minimum and maximum height.
  59. If good enough, the terrain class can be in an game engine around the core renderer.
  60. The core has the generic stuff that everyone needs.
  61. The game engine has lots of features that are useful for games.
  62. * Image loading
  63. * Advanced model format
  64. * Terrain model
  65. * Encapsulation of internal complexity with a procedural API
  66. Notes:
  67. * If sunlight is aligned with an axis then an algorithm for ray-tracing the mountain can be made very efficient.
  68. Remember at what height the light reached in the previous pixel and lower it by the pixelwise light slope.
  69. If it goes below the ground's height, set it to the ground's height.
  70. If the light is above the ground level, the sun is occluded.
  71. The thresholding can be made soft by linearly fading when light is a bit above the ground as if grass absorbed the light partially.
  72. Having a static lightmap might however not need this optimizing restriction.
  73. */
  74. using namespace dsr;
  75. const String applicationFolder = file_getApplicationFolder();
  76. const String mediaFolder = file_combinePaths(applicationFolder, U"media");
  77. // A real point (x, y, z) may touch a certain tile at integer indices (tileU, tileV) when:
  78. // tileU <= x <= tileU + 1.0
  79. // 0.0f <= y <= highestGround
  80. // tileV <= -z <= tileV + 1.0
  81. // Heightmaps use integers in the range 0..255 to express heights from 0.0 to highestGround
  82. static const float highestGround = 5.0f;
  83. static const float heightPerUnit = highestGround / 255.0f; // One unit of height converted to world space
  84. // One tile in a height map is 1x1 xz units in world space
  85. static const uint32_t colorDensityShift = 4; // 2 ^ colorDensityShift = tileColorDensity
  86. static const uint32_t tileColorDensity = 1 << colorDensityShift; // tileColorDensity² color pixels per tile. Last tileColorDensity-1 rows and columns are unused in the high-resolution maps.
  87. static const uint32_t colorDensityRemainderMask = tileColorDensity - 1; // Only the colorDensityShift last bits
  88. static const uint32_t colorDensityWholeMask = ~colorDensityRemainderMask; // Masking out the colorDensityShift last bits
  89. static const float reciprocalDensity = 1.0f / (float)tileColorDensity;
  90. static const float squareReciprocalDensity = reciprocalDensity * reciprocalDensity;
  91. // Returns floor(x / tileColorDensity)
  92. static inline uint32_t wholeTile(uint32_t x) {
  93. return (x & colorDensityWholeMask) >> colorDensityShift;
  94. }
  95. // Returns x % tileColorDensity
  96. static inline uint32_t remTile(uint32_t x) {
  97. return x & colorDensityRemainderMask;
  98. }
  99. // Returns tileColorDensity - x
  100. static inline uint32_t invRem(uint32_t x) {
  101. return tileColorDensity - x;
  102. }
  103. FVector3D gridToWorld(float tileU, float tileV, float height) {
  104. return FVector3D(tileU, height, -tileV);
  105. }
  106. FVector3D worldToGrid(FVector3D worldSpace) {
  107. return FVector3D(worldSpace.x, -worldSpace.z, worldSpace.y);
  108. }
  109. float getHeight(const ImageU8& heightMap, int u, int v) {
  110. return image_readPixel_border(heightMap, u, v) * heightPerUnit;
  111. }
  112. int createGridPart(Model& targetModel, const ImageU8& heightMap) {
  113. int mapWidth = image_getWidth(heightMap);
  114. int mapHeight = image_getHeight(heightMap);
  115. float scaleU = 1.0f / (mapWidth - 1.0f);
  116. float scaleV = 1.0f / (mapHeight - 1.0f);
  117. // Create a part for the polygons
  118. int part = model_addEmptyPart(targetModel, U"grid");
  119. for (int z = 0; z < mapHeight; z++) {
  120. for (int x = 0; x < mapWidth; x++) {
  121. // Sample the height map and convert to world space
  122. float height = getHeight(heightMap, x, z);
  123. // Create a position from the 3D index
  124. FVector3D position = gridToWorld(x, z, height);
  125. // Add the point to the model
  126. model_addPoint(targetModel, position);
  127. if (x > 0 && z > 0) {
  128. // Create vertex data
  129. // A-B
  130. // |
  131. // D-C
  132. int px = x - 1;
  133. int pz = z - 1;
  134. int indexA = px + pz * mapWidth;
  135. int indexB = x + pz * mapWidth;
  136. int indexC = x + z * mapWidth;
  137. int indexD = px + z * mapWidth;
  138. FVector4D texA = FVector4D(px * scaleU, pz * scaleV, 0.0f, 0.0f);
  139. FVector4D texB = FVector4D(x * scaleU, pz * scaleV, 0.0f, 0.0f);
  140. FVector4D texC = FVector4D(x * scaleU, z * scaleV, 0.0f, 0.0f);
  141. FVector4D texD = FVector4D(px * scaleU, z * scaleV, 0.0f, 0.0f);
  142. // Create a polygon unless it's at the bottom
  143. if (image_readPixel_border(heightMap, x-1, z-1) > 0 || image_readPixel_border(heightMap, x, z-1) > 0 || image_readPixel_border(heightMap, x-1, z) > 0 || image_readPixel_border(heightMap, x, z) > 0) {
  144. if ((x + z) % 2 == 0) {
  145. int poly = model_addQuad(targetModel, part, indexA, indexB, indexC, indexD);
  146. model_setTexCoord(targetModel, part, poly, 0, texA);
  147. model_setTexCoord(targetModel, part, poly, 1, texB);
  148. model_setTexCoord(targetModel, part, poly, 2, texC);
  149. model_setTexCoord(targetModel, part, poly, 3, texD);
  150. } else {
  151. int poly = model_addQuad(targetModel, part, indexB, indexC, indexD, indexA);
  152. model_setTexCoord(targetModel, part, poly, 0, texB);
  153. model_setTexCoord(targetModel, part, poly, 1, texC);
  154. model_setTexCoord(targetModel, part, poly, 2, texD);
  155. model_setTexCoord(targetModel, part, poly, 3, texA);
  156. }
  157. }
  158. }
  159. }
  160. }
  161. return part;
  162. }
  163. static Model createGrid(const ImageU8& heightMap, const TextureRgbaU8& colorMap) {
  164. Model model = model_create();
  165. int part = createGridPart(model, heightMap);
  166. model_setDiffuseMap(model, part, colorMap);
  167. return model;
  168. }
  169. static inline int saturateFloat(float value) {
  170. if (!(value >= 0.0f)) {
  171. // NaN or negative
  172. return 0;
  173. } else if (value > 255.0f) {
  174. // Too large
  175. return 255;
  176. } else {
  177. // Round to closest
  178. return (int)(value + 0.5f);
  179. }
  180. }
  181. float sampleFixedBilinear(const ImageU8& heightMap, uint32_t x, uint32_t y) {
  182. // Get whole coordinates
  183. uint32_t lowX = wholeTile(x);
  184. uint32_t lowY = wholeTile(y);
  185. // Sample neighbors
  186. uint32_t upperLeft = image_readPixel_clamp(heightMap, lowX, lowY);
  187. uint32_t upperRight = image_readPixel_clamp(heightMap, lowX + 1, lowY);
  188. uint32_t lowerLeft = image_readPixel_clamp(heightMap, lowX, lowY + 1);
  189. uint32_t lowerRight = image_readPixel_clamp(heightMap, lowX + 1, lowY + 1);
  190. // Get weights
  191. uint32_t wX = remTile(x);
  192. uint32_t wY = remTile(y);
  193. uint32_t iwX = invRem(wX);
  194. uint32_t iwY = invRem(wY);
  195. // Combine
  196. uint32_t upper = upperLeft * iwX + upperRight * wX;
  197. uint32_t lower = lowerLeft * iwX + lowerRight * wX;
  198. uint32_t center = upper * iwY + lower * wY;
  199. // Normalize as float
  200. return (float)center * squareReciprocalDensity;
  201. }
  202. ColorRgbaI32 sampleColorRampLinear(const ImageRgbaU8& colorRamp, float x) {
  203. assert(image_getWidth(colorRamp) == 256 && image_getHeight(colorRamp) == 1);
  204. if (x < 0.0f) {
  205. return image_readPixel_clamp(colorRamp, 0, 0);
  206. } else if (x > 255.0f) {
  207. return image_readPixel_clamp(colorRamp, 255, 0);
  208. } else {
  209. int low = (int)x;
  210. int high = low + 1;
  211. float weight = x - low;
  212. ColorRgbaI32 lowColor = image_readPixel_clamp(colorRamp, low, 0);
  213. ColorRgbaI32 highColor = image_readPixel_clamp(colorRamp, high, 0);
  214. return ColorRgbaI32::mix(lowColor, highColor, weight);
  215. }
  216. }
  217. // Represents the height in a finer pixel density for material effects
  218. void generateBumpMap(ImageF32& targetBumpMap, const ImageU8& heightMap, const ImageU8& bumpPattern) {
  219. for (int y = 0; y < image_getHeight(targetBumpMap); y++) {
  220. for (int x = 0; x < image_getWidth(targetBumpMap); x++) {
  221. // TODO: Apply gaussian blur after bilinear interpolation to hide seams.
  222. float height = (
  223. sampleFixedBilinear(heightMap, x, y)
  224. + sampleFixedBilinear(heightMap, x + 8, y - 10)
  225. + sampleFixedBilinear(heightMap, x - 10, y - 8)
  226. + sampleFixedBilinear(heightMap, x - 8, y + 10)
  227. + sampleFixedBilinear(heightMap, x + 10, y + 8)
  228. + sampleFixedBilinear(heightMap, x - 4, y - 6)
  229. + sampleFixedBilinear(heightMap, x + 6, y - 4)
  230. + sampleFixedBilinear(heightMap, x + 4, y + 6)
  231. + sampleFixedBilinear(heightMap, x - 6, y + 4)
  232. + sampleFixedBilinear(heightMap, x + 3, y - 5)
  233. + sampleFixedBilinear(heightMap, x - 5, y - 3)
  234. + sampleFixedBilinear(heightMap, x - 3, y + 5)
  235. + sampleFixedBilinear(heightMap, x + 5, y + 3)
  236. ) / 13.0f;
  237. float bump = image_readPixel_tile(bumpPattern, x, y) - 127.5f;
  238. image_writePixel(targetBumpMap, x, y, std::max(0.0f, height + bump * 0.1f));
  239. }
  240. }
  241. }
  242. FVector3D getNormal(const ImageF32& bumpMap, int x, int y) {
  243. float bumpLeft = image_readPixel_clamp(bumpMap, x - 1, y);
  244. float bumpRight = image_readPixel_clamp(bumpMap, x + 1, y);
  245. float bumpUp = image_readPixel_clamp(bumpMap, x, y - 1);
  246. float bumpDown = image_readPixel_clamp(bumpMap, x, y + 1);
  247. static const float DistancePerTwoPixels = 2.0 / tileColorDensity; // From -1 to +1 in pixels converted to world space distance
  248. static const float scale = heightPerUnit / DistancePerTwoPixels;
  249. return normalize(FVector3D((bumpUp - bumpDown) * scale, 1.0f, (bumpRight - bumpLeft) * scale));
  250. }
  251. void generateLightMap(ImageF32& targetLightMap, const ImageF32& bumpMap, const FVector3D& sunDirection, float ambient) {
  252. for (int y = 0; y < image_getHeight(targetLightMap); y++) {
  253. for (int x = 0; x < image_getWidth(targetLightMap); x++) {
  254. FVector3D surfaceNormal = getNormal(bumpMap, x, y);
  255. float angularIntensity = std::max(0.0f, dotProduct(surfaceNormal, -sunDirection));
  256. image_writePixel(targetLightMap, x, y, angularIntensity + ambient);
  257. }
  258. }
  259. }
  260. void generateDiffuseMap(ImageRgbaU8& targetDiffuseMap, const ImageF32& bumpMap, const ImageRgbaU8& heightColorRamp) {
  261. for (int y = 0; y < image_getHeight(targetDiffuseMap); y++) {
  262. for (int x = 0; x < image_getWidth(targetDiffuseMap); x++) {
  263. float height = image_readPixel_clamp(bumpMap, x, y);
  264. ColorRgbaI32 rampColor = sampleColorRampLinear(heightColorRamp, height);
  265. /*int rx = remTile(x);
  266. int ry = remTile(y);
  267. bool gridLine = rx == 0 || ry == 0 || rx == tileColorDensity - 1 || ry == tileColorDensity - 1;
  268. if (gridLine) {
  269. rampColor = ColorRgbaI32(
  270. rampColor.red / 2,
  271. rampColor.green / 2,
  272. rampColor.blue / 2,
  273. 255
  274. );
  275. }*/
  276. image_writePixel(targetDiffuseMap, x, y, rampColor);
  277. }
  278. }
  279. }
  280. // Full update of the ground
  281. void updateColorMap(ImageRgbaU8& targetColorMap, const ImageRgbaU8& diffuseMap, const ImageF32& lightMap) {
  282. for (int y = 0; y < image_getHeight(targetColorMap); y++) {
  283. for (int x = 0; x < image_getWidth(targetColorMap); x++) {
  284. ColorRgbaI32 diffuse = image_readPixel_clamp(diffuseMap, x, y);
  285. float light = image_readPixel_clamp(lightMap, x, y);
  286. image_writePixel(targetColorMap, x, y, diffuse * light);
  287. }
  288. }
  289. }
  290. // Variables
  291. IVector2D mousePos;
  292. bool running = true;
  293. bool showBuffers = false;
  294. // The window handle
  295. Window window;
  296. DSR_MAIN_CALLER(dsrMain)
  297. void dsrMain(List<String> args) {
  298. // Create a window
  299. window = window_create(U"David Piuva's Software Renderer - Terrain example", 1600, 900);
  300. // Bind methods to events
  301. window_setKeyboardEvent(window, [](const KeyboardEvent& event) {
  302. if (event.keyboardEventType == KeyboardEventType::KeyDown) {
  303. DsrKey key = event.dsrKey;
  304. if (key == DsrKey_B) {
  305. showBuffers = !showBuffers;
  306. } else if (key == DsrKey_F11) {
  307. window_setFullScreen(window, !window_isFullScreen(window));
  308. } else if (key == DsrKey_Escape) {
  309. running = false;
  310. }
  311. }
  312. });
  313. window_setMouseEvent(window, [](const MouseEvent& event) {
  314. mousePos = event.position;
  315. });
  316. window_setCloseEvent(window, []() {
  317. running = false;
  318. });
  319. // Load height map
  320. ImageU8 heightMap = image_get_red(image_load_RgbaU8(file_combinePaths(mediaFolder, U"HeightMap.png")));
  321. // Load generic cloud pattern
  322. ImageU8 genericCloudPattern = image_get_red(image_load_RgbaU8(file_combinePaths(mediaFolder, U"Cloud.png")));
  323. // Load height ramp
  324. ImageRgbaU8 heightRamp = image_load_RgbaU8(file_combinePaths(mediaFolder, U"RampIsland.png"));
  325. // Get dimensions
  326. const int heighMapWidth = image_getWidth(heightMap);
  327. const int heighMapHeight = image_getHeight(heightMap);
  328. const int colorMapWidth = heighMapWidth * tileColorDensity;
  329. const int colorMapHeight = heighMapHeight * tileColorDensity;
  330. // Create a bump map in the same 0..255 range as the height map, but using floats
  331. ImageF32 bumpMap = image_create_F32(colorMapWidth, colorMapHeight);
  332. generateBumpMap(bumpMap, heightMap, genericCloudPattern);
  333. // Create a light map
  334. ImageF32 lightMap = image_create_F32(colorMapWidth, colorMapHeight);
  335. FVector3D sunDirection = normalize(FVector3D(0.3f, -1.0f, 1.0f));
  336. float ambient = 0.2f;
  337. generateLightMap(lightMap, bumpMap, sunDirection, ambient);
  338. // Create a diffuse image
  339. ImageRgbaU8 diffuseMap = image_create_RgbaU8(colorMapWidth, colorMapHeight);
  340. generateDiffuseMap(diffuseMap, bumpMap, heightRamp);
  341. // Create a color texture with 5 resolutions.
  342. TextureRgbaU8 colorTexture = texture_create_RgbaU8(colorMapWidth, colorMapHeight, 5);
  343. // Get the highest texture resolution as an image for easy manipulation.
  344. ImageRgbaU8 colorMap = texture_getMipLevelImage(colorTexture, 0);
  345. // Update the color map and texture.
  346. updateColorMap(colorMap, diffuseMap, lightMap);
  347. texture_generatePyramid(colorTexture);
  348. // Create a ground model
  349. Model ground = createGrid(heightMap, colorTexture);
  350. // Create a renderer for multi-threading
  351. Renderer worker = renderer_create();
  352. while(running) {
  353. double startTime;
  354. window_executeEvents(window);
  355. // Request buffers after executing the events, to get newly allocated buffers after resize events
  356. auto colorBuffer = window_getCanvas(window);
  357. auto depthBuffer = window_getDepthBuffer(window);
  358. // Get target size
  359. int targetWidth = image_getWidth(colorBuffer);
  360. int targetHeight = image_getHeight(colorBuffer);
  361. // Paint the background color
  362. startTime = time_getSeconds();
  363. image_fill(colorBuffer, ColorRgbaI32(0, 0, 0, 0)); // Setting each channel to the same value can use memset for faster filling
  364. printText("Fill sky: ", (time_getSeconds() - startTime) * 1000.0, " ms\n");
  365. // Clear the depth buffer
  366. startTime = time_getSeconds();
  367. image_fill(depthBuffer, 0.0f); // Infinite reciprocal depth using default zero
  368. printText("Clear depth: ", (time_getSeconds() - startTime) * 1000.0, " ms\n");
  369. // Create a camera
  370. const double speed = 0.2f;
  371. double timer = time_getSeconds() * speed;
  372. float distance = mousePos.y * 0.03f + 10.0f;
  373. FVector3D worldCenter = FVector3D(heighMapWidth * 0.5f, 0.0f, heighMapHeight * -0.5f);
  374. FVector3D cameraOffset = FVector3D(sin(timer) * distance, mousePos.x * 0.03f + 10.0f, cos(timer) * distance);
  375. FVector3D cameraPosition = worldCenter + cameraOffset;
  376. FMatrix3x3 cameraRotation = FMatrix3x3::makeAxisSystem(-cameraOffset, FVector3D(0.0f, 1.0f, 0.0f));
  377. Camera camera = Camera::createPerspective(Transform3D(cameraPosition, cameraRotation), targetWidth, targetHeight);
  378. // Render the ground using multi-threading
  379. renderer_begin(worker, colorBuffer, depthBuffer);
  380. startTime = time_getSeconds();
  381. renderer_giveTask(worker, ground, Transform3D(), camera);
  382. printText("Project triangles: ", (time_getSeconds() - startTime) * 1000.0, " ms\n");
  383. startTime = time_getSeconds();
  384. renderer_end(worker);
  385. printText("Rasterize triangles: ", (time_getSeconds() - startTime) * 1000.0, " ms\n");
  386. if (showBuffers) {
  387. startTime = time_getSeconds();
  388. //draw_copy(colorBuffer, colorMap, mousePos.x, mousePos.y);
  389. //draw_copy(colorBuffer, heightMap, mousePos.x, mousePos.y);
  390. draw_copy(colorBuffer, bumpMap, mousePos.x, mousePos.y);
  391. printText("Show buffers: ", (time_getSeconds() - startTime) * 1000.0, " ms\n");
  392. }
  393. window_showCanvas(window);
  394. }
  395. printText("\nTerminating the application.\n");
  396. }