spriteAPI.cpp 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483
  1. 
  2. #include "spriteAPI.h"
  3. #include "Octree.h"
  4. #include "DirtyRectangles.h"
  5. #include "importer.h"
  6. #include "../../DFPSR/render/ITriangle2D.h"
  7. #include "../../DFPSR/base/endian.h"
  8. #include "../../DFPSR/math/scalar.h"
  9. // Comment out a flag to disable an optimization when debugging
  10. #define DIRTY_RECTANGLE_OPTIMIZATION
  11. namespace dsr {
  12. template <bool HIGH_QUALITY>
  13. static IRect renderModel(const Model& model, OrthoView view, ImageF32 depthBuffer, ImageRgbaU8 diffuseTarget, ImageRgbaU8 normalTarget, const FVector2D& worldOrigin, const Transform3D& modelToWorldSpace);
  14. template <bool HIGH_QUALITY>
  15. static IRect renderDenseModel(const DenseModel& model, OrthoView view, ImageF32 depthBuffer, ImageRgbaU8 diffuseTarget, ImageRgbaU8 normalTarget, const FVector2D& worldOrigin, const Transform3D& modelToWorldSpace);
  16. static Transform3D combineWorldToScreenTransform(const FMatrix3x3& worldSpaceToScreenDepth, const FVector2D& worldOrigin) {
  17. return Transform3D(FVector3D(worldOrigin.x, worldOrigin.y, 0.0f), worldSpaceToScreenDepth);
  18. }
  19. static Transform3D combineModelToScreenTransform(const Transform3D& modelToWorldSpace, const FMatrix3x3& worldSpaceToScreenDepth, const FVector2D& worldOrigin) {
  20. return modelToWorldSpace * combineWorldToScreenTransform(worldSpaceToScreenDepth, worldOrigin);
  21. }
  22. static FVector3D IVector3DToFVector3D(const IVector3D& v) {
  23. return FVector3D(v.x, v.y, v.z);
  24. }
  25. static IVector3D FVector3DToIVector3D(const FVector3D& v) {
  26. return IVector3D(v.x, v.y, v.z);
  27. }
  28. struct SpriteConfig {
  29. int centerX, centerY; // The sprite's origin in pixels relative to the upper left corner
  30. int frameRows; // The atlas has one row for each frame
  31. int propertyColumns; // The atlas has one column for each type of information
  32. // The 3D model's bound in tile units
  33. // The height image goes from 0 at minimum Y to 255 at maximum Y
  34. FVector3D minBound, maxBound;
  35. // Shadow shapes
  36. List<FVector3D> points; // 3D points for the triangles to refer to by index
  37. List<int32_t> triangleIndices; // Triangle indices stored in multiples of three integers
  38. // Construction
  39. SpriteConfig(int centerX, int centerY, int frameRows, int propertyColumns, FVector3D minBound, FVector3D maxBound)
  40. : centerX(centerX), centerY(centerY), frameRows(frameRows), propertyColumns(propertyColumns), minBound(minBound), maxBound(maxBound) {}
  41. explicit SpriteConfig(const ReadableString& content) {
  42. config_parse_ini(content, [this](const ReadableString& block, const ReadableString& key, const ReadableString& value) {
  43. if (string_length(block) == 0) {
  44. if (string_caseInsensitiveMatch(key, U"CenterX")) {
  45. this->centerX = string_toInteger(value);
  46. } else if (string_caseInsensitiveMatch(key, U"CenterY")) {
  47. this->centerY = string_toInteger(value);
  48. } else if (string_caseInsensitiveMatch(key, U"FrameRows")) {
  49. this->frameRows = string_toInteger(value);
  50. } else if (string_caseInsensitiveMatch(key, U"PropertyColumns")) {
  51. this->propertyColumns = string_toInteger(value);
  52. } else if (string_caseInsensitiveMatch(key, U"MinBound")) {
  53. this->minBound = parseFVector3D(value);
  54. } else if (string_caseInsensitiveMatch(key, U"MaxBound")) {
  55. this->maxBound = parseFVector3D(value);
  56. } else if (string_caseInsensitiveMatch(key, U"Points")) {
  57. List<String> values = string_split(value, U',');
  58. if (values.length() % 3 != 0) {
  59. throwError("Points contained ", values.length(), " values, which is not evenly divisible by three!");
  60. } else {
  61. this->points.clear();
  62. this->points.reserve(values.length() / 3);
  63. for (int v = 0; v < values.length(); v += 3) {
  64. this->points.push(FVector3D(string_toDouble(values[v]), string_toDouble(values[v+1]), string_toDouble(values[v+2])));
  65. }
  66. }
  67. } else if (string_caseInsensitiveMatch(key, U"TriangleIndices")) {
  68. List<String> values = string_split(value, U',');
  69. if (values.length() % 3 != 0) {
  70. throwError("TriangleIndices contained ", values.length(), " values, which is not evenly divisible by three!");
  71. } else {
  72. this->triangleIndices.clear();
  73. this->triangleIndices.reserve(values.length());
  74. for (int v = 0; v < values.length(); v++) {
  75. this->triangleIndices.push(string_toInteger(values[v]));
  76. }
  77. }
  78. } else {
  79. printText("Unrecognized key \"", key, "\" in sprite configuration file.\n");
  80. }
  81. } else {
  82. printText("Unrecognized block \"", block, "\" in sprite configuration file.\n");
  83. }
  84. });
  85. }
  86. // Add model as a persistent shadow caster in the sprite configuration
  87. void appendShadow(const Model& model) {
  88. points.reserve(this->points.length() + model_getNumberOfPoints(model));
  89. for (int p = 0; p < model_getNumberOfPoints(model); p++) {
  90. this->points.push(model_getPoint(model, p));
  91. }
  92. for (int part = 0; part < model_getNumberOfParts(model); part++) {
  93. for (int poly = 0; poly < model_getNumberOfPolygons(model, part); poly++) {
  94. int vertexCount = model_getPolygonVertexCount(model, part, poly);
  95. int vertA = 0;
  96. int indexA = model_getVertexPointIndex(model, part, poly, vertA);
  97. for (int vertB = 1; vertB < vertexCount - 1; vertB++) {
  98. int vertC = vertB + 1;
  99. int indexB = model_getVertexPointIndex(model, part, poly, vertB);
  100. int indexC = model_getVertexPointIndex(model, part, poly, vertC);
  101. triangleIndices.push(indexA); triangleIndices.push(indexB); triangleIndices.push(indexC);
  102. }
  103. }
  104. }
  105. }
  106. String toIni() {
  107. // General information
  108. String result = string_combine(
  109. U"; Sprite configuration file\n",
  110. U"CenterX=", this->centerX, "\n",
  111. U"CenterY=", this->centerY, "\n",
  112. U"FrameRows=", this->frameRows, "\n",
  113. U"PropertyColumns=", this->propertyColumns, "\n",
  114. U"MinBound=", this->minBound, "\n",
  115. U"MaxBound=", this->maxBound, "\n"
  116. );
  117. // Low-resolution 3D shape
  118. if (this->points.length() > 0) {
  119. string_append(result, U"Points=");
  120. for (int p = 0; p < this->points.length(); p++) {
  121. if (p > 0) {
  122. string_append(result, U", ");
  123. }
  124. string_append(result, this->points[p]);
  125. }
  126. string_append(result, U"\n");
  127. string_append(result, U"TriangleIndices=");
  128. for (int i = 0; i < this->triangleIndices.length(); i+=3) {
  129. if (i > 0) {
  130. string_append(result, U", ");
  131. }
  132. string_append(result, this->triangleIndices[i], U",", this->triangleIndices[i+1], U",", this->triangleIndices[i+2]);
  133. }
  134. string_append(result, U"\n");
  135. }
  136. return result;
  137. }
  138. };
  139. static ImageF32 scaleHeightImage(const ImageRgbaU8& heightImage, float minHeight, float maxHeight, const ImageRgbaU8& colorImage) {
  140. float scale = (maxHeight - minHeight) / 255.0f;
  141. float offset = minHeight;
  142. int width = image_getWidth(heightImage);
  143. int height = image_getHeight(heightImage);
  144. ImageF32 result = image_create_F32(width, height);
  145. for (int y = 0; y < height; y++) {
  146. for (int x = 0; x < width; x++) {
  147. float value = image_readPixel_clamp(heightImage, x, y).red;
  148. if (image_readPixel_clamp(colorImage, x, y).alpha > 127) {
  149. image_writePixel(result, x, y, (value * scale) + offset);
  150. } else {
  151. image_writePixel(result, x, y, -std::numeric_limits<float>::infinity());
  152. }
  153. }
  154. }
  155. return result;
  156. }
  157. struct SpriteFrame {
  158. IVector2D centerPoint;
  159. ImageRgbaU8 colorImage; // (Red, Green, Blue, _)
  160. ImageRgbaU8 normalImage; // (NormalX, NormalY, NormalZ, _)
  161. ImageF32 heightImage;
  162. SpriteFrame(const IVector2D& centerPoint, const ImageRgbaU8& colorImage, const ImageRgbaU8& normalImage, const ImageF32& heightImage)
  163. : centerPoint(centerPoint), colorImage(colorImage), normalImage(normalImage), heightImage(heightImage) {}
  164. };
  165. struct SpriteType {
  166. public:
  167. IVector3D minBoundMini, maxBoundMini;
  168. List<SpriteFrame> frames;
  169. // TODO: Compress the data using a shadow-only model type of only positions and triangle indices in a single part.
  170. // The shadow model will have its own rendering method excluding the color target.
  171. // Shadow rendering can be a lot simpler by not calculating any vertex weights
  172. // just interpolate the depth using addition, compare to the old value and write the new depth value.
  173. Model shadowModel;
  174. public:
  175. // folderPath should end with a path separator
  176. SpriteType(const String& folderPath, const String& spriteName) {
  177. // Load the image atlas
  178. ImageRgbaU8 loadedAtlas = image_load_RgbaU8(string_combine(folderPath, spriteName, U".png"));
  179. // Load the settings
  180. const SpriteConfig configuration = SpriteConfig(string_load(string_combine(folderPath, spriteName, U".ini")));
  181. this->minBoundMini = IVector3D(
  182. floor(configuration.minBound.x * ortho_miniUnitsPerTile),
  183. floor(configuration.minBound.y * ortho_miniUnitsPerTile),
  184. floor(configuration.minBound.z * ortho_miniUnitsPerTile)
  185. );
  186. this->maxBoundMini = IVector3D(
  187. ceil(configuration.maxBound.x * ortho_miniUnitsPerTile),
  188. ceil(configuration.maxBound.y * ortho_miniUnitsPerTile),
  189. ceil(configuration.maxBound.z * ortho_miniUnitsPerTile)
  190. );
  191. int width = image_getWidth(loadedAtlas) / configuration.propertyColumns;
  192. int height = image_getHeight(loadedAtlas) / configuration.frameRows;
  193. for (int a = 0; a < configuration.frameRows; a++) {
  194. ImageRgbaU8 colorImage = image_getSubImage(loadedAtlas, IRect(0, a * height, width, height));
  195. ImageRgbaU8 heightImage = image_getSubImage(loadedAtlas, IRect(width, a * height, width, height));
  196. ImageRgbaU8 normalImage = image_getSubImage(loadedAtlas, IRect(width * 2, a * height, width, height));
  197. ImageF32 scaledHeightImage = scaleHeightImage(heightImage, configuration.minBound.y, configuration.maxBound.y, colorImage);
  198. this->frames.pushConstruct(IVector2D(configuration.centerX, configuration.centerY), colorImage, normalImage, scaledHeightImage);
  199. }
  200. // Create a model for rendering shadows
  201. if (configuration.points.length() > 0) {
  202. this->shadowModel = model_create();
  203. for (int p = 0; p < configuration.points.length(); p++) {
  204. model_addPoint(this->shadowModel, configuration.points[p]);
  205. }
  206. model_addEmptyPart(this->shadowModel, U"Shadow");
  207. for (int t = 0; t < configuration.triangleIndices.length(); t+=3) {
  208. model_addTriangle(this->shadowModel, 0, configuration.triangleIndices[t], configuration.triangleIndices[t+1], configuration.triangleIndices[t+2]);
  209. }
  210. }
  211. }
  212. public:
  213. // TODO: Force frame count to a power of two or replace modulo with look-up tables in sprite configurations.
  214. int getFrameIndex(Direction direction) {
  215. const int frameFromDir[dir360] = {4, 1, 5, 2, 6, 3, 7, 0};
  216. return frameFromDir[correctDirection(direction)] % this->frames.length();
  217. }
  218. };
  219. struct DenseTriangle {
  220. public:
  221. FVector3D colorA, colorB, colorC, posA, posB, posC, normalA, normalB, normalC;
  222. public:
  223. DenseTriangle() {}
  224. DenseTriangle(
  225. const FVector3D& colorA, const FVector3D& colorB, const FVector3D& colorC,
  226. const FVector3D& posA, const FVector3D& posB, const FVector3D& posC,
  227. const FVector3D& normalA, const FVector3D& normalB, const FVector3D& normalC)
  228. : colorA(colorA), colorB(colorB), colorC(colorC),
  229. posA(posA), posB(posB), posC(posC),
  230. normalA(normalA), normalB(normalB), normalC(normalC) {}
  231. };
  232. // The raw format for dense models using vertex colors instead of textures
  233. // Due to the high number of triangles, indexing positions would cause a lot of cache misses
  234. struct DenseModelImpl {
  235. public:
  236. Array<DenseTriangle> triangles;
  237. FVector3D minBound, maxBound;
  238. public:
  239. // Optimize an existing model
  240. DenseModelImpl(const Model& original);
  241. };
  242. struct ModelType {
  243. public:
  244. DenseModel visibleModel;
  245. Model shadowModel;
  246. public:
  247. // folderPath should end with a path separator
  248. ModelType(const String& folderPath, const String& visibleModelName, const String& shadowModelName) {
  249. this->visibleModel = DenseModel_create(importer_loadModel(folderPath + visibleModelName, true, Transform3D()));
  250. this->shadowModel = importer_loadModel(folderPath + shadowModelName, true, Transform3D());
  251. }
  252. ModelType(const DenseModel& visibleModel, const Model& shadowModel)
  253. : visibleModel(visibleModel), shadowModel(shadowModel) {}
  254. };
  255. // Global list of all sprite types ever loaded
  256. List<SpriteType> spriteTypes;
  257. int spriteWorld_loadSpriteTypeFromFile(const String& folderPath, const String& spriteName) {
  258. spriteTypes.pushConstruct(folderPath, spriteName);
  259. return spriteTypes.length() - 1;
  260. }
  261. int spriteWorld_getSpriteTypeCount() {
  262. return spriteTypes.length();
  263. }
  264. // Global list of all model types ever loaded
  265. List<ModelType> modelTypes;
  266. int spriteWorld_loadModelTypeFromFile(const String& folderPath, const String& visibleModelName, const String& shadowModelName) {
  267. modelTypes.pushConstruct(folderPath, visibleModelName, shadowModelName);
  268. return modelTypes.length() - 1;
  269. }
  270. int spriteWorld_getModelTypeCount() {
  271. return modelTypes.length();
  272. }
  273. static int getSpriteFrameIndex(const SpriteInstance& sprite, OrthoView view) {
  274. return spriteTypes[sprite.typeIndex].getFrameIndex(view.worldDirection + sprite.direction);
  275. }
  276. // Returns a 2D bounding box of affected target pixels
  277. static IRect drawSprite(const SpriteInstance& sprite, const OrthoView& ortho, const IVector2D& worldCenter, ImageF32 targetHeight, ImageRgbaU8 targetColor, ImageRgbaU8 targetNormal) {
  278. int frameIndex = getSpriteFrameIndex(sprite, ortho);
  279. const SpriteFrame* frame = &spriteTypes[sprite.typeIndex].frames[frameIndex];
  280. IVector2D screenSpace = ortho.miniTilePositionToScreenPixel(sprite.location, worldCenter) - frame->centerPoint;
  281. float heightOffset = sprite.location.y * ortho_tilesPerMiniUnit;
  282. draw_higher(targetHeight, frame->heightImage, targetColor, frame->colorImage, targetNormal, frame->normalImage, screenSpace.x, screenSpace.y, heightOffset);
  283. return IRect(screenSpace.x, screenSpace.y, image_getWidth(frame->colorImage), image_getHeight(frame->colorImage));
  284. }
  285. static IRect drawModel(const ModelInstance& instance, const OrthoView& ortho, const IVector2D& worldCenter, ImageF32 targetHeight, ImageRgbaU8 targetColor, ImageRgbaU8 targetNormal) {
  286. return renderDenseModel<false>(modelTypes[instance.typeIndex].visibleModel, ortho, targetHeight, targetColor, targetNormal, FVector2D(worldCenter.x, worldCenter.y), instance.location);
  287. }
  288. // The camera transform for each direction
  289. FMatrix3x3 ShadowCubeMapSides[6] = {
  290. FMatrix3x3::makeAxisSystem(FVector3D( 1.0f, 0.0f, 0.0f), FVector3D(0.0f, 1.0f, 0.0f)),
  291. FMatrix3x3::makeAxisSystem(FVector3D(-1.0f, 0.0f, 0.0f), FVector3D(0.0f, 1.0f, 0.0f)),
  292. FMatrix3x3::makeAxisSystem(FVector3D( 0.0f, 1.0f, 0.0f), FVector3D(0.0f, 0.0f, 1.0f)),
  293. FMatrix3x3::makeAxisSystem(FVector3D( 0.0f,-1.0f, 0.0f), FVector3D(0.0f, 0.0f, 1.0f)),
  294. FMatrix3x3::makeAxisSystem(FVector3D( 0.0f, 0.0f, 1.0f), FVector3D(0.0f, 1.0f, 0.0f)),
  295. FMatrix3x3::makeAxisSystem(FVector3D( 0.0f, 0.0f,-1.0f), FVector3D(0.0f, 1.0f, 0.0f))
  296. };
  297. // TODO: Move to the ortho API using a safe getter in modulo
  298. FMatrix3x3 spriteDirections[8] = {
  299. FMatrix3x3::makeAxisSystem(FVector3D( 0.0f, 0.0f, 1.0f), FVector3D(0.0f, 1.0f, 0.0f)),
  300. FMatrix3x3::makeAxisSystem(FVector3D( 1.0f, 0.0f, 1.0f), FVector3D(0.0f, 1.0f, 0.0f)),
  301. FMatrix3x3::makeAxisSystem(FVector3D( 1.0f, 0.0f, 0.0f), FVector3D(0.0f, 1.0f, 0.0f)),
  302. FMatrix3x3::makeAxisSystem(FVector3D( 1.0f, 0.0f,-1.0f), FVector3D(0.0f, 1.0f, 0.0f)),
  303. FMatrix3x3::makeAxisSystem(FVector3D( 0.0f, 0.0f,-1.0f), FVector3D(0.0f, 1.0f, 0.0f)),
  304. FMatrix3x3::makeAxisSystem(FVector3D(-1.0f, 0.0f,-1.0f), FVector3D(0.0f, 1.0f, 0.0f)),
  305. FMatrix3x3::makeAxisSystem(FVector3D(-1.0f, 0.0f, 0.0f), FVector3D(0.0f, 1.0f, 0.0f)),
  306. FMatrix3x3::makeAxisSystem(FVector3D(-1.0f, 0.0f, 1.0f), FVector3D(0.0f, 1.0f, 0.0f))
  307. };
  308. struct CubeMapF32 {
  309. int resolution; // The width and height of each shadow depth image or 0 if no shadows are casted
  310. AlignedImageF32 cubeMap; // A vertical sequence of reciprocal depth images for the six sides of the cube
  311. ImageF32 cubeMapViews[6]; // Sub-images sharing their allocations with cubeMap as sub-images
  312. explicit CubeMapF32(int resolution) : resolution(resolution) {
  313. this->cubeMap = image_create_F32(resolution, resolution * 6);
  314. for (int s = 0; s < 6; s++) {
  315. this->cubeMapViews[s] = image_getSubImage(this->cubeMap, IRect(0, s * resolution, resolution, resolution));
  316. }
  317. }
  318. void clear() {
  319. image_fill(this->cubeMap, 0.0f);
  320. }
  321. };
  322. class PointLight {
  323. public:
  324. FVector3D position; // The world-space center in tile units
  325. float radius; // The light radius in tile units
  326. float intensity; // The color's brightness multiplier (using float to allow smooth fading)
  327. ColorRgbI32 color; // The color of the light (using integers to detect when the color is uniform)
  328. bool shadowCasting; // Casting shadows when enabled
  329. public:
  330. PointLight(FVector3D position, float radius, float intensity, ColorRgbI32 color, bool shadowCasting)
  331. : position(position), radius(radius), intensity(intensity), color(color), shadowCasting(shadowCasting) {}
  332. public:
  333. void renderModelShadow(CubeMapF32& shadowTarget, const ModelInstance& modelInstance, const FMatrix3x3& normalToWorld) const {
  334. Model model = modelTypes[modelInstance.typeIndex].shadowModel;
  335. if (model_exists(model)) {
  336. // Place the model relative to the light source's position, to make rendering in light-space easier
  337. Transform3D modelToWorldTransform = modelInstance.location;
  338. modelToWorldTransform.position = modelToWorldTransform.position - this->position;
  339. for (int s = 0; s < 6; s++) {
  340. Camera camera = Camera::createPerspective(Transform3D(FVector3D(), ShadowCubeMapSides[s] * normalToWorld), shadowTarget.resolution, shadowTarget.resolution);
  341. model_renderDepth(model, modelToWorldTransform, shadowTarget.cubeMapViews[s], camera);
  342. }
  343. }
  344. }
  345. void renderSpriteShadow(CubeMapF32& shadowTarget, const SpriteInstance& spriteInstance, const FMatrix3x3& normalToWorld) const {
  346. if (spriteInstance.shadowCasting) {
  347. Model model = spriteTypes[spriteInstance.typeIndex].shadowModel;
  348. if (model_exists(model)) {
  349. // Place the model relative to the light source's position, to make rendering in light-space easier
  350. Transform3D modelToWorldTransform = Transform3D(ortho_miniToFloatingTile(spriteInstance.location) - this->position, spriteDirections[spriteInstance.direction]);
  351. for (int s = 0; s < 6; s++) {
  352. Camera camera = Camera::createPerspective(Transform3D(FVector3D(), ShadowCubeMapSides[s] * normalToWorld), shadowTarget.resolution, shadowTarget.resolution);
  353. model_renderDepth(model, modelToWorldTransform, shadowTarget.cubeMapViews[s], camera);
  354. }
  355. }
  356. }
  357. }
  358. // Render shadows from passive sprites
  359. void renderPassiveShadows(CubeMapF32& shadowTarget, Octree<SpriteInstance>& sprites, const FMatrix3x3& normalToWorld) const {
  360. IVector3D center = ortho_floatingTileToMini(this->position);
  361. IVector3D minBound = center - ortho_floatingTileToMini(radius);
  362. IVector3D maxBound = center + ortho_floatingTileToMini(radius);
  363. sprites.map(minBound, maxBound, [this, shadowTarget, normalToWorld](SpriteInstance& sprite, const IVector3D origin, const IVector3D minBound, const IVector3D maxBound) mutable {
  364. this->renderSpriteShadow(shadowTarget, sprite, normalToWorld);
  365. return LeafAction::None;
  366. });
  367. }
  368. // Render shadows from passive models
  369. void renderPassiveShadows(CubeMapF32& shadowTarget, Octree<ModelInstance>& models, const FMatrix3x3& normalToWorld) const {
  370. IVector3D center = ortho_floatingTileToMini(this->position);
  371. IVector3D minBound = center - ortho_floatingTileToMini(radius);
  372. IVector3D maxBound = center + ortho_floatingTileToMini(radius);
  373. models.map(minBound, maxBound, [this, shadowTarget, normalToWorld](ModelInstance& model, const IVector3D origin, const IVector3D minBound, const IVector3D maxBound) mutable {
  374. this->renderModelShadow(shadowTarget, model, normalToWorld);
  375. return LeafAction::None;
  376. });
  377. }
  378. public:
  379. void illuminate(const OrthoView& camera, const IVector2D& worldCenter, OrderedImageRgbaU8& lightBuffer, const OrderedImageRgbaU8& normalBuffer, const AlignedImageF32& heightBuffer, const CubeMapF32& shadowSource) const {
  380. if (this->shadowCasting) {
  381. addPointLight(camera, worldCenter, lightBuffer, normalBuffer, heightBuffer, this->position, this->radius, this->intensity, this->color, shadowSource.cubeMap);
  382. } else {
  383. addPointLight(camera, worldCenter, lightBuffer, normalBuffer, heightBuffer, this->position, this->radius, this->intensity, this->color);
  384. }
  385. }
  386. };
  387. class DirectedLight {
  388. public:
  389. FVector3D direction; // The world-space direction
  390. float intensity; // The color's brightness multiplier (using float to allow smooth fading)
  391. ColorRgbI32 color; // The color of the light (using integers to detect when the color is uniform)
  392. public:
  393. DirectedLight(FVector3D direction, float intensity, ColorRgbI32 color)
  394. : direction(direction), intensity(intensity), color(color) {}
  395. public:
  396. void illuminate(const OrthoView& camera, const IVector2D& worldCenter, OrderedImageRgbaU8& lightBuffer, const OrderedImageRgbaU8& normalBuffer, bool overwrite = false) const {
  397. if (overwrite) {
  398. setDirectedLight(camera, lightBuffer, normalBuffer, this->direction, this->intensity, this->color);
  399. } else {
  400. addDirectedLight(camera, lightBuffer, normalBuffer, this->direction, this->intensity, this->color);
  401. }
  402. }
  403. };
  404. IVector3D getBoxCorner(const IVector3D& minBound, const IVector3D& maxBound, int cornerIndex) {
  405. assert(cornerIndex >= 0 && cornerIndex < 8);
  406. return IVector3D(
  407. ((uint32_t)cornerIndex & 1u) ? maxBound.x : minBound.x,
  408. ((uint32_t)cornerIndex & 2u) ? maxBound.y : minBound.y,
  409. ((uint32_t)cornerIndex & 4u) ? maxBound.z : minBound.z
  410. );
  411. }
  412. static bool orthoCullingTest(const OrthoView& ortho, const IVector3D& minBound, const IVector3D& maxBound, const IRect& seenRegion) {
  413. IVector2D corners[8];
  414. for (int c = 0; c < 8; c++) {
  415. corners[c] = ortho.miniTileOffsetToScreenPixel(getBoxCorner(minBound, maxBound, c));
  416. }
  417. if (corners[0].x < seenRegion.left()
  418. && corners[1].x < seenRegion.left()
  419. && corners[2].x < seenRegion.left()
  420. && corners[3].x < seenRegion.left()
  421. && corners[4].x < seenRegion.left()
  422. && corners[5].x < seenRegion.left()
  423. && corners[6].x < seenRegion.left()
  424. && corners[7].x < seenRegion.left()) {
  425. return false;
  426. }
  427. if (corners[0].x > seenRegion.right()
  428. && corners[1].x > seenRegion.right()
  429. && corners[2].x > seenRegion.right()
  430. && corners[3].x > seenRegion.right()
  431. && corners[4].x > seenRegion.right()
  432. && corners[5].x > seenRegion.right()
  433. && corners[6].x > seenRegion.right()
  434. && corners[7].x > seenRegion.right()) {
  435. return false;
  436. }
  437. if (corners[0].y < seenRegion.top()
  438. && corners[1].y < seenRegion.top()
  439. && corners[2].y < seenRegion.top()
  440. && corners[3].y < seenRegion.top()
  441. && corners[4].y < seenRegion.top()
  442. && corners[5].y < seenRegion.top()
  443. && corners[6].y < seenRegion.top()
  444. && corners[7].y < seenRegion.top()) {
  445. return false;
  446. }
  447. if (corners[0].y > seenRegion.bottom()
  448. && corners[1].y > seenRegion.bottom()
  449. && corners[2].y > seenRegion.bottom()
  450. && corners[3].y > seenRegion.bottom()
  451. && corners[4].y > seenRegion.bottom()
  452. && corners[5].y > seenRegion.bottom()
  453. && corners[6].y > seenRegion.bottom()
  454. && corners[7].y > seenRegion.bottom()) {
  455. return false;
  456. }
  457. return true;
  458. }
  459. // BlockState keeps track of when the background itself needs to update from static objects being created or destroyed
  460. enum class BlockState {
  461. Unused,
  462. Ready,
  463. Dirty
  464. };
  465. class BackgroundBlock {
  466. public:
  467. static const int blockSize = 512;
  468. static const int maxDistance = blockSize * 2;
  469. IRect worldRegion;
  470. int cameraId = 0;
  471. BlockState state = BlockState::Unused;
  472. OrderedImageRgbaU8 diffuseBuffer;
  473. OrderedImageRgbaU8 normalBuffer;
  474. AlignedImageF32 heightBuffer;
  475. private:
  476. // Pre-condition: diffuseBuffer must be cleared unless sprites cover the whole block
  477. void draw(Octree<SpriteInstance>& sprites, Octree<ModelInstance>& models, const OrthoView& ortho) {
  478. image_fill(this->normalBuffer, ColorRgbaI32(128));
  479. image_fill(this->heightBuffer, -std::numeric_limits<float>::max());
  480. OcTreeFilter orthoCullingFilter = [ortho,this](const IVector3D& minBound, const IVector3D& maxBound){
  481. return orthoCullingTest(ortho, minBound, maxBound, this->worldRegion);
  482. };
  483. sprites.map(orthoCullingFilter, [this, ortho](SpriteInstance& sprite, const IVector3D origin, const IVector3D minBound, const IVector3D maxBound){
  484. drawSprite(sprite, ortho, -this->worldRegion.upperLeft(), this->heightBuffer, this->diffuseBuffer, this->normalBuffer);
  485. return LeafAction::None;
  486. });
  487. models.map(orthoCullingFilter, [this, ortho](ModelInstance& model, const IVector3D origin, const IVector3D minBound, const IVector3D maxBound){
  488. drawModel(model, ortho, -this->worldRegion.upperLeft(), this->heightBuffer, this->diffuseBuffer, this->normalBuffer);
  489. return LeafAction::None;
  490. });
  491. }
  492. public:
  493. BackgroundBlock(Octree<SpriteInstance>& sprites, Octree<ModelInstance>& models, const IRect& worldRegion, const OrthoView& ortho)
  494. : worldRegion(worldRegion), cameraId(ortho.id), state(BlockState::Ready),
  495. diffuseBuffer(image_create_RgbaU8(blockSize, blockSize)),
  496. normalBuffer(image_create_RgbaU8(blockSize, blockSize)),
  497. heightBuffer(image_create_F32(blockSize, blockSize)) {
  498. this->draw(sprites, models, ortho);
  499. }
  500. void update(Octree<SpriteInstance>& sprites, Octree<ModelInstance>& models, const IRect& worldRegion, const OrthoView& ortho) {
  501. this->worldRegion = worldRegion;
  502. this->cameraId = ortho.id;
  503. image_fill(this->diffuseBuffer, ColorRgbaI32(0));
  504. this->draw(sprites, models, ortho);
  505. this->state = BlockState::Ready;
  506. }
  507. void draw(ImageRgbaU8& diffuseTarget, ImageRgbaU8& normalTarget, ImageF32& heightTarget, const IRect& seenRegion) const {
  508. if (this->state != BlockState::Unused) {
  509. int left = this->worldRegion.left() - seenRegion.left();
  510. int top = this->worldRegion.top() - seenRegion.top();
  511. draw_copy(diffuseTarget, this->diffuseBuffer, left, top);
  512. draw_copy(normalTarget, this->normalBuffer, left, top);
  513. draw_copy(heightTarget, this->heightBuffer, left, top);
  514. }
  515. }
  516. void recycle() {
  517. //printText("Recycle block at ", this->worldRegion, "\n");
  518. this->state = BlockState::Unused;
  519. this->worldRegion = IRect();
  520. this->cameraId = -1;
  521. }
  522. };
  523. // TODO: A way to delete passive sprites and models using search criterias for bounding box and leaf content using a boolean lambda
  524. class SpriteWorldImpl {
  525. public:
  526. // World
  527. OrthoSystem ortho;
  528. // Having one passive and one active collection per member type allow packing elements tighter to reduce cache misses.
  529. // It also allow executing rendering sorted by which code has to be fetched into the instruction cache.
  530. // Sprites that rarely change and can be stored in a background image.
  531. Octree<SpriteInstance> passiveSprites;
  532. // Rarely moved models can be rendered using free rotation and uniform scaling to the background image.
  533. Octree<ModelInstance> passiveModels;
  534. // Temporary things are deleted when spriteWorld_clearTemporary is called
  535. List<SpriteInstance> temporarySprites;
  536. List<ModelInstance> temporaryModels;
  537. List<PointLight> temporaryPointLights;
  538. List<DirectedLight> temporaryDirectedLights;
  539. // View
  540. int cameraIndex = 0;
  541. IVector3D cameraLocation;
  542. // Deferred rendering
  543. OrderedImageRgbaU8 diffuseBuffer;
  544. OrderedImageRgbaU8 normalBuffer;
  545. AlignedImageF32 heightBuffer;
  546. OrderedImageRgbaU8 lightBuffer;
  547. // Passive background
  548. // TODO: How can split-screen use multiple cameras without duplicate blocks or deleting the other camera's blocks by distance?
  549. List<BackgroundBlock> backgroundBlocks;
  550. // 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
  551. DirtyRectangles dirtyBackground;
  552. private:
  553. // Reused buffers
  554. int shadowResolution;
  555. CubeMapF32 temporaryShadowMap;
  556. public:
  557. SpriteWorldImpl(const OrthoSystem &ortho, int shadowResolution)
  558. : ortho(ortho), passiveSprites(ortho_miniUnitsPerTile * 64), passiveModels(ortho_miniUnitsPerTile * 64), shadowResolution(shadowResolution), temporaryShadowMap(shadowResolution) {}
  559. public:
  560. void updateBlockAt(const IRect& blockRegion, const IRect& seenRegion) {
  561. int unusedBlockIndex = -1;
  562. // Find an existing block
  563. for (int b = 0; b < this->backgroundBlocks.length(); b++) {
  564. BackgroundBlock* currentBlockPtr = &this->backgroundBlocks[b];
  565. if (currentBlockPtr->state != BlockState::Unused) {
  566. // Check direction
  567. if (currentBlockPtr->cameraId == this->ortho.view[this->cameraIndex].id) {
  568. // Check location
  569. if (currentBlockPtr->worldRegion.left() == blockRegion.left() && currentBlockPtr->worldRegion.top() == blockRegion.top()) {
  570. // Update if needed
  571. if (currentBlockPtr->state == BlockState::Dirty) {
  572. currentBlockPtr->update(this->passiveSprites, this->passiveModels, blockRegion, this->ortho.view[this->cameraIndex]);
  573. }
  574. // Use the block
  575. return;
  576. } else {
  577. // See if the block is too far from the camera
  578. if (currentBlockPtr->worldRegion.right() < seenRegion.left() - BackgroundBlock::maxDistance
  579. || currentBlockPtr->worldRegion.left() > seenRegion.right() + BackgroundBlock::maxDistance
  580. || currentBlockPtr->worldRegion.bottom() < seenRegion.top() - BackgroundBlock::maxDistance
  581. || currentBlockPtr->worldRegion.top() > seenRegion.bottom() + BackgroundBlock::maxDistance) {
  582. // Recycle because it's too far away
  583. currentBlockPtr->recycle();
  584. unusedBlockIndex = b;
  585. }
  586. }
  587. } else{
  588. // Recycle directly when another camera angle is used
  589. currentBlockPtr->recycle();
  590. unusedBlockIndex = b;
  591. }
  592. } else {
  593. unusedBlockIndex = b;
  594. }
  595. }
  596. // If none of them matched, we should've passed by any unused block already
  597. if (unusedBlockIndex > -1) {
  598. // We have a block to reuse
  599. this->backgroundBlocks[unusedBlockIndex].update(this->passiveSprites, this->passiveModels, blockRegion, this->ortho.view[this->cameraIndex]);
  600. } else {
  601. // Create a new block
  602. this->backgroundBlocks.pushConstruct(this->passiveSprites, this->passiveModels, blockRegion, this->ortho.view[this->cameraIndex]);
  603. }
  604. }
  605. void invalidateBlockAt(int left, int top) {
  606. // Find an existing block
  607. for (int b = 0; b < this->backgroundBlocks.length(); b++) {
  608. BackgroundBlock* currentBlockPtr = &this->backgroundBlocks[b];
  609. // Assuming that alternative camera angles will be removed when drawing next time
  610. if (currentBlockPtr->state == BlockState::Ready
  611. && currentBlockPtr->worldRegion.left() == left
  612. && currentBlockPtr->worldRegion.top() == top) {
  613. // Make dirty to force an update
  614. currentBlockPtr->state = BlockState::Dirty;
  615. }
  616. }
  617. }
  618. // Make sure that each pixel in seenRegion is occupied by an updated background block
  619. void updateBlocks(const IRect& seenRegion) {
  620. // Round inclusive pixel indices down to containing blocks and iterate over them in strides along x and y
  621. int64_t roundedLeft = roundDown(seenRegion.left(), BackgroundBlock::blockSize);
  622. int64_t roundedTop = roundDown(seenRegion.top(), BackgroundBlock::blockSize);
  623. int64_t roundedRight = roundDown(seenRegion.right() - 1, BackgroundBlock::blockSize);
  624. int64_t roundedBottom = roundDown(seenRegion.bottom() - 1, BackgroundBlock::blockSize);
  625. for (int64_t y = roundedTop; y <= roundedBottom; y += BackgroundBlock::blockSize) {
  626. for (int64_t x = roundedLeft; x <= roundedRight; x += BackgroundBlock::blockSize) {
  627. // Make sure that a block is allocated and pre-drawn at this location
  628. this->updateBlockAt(IRect(x, y, BackgroundBlock::blockSize, BackgroundBlock::blockSize), seenRegion);
  629. }
  630. }
  631. }
  632. void drawDeferred(OrderedImageRgbaU8& diffuseTarget, OrderedImageRgbaU8& normalTarget, AlignedImageF32& heightTarget, const IRect& seenRegion) {
  633. // Check image dimensions
  634. assert(image_getWidth(diffuseTarget) == seenRegion.width() && image_getHeight(diffuseTarget) == seenRegion.height());
  635. assert(image_getWidth(normalTarget) == seenRegion.width() && image_getHeight(normalTarget) == seenRegion.height());
  636. assert(image_getWidth(heightTarget) == seenRegion.width() && image_getHeight(heightTarget) == seenRegion.height());
  637. this->dirtyBackground.setTargetResolution(seenRegion.width(), seenRegion.height());
  638. // Draw passive sprites to blocks
  639. this->updateBlocks(seenRegion);
  640. // Draw background blocks to the target images
  641. for (int b = 0; b < this->backgroundBlocks.length(); b++) {
  642. #ifdef DIRTY_RECTANGLE_OPTIMIZATION
  643. // Optimized version
  644. for (int64_t r = 0; r < this->dirtyBackground.getRectangleCount(); r++) {
  645. IRect screenClip = this->dirtyBackground.getRectangle(r);
  646. IRect worldClip = screenClip + seenRegion.upperLeft();
  647. ImageRgbaU8 clippedDiffuseTarget = image_getSubImage(diffuseTarget, screenClip);
  648. ImageRgbaU8 clippedNormalTarget = image_getSubImage(normalTarget, screenClip);
  649. ImageF32 clippedHeightTarget = image_getSubImage(heightTarget, screenClip);
  650. this->backgroundBlocks[b].draw(clippedDiffuseTarget, clippedNormalTarget, clippedHeightTarget, worldClip);
  651. }
  652. #else
  653. // Reference implementation
  654. this->backgroundBlocks[b].draw(diffuseTarget, normalTarget, heightTarget, seenRegion);
  655. #endif
  656. }
  657. // Reset dirty rectangles so that active sprites may record changes
  658. this->dirtyBackground.noneDirty();
  659. // Draw active sprites to the targets
  660. for (int s = 0; s < this->temporarySprites.length(); s++) {
  661. IRect drawnRegion = drawSprite(this->temporarySprites[s], this->ortho.view[this->cameraIndex], -seenRegion.upperLeft(), heightTarget, diffuseTarget, normalTarget);
  662. this->dirtyBackground.makeRegionDirty(drawnRegion);
  663. }
  664. for (int s = 0; s < this->temporaryModels.length(); s++) {
  665. IRect drawnRegion = drawModel(this->temporaryModels[s], this->ortho.view[this->cameraIndex], -seenRegion.upperLeft(), heightTarget, diffuseTarget, normalTarget);
  666. this->dirtyBackground.makeRegionDirty(drawnRegion);
  667. }
  668. }
  669. public:
  670. // modifiedRegion is given in pixels relative to the world origin for the current camera angle
  671. void updatePassiveRegion(const IRect& modifiedRegion) {
  672. int64_t roundedLeft = roundDown(modifiedRegion.left(), BackgroundBlock::blockSize);
  673. int64_t roundedTop = roundDown(modifiedRegion.top(), BackgroundBlock::blockSize);
  674. int64_t roundedRight = roundDown(modifiedRegion.right() - 1, BackgroundBlock::blockSize);
  675. int64_t roundedBottom = roundDown(modifiedRegion.bottom() - 1, BackgroundBlock::blockSize);
  676. for (int64_t y = roundedTop; y <= roundedBottom; y += BackgroundBlock::blockSize) {
  677. for (int64_t x = roundedLeft; x <= roundedRight; x += BackgroundBlock::blockSize) {
  678. // Make sure that a block is allocated and pre-drawn at this location
  679. this->invalidateBlockAt(x, y);
  680. }
  681. }
  682. // Redrawing the whole background to the screen is very cheap using memcpy, so no need to optimize this rare event
  683. this->dirtyBackground.allDirty();
  684. }
  685. IVector2D findWorldCenter(const AlignedImageRgbaU8& colorTarget) const {
  686. return IVector2D(image_getWidth(colorTarget) / 2, image_getHeight(colorTarget) / 2) - this->ortho.miniTileOffsetToScreenPixel(this->cameraLocation, this->cameraIndex);
  687. }
  688. void draw(AlignedImageRgbaU8& colorTarget) {
  689. double startTime;
  690. IVector2D worldCenter = this->findWorldCenter(colorTarget);
  691. // Resize when the window has resized or the buffers haven't been allocated before
  692. int width = image_getWidth(colorTarget);
  693. int height = image_getHeight(colorTarget);
  694. if (image_getWidth(this->diffuseBuffer) != width || image_getHeight(this->diffuseBuffer) != height) {
  695. this->diffuseBuffer = image_create_RgbaU8(width, height);
  696. this->normalBuffer = image_create_RgbaU8(width, height);
  697. this->lightBuffer = image_create_RgbaU8(width, height);
  698. this->heightBuffer = image_create_F32(width, height);
  699. }
  700. IRect worldRegion = IRect(-worldCenter.x, -worldCenter.y, width, height);
  701. startTime = time_getSeconds();
  702. this->drawDeferred(this->diffuseBuffer, this->normalBuffer, this->heightBuffer, worldRegion);
  703. debugText("Draw deferred: ", (time_getSeconds() - startTime) * 1000.0, " ms\n");
  704. // Illuminate using directed lights
  705. if (this->temporaryDirectedLights.length() > 0) {
  706. startTime = time_getSeconds();
  707. // Overwriting any light from the previous frame
  708. for (int p = 0; p < this->temporaryDirectedLights.length(); p++) {
  709. this->temporaryDirectedLights[p].illuminate(this->ortho.view[this->cameraIndex], worldCenter, this->lightBuffer, this->normalBuffer, p == 0);
  710. }
  711. debugText("Sun light: ", (time_getSeconds() - startTime) * 1000.0, " ms\n");
  712. } else {
  713. startTime = time_getSeconds();
  714. image_fill(this->lightBuffer, ColorRgbaI32(0)); // Set light to black
  715. debugText("Clear light: ", (time_getSeconds() - startTime) * 1000.0, " ms\n");
  716. }
  717. // Illuminate using point lights
  718. for (int p = 0; p < this->temporaryPointLights.length(); p++) {
  719. PointLight *currentLight = &this->temporaryPointLights[p];
  720. if (currentLight->shadowCasting) {
  721. startTime = time_getSeconds();
  722. this->temporaryShadowMap.clear();
  723. // Shadows from background sprites
  724. currentLight->renderPassiveShadows(this->temporaryShadowMap, this->passiveSprites, ortho.view[this->cameraIndex].normalToWorldSpace);
  725. currentLight->renderPassiveShadows(this->temporaryShadowMap, this->passiveModels, ortho.view[this->cameraIndex].normalToWorldSpace);
  726. // Shadows from temporary sprites
  727. for (int s = 0; s < this->temporarySprites.length(); s++) {
  728. currentLight->renderSpriteShadow(this->temporaryShadowMap, this->temporarySprites[s], ortho.view[this->cameraIndex].normalToWorldSpace);
  729. }
  730. // Shadows from temporary models
  731. for (int s = 0; s < this->temporaryModels.length(); s++) {
  732. currentLight->renderModelShadow(this->temporaryShadowMap, this->temporaryModels[s], ortho.view[this->cameraIndex].normalToWorldSpace);
  733. }
  734. debugText("Cast point-light shadows: ", (time_getSeconds() - startTime) * 1000.0, " ms\n");
  735. }
  736. startTime = time_getSeconds();
  737. currentLight->illuminate(this->ortho.view[this->cameraIndex], worldCenter, this->lightBuffer, this->normalBuffer, this->heightBuffer, this->temporaryShadowMap);
  738. debugText("Illuminate from point-light: ", (time_getSeconds() - startTime) * 1000.0, " ms\n");
  739. }
  740. // Draw the final image to the target by multiplying diffuse with light
  741. startTime = time_getSeconds();
  742. blendLight(colorTarget, this->diffuseBuffer, this->lightBuffer);
  743. debugText("Blend light: ", (time_getSeconds() - startTime) * 1000.0, " ms\n");
  744. }
  745. };
  746. SpriteWorld spriteWorld_create(OrthoSystem ortho, int shadowResolution) {
  747. return std::make_shared<SpriteWorldImpl>(ortho, shadowResolution);
  748. }
  749. #define MUST_EXIST(OBJECT, METHOD) if (OBJECT.get() == nullptr) { throwError("The " #OBJECT " handle was null in " #METHOD "\n"); }
  750. // Get the eight corners of an axis-aligned bounding box
  751. static void getCorners(const FVector3D& minBound, const FVector3D& maxBound, FVector3D* resultCorners) {
  752. resultCorners[0] = FVector3D(minBound.x, minBound.y, minBound.z);
  753. resultCorners[1] = FVector3D(maxBound.x, minBound.y, minBound.z);
  754. resultCorners[2] = FVector3D(minBound.x, maxBound.y, minBound.z);
  755. resultCorners[3] = FVector3D(maxBound.x, maxBound.y, minBound.z);
  756. resultCorners[4] = FVector3D(minBound.x, minBound.y, maxBound.z);
  757. resultCorners[5] = FVector3D(maxBound.x, minBound.y, maxBound.z);
  758. resultCorners[6] = FVector3D(minBound.x, maxBound.y, maxBound.z);
  759. resultCorners[7] = FVector3D(maxBound.x, maxBound.y, maxBound.z);
  760. }
  761. // Transform the eight corners of an axis-aligned bounding box
  762. static void transformCorners(const FVector3D& minBound, const FVector3D& maxBound, const Transform3D& transform, FVector3D* resultCorners) {
  763. resultCorners[0] = transform.transformPoint(FVector3D(minBound.x, minBound.y, minBound.z));
  764. resultCorners[1] = transform.transformPoint(FVector3D(maxBound.x, minBound.y, minBound.z));
  765. resultCorners[2] = transform.transformPoint(FVector3D(minBound.x, maxBound.y, minBound.z));
  766. resultCorners[3] = transform.transformPoint(FVector3D(maxBound.x, maxBound.y, minBound.z));
  767. resultCorners[4] = transform.transformPoint(FVector3D(minBound.x, minBound.y, maxBound.z));
  768. resultCorners[5] = transform.transformPoint(FVector3D(maxBound.x, minBound.y, maxBound.z));
  769. resultCorners[6] = transform.transformPoint(FVector3D(minBound.x, maxBound.y, maxBound.z));
  770. resultCorners[7] = transform.transformPoint(FVector3D(maxBound.x, maxBound.y, maxBound.z));
  771. }
  772. // References:
  773. // world contains the camera system for convenience
  774. // Input:
  775. // transform tells how the local bounds are transformed into mini-tile world-space
  776. // localMinBound and localMaxBound is the local mini-tile bound relative to the given origin
  777. // Output:
  778. // worldMinBound and worldMaxBound is the bound in mini-tile coordinates relative to world origin
  779. static void get3DBounds(
  780. SpriteWorld& world, const Transform3D& transform, const FVector3D& localMinBound, const FVector3D& localMaxBound, IVector3D& worldMinBound, IVector3D& worldMaxBound) {
  781. // Transform from local to global coordinates
  782. FVector3D transformedCorners[8];
  783. transformCorners(localMinBound, localMaxBound, transform, transformedCorners);
  784. // Initialize 3D bound to the center point so that tree branches expand bounds to include the origins of every leaf
  785. // This make searches a lot easier for off-centered sprites and models by belonging to a coordinate independent of the design
  786. worldMinBound = FVector3DToIVector3D(transform.position);
  787. worldMaxBound = FVector3DToIVector3D(transform.position);
  788. for (int c = 0; c < 8; c++) {
  789. FVector3D miniSpaceCorner = transformedCorners[c];
  790. replaceWithSmaller(worldMinBound.x, (int32_t)floor(miniSpaceCorner.x));
  791. replaceWithSmaller(worldMinBound.y, (int32_t)floor(miniSpaceCorner.y));
  792. replaceWithSmaller(worldMinBound.z, (int32_t)floor(miniSpaceCorner.z));
  793. replaceWithLarger(worldMaxBound.x, (int32_t)ceil(miniSpaceCorner.x));
  794. replaceWithLarger(worldMaxBound.y, (int32_t)ceil(miniSpaceCorner.y));
  795. replaceWithLarger(worldMaxBound.z, (int32_t)ceil(miniSpaceCorner.z));
  796. }
  797. }
  798. // References:
  799. // world contains the camera system for convenience
  800. // Input:
  801. // worldMinBound and worldMaxBound is the bound in mini-tile coordinates relative to world origin
  802. // Output:
  803. // globalPixelMinBound and globalPixelMaxBound is the bound in pixel coordinates relative to world origin
  804. static void getScreenBounds(SpriteWorld& world, const IVector3D& worldMinBound, const IVector3D& worldMaxBound, IVector2D& globalPixelMinBound, IVector2D& globalPixelMaxBound) {
  805. // Create a transform for global pixels
  806. Transform3D worldToGlobalPixels = combineWorldToScreenTransform(world->ortho.view[world->cameraIndex].worldSpaceToScreenDepth, FVector2D());
  807. FVector3D corners[8];
  808. getCorners(IVector3DToFVector3D(worldMinBound) * ortho_tilesPerMiniUnit, IVector3DToFVector3D(worldMaxBound) * ortho_tilesPerMiniUnit, corners);
  809. // Screen bound
  810. FVector3D firstGlobalPixelSpaceCorner = worldToGlobalPixels.transformPoint(corners[0]);
  811. globalPixelMinBound = IVector2D((int32_t)floor(firstGlobalPixelSpaceCorner.x), (int32_t)floor(firstGlobalPixelSpaceCorner.y));
  812. globalPixelMaxBound = IVector2D((int32_t)ceil(firstGlobalPixelSpaceCorner.x), (int32_t)ceil(firstGlobalPixelSpaceCorner.y));
  813. for (int c = 0; c < 8; c++) {
  814. FVector3D globalPixelSpaceCorner = worldToGlobalPixels.transformPoint(corners[c]);
  815. replaceWithSmaller(globalPixelMinBound.x, (int32_t)floor(globalPixelSpaceCorner.x));
  816. replaceWithSmaller(globalPixelMinBound.y, (int32_t)floor(globalPixelSpaceCorner.y));
  817. replaceWithLarger(globalPixelMaxBound.x, (int32_t)ceil(globalPixelSpaceCorner.x));
  818. replaceWithLarger(globalPixelMaxBound.y, (int32_t)ceil(globalPixelSpaceCorner.y));
  819. }
  820. }
  821. void spriteWorld_addBackgroundSprite(SpriteWorld& world, const SpriteInstance& sprite) {
  822. MUST_EXIST(world, spriteWorld_addBackgroundSprite);
  823. if (sprite.typeIndex < 0 || sprite.typeIndex >= spriteTypes.length()) { throwError(U"Sprite type index ", sprite.typeIndex, " is out of bound!\n"); }
  824. // Get world aligned 3D bounds based on the local bounding box
  825. IVector3D worldMinBound = sprite.location, worldMaxBound = sprite.location;
  826. get3DBounds(world, Transform3D(IVector3DToFVector3D(sprite.location), spriteDirections[sprite.direction]), IVector3DToFVector3D(spriteTypes[sprite.typeIndex].minBoundMini), IVector3DToFVector3D(spriteTypes[sprite.typeIndex].maxBoundMini), worldMinBound, worldMaxBound);
  827. // No need for getScreenBounds when the sprite has known image bounds that are more precise
  828. // Add the passive sprite to the octree
  829. world->passiveSprites.insert(sprite, sprite.location, worldMinBound, worldMaxBound);
  830. // Find the affected passive region and make it dirty
  831. int frameIndex = getSpriteFrameIndex(sprite, world->ortho.view[world->cameraIndex]);
  832. const SpriteFrame* frame = &spriteTypes[sprite.typeIndex].frames[frameIndex];
  833. IVector2D upperLeft = world->ortho.miniTilePositionToScreenPixel(sprite.location, world->cameraIndex, IVector2D()) - frame->centerPoint;
  834. IRect region = IRect(upperLeft.x, upperLeft.y, image_getWidth(frame->colorImage), image_getHeight(frame->colorImage));
  835. world->updatePassiveRegion(region);
  836. }
  837. void spriteWorld_addBackgroundModel(SpriteWorld& world, const ModelInstance& instance) {
  838. MUST_EXIST(world, spriteWorld_addBackgroundModel);
  839. if (instance.typeIndex < 0 || instance.typeIndex >= modelTypes.length()) { throwError(U"Model type index ", instance.typeIndex, " is out of bound!\n"); }
  840. // Get the origin and outer bounds
  841. ModelType *type = &(modelTypes[instance.typeIndex]);
  842. // Transform the bounds
  843. IVector3D origin = ortho_floatingTileToMini(instance.location.position);
  844. // Get world aligned 3D bounds based on the local bounding box
  845. IVector3D worldMinBound = origin, worldMaxBound = origin;
  846. IVector2D globalPixelMinBound, globalPixelMaxBound;
  847. Transform3D transform = Transform3D(instance.location.position * (float)ortho_miniUnitsPerTile, instance.location.transform);
  848. get3DBounds(world, transform, type->visibleModel->minBound * (float)ortho_miniUnitsPerTile, type->visibleModel->maxBound * (float)ortho_miniUnitsPerTile, worldMinBound, worldMaxBound);
  849. // Getting screen bounds from world aligned bounds will grow even more when transformed to the screen, but this won't affect already dirty regions when adding many models at the same time
  850. getScreenBounds(world, worldMinBound, worldMaxBound, globalPixelMinBound, globalPixelMaxBound);
  851. // Add the passive model to the octree
  852. world->passiveModels.insert(instance, origin, worldMinBound, worldMaxBound);
  853. // Make the affected region dirty
  854. world->updatePassiveRegion(IRect(globalPixelMinBound.x, globalPixelMinBound.y, globalPixelMaxBound.x - globalPixelMinBound.x, globalPixelMaxBound.y - globalPixelMinBound.y));
  855. }
  856. //using SpriteSelection = std::function<bool(SpriteInstance&, const IVector3D, const IVector3D, const IVector3D)>;
  857. void spriteWorld_removeBackgroundSprites(SpriteWorld& world, const IVector3D& searchMinBound, const IVector3D& searchMaxBound, const SpriteSelection& filter) {
  858. world->passiveSprites.map(searchMinBound, searchMaxBound, [world, filter](SpriteInstance& sprite, const IVector3D origin, const IVector3D minBound, const IVector3D maxBound) mutable {
  859. if (filter(sprite, origin, minBound, maxBound)) {
  860. IVector2D globalPixelMinBound, globalPixelMaxBound;
  861. getScreenBounds(world, minBound, maxBound, globalPixelMinBound, globalPixelMaxBound);
  862. world->updatePassiveRegion(IRect(globalPixelMinBound.x, globalPixelMinBound.y, globalPixelMaxBound.x - globalPixelMinBound.x, globalPixelMaxBound.y - globalPixelMinBound.y));
  863. return LeafAction::Erase;
  864. } else {
  865. return LeafAction::None;
  866. }
  867. });
  868. }
  869. void spriteWorld_removeBackgroundSprites(SpriteWorld& world, const IVector3D& searchMinBound, const IVector3D& searchMaxBound) {
  870. spriteWorld_removeBackgroundSprites(world, searchMinBound, searchMaxBound, [](SpriteInstance& sprite, const IVector3D origin, const IVector3D minBound, const IVector3D maxBound) {
  871. return true; // Erase everything in the bound
  872. });
  873. }
  874. //using ModelSelection = std::function<bool(ModelInstance&, const IVector3D, const IVector3D, const IVector3D)>;
  875. void spriteWorld_removeBackgroundModels(SpriteWorld& world, const IVector3D& searchMinBound, const IVector3D& searchMaxBound, const ModelSelection& filter) {
  876. world->passiveModels.map(searchMinBound, searchMaxBound, [world, filter](ModelInstance& model, const IVector3D origin, const IVector3D minBound, const IVector3D maxBound) mutable {
  877. if (filter(model, origin, minBound, maxBound)) {
  878. IVector2D globalPixelMinBound, globalPixelMaxBound;
  879. getScreenBounds(world, minBound, maxBound, globalPixelMinBound, globalPixelMaxBound);
  880. world->updatePassiveRegion(IRect(globalPixelMinBound.x, globalPixelMinBound.y, globalPixelMaxBound.x - globalPixelMinBound.x, globalPixelMaxBound.y - globalPixelMinBound.y));
  881. return LeafAction::Erase;
  882. } else {
  883. return LeafAction::None;
  884. }
  885. });
  886. }
  887. void spriteWorld_removeBackgroundModels(SpriteWorld& world, const IVector3D& searchMinBound, const IVector3D& searchMaxBound) {
  888. spriteWorld_removeBackgroundModels(world, searchMinBound, searchMaxBound, [](ModelInstance& model, const IVector3D origin, const IVector3D minBound, const IVector3D maxBound) {
  889. return true; // Erase everything in the bound
  890. });
  891. }
  892. void spriteWorld_addTemporarySprite(SpriteWorld& world, const SpriteInstance& sprite) {
  893. MUST_EXIST(world, spriteWorld_addTemporarySprite);
  894. if (sprite.typeIndex < 0 || sprite.typeIndex >= spriteTypes.length()) { throwError(U"Sprite type index ", sprite.typeIndex, " is out of bound!\n"); }
  895. // Add the temporary sprite
  896. world->temporarySprites.push(sprite);
  897. }
  898. void spriteWorld_addTemporaryModel(SpriteWorld& world, const ModelInstance& instance) {
  899. MUST_EXIST(world, spriteWorld_addTemporaryModel);
  900. // Add the temporary model
  901. world->temporaryModels.push(instance);
  902. }
  903. void spriteWorld_createTemporary_pointLight(SpriteWorld& world, const FVector3D position, float radius, float intensity, ColorRgbI32 color, bool shadowCasting) {
  904. MUST_EXIST(world, spriteWorld_createTemporary_pointLight);
  905. world->temporaryPointLights.pushConstruct(position, radius, intensity, color, shadowCasting);
  906. }
  907. void spriteWorld_createTemporary_directedLight(SpriteWorld& world, const FVector3D direction, float intensity, ColorRgbI32 color) {
  908. MUST_EXIST(world, spriteWorld_createTemporary_pointLight);
  909. world->temporaryDirectedLights.pushConstruct(direction, intensity, color);
  910. }
  911. void spriteWorld_clearTemporary(SpriteWorld& world) {
  912. MUST_EXIST(world, spriteWorld_clearTemporary);
  913. world->temporarySprites.clear();
  914. world->temporaryModels.clear();
  915. world->temporaryPointLights.clear();
  916. world->temporaryDirectedLights.clear();
  917. }
  918. void spriteWorld_draw(SpriteWorld& world, AlignedImageRgbaU8& colorTarget) {
  919. MUST_EXIST(world, spriteWorld_draw);
  920. world->draw(colorTarget);
  921. }
  922. #define BOX_LINE(INDEX_A, INDEX_B) draw_line(target, corners[INDEX_A].x, corners[INDEX_A].y, corners[INDEX_B].x, corners[INDEX_B].y, color);
  923. void debugDrawBound(SpriteWorld& world, const IVector2D& worldCenter, AlignedImageRgbaU8& target, const ColorRgbaI32& color, const IVector3D& minBound, const IVector3D& maxBound) {
  924. IVector2D corners[8];
  925. for (int c = 0; c < 8; c++) {
  926. // TODO: Convert to real screen pixels using the camera offset.
  927. corners[c] = world->ortho.view[world->cameraIndex].miniTilePositionToScreenPixel(getBoxCorner(minBound, maxBound, c), worldCenter);
  928. }
  929. BOX_LINE(0, 1);
  930. BOX_LINE(2, 3);
  931. BOX_LINE(4, 5);
  932. BOX_LINE(6, 7);
  933. BOX_LINE(0, 2);
  934. BOX_LINE(1, 3);
  935. BOX_LINE(4, 6);
  936. BOX_LINE(5, 7);
  937. BOX_LINE(0, 4);
  938. BOX_LINE(1, 5);
  939. BOX_LINE(2, 6);
  940. BOX_LINE(3, 7);
  941. }
  942. void spriteWorld_debug_octrees(SpriteWorld& world, AlignedImageRgbaU8& colorTarget) {
  943. MUST_EXIST(world, spriteWorld_debug_octrees);
  944. IVector2D worldCenter = world->findWorldCenter(colorTarget);
  945. IRect seenRegion = IRect(-worldCenter.x, -worldCenter.y, image_getWidth(colorTarget), image_getHeight(colorTarget));
  946. OcTreeFilter orthoCullingFilter = [&world, &worldCenter, &seenRegion, &colorTarget](const IVector3D& minBound, const IVector3D& maxBound){
  947. debugDrawBound(world, worldCenter, colorTarget, ColorRgbaI32(100, 100, 100, 255), minBound, maxBound);
  948. return orthoCullingTest(world->ortho.view[world->cameraIndex], minBound, maxBound, seenRegion);
  949. };
  950. world->passiveSprites.map(orthoCullingFilter, [&world, &worldCenter, &colorTarget](SpriteInstance& sprite, const IVector3D origin, const IVector3D minBound, const IVector3D maxBound){
  951. debugDrawBound(world, worldCenter, colorTarget, ColorRgbaI32(0, 255, 0, 255), minBound, maxBound);
  952. return LeafAction::None;
  953. });
  954. world->passiveModels.map(orthoCullingFilter, [&world, &worldCenter, &colorTarget](ModelInstance& model, const IVector3D origin, const IVector3D minBound, const IVector3D maxBound){
  955. debugDrawBound(world, worldCenter, colorTarget, ColorRgbaI32(0, 0, 255, 255), minBound, maxBound);
  956. return LeafAction::None;
  957. });
  958. }
  959. IVector3D spriteWorld_findGroundAtPixel(SpriteWorld& world, const AlignedImageRgbaU8& colorBuffer, const IVector2D& pixelLocation) {
  960. MUST_EXIST(world, spriteWorld_findGroundAtPixel);
  961. return world->ortho.pixelToMiniPosition(pixelLocation, world->cameraIndex, world->findWorldCenter(colorBuffer));
  962. }
  963. void spriteWorld_setCameraLocation(SpriteWorld& world, const IVector3D miniTileLocation) {
  964. MUST_EXIST(world, spriteWorld_setCameraLocation);
  965. if (world->cameraLocation != miniTileLocation) {
  966. world->cameraLocation = miniTileLocation;
  967. world->dirtyBackground.allDirty();
  968. }
  969. }
  970. void spriteWorld_moveCameraInPixels(SpriteWorld& world, const IVector2D& pixelOffset) {
  971. MUST_EXIST(world, spriteWorld_moveCameraInPixels);
  972. if (pixelOffset.x != 0 || pixelOffset.y != 0) {
  973. world->cameraLocation = world->cameraLocation + world->ortho.pixelToMiniOffset(pixelOffset, world->cameraIndex);
  974. world->dirtyBackground.allDirty();
  975. }
  976. }
  977. AlignedImageRgbaU8 spriteWorld_getDiffuseBuffer(SpriteWorld& world) {
  978. MUST_EXIST(world, spriteWorld_getDiffuseBuffer);
  979. return world->diffuseBuffer;
  980. }
  981. OrderedImageRgbaU8 spriteWorld_getNormalBuffer(SpriteWorld& world) {
  982. MUST_EXIST(world, spriteWorld_getNormalBuffer);
  983. return world->normalBuffer;
  984. }
  985. OrderedImageRgbaU8 spriteWorld_getLightBuffer(SpriteWorld& world) {
  986. MUST_EXIST(world, spriteWorld_getLightBuffer);
  987. return world->lightBuffer;
  988. }
  989. AlignedImageF32 spriteWorld_getHeightBuffer(SpriteWorld& world) {
  990. MUST_EXIST(world, spriteWorld_getHeightBuffer);
  991. return world->heightBuffer;
  992. }
  993. int spriteWorld_getCameraDirectionIndex(SpriteWorld& world) {
  994. MUST_EXIST(world, spriteWorld_getCameraDirectionIndex);
  995. return world->cameraIndex;
  996. }
  997. void spriteWorld_setCameraDirectionIndex(SpriteWorld& world, int index) {
  998. MUST_EXIST(world, spriteWorld_setCameraDirectionIndex);
  999. if (index != world->cameraIndex) {
  1000. world->cameraIndex = index;
  1001. world->dirtyBackground.allDirty();
  1002. }
  1003. }
  1004. static FVector3D FVector4Dto3D(FVector4D v) {
  1005. return FVector3D(v.x, v.y, v.z);
  1006. }
  1007. static FVector2D FVector3Dto2D(FVector3D v) {
  1008. return FVector2D(v.x, v.y);
  1009. }
  1010. // Get the pixel bound from a projected vertex point in floating pixel coordinates
  1011. static IRect boundFromVertex(const FVector3D& screenProjection) {
  1012. return IRect((int)(screenProjection.x), (int)(screenProjection.y), 1, 1);
  1013. }
  1014. // Returns true iff the box might be seen using a pessimistic test
  1015. static IRect boundingBoxToRectangle(const FVector3D& minBound, const FVector3D& maxBound, const Transform3D& objectToScreenSpace) {
  1016. FVector3D points[8];
  1017. transformCorners(minBound, maxBound, objectToScreenSpace, points);
  1018. IRect result = boundFromVertex(points[0]);
  1019. for (int p = 1; p < 8; p++) {
  1020. result = IRect::merge(result, boundFromVertex(points[p]));
  1021. }
  1022. return result;
  1023. }
  1024. static IRect getBackCulledTriangleBound(const FVector3D& a, const FVector3D& b, const FVector3D& c) {
  1025. if (((c.x - a.x) * (b.y - a.y)) + ((c.y - a.y) * (a.x - b.x)) >= 0.0f) {
  1026. // Back facing
  1027. return IRect();
  1028. } else {
  1029. // Front facing
  1030. int leftBound = (int)std::min(std::min(a.x, b.x), c.x);
  1031. int topBound = (int)std::min(std::min(a.y, b.y), c.y);
  1032. int rightBound = (int)(std::max(std::max(a.x, b.x), c.x)) + 1;
  1033. int bottomBound = (int)(std::max(std::max(a.y, b.y), c.y)) + 1;
  1034. return IRect(leftBound, topBound, rightBound - leftBound, bottomBound - topBound);
  1035. }
  1036. }
  1037. static FVector3D normalFromPoints(const FVector3D& A, const FVector3D& B, const FVector3D& C) {
  1038. return normalize(crossProduct(B - A, C - A));
  1039. }
  1040. static FVector3D getAverageNormal(const Model& model, int part, int poly) {
  1041. int vertexCount = model_getPolygonVertexCount(model, part, poly);
  1042. FVector3D normalSum;
  1043. for (int t = 0; t < vertexCount - 2; t++) {
  1044. normalSum = normalSum + normalFromPoints(
  1045. model_getVertexPosition(model, part, poly, 0),
  1046. model_getVertexPosition(model, part, poly, t + 1),
  1047. model_getVertexPosition(model, part, poly, t + 2)
  1048. );
  1049. }
  1050. return normalize(normalSum);
  1051. }
  1052. DenseModel DenseModel_create(const Model& original) {
  1053. return std::make_shared<DenseModelImpl>(original);
  1054. }
  1055. static int getTriangleCount(const Model& original) {
  1056. int triangleCount = 0;
  1057. for (int part = 0; part < model_getNumberOfParts(original); part++) {
  1058. for (int poly = 0; poly < model_getNumberOfPolygons(original, part); poly++) {
  1059. int vertexCount = model_getPolygonVertexCount(original, part, poly);
  1060. triangleCount += vertexCount - 2;
  1061. }
  1062. }
  1063. return triangleCount;
  1064. }
  1065. DenseModelImpl::DenseModelImpl(const Model& original)
  1066. : triangles(getTriangleCount(original), DenseTriangle()) {
  1067. // Get the bounding box
  1068. model_getBoundingBox(original, this->minBound, this->maxBound);
  1069. // Generate normals
  1070. int pointCount = model_getNumberOfPoints(original);
  1071. Array<FVector3D> normalPoints(pointCount, FVector3D());
  1072. // Calculate smooth normals in object-space, by adding each polygon's normal to each child vertex
  1073. for (int part = 0; part < model_getNumberOfParts(original); part++) {
  1074. for (int poly = 0; poly < model_getNumberOfPolygons(original, part); poly++) {
  1075. FVector3D polygonNormal = getAverageNormal(original, part, poly);
  1076. for (int vert = 0; vert < model_getPolygonVertexCount(original, part, poly); vert++) {
  1077. int point = model_getVertexPointIndex(original, part, poly, vert);
  1078. normalPoints[point] = normalPoints[point] + polygonNormal;
  1079. }
  1080. }
  1081. }
  1082. // Normalize the result per vertex, to avoid having unbalanced weights when normalizing per pixel
  1083. for (int point = 0; point < pointCount; point++) {
  1084. normalPoints[point] = normalize(normalPoints[point]);
  1085. }
  1086. // Generate a simpler triangle structure
  1087. int triangleIndex = 0;
  1088. for (int part = 0; part < model_getNumberOfParts(original); part++) {
  1089. for (int poly = 0; poly < model_getNumberOfPolygons(original, part); poly++) {
  1090. int vertexCount = model_getPolygonVertexCount(original, part, poly);
  1091. int vertA = 0;
  1092. int indexA = model_getVertexPointIndex(original, part, poly, vertA);
  1093. for (int vertB = 1; vertB < vertexCount - 1; vertB++) {
  1094. int vertC = vertB + 1;
  1095. int indexB = model_getVertexPointIndex(original, part, poly, vertB);
  1096. int indexC = model_getVertexPointIndex(original, part, poly, vertC);
  1097. triangles[triangleIndex] =
  1098. DenseTriangle(
  1099. FVector4Dto3D(model_getVertexColor(original, part, poly, vertA)) * 255.0f,
  1100. FVector4Dto3D(model_getVertexColor(original, part, poly, vertB)) * 255.0f,
  1101. FVector4Dto3D(model_getVertexColor(original, part, poly, vertC)) * 255.0f,
  1102. model_getPoint(original, indexA), model_getPoint(original, indexB), model_getPoint(original, indexC),
  1103. normalPoints[indexA], normalPoints[indexB], normalPoints[indexC]
  1104. );
  1105. triangleIndex++;
  1106. }
  1107. }
  1108. }
  1109. }
  1110. // Pre-conditions:
  1111. // * All images must exist and have the same dimensions
  1112. // * diffuseTarget and normalTarget must have RGBA pack order
  1113. // * All triangles in model must be contained within the image bounds after being projected using view
  1114. // Post-condition:
  1115. // Returns the dirty pixel bound based on projected positions
  1116. // worldOrigin is the perceived world's origin in target pixel coordinates
  1117. // modelToWorldSpace is used to place the model freely in the world
  1118. template <bool HIGH_QUALITY>
  1119. static IRect renderDenseModel(const DenseModel& model, OrthoView view, ImageF32 depthBuffer, ImageRgbaU8 diffuseTarget, ImageRgbaU8 normalTarget, const FVector2D& worldOrigin, const Transform3D& modelToWorldSpace) {
  1120. // Combine position transforms
  1121. Transform3D objectToScreenSpace = combineModelToScreenTransform(modelToWorldSpace, view.worldSpaceToScreenDepth, worldOrigin);
  1122. // Create a pessimistic 2D bound from the 3D bounding box
  1123. IRect pessimisticBound = boundingBoxToRectangle(model->minBound, model->maxBound, objectToScreenSpace);
  1124. // Get the target image bound
  1125. IRect clipBound = image_getBound(depthBuffer);
  1126. // Fast culling test
  1127. if (!IRect::overlaps(pessimisticBound, clipBound)) {
  1128. // Nothing drawn, no dirty rectangle
  1129. return IRect();
  1130. }
  1131. // Combine normal transforms
  1132. FMatrix3x3 modelToNormalSpace = modelToWorldSpace.transform * transpose(view.normalToWorldSpace);
  1133. // Get image properties
  1134. int diffuseStride = image_getStride(diffuseTarget);
  1135. int normalStride = image_getStride(normalTarget);
  1136. int heightStride = image_getStride(depthBuffer);
  1137. // Call getters in advance to avoid call overhead in the loops
  1138. SafePointer<uint32_t> diffuseData = image_getSafePointer(diffuseTarget);
  1139. SafePointer<uint32_t> normalData = image_getSafePointer(normalTarget);
  1140. SafePointer<float> heightData = image_getSafePointer(depthBuffer);
  1141. // Render triangles
  1142. for (int tri = 0; tri < model->triangles.length(); tri++) {
  1143. DenseTriangle triangle = model->triangles[tri];
  1144. // Transform positions
  1145. FVector3D projectedA = objectToScreenSpace.transformPoint(triangle.posA);
  1146. FVector3D projectedB = objectToScreenSpace.transformPoint(triangle.posB);
  1147. FVector3D projectedC = objectToScreenSpace.transformPoint(triangle.posC);
  1148. IRect triangleBound = IRect::cut(clipBound, getBackCulledTriangleBound(projectedA, projectedB, projectedC));
  1149. if (triangleBound.hasArea()) {
  1150. // Find the first row
  1151. SafePointer<uint32_t> diffuseRow = diffuseData;
  1152. diffuseRow.increaseBytes(diffuseStride * triangleBound.top());
  1153. SafePointer<uint32_t> normalRow = normalData;
  1154. normalRow.increaseBytes(normalStride * triangleBound.top());
  1155. SafePointer<float> heightRow = heightData;
  1156. heightRow.increaseBytes(heightStride * triangleBound.top());
  1157. // Pre-compute matrix inverse for vertex weights
  1158. FVector2D cornerA = FVector3Dto2D(projectedA);
  1159. FVector2D cornerB = FVector3Dto2D(projectedB);
  1160. FVector2D cornerC = FVector3Dto2D(projectedC);
  1161. FMatrix2x2 offsetToWeight = inverse(FMatrix2x2(cornerB - cornerA, cornerC - cornerA));
  1162. // Transform normals
  1163. FVector3D normalA = modelToNormalSpace.transform(triangle.normalA);
  1164. FVector3D normalB = modelToNormalSpace.transform(triangle.normalB);
  1165. FVector3D normalC = modelToNormalSpace.transform(triangle.normalC);
  1166. // Iterate over the triangle's bounding box
  1167. for (int y = triangleBound.top(); y < triangleBound.bottom(); y++) {
  1168. SafePointer<uint32_t> diffusePixel = diffuseRow + triangleBound.left();
  1169. SafePointer<uint32_t> normalPixel = normalRow + triangleBound.left();
  1170. SafePointer<float> heightPixel = heightRow + triangleBound.left();
  1171. for (int x = triangleBound.left(); x < triangleBound.right(); x++) {
  1172. FVector2D weightBC = offsetToWeight.transform(FVector2D(x + 0.5f, y + 0.5f) - cornerA);
  1173. FVector3D weight = FVector3D(1.0f - (weightBC.x + weightBC.y), weightBC.x, weightBC.y);
  1174. // Check if the pixel is inside the triangle
  1175. if (weight.x >= -0.00001f && weight.y >= -0.00001f && weight.z >= -0.00001f ) {
  1176. float height = interpolateUsingAffineWeight(projectedA.z, projectedB.z, projectedC.z, weight);
  1177. if (height > *heightPixel) {
  1178. FVector3D vertexColor = interpolateUsingAffineWeight(triangle.colorA, triangle.colorB, triangle.colorC, weight);
  1179. *heightPixel = height;
  1180. // Write data directly without saturation (Do not use colors outside of the visible range!)
  1181. *diffusePixel = ((uint32_t)vertexColor.x) | ENDIAN_POS_ADDR(((uint32_t)vertexColor.y), 8) | ENDIAN_POS_ADDR(((uint32_t)vertexColor.z), 16) | ENDIAN_POS_ADDR(255, 24);
  1182. if (HIGH_QUALITY) {
  1183. FVector3D normal = (normalize(interpolateUsingAffineWeight(normalA, normalB, normalC, weight)) + 1.0f) * 127.5f;
  1184. *normalPixel = ((uint32_t)normal.x) | ENDIAN_POS_ADDR(((uint32_t)normal.y), 8) | ENDIAN_POS_ADDR(((uint32_t)normal.z), 16) | ENDIAN_POS_ADDR(255, 24);
  1185. } else {
  1186. FVector3D normal = (interpolateUsingAffineWeight(normalA, normalB, normalC, weight) + 1.0f) * 127.5f;
  1187. *normalPixel = ((uint32_t)normal.x) | ENDIAN_POS_ADDR(((uint32_t)normal.y), 8) | ENDIAN_POS_ADDR(((uint32_t)normal.z), 16) | ENDIAN_POS_ADDR(255, 24);
  1188. }
  1189. }
  1190. }
  1191. diffusePixel += 1;
  1192. normalPixel += 1;
  1193. heightPixel += 1;
  1194. }
  1195. diffuseRow.increaseBytes(diffuseStride);
  1196. normalRow.increaseBytes(normalStride);
  1197. heightRow.increaseBytes(heightStride);
  1198. }
  1199. }
  1200. }
  1201. return pessimisticBound;
  1202. }
  1203. void sprite_generateFromModel(ImageRgbaU8& targetAtlas, String& targetConfigText, const Model& visibleModel, const Model& shadowModel, const OrthoSystem& ortho, const String& targetPath, int cameraAngles) {
  1204. // Validate input
  1205. if (cameraAngles < 1) {
  1206. printText(" Need at least one camera angle to generate a sprite!\n");
  1207. return;
  1208. } else if (!model_exists(visibleModel)) {
  1209. printText(" There's nothing to render, because visible model does not exist!\n");
  1210. return;
  1211. } else if (model_getNumberOfParts(visibleModel) == 0) {
  1212. printText(" There's nothing to render in the visible model, because there are no parts in the visible model!\n");
  1213. return;
  1214. } else {
  1215. // Measure the bounding cylinder for determining the uncropped image size
  1216. FVector3D minBound, maxBound;
  1217. model_getBoundingBox(visibleModel, minBound, maxBound);
  1218. // Check if generating a bound failed
  1219. if (minBound.x > maxBound.x) {
  1220. printText(" There's nothing visible in the model, because the 3D bounding box had no points to be created from!\n");
  1221. return;
  1222. }
  1223. printText(" Representing height from ", minBound.y, " to ", maxBound.y, " encoded using 8-bits\n");
  1224. // Calculate initial image size
  1225. float worstCaseDiameter = (std::max(maxBound.x, -minBound.x) + std::max(maxBound.y, -minBound.y) + std::max(maxBound.z, -minBound.z)) * 2;
  1226. int maxRes = roundUp(worstCaseDiameter * ortho.pixelsPerTile, 2) + 4; // Round up to even pixels and add 4 padding pixels
  1227. // Allocate square images from the pessimistic size estimation
  1228. int width = maxRes;
  1229. int height = maxRes;
  1230. ImageF32 depthBuffer = image_create_F32(width, height);
  1231. ImageRgbaU8 colorImage[cameraAngles];
  1232. ImageRgbaU8 heightImage[cameraAngles];
  1233. ImageRgbaU8 normalImage[cameraAngles];
  1234. for (int a = 0; a < cameraAngles; a++) {
  1235. colorImage[a] = image_create_RgbaU8(width, height);
  1236. heightImage[a] = image_create_RgbaU8(width, height);
  1237. normalImage[a] = image_create_RgbaU8(width, height);
  1238. }
  1239. // Generate the optimized model structure with normals
  1240. DenseModel denseModel = DenseModel_create(visibleModel);
  1241. // Render the model to multiple render targets at once
  1242. float heightScale = 255.0f / (maxBound.y - minBound.y);
  1243. for (int a = 0; a < cameraAngles; a++) {
  1244. image_fill(depthBuffer, -1000000000.0f);
  1245. image_fill(colorImage[a], ColorRgbaI32(0, 0, 0, 0));
  1246. FVector2D origin = FVector2D((float)width * 0.5f, (float)height * 0.5f);
  1247. renderDenseModel<true>(denseModel, ortho.view[a], depthBuffer, colorImage[a], normalImage[a], origin, Transform3D());
  1248. // Convert height into an 8 bit channel for saving
  1249. for (int y = 0; y < height; y++) {
  1250. for (int x = 0; x < width; x++) {
  1251. int32_t opacityPixel = image_readPixel_clamp(colorImage[a], x, y).alpha;
  1252. int32_t heightPixel = (image_readPixel_clamp(depthBuffer, x, y) - minBound.y) * heightScale;
  1253. image_writePixel(heightImage[a], x, y, ColorRgbaI32(heightPixel, 0, 0, opacityPixel));
  1254. }
  1255. }
  1256. }
  1257. // Crop all images uniformly for easy atlas packing
  1258. int32_t minX = width;
  1259. int32_t minY = height;
  1260. int32_t maxX = 0;
  1261. int32_t maxY = 0;
  1262. for (int a = 0; a < cameraAngles; a++) {
  1263. for (int y = 0; y < height; y++) {
  1264. for (int x = 0; x < width; x++) {
  1265. if (image_readPixel_border(colorImage[a], x, y).alpha) {
  1266. if (x < minX) minX = x;
  1267. if (x > maxX) maxX = x;
  1268. if (y < minY) minY = y;
  1269. if (y > maxY) maxY = y;
  1270. }
  1271. }
  1272. }
  1273. }
  1274. // Check if cropping failed
  1275. if (minX > maxX) {
  1276. printText(" There's nothing visible in the model, because cropping the final images returned nothing!\n");
  1277. return;
  1278. }
  1279. IRect cropRegion = IRect(minX, minY, (maxX + 1) - minX, (maxY + 1) - minY);
  1280. if (cropRegion.width() < 1 || cropRegion.height() < 1) {
  1281. printText(" Cropping failed to find any drawn pixels!\n");
  1282. return;
  1283. }
  1284. for (int a = 0; a < cameraAngles; a++) {
  1285. colorImage[a] = image_getSubImage(colorImage[a], cropRegion);
  1286. heightImage[a] = image_getSubImage(heightImage[a], cropRegion);
  1287. normalImage[a] = image_getSubImage(normalImage[a], cropRegion);
  1288. }
  1289. int croppedWidth = cropRegion.width();
  1290. int croppedHeight = cropRegion.height();
  1291. int centerX = width / 2 - cropRegion.left();
  1292. int centerY = height / 2 - cropRegion.top();
  1293. printText(" Cropped images of ", croppedWidth, "x", croppedHeight, " pixels with centers at (", centerX, ", ", centerY, ")\n");
  1294. // Pack everything into an image atlas
  1295. targetAtlas = image_create_RgbaU8(croppedWidth * 3, croppedHeight * cameraAngles);
  1296. for (int a = 0; a < cameraAngles; a++) {
  1297. draw_copy(targetAtlas, colorImage[a], 0, a * croppedHeight);
  1298. draw_copy(targetAtlas, heightImage[a], croppedWidth, a * croppedHeight);
  1299. draw_copy(targetAtlas, normalImage[a], croppedWidth * 2, a * croppedHeight);
  1300. }
  1301. SpriteConfig config = SpriteConfig(centerX, centerY, cameraAngles, 3, minBound, maxBound);
  1302. if (model_exists(shadowModel) && model_getNumberOfPoints(shadowModel) > 0) {
  1303. config.appendShadow(shadowModel);
  1304. }
  1305. targetConfigText = config.toIni();
  1306. }
  1307. }
  1308. // Allowing the last decimals to deviate a bit because floating-point operations are rounded differently between computers
  1309. static bool approximateTextMatch(const ReadableString &a, const ReadableString &b, double tolerance = 0.00002) {
  1310. int readerA = 0, readerB = 0;
  1311. while (readerA < string_length(a) && readerB < string_length(b)) {
  1312. DsrChar charA = a[readerA];
  1313. DsrChar charB = b[readerB];
  1314. if (character_isValueCharacter(charA) && character_isValueCharacter(charB)) {
  1315. // Scan forward on both sides while consuming content and comparing the actual value
  1316. int startA = readerA;
  1317. int startB = readerB;
  1318. // Only move forward on valid characters
  1319. if (a[readerA] == U'-') { readerA++; }
  1320. if (b[readerB] == U'-') { readerB++; }
  1321. while (character_isDigit(a[readerA])) { readerA++; }
  1322. while (character_isDigit(b[readerB])) { readerB++; }
  1323. if (a[readerA] == U'.') { readerA++; }
  1324. if (b[readerB] == U'.') { readerB++; }
  1325. while (character_isDigit(a[readerA])) { readerA++; }
  1326. while (character_isDigit(b[readerB])) { readerB++; }
  1327. // Approximate values
  1328. double valueA = string_toDouble(string_exclusiveRange(a, startA, readerA));
  1329. double valueB = string_toDouble(string_exclusiveRange(b, startB, readerB));
  1330. // Check the difference
  1331. double diff = valueB - valueA;
  1332. if (diff > tolerance || diff < -tolerance) {
  1333. // Too big difference, this is probably not a rounding error
  1334. return false;
  1335. }
  1336. } else if (charA != charB) {
  1337. // Difference with a non-value involved
  1338. return false;
  1339. }
  1340. readerA++;
  1341. readerB++;
  1342. }
  1343. if (readerA < string_length(a) - 1 || readerB < string_length(b) - 1) {
  1344. // One text had unmatched remains after the other reached its end
  1345. return false;
  1346. } else {
  1347. return true;
  1348. }
  1349. }
  1350. void sprite_generateFromModel(const Model& visibleModel, const Model& shadowModel, const OrthoSystem& ortho, const String& targetPath, int cameraAngles, bool debug) {
  1351. // Generate an image and a configuration file from the visible model
  1352. ImageRgbaU8 atlasImage; String configText;
  1353. sprite_generateFromModel(atlasImage, configText, visibleModel, shadowModel, ortho, targetPath, cameraAngles);
  1354. // Save the result on success
  1355. if (string_length(configText) > 0) {
  1356. // Save the atlas
  1357. String atlasPath = targetPath + U".png";
  1358. // Try loading any existing image
  1359. ImageRgbaU8 existingAtlasImage = image_load_RgbaU8(atlasPath, false);
  1360. if (image_exists(existingAtlasImage)) {
  1361. int difference = image_maxDifference(atlasImage, existingAtlasImage);
  1362. if (difference <= 2) {
  1363. printText(" No significant changes against ", targetPath, ".\n");
  1364. } else {
  1365. image_save(atlasImage, atlasPath);
  1366. printText(" Updated ", targetPath, " with a deviation of ", difference, ".\n");
  1367. }
  1368. } else {
  1369. // Only save if there was no existing image or it differed significantly from the new result
  1370. // This comparison is made to avoid flooding version history with changes from invisible differences in color rounding
  1371. image_save(atlasImage, atlasPath);
  1372. printText(" Saved atlas to ", targetPath, ".\n");
  1373. }
  1374. // Save the configuration
  1375. String configPath = targetPath + U".ini";
  1376. String oldConfixText = string_load(configPath, false);
  1377. if (approximateTextMatch(configText, oldConfixText)) {
  1378. printText(" No significant changes against ", targetPath, ".\n\n");
  1379. } else {
  1380. string_save(targetPath + U".ini", configText);
  1381. printText(" Saved sprite config to ", targetPath, ".\n\n");
  1382. }
  1383. if (debug) {
  1384. ImageRgbaU8 debugImage; String garbageText;
  1385. // TODO: Show overlap between visible and shadow so that shadow outside of visible is displayed as bright red on a dark model.
  1386. // The number of visible shadow pixels should be reported automatically
  1387. // in an error message at the end of the total execution together with file names.
  1388. sprite_generateFromModel(debugImage, garbageText, shadowModel, Model(), ortho, targetPath + U"Debug", 8);
  1389. image_save(debugImage, targetPath + U"Debug.png");
  1390. }
  1391. }
  1392. }
  1393. }