spriteAPI.cpp 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107
  1. #include "spriteAPI.h"
  2. #include "Octree.h"
  3. #include "DirtyRectangles.h"
  4. #include "importer.h"
  5. #include "../../../DFPSR/render/ITriangle2D.h"
  6. // Comment out a flag to disable an optimization when debugging
  7. #define DIRTY_RECTANGLE_OPTIMIZATION
  8. namespace dsr {
  9. static IRect renderModel(Model model, OrthoView view, ImageF32 depthBuffer, ImageRgbaU8 diffuseTarget, ImageRgbaU8 normalTarget, FVector2D worldOrigin, Transform3D modelToWorldSpace);
  10. struct SpriteConfig {
  11. int centerX, centerY; // The sprite's origin in pixels relative to the upper left corner
  12. int frameRows; // The atlas has one row for each frame
  13. int propertyColumns; // The atlas has one column for each type of information
  14. // The 3D model's bound in tile units
  15. // The height image goes from 0 at minimum Y to 255 at maximum Y
  16. FVector3D minBound, maxBound;
  17. // Shadow shapes
  18. List<FVector3D> points; // 3D points for the triangles to refer to by index
  19. List<int32_t> triangleIndices; // Triangle indices stored in multiples of three integers
  20. // Construction
  21. SpriteConfig(int centerX, int centerY, int frameRows, int propertyColumns, FVector3D minBound, FVector3D maxBound)
  22. : centerX(centerX), centerY(centerY), frameRows(frameRows), propertyColumns(propertyColumns), minBound(minBound), maxBound(maxBound) {}
  23. explicit SpriteConfig(const ReadableString& content) {
  24. config_parse_ini(content, [this](const ReadableString& block, const ReadableString& key, const ReadableString& value) {
  25. if (string_length(block) == 0) {
  26. if (string_caseInsensitiveMatch(key, U"CenterX")) {
  27. this->centerX = string_toInteger(value);
  28. } else if (string_caseInsensitiveMatch(key, U"CenterY")) {
  29. this->centerY = string_toInteger(value);
  30. } else if (string_caseInsensitiveMatch(key, U"FrameRows")) {
  31. this->frameRows = string_toInteger(value);
  32. } else if (string_caseInsensitiveMatch(key, U"PropertyColumns")) {
  33. this->propertyColumns = string_toInteger(value);
  34. } else if (string_caseInsensitiveMatch(key, U"MinBound")) {
  35. this->minBound = parseFVector3D(value);
  36. } else if (string_caseInsensitiveMatch(key, U"MaxBound")) {
  37. this->maxBound = parseFVector3D(value);
  38. } else if (string_caseInsensitiveMatch(key, U"Points")) {
  39. List<String> values = string_split(value, U',');
  40. if (values.length() % 3 != 0) {
  41. throwError("Points contained ", values.length(), " values, which is not evenly divisible by three!");
  42. } else {
  43. this->points.clear();
  44. this->points.reserve(values.length() / 3);
  45. for (int v = 0; v < values.length(); v += 3) {
  46. this->points.push(FVector3D(string_toDouble(values[v]), string_toDouble(values[v+1]), string_toDouble(values[v+2])));
  47. }
  48. }
  49. } else if (string_caseInsensitiveMatch(key, U"TriangleIndices")) {
  50. List<String> values = string_split(value, U',');
  51. if (values.length() % 3 != 0) {
  52. throwError("TriangleIndices contained ", values.length(), " values, which is not evenly divisible by three!");
  53. } else {
  54. this->triangleIndices.clear();
  55. this->triangleIndices.reserve(values.length());
  56. for (int v = 0; v < values.length(); v++) {
  57. this->triangleIndices.push(string_toInteger(values[v]));
  58. }
  59. }
  60. } else {
  61. printText("Unrecognized key \"", key, "\" in sprite configuration file.\n");
  62. }
  63. } else {
  64. printText("Unrecognized block \"", block, "\" in sprite configuration file.\n");
  65. }
  66. });
  67. }
  68. // Add model as a persistent shadow caster in the sprite configuration
  69. void appendShadow(const Model& model) {
  70. points.reserve(this->points.length() + model_getNumberOfPoints(model));
  71. for (int p = 0; p < model_getNumberOfPoints(model); p++) {
  72. this->points.push(model_getPoint(model, p));
  73. }
  74. for (int part = 0; part < model_getNumberOfParts(model); part++) {
  75. for (int poly = 0; poly < model_getNumberOfPolygons(model, part); poly++) {
  76. int vertexCount = model_getPolygonVertexCount(model, part, poly);
  77. int vertA = 0;
  78. int indexA = model_getVertexPointIndex(model, part, poly, vertA);
  79. for (int vertB = 1; vertB < vertexCount - 1; vertB++) {
  80. int vertC = vertB + 1;
  81. int indexB = model_getVertexPointIndex(model, part, poly, vertB);
  82. int indexC = model_getVertexPointIndex(model, part, poly, vertC);
  83. triangleIndices.push(indexA); triangleIndices.push(indexB); triangleIndices.push(indexC);
  84. }
  85. }
  86. }
  87. }
  88. String toIni() {
  89. // General information
  90. String result = string_combine(
  91. U"; Sprite configuration file\n",
  92. U"CenterX=", this->centerX, "\n",
  93. U"CenterY=", this->centerY, "\n",
  94. U"FrameRows=", this->frameRows, "\n",
  95. U"PropertyColumns=", this->propertyColumns, "\n",
  96. U"MinBound=", this->minBound, "\n",
  97. U"MaxBound=", this->maxBound, "\n"
  98. );
  99. // Low-resolution 3D shape
  100. if (this->points.length() > 0) {
  101. string_append(result, U"Points=");
  102. for (int p = 0; p < this->points.length(); p++) {
  103. if (p > 0) {
  104. string_append(result, U", ");
  105. }
  106. string_append(result, this->points[p]);
  107. }
  108. string_append(result, U"\n");
  109. string_append(result, U"TriangleIndices=");
  110. for (int i = 0; i < this->triangleIndices.length(); i+=3) {
  111. if (i > 0) {
  112. string_append(result, U", ");
  113. }
  114. string_append(result, this->triangleIndices[i], U",", this->triangleIndices[i+1], U",", this->triangleIndices[i+2]);
  115. }
  116. string_append(result, U"\n");
  117. }
  118. return result;
  119. }
  120. };
  121. static ImageF32 scaleHeightImage(const ImageRgbaU8& heightImage, float minHeight, float maxHeight, const ImageRgbaU8& colorImage) {
  122. float scale = (maxHeight - minHeight) / 255.0f;
  123. float offset = minHeight;
  124. int width = image_getWidth(heightImage);
  125. int height = image_getHeight(heightImage);
  126. ImageF32 result = image_create_F32(width, height);
  127. for (int y = 0; y < height; y++) {
  128. for (int x = 0; x < width; x++) {
  129. float value = image_readPixel_clamp(heightImage, x, y).red;
  130. if (image_readPixel_clamp(colorImage, x, y).alpha > 127) {
  131. image_writePixel(result, x, y, (value * scale) + offset);
  132. } else {
  133. image_writePixel(result, x, y, -std::numeric_limits<float>::infinity());
  134. }
  135. }
  136. }
  137. return result;
  138. }
  139. struct SpriteFrame {
  140. IVector2D centerPoint;
  141. ImageRgbaU8 colorImage; // (Red, Green, Blue, _)
  142. ImageRgbaU8 normalImage; // (NormalX, NormalY, NormalZ, _)
  143. ImageF32 heightImage;
  144. SpriteFrame(const IVector2D& centerPoint, const ImageRgbaU8& colorImage, const ImageRgbaU8& normalImage, const ImageF32& heightImage)
  145. : centerPoint(centerPoint), colorImage(colorImage), normalImage(normalImage), heightImage(heightImage) {}
  146. };
  147. struct SpriteType {
  148. public:
  149. IVector3D minBoundMini, maxBoundMini;
  150. List<SpriteFrame> frames;
  151. // TODO: Compress the data using a shadow-only model type of only positions and triangle indices in a single part.
  152. // The shadow model will have its own rendering method excluding the color target.
  153. // Shadow rendering can be a lot simpler by not calculating any vertex weights
  154. // just interpolate the depth using addition, compare to the old value and write the new depth value.
  155. Model shadowModel;
  156. public:
  157. // folderPath should end with a path separator
  158. SpriteType(const String& folderPath, const String& spriteName) {
  159. // Load the image atlas
  160. ImageRgbaU8 loadedAtlas = image_load_RgbaU8(string_combine(folderPath, spriteName, U".png"));
  161. // Load the settings
  162. const SpriteConfig configuration = SpriteConfig(string_load(string_combine(folderPath, spriteName, U".ini")));
  163. this->minBoundMini = IVector3D(
  164. floor(configuration.minBound.x * ortho_miniUnitsPerTile),
  165. floor(configuration.minBound.y * ortho_miniUnitsPerTile),
  166. floor(configuration.minBound.z * ortho_miniUnitsPerTile)
  167. );
  168. this->maxBoundMini = IVector3D(
  169. ceil(configuration.maxBound.x * ortho_miniUnitsPerTile),
  170. ceil(configuration.maxBound.y * ortho_miniUnitsPerTile),
  171. ceil(configuration.maxBound.z * ortho_miniUnitsPerTile)
  172. );
  173. int width = image_getWidth(loadedAtlas) / configuration.propertyColumns;
  174. int height = image_getHeight(loadedAtlas) / configuration.frameRows;
  175. for (int a = 0; a < configuration.frameRows; a++) {
  176. ImageRgbaU8 colorImage = image_getSubImage(loadedAtlas, IRect(0, a * height, width, height));
  177. ImageRgbaU8 heightImage = image_getSubImage(loadedAtlas, IRect(width, a * height, width, height));
  178. ImageRgbaU8 normalImage = image_getSubImage(loadedAtlas, IRect(width * 2, a * height, width, height));
  179. ImageF32 scaledHeightImage = scaleHeightImage(heightImage, configuration.minBound.y, configuration.maxBound.y, colorImage);
  180. this->frames.pushConstruct(IVector2D(configuration.centerX, configuration.centerY), colorImage, normalImage, scaledHeightImage);
  181. }
  182. // Create a model for rendering shadows
  183. if (configuration.points.length() > 0) {
  184. this->shadowModel = model_create();
  185. for (int p = 0; p < configuration.points.length(); p++) {
  186. model_addPoint(this->shadowModel, configuration.points[p]);
  187. }
  188. model_addEmptyPart(this->shadowModel, U"Shadow");
  189. for (int t = 0; t < configuration.triangleIndices.length(); t+=3) {
  190. model_addTriangle(this->shadowModel, 0, configuration.triangleIndices[t], configuration.triangleIndices[t+1], configuration.triangleIndices[t+2]);
  191. }
  192. }
  193. }
  194. public:
  195. // TODO: Force frame count to a power of two or replace modulo with look-up tables in sprite configurations.
  196. int getFrameIndex(Direction direction) {
  197. const int frameFromDir[dir360] = {4, 1, 5, 2, 6, 3, 7, 0};
  198. return frameFromDir[correctDirection(direction)] % this->frames.length();
  199. }
  200. };
  201. // Global list of all sprite types ever loaded
  202. List<SpriteType> types;
  203. static int getSpriteFrameIndex(const Sprite& sprite, OrthoView view) {
  204. return types[sprite.typeIndex].getFrameIndex(view.worldDirection + sprite.direction);
  205. }
  206. // Returns a 2D bounding box of affected target pixels
  207. static IRect drawSprite(const Sprite& sprite, const OrthoView& ortho, const IVector2D& worldCenter, ImageF32 targetHeight, ImageRgbaU8 targetColor, ImageRgbaU8 targetNormal) {
  208. int frameIndex = getSpriteFrameIndex(sprite, ortho);
  209. const SpriteFrame* frame = &types[sprite.typeIndex].frames[frameIndex];
  210. IVector2D screenSpace = ortho.miniTilePositionToScreenPixel(sprite.location, worldCenter) - frame->centerPoint;
  211. float heightOffset = sprite.location.y * ortho_tilesPerMiniUnit;
  212. draw_higher(targetHeight, frame->heightImage, targetColor, frame->colorImage, targetNormal, frame->normalImage, screenSpace.x, screenSpace.y, heightOffset);
  213. return IRect(screenSpace.x, screenSpace.y, image_getWidth(frame->colorImage), image_getHeight(frame->colorImage));
  214. }
  215. static IRect drawModel(const ModelInstance& instance, const OrthoView& ortho, const IVector2D& worldCenter, ImageF32 targetHeight, ImageRgbaU8 targetColor, ImageRgbaU8 targetNormal) {
  216. // TODO: Get the model's bounding box to get a faster camera culling before letting renderModel do the more precise test using transformed geometry data
  217. return renderModel(instance.visibleModel, ortho, targetHeight, targetColor, targetNormal, FVector2D(worldCenter.x, worldCenter.y), instance.location);
  218. }
  219. // The camera transform for each direction
  220. FMatrix3x3 ShadowCubeMapSides[6] = {
  221. FMatrix3x3::makeAxisSystem(FVector3D( 1.0f, 0.0f, 0.0f), FVector3D(0.0f, 1.0f, 0.0f)),
  222. FMatrix3x3::makeAxisSystem(FVector3D(-1.0f, 0.0f, 0.0f), FVector3D(0.0f, 1.0f, 0.0f)),
  223. FMatrix3x3::makeAxisSystem(FVector3D( 0.0f, 1.0f, 0.0f), FVector3D(0.0f, 0.0f, 1.0f)),
  224. FMatrix3x3::makeAxisSystem(FVector3D( 0.0f,-1.0f, 0.0f), FVector3D(0.0f, 0.0f, 1.0f)),
  225. FMatrix3x3::makeAxisSystem(FVector3D( 0.0f, 0.0f, 1.0f), FVector3D(0.0f, 1.0f, 0.0f)),
  226. FMatrix3x3::makeAxisSystem(FVector3D( 0.0f, 0.0f,-1.0f), FVector3D(0.0f, 1.0f, 0.0f))
  227. };
  228. // TODO: Move to the ortho API using a safe getter in modulo
  229. FMatrix3x3 spriteDirections[8] = {
  230. FMatrix3x3::makeAxisSystem(FVector3D( 0.0f, 0.0f, 1.0f), FVector3D(0.0f, 1.0f, 0.0f)),
  231. FMatrix3x3::makeAxisSystem(FVector3D( 1.0f, 0.0f, 1.0f), FVector3D(0.0f, 1.0f, 0.0f)),
  232. FMatrix3x3::makeAxisSystem(FVector3D( 1.0f, 0.0f, 0.0f), FVector3D(0.0f, 1.0f, 0.0f)),
  233. FMatrix3x3::makeAxisSystem(FVector3D( 1.0f, 0.0f,-1.0f), FVector3D(0.0f, 1.0f, 0.0f)),
  234. FMatrix3x3::makeAxisSystem(FVector3D( 0.0f, 0.0f,-1.0f), FVector3D(0.0f, 1.0f, 0.0f)),
  235. FMatrix3x3::makeAxisSystem(FVector3D(-1.0f, 0.0f,-1.0f), FVector3D(0.0f, 1.0f, 0.0f)),
  236. FMatrix3x3::makeAxisSystem(FVector3D(-1.0f, 0.0f, 0.0f), FVector3D(0.0f, 1.0f, 0.0f)),
  237. FMatrix3x3::makeAxisSystem(FVector3D(-1.0f, 0.0f, 1.0f), FVector3D(0.0f, 1.0f, 0.0f))
  238. };
  239. struct CubeMapF32 {
  240. int resolution; // The width and height of each shadow depth image or 0 if no shadows are casted
  241. AlignedImageF32 cubeMap; // A vertical sequence of reciprocal depth images for the six sides of the cube
  242. ImageF32 cubeMapViews[6]; // Sub-images sharing their allocations with cubeMap as sub-images
  243. explicit CubeMapF32(int resolution) : resolution(resolution) {
  244. this->cubeMap = image_create_F32(resolution, resolution * 6);
  245. for (int s = 0; s < 6; s++) {
  246. this->cubeMapViews[s] = image_getSubImage(this->cubeMap, IRect(0, s * resolution, resolution, resolution));
  247. }
  248. }
  249. void clear() {
  250. image_fill(this->cubeMap, 0.0f);
  251. }
  252. };
  253. class PointLight {
  254. public:
  255. FVector3D position; // The world-space center in tile units
  256. float radius; // The light radius in tile units
  257. float intensity; // The color's brightness multiplier (using float to allow smooth fading)
  258. ColorRgbI32 color; // The color of the light (using integers to detect when the color is uniform)
  259. bool shadowCasting; // Casting shadows when enabled
  260. public:
  261. PointLight(FVector3D position, float radius, float intensity, ColorRgbI32 color, bool shadowCasting)
  262. : position(position), radius(radius), intensity(intensity), color(color), shadowCasting(shadowCasting) {}
  263. public:
  264. void renderModelShadow(CubeMapF32& shadowTarget, const ModelInstance& instance, const FMatrix3x3& normalToWorld) const {
  265. Model model = instance.shadowModel;
  266. if (model_exists(model)) {
  267. // Place the model relative to the light source's position, to make rendering in light-space easier
  268. Transform3D modelToWorldTransform = instance.location;
  269. modelToWorldTransform.position = modelToWorldTransform.position - this->position;
  270. for (int s = 0; s < 6; s++) {
  271. Camera camera = Camera::createPerspective(Transform3D(FVector3D(), ShadowCubeMapSides[s] * normalToWorld), shadowTarget.resolution, shadowTarget.resolution);
  272. model_renderDepth(model, modelToWorldTransform, shadowTarget.cubeMapViews[s], camera);
  273. }
  274. }
  275. }
  276. void renderSpriteShadow(CubeMapF32& shadowTarget, const Sprite& sprite, const FMatrix3x3& normalToWorld) const {
  277. if (sprite.shadowCasting) {
  278. Model model = types[sprite.typeIndex].shadowModel;
  279. if (model_exists(model)) {
  280. // Place the model relative to the light source's position, to make rendering in light-space easier
  281. Transform3D modelToWorldTransform = Transform3D(ortho_miniToFloatingTile(sprite.location) - this->position, spriteDirections[sprite.direction]);
  282. for (int s = 0; s < 6; s++) {
  283. Camera camera = Camera::createPerspective(Transform3D(FVector3D(), ShadowCubeMapSides[s] * normalToWorld), shadowTarget.resolution, shadowTarget.resolution);
  284. model_renderDepth(model, modelToWorldTransform, shadowTarget.cubeMapViews[s], camera);
  285. }
  286. }
  287. }
  288. }
  289. void renderSpriteShadows(CubeMapF32& shadowTarget, Octree<Sprite>& sprites, const FMatrix3x3& normalToWorld) const {
  290. IVector3D center = ortho_floatingTileToMini(this->position);
  291. IVector3D minBound = center - ortho_floatingTileToMini(radius);
  292. IVector3D maxBound = center + ortho_floatingTileToMini(radius);
  293. sprites.map(minBound, maxBound, [this, shadowTarget, normalToWorld](Sprite& sprite, const IVector3D origin, const IVector3D minBound, const IVector3D maxBound) mutable {
  294. this->renderSpriteShadow(shadowTarget, sprite, normalToWorld);
  295. });
  296. }
  297. public:
  298. void illuminate(const OrthoView& camera, const IVector2D& worldCenter, OrderedImageRgbaU8& lightBuffer, const OrderedImageRgbaU8& normalBuffer, const AlignedImageF32& heightBuffer, const CubeMapF32& shadowSource) const {
  299. if (this->shadowCasting) {
  300. addPointLight(camera, worldCenter, lightBuffer, normalBuffer, heightBuffer, this->position, this->radius, this->intensity, this->color, shadowSource.cubeMap);
  301. } else {
  302. addPointLight(camera, worldCenter, lightBuffer, normalBuffer, heightBuffer, this->position, this->radius, this->intensity, this->color);
  303. }
  304. }
  305. };
  306. class DirectedLight {
  307. public:
  308. FVector3D direction; // The world-space direction
  309. float intensity; // The color's brightness multiplier (using float to allow smooth fading)
  310. ColorRgbI32 color; // The color of the light (using integers to detect when the color is uniform)
  311. public:
  312. DirectedLight(FVector3D direction, float intensity, ColorRgbI32 color)
  313. : direction(direction), intensity(intensity), color(color) {}
  314. public:
  315. void illuminate(const OrthoView& camera, const IVector2D& worldCenter, OrderedImageRgbaU8& lightBuffer, const OrderedImageRgbaU8& normalBuffer, bool overwrite = false) const {
  316. if (overwrite) {
  317. setDirectedLight(camera, lightBuffer, normalBuffer, this->direction, this->intensity, this->color);
  318. } else {
  319. addDirectedLight(camera, lightBuffer, normalBuffer, this->direction, this->intensity, this->color);
  320. }
  321. }
  322. };
  323. // BlockState keeps track of when the background itself needs to update from static objects being created or destroyed
  324. enum class BlockState {
  325. Unused,
  326. Ready,
  327. Dirty
  328. };
  329. class BackgroundBlock {
  330. public:
  331. static const int blockSize = 512;
  332. static const int maxDistance = blockSize * 2;
  333. IRect worldRegion;
  334. int cameraId = 0;
  335. BlockState state = BlockState::Unused;
  336. OrderedImageRgbaU8 diffuseBuffer;
  337. OrderedImageRgbaU8 normalBuffer;
  338. AlignedImageF32 heightBuffer;
  339. private:
  340. IVector3D getBoxCorner(const IVector3D& minBound, const IVector3D& maxBound, int cornerIndex) {
  341. assert(cornerIndex >= 0 && cornerIndex < 8);
  342. return IVector3D(
  343. ((uint32_t)cornerIndex & 1u) ? maxBound.x : minBound.x,
  344. ((uint32_t)cornerIndex & 2u) ? maxBound.y : minBound.y,
  345. ((uint32_t)cornerIndex & 4u) ? maxBound.z : minBound.z
  346. );
  347. }
  348. // Pre-condition: diffuseBuffer must be cleared unless sprites cover the whole block
  349. void draw(Octree<Sprite>& sprites, const OrthoView& ortho) {
  350. image_fill(this->normalBuffer, ColorRgbaI32(128));
  351. image_fill(this->heightBuffer, -std::numeric_limits<float>::max());
  352. sprites.map(
  353. [ortho,this](const IVector3D& minBound, const IVector3D& maxBound){
  354. IVector2D corners[8];
  355. for (int c = 0; c < 8; c++) {
  356. corners[c] = ortho.miniTileOffsetToScreenPixel(getBoxCorner(minBound, maxBound, c));
  357. }
  358. if (corners[0].x < this->worldRegion.left()
  359. && corners[1].x < this->worldRegion.left()
  360. && corners[2].x < this->worldRegion.left()
  361. && corners[3].x < this->worldRegion.left()
  362. && corners[4].x < this->worldRegion.left()
  363. && corners[5].x < this->worldRegion.left()
  364. && corners[6].x < this->worldRegion.left()
  365. && corners[7].x < this->worldRegion.left()) {
  366. return false;
  367. }
  368. if (corners[0].x > this->worldRegion.right()
  369. && corners[1].x > this->worldRegion.right()
  370. && corners[2].x > this->worldRegion.right()
  371. && corners[3].x > this->worldRegion.right()
  372. && corners[4].x > this->worldRegion.right()
  373. && corners[5].x > this->worldRegion.right()
  374. && corners[6].x > this->worldRegion.right()
  375. && corners[7].x > this->worldRegion.right()) {
  376. return false;
  377. }
  378. if (corners[0].y < this->worldRegion.top()
  379. && corners[1].y < this->worldRegion.top()
  380. && corners[2].y < this->worldRegion.top()
  381. && corners[3].y < this->worldRegion.top()
  382. && corners[4].y < this->worldRegion.top()
  383. && corners[5].y < this->worldRegion.top()
  384. && corners[6].y < this->worldRegion.top()
  385. && corners[7].y < this->worldRegion.top()) {
  386. return false;
  387. }
  388. if (corners[0].y > this->worldRegion.bottom()
  389. && corners[1].y > this->worldRegion.bottom()
  390. && corners[2].y > this->worldRegion.bottom()
  391. && corners[3].y > this->worldRegion.bottom()
  392. && corners[4].y > this->worldRegion.bottom()
  393. && corners[5].y > this->worldRegion.bottom()
  394. && corners[6].y > this->worldRegion.bottom()
  395. && corners[7].y > this->worldRegion.bottom()) {
  396. return false;
  397. }
  398. return true;
  399. },
  400. [this, ortho](Sprite& sprite, const IVector3D origin, const IVector3D minBound, const IVector3D maxBound){
  401. drawSprite(sprite, ortho, -this->worldRegion.upperLeft(), this->heightBuffer, this->diffuseBuffer, this->normalBuffer);
  402. });
  403. }
  404. public:
  405. BackgroundBlock(Octree<Sprite>& sprites, const IRect& worldRegion, const OrthoView& ortho)
  406. : worldRegion(worldRegion), cameraId(ortho.id), state(BlockState::Ready),
  407. diffuseBuffer(image_create_RgbaU8(blockSize, blockSize)),
  408. normalBuffer(image_create_RgbaU8(blockSize, blockSize)),
  409. heightBuffer(image_create_F32(blockSize, blockSize)) {
  410. this->draw(sprites, ortho);
  411. }
  412. void update(Octree<Sprite>& sprites, const IRect& worldRegion, const OrthoView& ortho) {
  413. this->worldRegion = worldRegion;
  414. this->cameraId = ortho.id;
  415. image_fill(this->diffuseBuffer, ColorRgbaI32(0));
  416. this->draw(sprites, ortho);
  417. this->state = BlockState::Ready;
  418. }
  419. void draw(ImageRgbaU8& diffuseTarget, ImageRgbaU8& normalTarget, ImageF32& heightTarget, const IRect& seenRegion) const {
  420. if (this->state != BlockState::Unused) {
  421. int left = this->worldRegion.left() - seenRegion.left();
  422. int top = this->worldRegion.top() - seenRegion.top();
  423. draw_copy(diffuseTarget, this->diffuseBuffer, left, top);
  424. draw_copy(normalTarget, this->normalBuffer, left, top);
  425. draw_copy(heightTarget, this->heightBuffer, left, top);
  426. }
  427. }
  428. void recycle() {
  429. //printText("Recycle block at ", this->worldRegion, "\n");
  430. this->state = BlockState::Unused;
  431. this->worldRegion = IRect();
  432. this->cameraId = -1;
  433. }
  434. };
  435. // TODO: A way to delete passive sprites using search criterias for bounding box and leaf content using a boolean lambda
  436. class SpriteWorldImpl {
  437. public:
  438. // World
  439. OrthoSystem ortho;
  440. // Sprites that rarely change and can be stored in a background image. (no animations allowed)
  441. // TODO: Don't store the position twice, by keeping it separate from the Sprite struct.
  442. Octree<Sprite> passiveSprites;
  443. // Temporary things are deleted when spriteWorld_clearTemporary is called
  444. List<Sprite> temporarySprites;
  445. List<ModelInstance> temporaryModels;
  446. List<PointLight> temporaryPointLights;
  447. List<DirectedLight> temporaryDirectedLights;
  448. // View
  449. int cameraIndex = 0;
  450. IVector3D cameraLocation;
  451. // Deferred rendering
  452. OrderedImageRgbaU8 diffuseBuffer;
  453. OrderedImageRgbaU8 normalBuffer;
  454. AlignedImageF32 heightBuffer;
  455. OrderedImageRgbaU8 lightBuffer;
  456. // Passive background
  457. // TODO: How can split-screen use multiple cameras without duplicate blocks or deleting the other camera's blocks by distance?
  458. List<BackgroundBlock> backgroundBlocks;
  459. // These dirty rectangles keep track of when the background has to be redrawn to the screen after having drawn a dynamic sprite, moved the camera or changed static geometry
  460. DirtyRectangles dirtyBackground;
  461. private:
  462. // Reused buffers
  463. int shadowResolution;
  464. CubeMapF32 temporaryShadowMap;
  465. public:
  466. SpriteWorldImpl(const OrthoSystem &ortho, int shadowResolution)
  467. : ortho(ortho), passiveSprites(ortho_miniUnitsPerTile * 64), shadowResolution(shadowResolution), temporaryShadowMap(shadowResolution) {}
  468. public:
  469. void updateBlockAt(const IRect& blockRegion, const IRect& seenRegion) {
  470. int unusedBlockIndex = -1;
  471. // Find an existing block
  472. for (int b = 0; b < this->backgroundBlocks.length(); b++) {
  473. BackgroundBlock* currentBlockPtr = &this->backgroundBlocks[b];
  474. if (currentBlockPtr->state != BlockState::Unused) {
  475. // Check direction
  476. if (currentBlockPtr->cameraId == this->ortho.view[this->cameraIndex].id) {
  477. // Check location
  478. if (currentBlockPtr->worldRegion.left() == blockRegion.left() && currentBlockPtr->worldRegion.top() == blockRegion.top()) {
  479. // Update if needed
  480. if (currentBlockPtr->state == BlockState::Dirty) {
  481. currentBlockPtr->update(this->passiveSprites, blockRegion, this->ortho.view[this->cameraIndex]);
  482. }
  483. // Use the block
  484. return;
  485. } else {
  486. // See if the block is too far from the camera
  487. if (currentBlockPtr->worldRegion.right() < seenRegion.left() - BackgroundBlock::maxDistance
  488. || currentBlockPtr->worldRegion.left() > seenRegion.right() + BackgroundBlock::maxDistance
  489. || currentBlockPtr->worldRegion.bottom() < seenRegion.top() - BackgroundBlock::maxDistance
  490. || currentBlockPtr->worldRegion.top() > seenRegion.bottom() + BackgroundBlock::maxDistance) {
  491. // Recycle because it's too far away
  492. currentBlockPtr->recycle();
  493. unusedBlockIndex = b;
  494. }
  495. }
  496. } else{
  497. // Recycle directly when another camera angle is used
  498. currentBlockPtr->recycle();
  499. unusedBlockIndex = b;
  500. }
  501. } else {
  502. unusedBlockIndex = b;
  503. }
  504. }
  505. // If none of them matched, we should've passed by any unused block already
  506. if (unusedBlockIndex > -1) {
  507. // We have a block to reuse
  508. this->backgroundBlocks[unusedBlockIndex].update(this->passiveSprites, blockRegion, this->ortho.view[this->cameraIndex]);
  509. } else {
  510. // Create a new block
  511. this->backgroundBlocks.pushConstruct(this->passiveSprites, blockRegion, this->ortho.view[this->cameraIndex]);
  512. }
  513. }
  514. void invalidateBlockAt(int left, int top) {
  515. // Find an existing block
  516. for (int b = 0; b < this->backgroundBlocks.length(); b++) {
  517. BackgroundBlock* currentBlockPtr = &this->backgroundBlocks[b];
  518. // Assuming that alternative camera angles will be removed when drawing next time
  519. if (currentBlockPtr->state == BlockState::Ready
  520. && currentBlockPtr->worldRegion.left() == left
  521. && currentBlockPtr->worldRegion.top() == top) {
  522. // Make dirty to force an update
  523. currentBlockPtr->state = BlockState::Dirty;
  524. }
  525. }
  526. }
  527. // Make sure that each pixel in seenRegion is occupied by an updated background block
  528. void updateBlocks(const IRect& seenRegion) {
  529. // Round inclusive pixel indices down to containing blocks and iterate over them in strides along x and y
  530. int64_t roundedLeft = roundDown(seenRegion.left(), BackgroundBlock::blockSize);
  531. int64_t roundedTop = roundDown(seenRegion.top(), BackgroundBlock::blockSize);
  532. int64_t roundedRight = roundDown(seenRegion.right() - 1, BackgroundBlock::blockSize);
  533. int64_t roundedBottom = roundDown(seenRegion.bottom() - 1, BackgroundBlock::blockSize);
  534. for (int64_t y = roundedTop; y <= roundedBottom; y += BackgroundBlock::blockSize) {
  535. for (int64_t x = roundedLeft; x <= roundedRight; x += BackgroundBlock::blockSize) {
  536. // Make sure that a block is allocated and pre-drawn at this location
  537. this->updateBlockAt(IRect(x, y, BackgroundBlock::blockSize, BackgroundBlock::blockSize), seenRegion);
  538. }
  539. }
  540. }
  541. void drawDeferred(OrderedImageRgbaU8& diffuseTarget, OrderedImageRgbaU8& normalTarget, AlignedImageF32& heightTarget, const IRect& seenRegion) {
  542. // Check image dimensions
  543. assert(image_getWidth(diffuseTarget) == seenRegion.width() && image_getHeight(diffuseTarget) == seenRegion.height());
  544. assert(image_getWidth(normalTarget) == seenRegion.width() && image_getHeight(normalTarget) == seenRegion.height());
  545. assert(image_getWidth(heightTarget) == seenRegion.width() && image_getHeight(heightTarget) == seenRegion.height());
  546. this->dirtyBackground.setTargetResolution(seenRegion.width(), seenRegion.height());
  547. // Draw passive sprites to blocks
  548. this->updateBlocks(seenRegion);
  549. // Draw background blocks to the target images
  550. for (int b = 0; b < this->backgroundBlocks.length(); b++) {
  551. #ifdef DIRTY_RECTANGLE_OPTIMIZATION
  552. // Optimized version
  553. for (int64_t r = 0; r < this->dirtyBackground.getRectangleCount(); r++) {
  554. IRect screenClip = this->dirtyBackground.getRectangle(r);
  555. IRect worldClip = screenClip + seenRegion.upperLeft();
  556. ImageRgbaU8 clippedDiffuseTarget = image_getSubImage(diffuseTarget, screenClip);
  557. ImageRgbaU8 clippedNormalTarget = image_getSubImage(normalTarget, screenClip);
  558. ImageF32 clippedHeightTarget = image_getSubImage(heightTarget, screenClip);
  559. this->backgroundBlocks[b].draw(clippedDiffuseTarget, clippedNormalTarget, clippedHeightTarget, worldClip);
  560. }
  561. #else
  562. // Reference implementation
  563. this->backgroundBlocks[b].draw(diffuseTarget, normalTarget, heightTarget, seenRegion);
  564. #endif
  565. }
  566. // Reset dirty rectangles so that active sprites may record changes
  567. this->dirtyBackground.noneDirty();
  568. // Draw active sprites to the targets
  569. for (int s = 0; s < this->temporarySprites.length(); s++) {
  570. IRect drawnRegion = drawSprite(this->temporarySprites[s], this->ortho.view[this->cameraIndex], -seenRegion.upperLeft(), heightTarget, diffuseTarget, normalTarget);
  571. this->dirtyBackground.makeRegionDirty(drawnRegion);
  572. }
  573. for (int s = 0; s < this->temporaryModels.length(); s++) {
  574. IRect drawnRegion = drawModel(this->temporaryModels[s], this->ortho.view[this->cameraIndex], -seenRegion.upperLeft(), heightTarget, diffuseTarget, normalTarget);
  575. this->dirtyBackground.makeRegionDirty(drawnRegion);
  576. }
  577. }
  578. public:
  579. void updatePassiveRegion(const IRect& modifiedRegion) {
  580. int64_t roundedLeft = roundDown(modifiedRegion.left(), BackgroundBlock::blockSize);
  581. int64_t roundedTop = roundDown(modifiedRegion.top(), BackgroundBlock::blockSize);
  582. int64_t roundedRight = roundDown(modifiedRegion.right() - 1, BackgroundBlock::blockSize);
  583. int64_t roundedBottom = roundDown(modifiedRegion.bottom() - 1, BackgroundBlock::blockSize);
  584. for (int64_t y = roundedTop; y <= roundedBottom; y += BackgroundBlock::blockSize) {
  585. for (int64_t x = roundedLeft; x <= roundedRight; x += BackgroundBlock::blockSize) {
  586. // Make sure that a block is allocated and pre-drawn at this location
  587. this->invalidateBlockAt(x, y);
  588. }
  589. }
  590. // Redrawing the whole background to the screen is very cheap using memcpy, so no need to optimize this rare event
  591. this->dirtyBackground.allDirty();
  592. }
  593. IVector2D findWorldCenter(const AlignedImageRgbaU8& colorTarget) const {
  594. return IVector2D(image_getWidth(colorTarget) / 2, image_getHeight(colorTarget) / 2) - this->ortho.miniTileOffsetToScreenPixel(this->cameraLocation, this->cameraIndex);
  595. }
  596. void draw(AlignedImageRgbaU8& colorTarget) {
  597. double startTime;
  598. IVector2D worldCenter = this->findWorldCenter(colorTarget);
  599. // Resize when the window has resized or the buffers haven't been allocated before
  600. int width = image_getWidth(colorTarget);
  601. int height = image_getHeight(colorTarget);
  602. if (image_getWidth(this->diffuseBuffer) != width || image_getHeight(this->diffuseBuffer) != height) {
  603. this->diffuseBuffer = image_create_RgbaU8(width, height);
  604. this->normalBuffer = image_create_RgbaU8(width, height);
  605. this->lightBuffer = image_create_RgbaU8(width, height);
  606. this->heightBuffer = image_create_F32(width, height);
  607. }
  608. IRect worldRegion = IRect(-worldCenter.x, -worldCenter.y, width, height);
  609. startTime = time_getSeconds();
  610. this->drawDeferred(this->diffuseBuffer, this->normalBuffer, this->heightBuffer, worldRegion);
  611. debugText("Draw deferred: ", (time_getSeconds() - startTime) * 1000.0, " ms\n");
  612. // Illuminate using directed lights
  613. if (this->temporaryDirectedLights.length() > 0) {
  614. startTime = time_getSeconds();
  615. // Overwriting any light from the previous frame
  616. for (int p = 0; p < this->temporaryDirectedLights.length(); p++) {
  617. this->temporaryDirectedLights[p].illuminate(this->ortho.view[this->cameraIndex], worldCenter, this->lightBuffer, this->normalBuffer, p == 0);
  618. }
  619. debugText("Sun light: ", (time_getSeconds() - startTime) * 1000.0, " ms\n");
  620. } else {
  621. startTime = time_getSeconds();
  622. image_fill(this->lightBuffer, ColorRgbaI32(0)); // Set light to black
  623. debugText("Clear light: ", (time_getSeconds() - startTime) * 1000.0, " ms\n");
  624. }
  625. // Illuminate using point lights
  626. for (int p = 0; p < this->temporaryPointLights.length(); p++) {
  627. PointLight *currentLight = &this->temporaryPointLights[p];
  628. if (currentLight->shadowCasting) {
  629. startTime = time_getSeconds();
  630. this->temporaryShadowMap.clear();
  631. // Shadows from background sprites
  632. currentLight->renderSpriteShadows(this->temporaryShadowMap, this->passiveSprites, ortho.view[this->cameraIndex].normalToWorldSpace);
  633. // Shadows from temporary sprites
  634. for (int s = 0; s < this->temporarySprites.length(); s++) {
  635. currentLight->renderSpriteShadow(this->temporaryShadowMap, this->temporarySprites[s], ortho.view[this->cameraIndex].normalToWorldSpace);
  636. }
  637. // Shadows from temporary models
  638. for (int s = 0; s < this->temporaryModels.length(); s++) {
  639. currentLight->renderModelShadow(this->temporaryShadowMap, this->temporaryModels[s], ortho.view[this->cameraIndex].normalToWorldSpace);
  640. }
  641. debugText("Cast point-light shadows: ", (time_getSeconds() - startTime) * 1000.0, " ms\n");
  642. }
  643. startTime = time_getSeconds();
  644. currentLight->illuminate(this->ortho.view[this->cameraIndex], worldCenter, this->lightBuffer, this->normalBuffer, this->heightBuffer, this->temporaryShadowMap);
  645. debugText("Illuminate from point-light: ", (time_getSeconds() - startTime) * 1000.0, " ms\n");
  646. }
  647. // Draw the final image to the target by multiplying diffuse with light
  648. startTime = time_getSeconds();
  649. blendLight(colorTarget, this->diffuseBuffer, this->lightBuffer);
  650. debugText("Blend light: ", (time_getSeconds() - startTime) * 1000.0, " ms\n");
  651. }
  652. };
  653. int sprite_loadTypeFromFile(const String& folderPath, const String& spriteName) {
  654. types.pushConstruct(folderPath, spriteName);
  655. return types.length() - 1;
  656. }
  657. int sprite_getTypeCount() {
  658. return types.length();
  659. }
  660. SpriteWorld spriteWorld_create(OrthoSystem ortho, int shadowResolution) {
  661. return std::make_shared<SpriteWorldImpl>(ortho, shadowResolution);
  662. }
  663. #define MUST_EXIST(OBJECT, METHOD) if (OBJECT.get() == nullptr) { throwError("The " #OBJECT " handle was null in " #METHOD "\n"); }
  664. void spriteWorld_addBackgroundSprite(SpriteWorld& world, const Sprite& sprite) {
  665. MUST_EXIST(world, spriteWorld_addBackgroundSprite);
  666. if (sprite.typeIndex < 0 || sprite.typeIndex >= types.length()) { throwError(U"Sprite type index ", sprite.typeIndex, " is out of bound!\n"); }
  667. // Add the passive sprite to the octree
  668. IVector3D origin = sprite.location;
  669. IVector3D minBound = origin + types[sprite.typeIndex].minBoundMini;
  670. IVector3D maxBound = origin + types[sprite.typeIndex].maxBoundMini;
  671. world->passiveSprites.insert(sprite, origin, minBound, maxBound);
  672. // Find the affected passive region and make it dirty
  673. int frameIndex = getSpriteFrameIndex(sprite, world->ortho.view[world->cameraIndex]);
  674. const SpriteFrame* frame = &types[sprite.typeIndex].frames[frameIndex];
  675. IVector2D upperLeft = world->ortho.miniTilePositionToScreenPixel(sprite.location, world->cameraIndex, IVector2D()) - frame->centerPoint;
  676. IRect region = IRect(upperLeft.x, upperLeft.y, image_getWidth(frame->colorImage), image_getHeight(frame->colorImage));
  677. world->updatePassiveRegion(region);
  678. }
  679. void spriteWorld_addTemporarySprite(SpriteWorld& world, const Sprite& sprite) {
  680. MUST_EXIST(world, spriteWorld_addTemporarySprite);
  681. if (sprite.typeIndex < 0 || sprite.typeIndex >= types.length()) { throwError(U"Sprite type index ", sprite.typeIndex, " is out of bound!\n"); }
  682. // Add the temporary sprite
  683. world->temporarySprites.push(sprite);
  684. }
  685. void spriteWorld_addTemporaryModel(SpriteWorld& world, const ModelInstance& instance) {
  686. MUST_EXIST(world, spriteWorld_addTemporaryModel);
  687. // Add the temporary model
  688. world->temporaryModels.push(instance);
  689. }
  690. void spriteWorld_createTemporary_pointLight(SpriteWorld& world, const FVector3D position, float radius, float intensity, ColorRgbI32 color, bool shadowCasting) {
  691. MUST_EXIST(world, spriteWorld_createTemporary_pointLight);
  692. world->temporaryPointLights.pushConstruct(position, radius, intensity, color, shadowCasting);
  693. }
  694. void spriteWorld_createTemporary_directedLight(SpriteWorld& world, const FVector3D direction, float intensity, ColorRgbI32 color) {
  695. MUST_EXIST(world, spriteWorld_createTemporary_pointLight);
  696. world->temporaryDirectedLights.pushConstruct(direction, intensity, color);
  697. }
  698. void spriteWorld_clearTemporary(SpriteWorld& world) {
  699. MUST_EXIST(world, spriteWorld_clearTemporary);
  700. world->temporarySprites.clear();
  701. world->temporaryModels.clear();
  702. world->temporaryPointLights.clear();
  703. world->temporaryDirectedLights.clear();
  704. }
  705. void spriteWorld_draw(SpriteWorld& world, AlignedImageRgbaU8& colorTarget) {
  706. MUST_EXIST(world, spriteWorld_draw);
  707. world->draw(colorTarget);
  708. }
  709. IVector3D spriteWorld_findGroundAtPixel(SpriteWorld& world, const AlignedImageRgbaU8& colorBuffer, const IVector2D& pixelLocation) {
  710. MUST_EXIST(world, spriteWorld_findGroundAtPixel);
  711. return world->ortho.pixelToMiniPosition(pixelLocation, world->cameraIndex, world->findWorldCenter(colorBuffer));
  712. }
  713. void spriteWorld_moveCameraInPixels(SpriteWorld& world, const IVector2D& pixelOffset) {
  714. MUST_EXIST(world, spriteWorld_moveCameraInPixels);
  715. if (pixelOffset.x != 0 || pixelOffset.y != 0) {
  716. world->cameraLocation = world->cameraLocation + world->ortho.pixelToMiniOffset(pixelOffset, world->cameraIndex);
  717. world->dirtyBackground.allDirty();
  718. }
  719. }
  720. AlignedImageRgbaU8 spriteWorld_getDiffuseBuffer(SpriteWorld& world) {
  721. MUST_EXIST(world, spriteWorld_getDiffuseBuffer);
  722. return world->diffuseBuffer;
  723. }
  724. OrderedImageRgbaU8 spriteWorld_getNormalBuffer(SpriteWorld& world) {
  725. MUST_EXIST(world, spriteWorld_getNormalBuffer);
  726. return world->normalBuffer;
  727. }
  728. OrderedImageRgbaU8 spriteWorld_getLightBuffer(SpriteWorld& world) {
  729. MUST_EXIST(world, spriteWorld_getLightBuffer);
  730. return world->lightBuffer;
  731. }
  732. AlignedImageF32 spriteWorld_getHeightBuffer(SpriteWorld& world) {
  733. MUST_EXIST(world, spriteWorld_getHeightBuffer);
  734. return world->heightBuffer;
  735. }
  736. int spriteWorld_getCameraDirectionIndex(SpriteWorld& world) {
  737. MUST_EXIST(world, spriteWorld_getCameraDirectionIndex);
  738. return world->cameraIndex;
  739. }
  740. void spriteWorld_setCameraDirectionIndex(SpriteWorld& world, int index) {
  741. MUST_EXIST(world, spriteWorld_setCameraDirectionIndex);
  742. if (index != world->cameraIndex) {
  743. world->cameraIndex = index;
  744. world->dirtyBackground.allDirty();
  745. }
  746. }
  747. // Only because normals are stored as texture coordinates
  748. static FVector3D unpackNormals(FVector4D packedNormals) {
  749. return FVector3D(packedNormals.x, packedNormals.y, packedNormals.z);
  750. }
  751. // Pre-conditions:
  752. // * All images must exist and have the same dimensions
  753. // * All triangles in model must be contained within the image bounds after being projected using view
  754. // Post-condition:
  755. // Returns the dirty pixel bound based on projected positions
  756. // worldOrigin is the perceived world's origin in target pixel coordinates
  757. // modelToWorldSpace is used to place the model freely in the world
  758. static IRect renderModel(Model model, OrthoView view, ImageF32 depthBuffer, ImageRgbaU8 diffuseTarget, ImageRgbaU8 normalTarget, FVector2D worldOrigin, Transform3D modelToWorldSpace) {
  759. // Get the model's 3D bound
  760. //FVector3D minBound, maxBound;
  761. //model_getBoundingBox(model, minBound, maxBound);
  762. // TODO: Quick culling test based on the 3D bounding box using only 8 points.
  763. int pointCount = model_getNumberOfPoints(model);
  764. IRect clipBound = image_getBound(depthBuffer);
  765. Array<FVector3D> projectedPoints(pointCount, FVector3D()); // pixel X, pixel Y, mini-tile height
  766. // Combine transforms
  767. // TODO: Combine objectToWorldSpace with worldOrigin to get screen space directly
  768. Transform3D objectToWorldSpace = modelToWorldSpace * Transform3D(FVector3D(), view.worldSpaceToScreenDepth);
  769. // Transform positions and return the dirty box
  770. IRect dirtyBox = IRect(clipBound.width(), clipBound.height(), -clipBound.width(), -clipBound.height());
  771. for (int point = 0; point < pointCount; point++) {
  772. FVector3D projected = objectToWorldSpace.transformPoint(model_getPoint(model, point));
  773. FVector3D screenProjection = FVector3D(projected.x + worldOrigin.x, projected.y + worldOrigin.y, projected.z);
  774. projectedPoints[point] = screenProjection;
  775. // Expand the dirty bound
  776. dirtyBox = IRect::merge(dirtyBox, IRect((int)(screenProjection.x), (int)(screenProjection.y), 1, 1));
  777. }
  778. // Skip early if the more precise culling test fails
  779. if (!(IRect::cut(clipBound, dirtyBox).hasArea())) {
  780. // Nothing drawn, no dirty rectangle
  781. return IRect();
  782. }
  783. // Combine normal transforms
  784. FMatrix3x3 normalToWorldSpace = view.normalToWorldSpace;
  785. // Render polygons as triangle fans
  786. for (int part = 0; part < model_getNumberOfParts(model); part++) {
  787. for (int poly = 0; poly < model_getNumberOfPolygons(model, part); poly++) {
  788. int vertexCount = model_getPolygonVertexCount(model, part, poly);
  789. int vertA = 0;
  790. FVector4D vertexColorA = model_getVertexColor(model, part, poly, vertA) * 255.0f;
  791. int indexA = model_getVertexPointIndex(model, part, poly, vertA);
  792. // TODO: Merge transforms into normalToWorldSpace in advance
  793. FVector3D normalA = normalToWorldSpace.transformTransposed(modelToWorldSpace.transform.transform(unpackNormals(model_getTexCoord(model, part, poly, vertA))));
  794. FVector3D pointA = projectedPoints[indexA];
  795. LVector2D subPixelA = LVector2D(safeRoundInt64(pointA.x * constants::unitsPerPixel), safeRoundInt64(pointA.y * constants::unitsPerPixel));
  796. for (int vertB = 1; vertB < vertexCount - 1; vertB++) {
  797. int vertC = vertB + 1;
  798. int indexB = model_getVertexPointIndex(model, part, poly, vertB);
  799. int indexC = model_getVertexPointIndex(model, part, poly, vertC);
  800. FVector4D vertexColorB = model_getVertexColor(model, part, poly, vertB) * 255.0f;
  801. FVector4D vertexColorC = model_getVertexColor(model, part, poly, vertC) * 255.0f;
  802. // TODO: Merge transforms into normalToWorldSpace in advance
  803. FVector3D normalB = normalToWorldSpace.transformTransposed(modelToWorldSpace.transform.transform(unpackNormals(model_getTexCoord(model, part, poly, vertB))));
  804. FVector3D normalC = normalToWorldSpace.transformTransposed(modelToWorldSpace.transform.transform(unpackNormals(model_getTexCoord(model, part, poly, vertC))));
  805. FVector3D pointB = projectedPoints[indexB];
  806. FVector3D pointC = projectedPoints[indexC];
  807. LVector2D subPixelB = LVector2D(safeRoundInt64(pointB.x * constants::unitsPerPixel), safeRoundInt64(pointB.y * constants::unitsPerPixel));
  808. LVector2D subPixelC = LVector2D(safeRoundInt64(pointC.x * constants::unitsPerPixel), safeRoundInt64(pointC.y * constants::unitsPerPixel));
  809. IRect triangleBound = IRect::cut(clipBound, getTriangleBound(subPixelA, subPixelB, subPixelC));
  810. int rowCount = triangleBound.height();
  811. if (rowCount > 0) {
  812. RowInterval rows[rowCount];
  813. rasterizeTriangle(subPixelA, subPixelB, subPixelC, rows, triangleBound);
  814. for (int y = triangleBound.top(); y < triangleBound.bottom(); y++) {
  815. int rowIndex = y - triangleBound.top();
  816. int left = rows[rowIndex].left;
  817. int right = rows[rowIndex].right;
  818. for (int x = left; x < right; x++) {
  819. FVector3D weight = getAffineWeight(FVector2D(pointA.x, pointA.y), FVector2D(pointB.x, pointB.y), FVector2D(pointC.x, pointC.y), FVector2D(x + 0.5f, y + 0.5f));
  820. float height = interpolateUsingAffineWeight(pointA.z, pointB.z, pointC.z, weight);
  821. if (height > image_readPixel_clamp(depthBuffer, x, y)) {
  822. FVector4D vertexColor = interpolateUsingAffineWeight(vertexColorA, vertexColorB, vertexColorC, weight);
  823. FVector3D normal = (normalize(interpolateUsingAffineWeight(normalA, normalB, normalC, weight)) + 1.0f) * 127.5f;
  824. image_writePixel(depthBuffer, x, y, height);
  825. image_writePixel(diffuseTarget, x, y, ColorRgbaI32(vertexColor.x, vertexColor.y, vertexColor.z, 255));
  826. image_writePixel(normalTarget, x, y, ColorRgbaI32(normal.x, normal.y, normal.z, 255));
  827. }
  828. }
  829. }
  830. }
  831. }
  832. }
  833. }
  834. return dirtyBox;
  835. }
  836. void sprite_generateFromModel(ImageRgbaU8& targetAtlas, String& targetConfigText, const Model& visibleModel, const Model& shadowModel, const OrthoSystem& ortho, const String& targetPath, int cameraAngles) {
  837. // Validate input
  838. if (cameraAngles < 1) {
  839. printText(" Need at least one camera angle to generate a sprite!\n");
  840. return;
  841. } else if (!model_exists(visibleModel)) {
  842. printText(" There's nothing to render, because visible model does not exist!\n");
  843. return;
  844. } else if (model_getNumberOfParts(visibleModel) == 0) {
  845. printText(" There's nothing to render in the visible model, because there are no parts in the visible model!\n");
  846. return;
  847. } else {
  848. // Measure the bounding cylinder for determining the uncropped image size
  849. FVector3D minBound = FVector3D(std::numeric_limits<float>::max());
  850. FVector3D maxBound = FVector3D(-std::numeric_limits<float>::max());
  851. for (int p = 0; p < model_getNumberOfPoints(visibleModel); p++) {
  852. FVector3D point = model_getPoint(visibleModel, p);
  853. if (point.x < minBound.x) { minBound.x = point.x; }
  854. if (point.y < minBound.y) { minBound.y = point.y; }
  855. if (point.z < minBound.z) { minBound.z = point.z; }
  856. if (point.x > maxBound.x) { maxBound.x = point.x; }
  857. if (point.y > maxBound.y) { maxBound.y = point.y; }
  858. if (point.z > maxBound.z) { maxBound.z = point.z; }
  859. }
  860. // Check if generating a bound failed
  861. if (minBound.x > maxBound.x) {
  862. printText(" There's nothing visible in the model, because the 3D bounding box had no points to be created from!\n");
  863. return;
  864. }
  865. printText(" Representing height from ", minBound.y, " to ", maxBound.y, " encoded using 8-bits\n");
  866. // Calculate initial image size
  867. float worstCaseDiameter = (std::max(maxBound.x, -minBound.x) + std::max(maxBound.y, -minBound.y) + std::max(maxBound.z, -minBound.z)) * 2;
  868. int maxRes = roundUp(worstCaseDiameter * ortho.pixelsPerTile, 2) + 4; // Round up to even pixels and add 4 padding pixels
  869. // Allocate square images from the pessimistic size estimation
  870. int width = maxRes;
  871. int height = maxRes;
  872. ImageF32 depthBuffer = image_create_F32(width, height);
  873. ImageRgbaU8 colorImage[cameraAngles];
  874. ImageRgbaU8 heightImage[cameraAngles];
  875. ImageRgbaU8 normalImage[cameraAngles];
  876. for (int a = 0; a < cameraAngles; a++) {
  877. colorImage[a] = image_create_RgbaU8(width, height);
  878. heightImage[a] = image_create_RgbaU8(width, height);
  879. normalImage[a] = image_create_RgbaU8(width, height);
  880. }
  881. // Render the model to multiple render targets at once
  882. float heightScale = 255.0f / (maxBound.y - minBound.y);
  883. for (int a = 0; a < cameraAngles; a++) {
  884. image_fill(depthBuffer, -1000000000.0f);
  885. image_fill(colorImage[a], ColorRgbaI32(0, 0, 0, 0));
  886. importer_generateNormalsIntoTextureCoordinates(visibleModel);
  887. FVector2D origin = FVector2D((float)width * 0.5f, (float)height * 0.5f);
  888. renderModel(visibleModel, ortho.view[a], depthBuffer, colorImage[a], normalImage[a], origin, Transform3D());
  889. // Convert height into an 8 bit channel for saving
  890. for (int y = 0; y < height; y++) {
  891. for (int x = 0; x < width; x++) {
  892. int32_t opacityPixel = image_readPixel_clamp(colorImage[a], x, y).alpha;
  893. int32_t heightPixel = (image_readPixel_clamp(depthBuffer, x, y) - minBound.y) * heightScale;
  894. image_writePixel(heightImage[a], x, y, ColorRgbaI32(heightPixel, 0, 0, opacityPixel));
  895. }
  896. }
  897. }
  898. // Crop all images uniformly for easy atlas packing
  899. int32_t minX = width;
  900. int32_t minY = height;
  901. int32_t maxX = 0;
  902. int32_t maxY = 0;
  903. for (int a = 0; a < cameraAngles; a++) {
  904. for (int y = 0; y < height; y++) {
  905. for (int x = 0; x < width; x++) {
  906. if (image_readPixel_border(colorImage[a], x, y).alpha) {
  907. if (x < minX) minX = x;
  908. if (x > maxX) maxX = x;
  909. if (y < minY) minY = y;
  910. if (y > maxY) maxY = y;
  911. }
  912. }
  913. }
  914. }
  915. // Check if cropping failed
  916. if (minX > maxX) {
  917. printText(" There's nothing visible in the model, because cropping the final images returned nothing!\n");
  918. return;
  919. }
  920. IRect cropRegion = IRect(minX, minY, (maxX + 1) - minX, (maxY + 1) - minY);
  921. if (cropRegion.width() < 1 || cropRegion.height() < 1) {
  922. printText(" Cropping failed to find any drawn pixels!\n");
  923. return;
  924. }
  925. for (int a = 0; a < cameraAngles; a++) {
  926. colorImage[a] = image_getSubImage(colorImage[a], cropRegion);
  927. heightImage[a] = image_getSubImage(heightImage[a], cropRegion);
  928. normalImage[a] = image_getSubImage(normalImage[a], cropRegion);
  929. }
  930. int croppedWidth = cropRegion.width();
  931. int croppedHeight = cropRegion.height();
  932. int centerX = width / 2 - cropRegion.left();
  933. int centerY = height / 2 - cropRegion.top();
  934. printText(" Cropped images of ", croppedWidth, "x", croppedHeight, " pixels with centers at (", centerX, ", ", centerY, ")\n");
  935. // Pack everything into an image atlas
  936. targetAtlas = image_create_RgbaU8(croppedWidth * 3, croppedHeight * cameraAngles);
  937. for (int a = 0; a < cameraAngles; a++) {
  938. draw_copy(targetAtlas, colorImage[a], 0, a * croppedHeight);
  939. draw_copy(targetAtlas, heightImage[a], croppedWidth, a * croppedHeight);
  940. draw_copy(targetAtlas, normalImage[a], croppedWidth * 2, a * croppedHeight);
  941. }
  942. SpriteConfig config = SpriteConfig(centerX, centerY, cameraAngles, 3, minBound, maxBound);
  943. if (model_exists(shadowModel) && model_getNumberOfPoints(shadowModel) > 0) {
  944. config.appendShadow(shadowModel);
  945. }
  946. targetConfigText = config.toIni();
  947. }
  948. }
  949. // Allowing the last decimals to deviate a bit because floating-point operations are rounded differently between computers
  950. static bool approximateTextMatch(const ReadableString &a, const ReadableString &b, double tolerance = 0.00002) {
  951. int readerA = 0, readerB = 0;
  952. while (readerA < string_length(a) && readerB < string_length(b)) {
  953. DsrChar charA = a[readerA];
  954. DsrChar charB = b[readerB];
  955. if (character_isValueCharacter(charA) && character_isValueCharacter(charB)) {
  956. // Scan forward on both sides while consuming content and comparing the actual value
  957. int startA = readerA;
  958. int startB = readerB;
  959. // Only move forward on valid characters
  960. if (a[readerA] == U'-') { readerA++; }
  961. if (b[readerB] == U'-') { readerB++; }
  962. while (character_isDigit(a[readerA])) { readerA++; }
  963. while (character_isDigit(b[readerB])) { readerB++; }
  964. if (a[readerA] == U'.') { readerA++; }
  965. if (b[readerB] == U'.') { readerB++; }
  966. while (character_isDigit(a[readerA])) { readerA++; }
  967. while (character_isDigit(b[readerB])) { readerB++; }
  968. // Approximate values
  969. double valueA = string_toDouble(string_exclusiveRange(a, startA, readerA));
  970. double valueB = string_toDouble(string_exclusiveRange(b, startB, readerB));
  971. // Check the difference
  972. double diff = valueB - valueA;
  973. if (diff > tolerance || diff < -tolerance) {
  974. // Too big difference, this is probably not a rounding error
  975. return false;
  976. }
  977. } else if (charA != charB) {
  978. // Difference with a non-value involved
  979. return false;
  980. }
  981. readerA++;
  982. readerB++;
  983. }
  984. if (readerA < string_length(a) - 1 || readerB < string_length(b) - 1) {
  985. // One text had unmatched remains after the other reached its end
  986. return false;
  987. } else {
  988. return true;
  989. }
  990. }
  991. void sprite_generateFromModel(const Model& visibleModel, const Model& shadowModel, const OrthoSystem& ortho, const String& targetPath, int cameraAngles, bool debug) {
  992. // Generate an image and a configuration file from the visible model
  993. ImageRgbaU8 atlasImage; String configText;
  994. sprite_generateFromModel(atlasImage, configText, visibleModel, shadowModel, ortho, targetPath, cameraAngles);
  995. // Save the result on success
  996. if (string_length(configText) > 0) {
  997. // Save the atlas
  998. String atlasPath = targetPath + U".png";
  999. // Try loading any existing image
  1000. ImageRgbaU8 existingAtlasImage = image_load_RgbaU8(atlasPath, false);
  1001. if (image_exists(existingAtlasImage)) {
  1002. int difference = image_maxDifference(atlasImage, existingAtlasImage);
  1003. if (difference <= 2) {
  1004. printText(" No significant changes against ", targetPath, ".\n");
  1005. } else {
  1006. image_save(atlasImage, atlasPath);
  1007. printText(" Updated ", targetPath, " with a deviation of ", difference, ".\n");
  1008. }
  1009. } else {
  1010. // Only save if there was no existing image or it differed significantly from the new result
  1011. // This comparison is made to avoid flooding version history with changes from invisible differences in color rounding
  1012. image_save(atlasImage, atlasPath);
  1013. printText(" Saved atlas to ", targetPath, ".\n");
  1014. }
  1015. // Save the configuration
  1016. String configPath = targetPath + U".ini";
  1017. String oldConfixText = string_load(configPath, false);
  1018. if (approximateTextMatch(configText, oldConfixText)) {
  1019. printText(" No significant changes against ", targetPath, ".\n\n");
  1020. } else {
  1021. string_save(targetPath + U".ini", configText);
  1022. printText(" Saved sprite config to ", targetPath, ".\n\n");
  1023. }
  1024. if (debug) {
  1025. ImageRgbaU8 debugImage; String garbageText;
  1026. // TODO: Show overlap between visible and shadow so that shadow outside of visible is displayed as bright red on a dark model.
  1027. // The number of visible shadow pixels should be reported automatically
  1028. // in an error message at the end of the total execution together with file names.
  1029. sprite_generateFromModel(debugImage, garbageText, shadowModel, Model(), ortho, targetPath + U"Debug", 8);
  1030. image_save(debugImage, targetPath + U"Debug.png");
  1031. }
  1032. }
  1033. }
  1034. }