rasteriver.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432
  1. #include <stdio.h>
  2. #include <CL/cl.h>
  3. #include <SDL2/SDL.h>
  4. #include "../headers/rasteriver.h"
  5. #define STB_IMAGE_IMPLEMENTATION
  6. #include "../headers/stb_image.h"
  7. #include "stdint.h"
  8. #include <math.h>
  9. #include <signal.h>
  10. RasterIver ri = {NULL};
  11. void debug(char *string, ...){
  12. va_list args;
  13. va_start(args, string);
  14. char message[500];
  15. strcpy(message, ri.prefix);
  16. strcat(message, string);
  17. vprintf(message, args);
  18. printf("\n");
  19. va_end(args);
  20. }
  21. RasterIver* RI_get_ri(){
  22. return &ri;
  23. }
  24. #define RI_realloc(__ptr, __size) written_RI_realloc(__ptr, __size, __func__, __LINE__)
  25. #define RI_malloc(__size) written_RI_malloc(__size, __func__, __LINE__)
  26. #define RI_calloc(__nmemb, __size) written_RI_calloc(__nmemb, __size, __func__, __LINE__)
  27. #define RI_free(__ptr) written_RI_free(__ptr, __func__, __LINE__)
  28. void* written_RI_realloc(void *__ptr, size_t __size, const char *caller, int line){
  29. void *pointer = realloc(__ptr, __size);
  30. if (ri.debug_memory) {
  31. int current_allocation_index = 0;
  32. int checking = 1;
  33. while (checking){
  34. if (!ri.allocation_table[current_allocation_index].reallocated_free && ri.allocation_table[current_allocation_index].pointer == __ptr){
  35. ri.allocation_table[current_allocation_index].reallocated_free = 1;
  36. checking = 0;
  37. }
  38. current_allocation_index++;
  39. if (current_allocation_index >= ri.allocation_search_limit){
  40. checking = 0;
  41. }
  42. }
  43. debug("[Memory Manager] Allocated (realloc) %zu bytes (func \"%s\":%d)", __size, caller, line);
  44. if (ri.current_allocation_index >= ri.allocation_table_length){
  45. ri.allocation_table_length += 50;
  46. ri.allocation_search_limit += 50;
  47. ri.allocation_table = RI_realloc(ri.allocation_table, sizeof(RI_memory_allocation) * ri.allocation_table_length);
  48. }
  49. ri.allocation_table[ri.current_allocation_index].allocated = 1;
  50. ri.allocation_table[ri.current_allocation_index].reallocated_alloc = 1;
  51. ri.allocation_table[ri.current_allocation_index].reallocated_free = 0;
  52. ri.allocation_table[ri.current_allocation_index].freed = 0;
  53. ri.allocation_table[ri.current_allocation_index].line = line;
  54. ri.allocation_table[ri.current_allocation_index].pointer = pointer;
  55. ri.allocation_table[ri.current_allocation_index].size = __size;
  56. ri.current_allocation_index++;
  57. }
  58. return pointer;
  59. }
  60. void* written_RI_malloc(size_t __size, const char *caller, int line){
  61. void *pointer = malloc(__size);
  62. if (ri.debug_memory) {
  63. debug("[Memory Manager] Allocated (malloc) %zu bytes (func \"%s\":%d)", __size, caller, line);
  64. if (ri.current_allocation_index >= ri.allocation_table_length){
  65. ri.allocation_table_length += 50;
  66. ri.allocation_search_limit += 50;
  67. ri.allocation_table = RI_realloc(ri.allocation_table, sizeof(RI_memory_allocation) * ri.allocation_table_length);
  68. }
  69. ri.allocation_table[ri.current_allocation_index].allocated = 1;
  70. ri.allocation_table[ri.current_allocation_index].reallocated_free = 0;
  71. ri.allocation_table[ri.current_allocation_index].reallocated_alloc = 0;
  72. ri.allocation_table[ri.current_allocation_index].freed = 0;
  73. ri.allocation_table[ri.current_allocation_index].line = line;
  74. ri.allocation_table[ri.current_allocation_index].pointer = pointer;
  75. ri.allocation_table[ri.current_allocation_index].size = __size;
  76. ri.current_allocation_index++;
  77. }
  78. return pointer;
  79. }
  80. void* written_RI_calloc(size_t __nmemb, size_t __size, const char *caller, int line){
  81. void *pointer = calloc(__nmemb, __size);
  82. if (ri.debug_memory) {
  83. debug("[Memory Manager] Allocated (calloc) %zu bytes (func \"%s\":%d)", __size * __nmemb, caller, line);
  84. if (ri.current_allocation_index >= ri.allocation_table_length){
  85. ri.allocation_table_length += 50;
  86. ri.allocation_search_limit += 50;
  87. ri.allocation_table = RI_realloc(ri.allocation_table, sizeof(RI_memory_allocation) * ri.allocation_table_length);
  88. }
  89. ri.allocation_table[ri.current_allocation_index].allocated = 1;
  90. ri.allocation_table[ri.current_allocation_index].reallocated_free = 0;
  91. ri.allocation_table[ri.current_allocation_index].reallocated_alloc = 0;
  92. ri.allocation_table[ri.current_allocation_index].freed = 0;
  93. ri.allocation_table[ri.current_allocation_index].line = line;
  94. ri.allocation_table[ri.current_allocation_index].pointer = pointer;
  95. ri.allocation_table[ri.current_allocation_index].size = __size * __nmemb;
  96. ri.current_allocation_index++;
  97. }
  98. return pointer;
  99. }
  100. void written_RI_free(void *__ptr, const char *caller, int line){
  101. if (ri.debug_memory) {
  102. size_t size = 0;
  103. int current_allocation_index = 0;
  104. int checking = 1;
  105. while (checking){
  106. if (!ri.allocation_table[current_allocation_index].reallocated_free && ri.allocation_table[current_allocation_index].pointer == __ptr){
  107. size = ri.allocation_table[current_allocation_index].size;
  108. ri.allocation_table[current_allocation_index].freed = 1;
  109. checking = 0;
  110. }
  111. current_allocation_index++;
  112. if (current_allocation_index >= ri.allocation_search_limit){
  113. checking = 0;
  114. }
  115. }
  116. debug("[Memory Manager] Freed %zu bytes (func \"%s\":%d)", size, caller, line);
  117. }
  118. free(__ptr);
  119. }
  120. RI_texture* RI_request_empty_texture(RI_vector_2 resolution){
  121. RI_texture *new_texture = RI_malloc(sizeof(RI_texture));
  122. new_texture->image_buffer = RI_malloc(sizeof(uint32_t) * resolution.x * resolution.y);
  123. new_texture->resolution = resolution;
  124. return new_texture;
  125. }
  126. void RI_clear_texture(RI_texture *target_texture){
  127. for (int i = 0; i < target_texture->resolution.x * target_texture->resolution.y; ++i){
  128. target_texture->image_buffer[i] = 0x00000000;
  129. }
  130. }
  131. void RI_draw_line(RI_texture *target_texture, RI_vector_2 point_a, RI_vector_2 point_b, uint32_t color){
  132. int num_pixels = distance_2(point_a, point_b);
  133. for (int i = 0; i < num_pixels; ++i){
  134. RI_vector_2 point_2_draw; vector_2_lerp(point_a, point_b, &point_2_draw, (double)i / (double)num_pixels);
  135. if (point_2_draw.x < 0 || point_2_draw.x >= target_texture->resolution.x || point_2_draw.y < 0 || point_2_draw.y >= target_texture->resolution.y) continue;
  136. target_texture->image_buffer[point_2_draw.y * target_texture->resolution.x + point_2_draw.x] = color;
  137. }
  138. }
  139. int ccw(RI_vector_2f a, RI_vector_2f b, RI_vector_2f c) {
  140. return (c.y - a.y) * (b.x - a.x) > (b.y - a.y) * (c.x - a.x);
  141. }
  142. int intersects_segments(RI_vector_2f a, RI_vector_2f b, RI_vector_2f c, RI_vector_2f d) {
  143. return (ccw(a, c, d) != ccw(b, c, d)) && (ccw(a, b, c) != ccw(a, b, d));
  144. }
  145. int intersects(RI_vector_2f a, RI_vector_2f b, RI_vector_2f c){
  146. RI_vector_2f d = {10000, c.y};
  147. return intersects_segments(a, b, c, d);
  148. }
  149. void render_glyph(RI_texture *target_texture, RI_vector_2f position, double size, uint32_t color, int bezier_resolution, double units_per_em, SP_glyph *glyph){
  150. // estimate
  151. int new_point_count = 0;
  152. int allocated_new_points = glyph->number_of_points * 3;
  153. RI_vector_2f *new_points = RI_malloc(sizeof(RI_vector_2f) * allocated_new_points);
  154. int *contour_ends = RI_malloc(sizeof(int) * glyph->number_of_contours);
  155. for (int contour = 0; contour < glyph->number_of_contours; ++contour){
  156. // if we are at contour 0, point_start is 0.
  157. // if contour is > 0 but != 0, point_start is equal to the previous index
  158. int point_start = contour > 0 ? (glyph->contour_end_indicies[contour - 1] + 1) : 0;
  159. int point_offset = point_start;
  160. // find first on-curve point because the first point isn't always on the curves
  161. // while (!(glyph->flags[point_offset] & 1)){
  162. // point_offset++;
  163. // }
  164. for (int point = 0; point <= glyph->contour_end_indicies[contour] + 1 - point_start; ++point){
  165. if (new_point_count + 1 >= allocated_new_points){
  166. allocated_new_points += 20;
  167. new_points = RI_realloc(new_points, sizeof(RI_vector_2f) * allocated_new_points);
  168. }
  169. int cur = (point % (glyph->contour_end_indicies[contour] + 1 - point_start) + point_offset);
  170. int next = ((point + 1) % (glyph->contour_end_indicies[contour] + 1 - point_start) + point_offset);
  171. new_points[new_point_count].x = (double)glyph->x_coords[cur] / units_per_em * size + position.x;
  172. new_points[new_point_count].y = (double)glyph->y_coords[cur] / units_per_em * size + position.y;
  173. new_point_count++;
  174. // if current and next glyph are both on or off the curve, add a point between them-
  175. if ((glyph->flags[cur] & 1) == (glyph->flags[next] & 1)){
  176. vector_2f_lerp((RI_vector_2f){(double)glyph->x_coords[cur] / units_per_em * size + position.x, (double)glyph->y_coords[cur] / units_per_em * size + position.y}, (RI_vector_2f){(double)glyph->x_coords[next] / units_per_em * size + position.x, (double)glyph->y_coords[next] / units_per_em * size + position.y}, &new_points[new_point_count], 0.5);
  177. new_point_count++;
  178. }
  179. }
  180. contour_ends[contour] = new_point_count;
  181. }
  182. allocated_new_points = new_point_count;
  183. // maybe make multiple arrays that are lists of lines in increments of y values so that we dont have to check every line even if its above the pixel (the ray only goes to the right)
  184. RI_vector_2f *lines = RI_malloc(sizeof(RI_vector_2f) * (new_point_count / 2) * (bezier_resolution <= 1 ? 2 : bezier_resolution) + 30);
  185. for (int contour = 0; contour < glyph->number_of_contours; ++contour){
  186. int p_start = (contour > 0 ? contour_ends[contour - 1] : 0);
  187. int p_end = contour_ends[contour];
  188. for (int point = p_start; point < p_end - 2; point += 2){
  189. RI_vector_2f point_a = new_points[point];
  190. RI_vector_2f point_b = new_points[(point + 1)];
  191. RI_vector_2f point_c = new_points[(point + 2)];
  192. RI_vector_2f prev_point = new_points[point];
  193. for (int i = 0; i < bezier_resolution; ++i){
  194. double w = (double)(i + 1) / (double)bezier_resolution;
  195. RI_vector_2f bez_point;
  196. if (bezier_resolution <= 1) bez_point = point_c;
  197. else vector_2f_bezier_interpolate(point_a, point_b, point_c, &bez_point, w);
  198. lines[point + i] = prev_point;
  199. lines[point + i + 1] = bez_point;
  200. prev_point = bez_point;
  201. }
  202. }
  203. }
  204. for (int y = fmax((int)((double)glyph->y_min / (double)units_per_em * size) + position.y, 0.0); y < fmin((int)((double)glyph->y_max / (double)units_per_em * size) + position.y, target_texture->resolution.y); ++y){
  205. for (int x = fmax((int)((double)glyph->x_min / (double)units_per_em * size) + position.x, 0.0); x < fmin((int)((double)glyph->x_max / (double)units_per_em * size) + position.x, target_texture->resolution.x); ++x){
  206. int intersections = 0;
  207. for (int contour = 0; contour < glyph->number_of_contours; ++contour){
  208. int p_start = (contour > 0 ? contour_ends[contour - 1] : 0);
  209. int p_end = contour_ends[contour];
  210. for (int point = p_start; point < p_end - 2; point += 2){
  211. for (int i = 0; i < bezier_resolution; ++i){
  212. if(intersects(lines[point + i], lines[point + i + 1], (RI_vector_2f){x, y})) intersections++;
  213. }
  214. }
  215. }
  216. if (intersections % 2 != 0) target_texture->image_buffer[y * target_texture->resolution.x + x] = color;
  217. }
  218. }
  219. RI_free(new_points);
  220. RI_free(contour_ends);
  221. }
  222. void RI_render_text(SP_font *font, RI_texture *target_texture, RI_vector_2f position, uint32_t color, int bezier_resolution, double size, char *text){
  223. int character_count = strlen(text);
  224. int current_advance_width = 0;
  225. RI_vector_2f glyph_position = position;
  226. for (int character_i = 0; character_i < character_count; ++character_i){
  227. int glyph = font->unicode_to_glyph_indicies[text[character_i]];
  228. if (glyph >= font->number_of_glyphs) glyph = 0;
  229. SP_glyph *current_glyph = &font->glyphs[glyph];
  230. glyph_position.x = position.x;// + ((double)font->h_metrics[glyph].left_side_bearing / font->units_per_em * size);
  231. if (current_glyph->number_of_contours > 0){
  232. render_glyph(target_texture, glyph_position, size, color, bezier_resolution, font->units_per_em, current_glyph);
  233. }
  234. else if (current_glyph->number_of_contours == 0){
  235. // do nothing, glyph is empty
  236. }
  237. else {
  238. for (int k = 0; k < current_glyph->number_of_components; k++){
  239. int c_glyph = current_glyph->components[k].glyph_index;
  240. double offset_x = (double)current_glyph->components[k].arg1;
  241. double offset_y = (double)current_glyph->components[k].arg2;
  242. glyph_position.x += offset_x / font->units_per_em * size;
  243. glyph_position.y += offset_y / font->units_per_em * size;
  244. render_glyph(target_texture, glyph_position, size, color, bezier_resolution, font->units_per_em, &font->glyphs[c_glyph]);
  245. glyph_position.x -= offset_x / font->units_per_em * size;
  246. glyph_position.y -= offset_y / font->units_per_em * size;
  247. }
  248. }
  249. // find kerning value
  250. double kerning_offset = 0;
  251. if (character_i + 1 < character_count && font->number_of_kerning_pairs > 0){
  252. uint32_t key = (glyph << 16) | (uint16_t)font->unicode_to_glyph_indicies[text[character_i + 1]];
  253. int low = 0;
  254. int high = font->number_of_kerning_pairs - 1;
  255. while (low <= high) {
  256. int mid = (low + high) / 2;
  257. uint32_t midKey = (font->kerning_pairs[mid].left << 16) | font->kerning_pairs[mid].right;
  258. if (midKey == key) {
  259. kerning_offset = (double)font->kerning_pairs[mid].value; // found it
  260. break;
  261. } else if (midKey < key) {
  262. low = mid + 1; // search right half
  263. } else {
  264. high = mid - 1; // search left half
  265. }
  266. }
  267. }
  268. position.x += (double)(font->h_metrics[glyph].advance_width + kerning_offset) / font->units_per_em * size;
  269. }
  270. }
  271. int RI_add_actors_to_scene(int RI_number_of_actors_to_add_to_scene, RI_actor *actors, RI_scene *scene){
  272. int previous_actor_count = scene->actor_count;
  273. scene->actor_count += RI_number_of_actors_to_add_to_scene;
  274. scene->actors = RI_realloc(scene->actors, sizeof(RI_actor *) * scene->actor_count);
  275. for (int i = 0; i < RI_number_of_actors_to_add_to_scene; ++i){
  276. scene->actors[i + previous_actor_count] = &actors[i];
  277. }
  278. return 0;
  279. }
  280. RI_scene* RI_request_scenes(int RI_number_of_requested_scenes){
  281. int previous_scene_count = ri.scene_count;
  282. ri.scene_count += RI_number_of_requested_scenes;
  283. ri.scenes = RI_realloc(ri.scenes, sizeof(RI_scene) * ri.scene_count);
  284. for (int i = 0; i < RI_number_of_requested_scenes; ++i){
  285. RI_scene new_scene = {0};
  286. new_scene.actor_count = 0;
  287. new_scene.actors = NULL;
  288. new_scene.faces_to_render = NULL;
  289. new_scene.antialiasing_subsample_resolution = 4;
  290. ri.scenes[i + previous_scene_count] = new_scene;
  291. }
  292. return ri.scenes;
  293. }
  294. RI_actor* RI_request_actors(int RI_number_of_requested_actors){
  295. int previous_actor_count = ri.actor_count;
  296. ri.actor_count += RI_number_of_requested_actors;
  297. ri.actors = RI_realloc(ri.actors, sizeof(RI_actor) * ri.actor_count);
  298. for (int i = 0; i < RI_number_of_requested_actors; ++i){
  299. RI_actor new_actor = {0};
  300. new_actor.mesh_reference = NULL;
  301. new_actor.material_reference = NULL;
  302. ri.actors[i + previous_actor_count] = new_actor;
  303. }
  304. return ri.actors;
  305. }
  306. RI_material* RI_request_materials(int RI_number_of_requested_materials){
  307. int previous_material_count = ri.material_count;
  308. ri.material_count += RI_number_of_requested_materials;
  309. ri.materials = RI_realloc(ri.materials, sizeof(RI_material) * ri.material_count);
  310. for (int i = previous_material_count; i < ri.material_count; ++i){
  311. ri.materials[i] = ri.error_material;
  312. }
  313. return ri.materials;
  314. }
  315. RI_texture* RI_request_textures(int RI_number_of_requested_textures, RI_texture_creation_data *texture_creation_data){
  316. int previous_loaded_texture_count = ri.loaded_texture_count;
  317. ri.loaded_texture_count += RI_number_of_requested_textures;
  318. ri.loaded_textures = RI_realloc(ri.loaded_textures, sizeof(RI_texture) * ri.loaded_texture_count);
  319. for (int i = 0; i < RI_number_of_requested_textures; i++){
  320. RI_texture new_texture = {0};
  321. char *current_texture_filename = texture_creation_data[i].filename;
  322. unsigned char* temp_texture = stbi_load(current_texture_filename, &new_texture.resolution.x, &new_texture.resolution.y, NULL, 4);
  323. if(stbi_failure_reason()){
  324. new_texture = ri.error_texture;
  325. }
  326. else {
  327. new_texture.image_buffer = RI_malloc(sizeof(uint32_t) * new_texture.resolution.x * new_texture.resolution.y);
  328. for (int i = 0; i < new_texture.resolution.x * new_texture.resolution.y; ++i){
  329. unsigned char r = temp_texture[i * 4];
  330. unsigned char g = temp_texture[i * 4 + 1];
  331. unsigned char b = temp_texture[i * 4 + 2];
  332. unsigned char a = temp_texture[i * 4 + 3];
  333. new_texture.image_buffer[i] = (a << 24 | r << 16 | g << 8 | b);
  334. }
  335. }
  336. ri.loaded_textures[previous_loaded_texture_count + i] = new_texture;
  337. stbi_image_free(temp_texture);
  338. }
  339. return ri.loaded_textures;
  340. }
  341. RI_mesh* RI_request_meshes(int RI_number_of_requested_meshes, char **filenames, int RI_return_just_mesh){
  342. int meshes_already_loaded_count = ri.loaded_mesh_count;
  343. RI_mesh* mesh;
  344. if (!RI_return_just_mesh) {
  345. ri.loaded_mesh_count += RI_number_of_requested_meshes;
  346. ri.loaded_meshes = RI_realloc(ri.loaded_meshes, sizeof(RI_mesh) * ri.loaded_mesh_count);
  347. }
  348. else {
  349. mesh = RI_malloc(sizeof(RI_mesh));
  350. }
  351. for (int i = 0; i < RI_number_of_requested_meshes; i++){
  352. RI_mesh new_mesh_data_struct = {0};
  353. FILE *file = fopen(filenames[i], "r");
  354. if (!file){
  355. debug("[Mesh Loader] Error! File \"%s\" not found", filenames[i]);
  356. RI_stop(1);
  357. }
  358. char line[512];
  359. while (fgets(line, sizeof(line), file)) {
  360. if (line[0] == 'f' && line[1] == ' ') { // face
  361. new_mesh_data_struct.face_count++;
  362. }
  363. else if (line[0] == 'v' && line[1] == ' ') { // vertex
  364. new_mesh_data_struct.vertex_count++;
  365. }
  366. else if (line[0] == 'v' && line[1] == 'n') { // normal
  367. new_mesh_data_struct.normal_count++;
  368. }
  369. else if (line[0] == 'v' && line[1] == 't') { // UV
  370. new_mesh_data_struct.uv_count++;
  371. }
  372. }
  373. fclose(file);
  374. new_mesh_data_struct.faces = RI_malloc(sizeof(RI_face) * new_mesh_data_struct.face_count);
  375. new_mesh_data_struct.vertex_positions = RI_malloc(sizeof(RI_vector_3f) * new_mesh_data_struct.vertex_count);
  376. new_mesh_data_struct.normals = RI_malloc(sizeof(RI_vector_3f) * new_mesh_data_struct.normal_count);
  377. new_mesh_data_struct.uvs = RI_malloc(sizeof(RI_vector_2f) * new_mesh_data_struct.uv_count);
  378. FILE *file_again = fopen(filenames[i], "r");
  379. int current_face_index = 0;
  380. int current_vertex_index = 0;
  381. int current_normal_index = 0;
  382. int current_uv_index = 0;
  383. int has_normals, has_uvs;
  384. has_normals = has_uvs = 0;
  385. while (fgets(line, sizeof(line), file_again)) {
  386. if (line[0] == 'f' && line[1] == ' ') {
  387. int vertex_0_index, vertex_1_index, vertex_2_index, normal_0_index, normal_1_index, normal_2_index, uv_0_index, uv_1_index, uv_2_index;
  388. int matches = sscanf(line, "f %d/%d/%d %d/%d/%d %d/%d/%d/",
  389. &vertex_0_index, &uv_0_index, &normal_0_index,
  390. &vertex_1_index, &uv_1_index, &normal_1_index,
  391. &vertex_2_index, &uv_2_index, &normal_2_index);
  392. if (matches != 9){
  393. vertex_0_index = -1;
  394. vertex_1_index = -1;
  395. vertex_2_index = -1;
  396. normal_0_index = -1;
  397. normal_1_index = -1;
  398. normal_2_index = -1;
  399. uv_0_index = -1;
  400. uv_1_index = -1;
  401. uv_2_index = -1;
  402. if (strchr(line, '/')){
  403. sscanf(line, "f %d//%d %d//%d %d//%d",
  404. &vertex_0_index, &normal_0_index,
  405. &vertex_1_index, &normal_1_index,
  406. &vertex_2_index, &normal_2_index);
  407. has_normals = 1;
  408. }
  409. else {
  410. sscanf(line, "f %d %d %d",
  411. &vertex_0_index,
  412. &vertex_1_index,
  413. &vertex_2_index);
  414. }
  415. }
  416. else {
  417. has_normals = has_uvs = 1;
  418. }
  419. new_mesh_data_struct.faces[current_face_index].position_0_index = vertex_0_index - 1;
  420. new_mesh_data_struct.faces[current_face_index].position_1_index = vertex_1_index - 1;
  421. new_mesh_data_struct.faces[current_face_index].position_2_index = vertex_2_index - 1;
  422. new_mesh_data_struct.faces[current_face_index].normal_0_index = normal_0_index - 1;
  423. new_mesh_data_struct.faces[current_face_index].normal_1_index = normal_1_index - 1;
  424. new_mesh_data_struct.faces[current_face_index].normal_2_index = normal_2_index - 1;
  425. new_mesh_data_struct.faces[current_face_index].uv_0_index = uv_0_index - 1;
  426. new_mesh_data_struct.faces[current_face_index].uv_1_index = uv_1_index - 1;
  427. new_mesh_data_struct.faces[current_face_index].uv_2_index = uv_2_index - 1;
  428. new_mesh_data_struct.faces[current_face_index].should_render = 1;
  429. ++current_face_index;
  430. }
  431. else if (line[0] == 'v' && line[1] == ' ') {
  432. double x, y, z;
  433. sscanf(line, "v %lf %lf %lf", &x, &y, &z);
  434. new_mesh_data_struct.vertex_positions[current_vertex_index].x = x;
  435. new_mesh_data_struct.vertex_positions[current_vertex_index].y = y;
  436. new_mesh_data_struct.vertex_positions[current_vertex_index].z = z;
  437. ++current_vertex_index;
  438. }
  439. else if (line[0] == 'v' && line[1] == 'n') {
  440. double x, y, z;
  441. sscanf(line, "vn %lf %lf %lf", &x, &y, &z);
  442. new_mesh_data_struct.normals[current_normal_index].x = x;
  443. new_mesh_data_struct.normals[current_normal_index].y = y;
  444. new_mesh_data_struct.normals[current_normal_index].z = z;
  445. ++current_normal_index;
  446. }
  447. else if (line[0] == 'v' && line[1] == 't') {
  448. double x, y, z;
  449. sscanf(line, "vt %lf %lf %lf", &x, &y, &z);
  450. new_mesh_data_struct.uvs[current_uv_index].x = x;
  451. new_mesh_data_struct.uvs[current_uv_index].y = y;
  452. // UVS are almost always 2D so we don't need Z (the type itself is a vector 2f, not 3f)
  453. ++current_uv_index;
  454. }
  455. }
  456. char* loading_mesh_notice_string;
  457. if (has_normals && !has_uvs) loading_mesh_notice_string = "normals";
  458. else if (!has_normals && has_uvs) loading_mesh_notice_string = "UVs";
  459. else if (!has_normals && !has_uvs) loading_mesh_notice_string = "normals and UVs";
  460. if (!has_normals || !has_uvs) debug("[Mesh Loader] Notice! Mesh \"%s\" is missing %s", filenames[i], loading_mesh_notice_string);
  461. new_mesh_data_struct.has_normals = has_normals;
  462. new_mesh_data_struct.has_uvs = has_uvs;
  463. // fclose(file_again);
  464. if (!RI_return_just_mesh) {
  465. ri.loaded_meshes[meshes_already_loaded_count + i] = new_mesh_data_struct;
  466. debug("[Mesh Loader] Loaded mesh \"%s\"! %d faces, %d verticies, %d normals, %d uvs", filenames[i], current_face_index, current_vertex_index, current_normal_index, current_uv_index);
  467. }
  468. else {
  469. *mesh = new_mesh_data_struct;
  470. }
  471. }
  472. if (!RI_return_just_mesh) return ri.loaded_meshes;
  473. else return mesh;
  474. }
  475. double mod(double a, double b){
  476. if(b < 0.0)
  477. return -mod(-a, -b);
  478. double ret = fmod(a, b);
  479. if(ret < 0.0)
  480. ret+=b;
  481. return ret;
  482. }
  483. uint32_t multiply_rgb(uint32_t color, double factor) {
  484. uint8_t a = (color >> 24) & 0xFF;
  485. uint8_t r = (color >> 16) & 0xFF;
  486. uint8_t g = (color >> 8) & 0xFF;
  487. uint8_t b = color & 0xFF;
  488. r = (uint8_t)fminf(fmaxf(r * factor, 0.0f), 255.0f);
  489. g = (uint8_t)fminf(fmaxf(g * factor, 0.0f), 255.0f);
  490. b = (uint8_t)fminf(fmaxf(b * factor, 0.0f), 255.0f);
  491. return (a << 24) | (r << 16) | (g << 8) | b;
  492. }
  493. int RI_render(RI_scene *scene, RI_texture *target_texture, int clear_texture){
  494. // do rendering stuff
  495. if (ri.running){
  496. double horizontal_fov_factor = target_texture->resolution.x / tanf(0.5 * scene->FOV);
  497. double vertical_fov_factor = target_texture->resolution.y / tanf(0.5 * scene->FOV);
  498. scene->min_clip = scene->minimum_clip_distance;
  499. if (!scene->faces_to_render){
  500. int total_faces = 0;
  501. for (int actor_index = 0; actor_index < scene->actor_count; ++actor_index){
  502. total_faces += scene->actors[actor_index]->mesh_reference->face_count;
  503. }
  504. scene->faces_to_render = RI_malloc(sizeof(RI_renderable_face) * total_faces * 2); // x2 because faces can be split
  505. scene->face_count = total_faces;
  506. }
  507. memset(scene->faces_to_render, 0, sizeof(RI_renderable_face) * scene->face_count * 2);
  508. int current_renderable_face_index = 0;
  509. int current_split_renderable_face_index = 0;
  510. for (int actor_index = 0; actor_index < scene->actor_count; ++actor_index){
  511. RI_actor *current_actor = scene->actors[actor_index];
  512. for (int face_index = 0; face_index < current_actor->mesh_reference->face_count; ++face_index){
  513. RI_face *cur_face = &current_actor->mesh_reference->faces[face_index];
  514. if (!cur_face->should_render){
  515. continue;
  516. }
  517. int vert_pos_0_index = cur_face->position_0_index;
  518. int vert_pos_1_index = cur_face->position_1_index;
  519. int vert_pos_2_index = cur_face->position_2_index;
  520. int normal_0_index = cur_face->normal_0_index;
  521. int normal_1_index = cur_face->normal_1_index;
  522. int normal_2_index = cur_face->normal_2_index;
  523. int uv_0_index = cur_face->uv_0_index;
  524. int uv_1_index = cur_face->uv_1_index;
  525. int uv_2_index = cur_face->uv_2_index;
  526. RI_renderable_face *cur_r_face = &scene->faces_to_render[current_renderable_face_index];
  527. cur_r_face->parent_actor = current_actor;
  528. cur_r_face->shrunk = 0;
  529. cur_r_face->split = 0;
  530. cur_r_face->material_reference = current_actor->material_reference;
  531. cur_r_face->position_0 = current_actor->mesh_reference->vertex_positions[vert_pos_0_index];
  532. cur_r_face->position_1 = current_actor->mesh_reference->vertex_positions[vert_pos_1_index];
  533. cur_r_face->position_2 = current_actor->mesh_reference->vertex_positions[vert_pos_2_index];
  534. if (current_actor->mesh_reference->has_uvs){
  535. cur_r_face->uv_0 = current_actor->mesh_reference->uvs[uv_0_index];
  536. cur_r_face->uv_1 = current_actor->mesh_reference->uvs[uv_1_index];
  537. cur_r_face->uv_2 = current_actor->mesh_reference->uvs[uv_2_index];
  538. }
  539. // scale
  540. vector_3f_hadamard(&cur_r_face->position_0, current_actor->transform.scale);
  541. vector_3f_hadamard(&cur_r_face->position_1, current_actor->transform.scale);
  542. vector_3f_hadamard(&cur_r_face->position_2, current_actor->transform.scale);
  543. // actor rotation
  544. quaternion_rotate(&cur_r_face->position_0, current_actor->transform.rotation);
  545. quaternion_rotate(&cur_r_face->position_1, current_actor->transform.rotation);
  546. quaternion_rotate(&cur_r_face->position_2, current_actor->transform.rotation);
  547. // object position
  548. vector_3f_element_wise_add(&cur_r_face->position_0, current_actor->transform.position);
  549. vector_3f_element_wise_add(&cur_r_face->position_1, current_actor->transform.position);
  550. vector_3f_element_wise_add(&cur_r_face->position_2, current_actor->transform.position);
  551. // camera position & rotation
  552. vector_3f_element_wise_subtract(&cur_r_face->position_0, scene->camera_position);
  553. vector_3f_element_wise_subtract(&cur_r_face->position_1, scene->camera_position);
  554. vector_3f_element_wise_subtract(&cur_r_face->position_2, scene->camera_position);
  555. quaternion_rotate(&cur_r_face->position_0, scene->camera_rotation);
  556. quaternion_rotate(&cur_r_face->position_1, scene->camera_rotation);
  557. quaternion_rotate(&cur_r_face->position_2, scene->camera_rotation);
  558. RI_vector_3f *pos_0 = &cur_r_face->position_0;
  559. RI_vector_3f *pos_1 = &cur_r_face->position_1;
  560. RI_vector_3f *pos_2 = &cur_r_face->position_2;
  561. int is_0_clipped = pos_0->z < scene->min_clip;
  562. int is_1_clipped = pos_1->z < scene->min_clip;
  563. int is_2_clipped = pos_2->z < scene->min_clip;
  564. int clip_count = is_0_clipped + is_1_clipped + is_2_clipped;
  565. cur_r_face->should_render = 1;
  566. switch(clip_count){
  567. case 3: // ignore polygon, it's behind the camera
  568. continue;
  569. break;
  570. case 2:{ // shrink poylgon
  571. RI_vector_3f *unclipped_point, *point_a, *point_b;
  572. RI_vector_3f *unclipped_normal, *normal_a, *normal_b;
  573. RI_vector_2f *unclipped_uv, *uv_a, *uv_b;
  574. if (!is_0_clipped){
  575. unclipped_point = &cur_r_face->position_0;
  576. point_a = &cur_r_face->position_1;
  577. point_b = &cur_r_face->position_2;
  578. unclipped_normal = &cur_r_face->normal_0;
  579. normal_a = &cur_r_face->normal_1;
  580. normal_b = &cur_r_face->normal_2;
  581. unclipped_uv = &cur_r_face->uv_0;
  582. uv_a = &cur_r_face->uv_1;
  583. uv_b = &cur_r_face->uv_2;
  584. }
  585. else if (!is_1_clipped){
  586. unclipped_point = &cur_r_face->position_1;
  587. point_a = &cur_r_face->position_2;
  588. point_b = &cur_r_face->position_0;
  589. unclipped_normal = &cur_r_face->normal_1;
  590. normal_a = &cur_r_face->normal_2;
  591. normal_b = &cur_r_face->normal_0;
  592. unclipped_uv = &cur_r_face->uv_1;
  593. uv_a = &cur_r_face->uv_2;
  594. uv_b = &cur_r_face->uv_0;
  595. }
  596. else if (!is_2_clipped){
  597. unclipped_point = &cur_r_face->position_2;
  598. point_a = &cur_r_face->position_0;
  599. point_b = &cur_r_face->position_1;
  600. unclipped_normal = &cur_r_face->normal_2;
  601. normal_a = &cur_r_face->normal_0;
  602. normal_b = &cur_r_face->normal_1;
  603. unclipped_uv = &cur_r_face->uv_2;
  604. uv_a = &cur_r_face->uv_0;
  605. uv_b = &cur_r_face->uv_1;
  606. }
  607. double fraction_a_to_unclip = (scene->min_clip - unclipped_point->z) / (point_a->z - unclipped_point->z);
  608. double fraction_b_to_unclip = (scene->min_clip - unclipped_point->z) / (point_b->z - unclipped_point->z);
  609. vector_3f_lerp(*unclipped_point, *point_a, point_a, fraction_a_to_unclip);
  610. vector_3f_lerp(*unclipped_point, *point_b, point_b, fraction_b_to_unclip);
  611. vector_3f_lerp(*unclipped_normal, *normal_a, normal_a, fraction_a_to_unclip);
  612. vector_3f_lerp(*unclipped_normal, *normal_b, normal_b, fraction_b_to_unclip);
  613. vector_2f_lerp(*unclipped_uv, *uv_a, uv_a, fraction_a_to_unclip);
  614. vector_2f_lerp(*unclipped_uv, *uv_b, uv_b, fraction_b_to_unclip);
  615. cur_r_face->shrunk = 1;
  616. break;}
  617. case 1: // split polygon
  618. RI_vector_3f clipped_point, point_a, point_b;
  619. RI_vector_3f clipped_normal, normal_a, normal_b;
  620. RI_vector_2f clipped_uv, uv_a, uv_b;
  621. cur_r_face->split = 1;
  622. if (is_0_clipped){
  623. clipped_point = cur_r_face->position_0;
  624. point_a = cur_r_face->position_1;
  625. point_b = cur_r_face->position_2;
  626. clipped_normal = cur_r_face->normal_0;
  627. normal_a = cur_r_face->normal_1;
  628. normal_b = cur_r_face->normal_2;
  629. clipped_uv = cur_r_face->uv_0;
  630. uv_a = cur_r_face->uv_1;
  631. uv_b = cur_r_face->uv_2;
  632. }
  633. else if (is_1_clipped){
  634. clipped_point = cur_r_face->position_1;
  635. point_a = cur_r_face->position_2;
  636. point_b = cur_r_face->position_0;
  637. clipped_normal = cur_r_face->normal_1;
  638. normal_a = cur_r_face->normal_2;
  639. normal_b = cur_r_face->normal_0;
  640. clipped_uv = cur_r_face->uv_1;
  641. uv_a = cur_r_face->uv_2;
  642. uv_b = cur_r_face->uv_0;
  643. }
  644. else if (is_2_clipped){
  645. clipped_point = cur_r_face->position_2;
  646. point_a = cur_r_face->position_0;
  647. point_b = cur_r_face->position_1;
  648. clipped_normal = cur_r_face->normal_2;
  649. normal_a = cur_r_face->normal_0;
  650. normal_b = cur_r_face->normal_1;
  651. clipped_uv = cur_r_face->uv_2;
  652. uv_a = cur_r_face->uv_0;
  653. uv_b = cur_r_face->uv_1;
  654. }
  655. double fraction_a_to_clip = (scene->min_clip - clipped_point.z) / (point_a.z - clipped_point.z);
  656. double fraction_b_to_clip = (scene->min_clip - clipped_point.z) / (point_b.z - clipped_point.z);
  657. RI_vector_3f new_point_a, new_point_b; // the new points that move along the polygon's edge to match the z value of min_clip.
  658. RI_vector_3f new_normal_a, new_normal_b; // they come from the clipped point which was originally only 1
  659. RI_vector_2f new_uv_a, new_uv_b;
  660. vector_3f_lerp(clipped_point, point_a, &new_point_a, fraction_a_to_clip);
  661. vector_3f_lerp(clipped_point, point_b, &new_point_b, fraction_b_to_clip);
  662. vector_3f_lerp(clipped_normal, normal_a, &new_normal_a, fraction_a_to_clip);
  663. vector_3f_lerp(clipped_normal, normal_b, &new_normal_b, fraction_b_to_clip);
  664. vector_2f_lerp(clipped_uv, uv_a, &new_uv_a, fraction_a_to_clip);
  665. vector_2f_lerp(clipped_uv, uv_b, &new_uv_b, fraction_b_to_clip);
  666. // okay, now we have a quad (in clockwise order, point a, point b, new point b, new point a)
  667. // quads are easy to turn into tris >w<
  668. RI_renderable_face *cur_r_split_face = &scene->faces_to_render[scene->face_count + current_split_renderable_face_index];
  669. cur_r_split_face->parent_actor = current_actor;
  670. cur_r_split_face->should_render = 1;
  671. cur_r_split_face->material_reference = cur_r_face->material_reference;
  672. cur_r_face->position_0 = point_a;
  673. cur_r_face->position_1 = point_b;
  674. cur_r_face->position_2 = new_point_a;
  675. cur_r_face->normal_0 = normal_a;
  676. cur_r_face->normal_1 = normal_b;
  677. cur_r_face->normal_2 = new_normal_a;
  678. cur_r_face->uv_0 = uv_a;
  679. cur_r_face->uv_1 = uv_b;
  680. cur_r_face->uv_2 = new_uv_a;
  681. cur_r_split_face->position_0 = point_b;
  682. cur_r_split_face->position_1 = new_point_b;
  683. cur_r_split_face->position_2 = new_point_a;
  684. cur_r_split_face->normal_0 = normal_b;
  685. cur_r_split_face->normal_1 = new_normal_b;
  686. cur_r_split_face->normal_2 = new_normal_a;
  687. cur_r_split_face->uv_0 = uv_b;
  688. cur_r_split_face->uv_1 = new_uv_b;
  689. cur_r_split_face->uv_2 = new_uv_a;
  690. cur_r_split_face->position_0.x = cur_r_split_face->position_0.x / cur_r_split_face->position_0.z * horizontal_fov_factor;
  691. cur_r_split_face->position_0.y = cur_r_split_face->position_0.y / cur_r_split_face->position_0.z * vertical_fov_factor;
  692. cur_r_split_face->position_1.x = cur_r_split_face->position_1.x / cur_r_split_face->position_1.z * horizontal_fov_factor;
  693. cur_r_split_face->position_1.y = cur_r_split_face->position_1.y / cur_r_split_face->position_1.z * vertical_fov_factor;
  694. cur_r_split_face->position_2.x = cur_r_split_face->position_2.x / cur_r_split_face->position_2.z * horizontal_fov_factor;
  695. cur_r_split_face->position_2.y = cur_r_split_face->position_2.y / cur_r_split_face->position_2.z * vertical_fov_factor;
  696. cur_r_split_face->min_screen_x = cur_r_split_face->position_0.x;
  697. if (cur_r_split_face->position_1.x < cur_r_split_face->min_screen_x) cur_r_split_face->min_screen_x = cur_r_split_face->position_1.x;
  698. if (cur_r_split_face->position_2.x < cur_r_split_face->min_screen_x) cur_r_split_face->min_screen_x = cur_r_split_face->position_2.x;
  699. cur_r_split_face->min_screen_x = fmax(cur_r_split_face->min_screen_x, -target_texture->resolution.x / 2);
  700. cur_r_split_face->max_screen_x = cur_r_split_face->position_0.x;
  701. if (cur_r_split_face->position_1.x > cur_r_split_face->max_screen_x) cur_r_split_face->max_screen_x = cur_r_split_face->position_1.x;
  702. if (cur_r_split_face->position_2.x > cur_r_split_face->max_screen_x) cur_r_split_face->max_screen_x = cur_r_split_face->position_2.x;
  703. cur_r_split_face->max_screen_x = fmin(cur_r_split_face->max_screen_x, target_texture->resolution.x / 2);
  704. cur_r_split_face->min_screen_y = cur_r_split_face->position_0.y;
  705. if (cur_r_split_face->position_1.y < cur_r_split_face->min_screen_y) cur_r_split_face->min_screen_y = cur_r_split_face->position_1.y;
  706. if (cur_r_split_face->position_2.y < cur_r_split_face->min_screen_y) cur_r_split_face->min_screen_y = cur_r_split_face->position_2.y;
  707. cur_r_split_face->min_screen_y = fmax(cur_r_split_face->min_screen_y, -target_texture->resolution.y / 2);
  708. cur_r_split_face->max_screen_y = cur_r_split_face->position_0.y;
  709. if (cur_r_split_face->position_1.y > cur_r_split_face->max_screen_y) cur_r_split_face->max_screen_y = cur_r_split_face->position_1.y;
  710. if (cur_r_split_face->position_2.y > cur_r_split_face->max_screen_y) cur_r_split_face->max_screen_y = cur_r_split_face->position_2.y;
  711. cur_r_split_face->max_screen_y = fmin(cur_r_split_face->max_screen_y, target_texture->resolution.y / 2);
  712. ++current_split_renderable_face_index;
  713. break;
  714. case 0: // no issues, ignore
  715. break;
  716. }
  717. current_actor->material_reference->vertex_shader(&cur_r_face->position_0, &cur_r_face->position_1, &cur_r_face->position_2, horizontal_fov_factor, vertical_fov_factor);
  718. cur_r_face->min_screen_x = pos_0->x;
  719. if (pos_1->x < cur_r_face->min_screen_x) cur_r_face->min_screen_x = pos_1->x;
  720. if (pos_2->x < cur_r_face->min_screen_x) cur_r_face->min_screen_x = pos_2->x;
  721. cur_r_face->min_screen_x = fmax(cur_r_face->min_screen_x, -target_texture->resolution.x / 2);
  722. cur_r_face->max_screen_x = pos_0->x;
  723. if (pos_1->x > cur_r_face->max_screen_x) cur_r_face->max_screen_x = pos_1->x;
  724. if (pos_2->x > cur_r_face->max_screen_x) cur_r_face->max_screen_x = pos_2->x;
  725. cur_r_face->max_screen_x = fmin(cur_r_face->max_screen_x, target_texture->resolution.x / 2);
  726. cur_r_face->min_screen_y = pos_0->y;
  727. if (pos_1->y < cur_r_face->min_screen_y) cur_r_face->min_screen_y = pos_1->y;
  728. if (pos_2->y < cur_r_face->min_screen_y) cur_r_face->min_screen_y = pos_2->y;
  729. cur_r_face->min_screen_y = fmax(cur_r_face->min_screen_y, -target_texture->resolution.y / 2);
  730. cur_r_face->max_screen_y = pos_0->y;
  731. if (pos_1->y > cur_r_face->max_screen_y) cur_r_face->max_screen_y = pos_1->y;
  732. if (pos_2->y > cur_r_face->max_screen_y) cur_r_face->max_screen_y = pos_2->y;
  733. cur_r_face->max_screen_y = fmin(cur_r_face->max_screen_y, target_texture->resolution.y / 2);
  734. ++current_renderable_face_index;
  735. }
  736. }
  737. if (ri.z_buffer_resolution.x * ri.z_buffer_resolution.y < target_texture->resolution.x * target_texture->resolution.y){
  738. ri.z_buffer = RI_realloc(ri.z_buffer, sizeof(double) * target_texture->resolution.x * target_texture->resolution.y);
  739. }
  740. for (int pixel_index = 0; pixel_index < target_texture->resolution.x * target_texture->resolution.y; ++pixel_index){
  741. if (clear_texture)
  742. target_texture->image_buffer[pixel_index] = 0xFF333333;
  743. ri.z_buffer[pixel_index] = 999999999;
  744. }
  745. for (int face_index = 0; face_index < current_renderable_face_index * 2; ++face_index){
  746. RI_renderable_face *current_face = &scene->faces_to_render[face_index];
  747. if (!current_face->should_render) continue;
  748. RI_material *mat = current_face->material_reference;
  749. RI_vector_2f *uv_0 = &current_face->uv_0;
  750. RI_vector_2f *uv_1 = &current_face->uv_1;
  751. RI_vector_2f *uv_2 = &current_face->uv_2;
  752. RI_vector_3f *normal_0 = &current_face->normal_0;
  753. RI_vector_3f *normal_1 = &current_face->normal_1;
  754. RI_vector_3f *normal_2 = &current_face->normal_2;
  755. if (mat == NULL){
  756. mat = &ri.error_material;
  757. }
  758. if(mat->flags & RI_MATERIAL_HAS_TEXTURE && mat->texture_reference == NULL){
  759. mat->texture_reference = &ri.error_texture;
  760. }
  761. if(mat->flags & RI_MATERIAL_HAS_BUMP_MAP && mat->bump_map_reference == NULL){
  762. mat->bump_map_reference = &ri.error_bump_map;
  763. }
  764. if(mat->flags & RI_MATERIAL_HAS_NORMAL_MAP && mat->normal_map_reference == NULL){
  765. mat->normal_map_reference = &ri.error_normal_map;
  766. }
  767. RI_vector_3f *pos_0 = &current_face->position_0;
  768. RI_vector_3f *pos_1 = &current_face->position_1;
  769. RI_vector_3f *pos_2 = &current_face->position_2;
  770. for (int pixel_y_index = current_face->min_screen_y; pixel_y_index < current_face->max_screen_y; ++pixel_y_index){
  771. for (int pixel_x_index = current_face->min_screen_x; pixel_x_index < current_face->max_screen_x; ++pixel_x_index){
  772. int x = pixel_x_index + target_texture->resolution.x / 2;
  773. int y = pixel_y_index + target_texture->resolution.y / 2;
  774. if (x < 0 || x >= target_texture->resolution.x || y < 0 || y >= target_texture->resolution.y) continue;
  775. double denominator, w0, w1, w2;
  776. denominator = (pos_1->y - pos_2->y) * (pos_0->x - pos_2->x) + (pos_2->x - pos_1->x) * (pos_0->y - pos_2->y);
  777. w0 = ((pos_1->y - pos_2->y) * (pixel_x_index - pos_2->x) + (pos_2->x - pos_1->x) * (pixel_y_index - pos_2->y)) / denominator;
  778. w1 = ((pos_2->y - pos_0->y) * (pixel_x_index - pos_0->x) + (pos_0->x - pos_2->x) * (pixel_y_index - pos_0->y)) / denominator;
  779. w2 = 1.0 - w0 - w1;
  780. if (!(mat->flags & RI_MATERIAL_DOUBLE_SIDED || scene->flags & RI_SCENE_DOUBLE_SIDED) && denominator > 0){
  781. continue;
  782. }
  783. double w_over_z = (w0 / pos_0->z + w1 / pos_1->z + w2 / pos_2->z);
  784. double interpolated_z = 1.0 / w_over_z;
  785. if (scene->flags & RI_SCENE_DEBUG_AABB) {
  786. target_texture->image_buffer[y * target_texture->resolution.x + x] += 0x0F0F0707;
  787. continue;
  788. }
  789. if (!(w0 >= 0 && w1 >= 0 && w2 >= 0) || ((mat->flags & RI_MATERIAL_WIREFRAME || scene->flags & RI_SCENE_WIREFRAME) && (w0 >= mat->wireframe_width && w1 >= mat->wireframe_width && w2 >= mat->wireframe_width))){
  790. continue;
  791. }
  792. if (!(mat->flags & RI_MATERIAL_DONT_DEPTH_TEST) && interpolated_z >= ri.z_buffer[y * target_texture->resolution.x + x]){
  793. continue;
  794. }
  795. if (scene->flags & RI_SCENE_DEBUG_OVERDRAW) {
  796. target_texture->image_buffer[y * target_texture->resolution.x + x] += 0x0F070F07;
  797. continue;
  798. }
  799. double alpha = 1;
  800. if (!(scene->flags & RI_SCENE_DONT_USE_AA) || !(mat->flags & RI_MATERIAL_DONT_USE_AA)){
  801. double total_inside = 0;
  802. for (double sub_y = 1.0 / (-scene->antialiasing_subsample_resolution / 2.0) - 0.5; sub_y < 1.0 / (scene->antialiasing_subsample_resolution / 2.0) - 0.5; sub_y += 1.0 / (scene->antialiasing_subsample_resolution / 2.0)){
  803. for (double sub_x = 1.0 / (-scene->antialiasing_subsample_resolution / 2.0) - 0.5; sub_x < 1.0 / (scene->antialiasing_subsample_resolution / 2.0) - 0.5; sub_x += 1.0 / (scene->antialiasing_subsample_resolution / 2.0)){
  804. w0 = ((pos_1->y - pos_2->y) * (pixel_x_index + sub_x - pos_2->x) + (pos_2->x - pos_1->x) * (pixel_y_index + sub_y - pos_2->y)) / denominator;
  805. w1 = ((pos_2->y - pos_0->y) * (pixel_x_index + sub_x - pos_0->x) + (pos_0->x - pos_2->x) * (pixel_y_index + sub_y - pos_0->y)) / denominator;
  806. w2 = 1.0 - w0 - w1;
  807. if(!(w0 >= 0 && w1 >= 0 && w2 >= 0)) total_inside++;
  808. }
  809. }
  810. alpha = 1.0 - total_inside / (scene->antialiasing_subsample_resolution * scene->antialiasing_subsample_resolution);
  811. }
  812. uint32_t pixel_color = 0x00000000;
  813. double ux, uy;
  814. ux = uy = -1;
  815. RI_vector_3f normal = {0};
  816. if (normal_0){
  817. normal.x = (w0 * (normal_0->x / pos_0->z) + w1 * (normal_1->x / pos_1->z) + w2 * (normal_2->x / pos_2->z)) / w_over_z;
  818. normal.y = (w0 * (normal_0->y / pos_0->z) + w1 * (normal_1->y / pos_1->z) + w2 * (normal_2->y / pos_2->z)) / w_over_z;
  819. normal.z = (w0 * (normal_0->z / pos_0->z) + w1 * (normal_1->z / pos_1->z) + w2 * (normal_2->z / pos_2->z)) / w_over_z;
  820. }
  821. if (mat->flags & RI_MATERIAL_HAS_TEXTURE && uv_0 && uv_1 && uv_2){
  822. ux = (w0 * (uv_0->x / pos_0->z) + w1 * (uv_1->x / pos_1->z) + w2 * (uv_2->x / pos_2->z)) / w_over_z;
  823. uy = (w0 * (uv_0->y / pos_0->z) + w1 * (uv_1->y / pos_1->z) + w2 * (uv_2->y / pos_2->z)) / w_over_z;
  824. if (mat->flags & RI_MATERIAL_USE_UV_LOOP_MULTIPLIER){
  825. ux *= mat->uv_loop_multiplier.x;
  826. uy *= mat->uv_loop_multiplier.y;
  827. }
  828. if (mat->flags & RI_MATERIAL_USE_UV_RENDER_RESOLUTION){
  829. ux *= (current_face->parent_actor->transform.scale.x / mat->texture_render_size.x);
  830. uy *= (current_face->parent_actor->transform.scale.y / mat->texture_render_size.y);
  831. }
  832. if (mat->flags & RI_MATERIAL_USE_UV_LOOP_MULTIPLIER || mat->flags & RI_MATERIAL_USE_UV_RENDER_RESOLUTION){
  833. ux = mod(ux, 1.0);
  834. uy = mod(-uy, 1.0);
  835. } else{
  836. ux = fmax(fmin(ux, 1.0), 0.0);
  837. uy = fmax(fmin(1.0 - uy, 1.0), 0.0);
  838. }
  839. RI_vector_2 texel_position = {mat->texture_reference->resolution.x * ux, mat->texture_reference->resolution.y * uy};
  840. if (texel_position.y * mat->texture_reference->resolution.x + texel_position.x < 0 || texel_position.y * mat->texture_reference->resolution.x + texel_position.x >= mat->texture_reference->resolution.x * mat->texture_reference->resolution.y)
  841. pixel_color = 0xFFFF00FF;
  842. else
  843. pixel_color = mat->texture_reference->image_buffer[texel_position.y * mat->texture_reference->resolution.x + texel_position.x];
  844. }
  845. else { // must be only an albedo
  846. if (mat->albedo) pixel_color = mat->albedo;
  847. else pixel_color = 0xFFFF77FF;
  848. }
  849. // tri culling debug
  850. // if (face_index >= current_renderable_face_index) pixel_color = 0xFF7777FF;
  851. // if (face_index < scene->face_count) pixel_color = 0xFF77FF77;
  852. // flip the texture
  853. // x = target_texture->resolution.x - 1 - x;
  854. // y = target_texture->resolution.y - 1 - y;
  855. if (scene->flags & RI_SCENE_DEBUG_CULLS){ // show unchanged tris in grey, shrunk tris in blue, split triangles in green (old tri) and red (new tri)
  856. if (current_face->shrunk) pixel_color = 0xFF7777FF;
  857. else if (current_face->split) pixel_color = 0xFF77FF77;
  858. else if (face_index >= current_renderable_face_index) pixel_color = 0xFFFF7777;
  859. else pixel_color = 0xFF777777;
  860. }
  861. double shader_result = shader_result = current_face->material_reference->fragment_shader(x, y, *pos_0, *pos_1, *pos_2, normal, (RI_vector_2f){ux, uy}, pixel_color);
  862. // set alpha after checking shader result becuase things with alpha 0 should still depth write
  863. if (shader_result <= 0) continue;
  864. alpha = shader_result;
  865. if (!(mat->flags & RI_MATERIAL_DONT_DEPTH_WRITE)){
  866. ri.z_buffer[y * target_texture->resolution.x + x] = interpolated_z;
  867. }
  868. if (x >= 0 && y >= 0 && x < target_texture->resolution.x && y < target_texture->resolution.y){
  869. target_texture->image_buffer[y * target_texture->resolution.x + x] = pixel_color;
  870. }
  871. }
  872. }
  873. }
  874. }
  875. else{
  876. RI_stop(0);
  877. }
  878. return 0;
  879. }
  880. void RI_tick(int clear_window_texture_after_rendering){
  881. SDL_UpdateTexture(ri.texture, NULL, ri.frame_buffer->image_buffer, ri.window_width * sizeof(uint32_t));
  882. SDL_RenderClear(ri.renderer);
  883. SDL_RenderCopyEx(ri.renderer, ri.texture, NULL, NULL, 0, NULL, SDL_FLIP_VERTICAL);
  884. SDL_RenderPresent(ri.renderer);
  885. // handle SDL events
  886. while (SDL_PollEvent(&ri.event)){
  887. switch (ri.event.type){
  888. case SDL_QUIT:
  889. ri.running = 0;
  890. }
  891. }
  892. if (clear_window_texture_after_rendering){
  893. RI_clear_texture(ri.frame_buffer);
  894. }
  895. ++ri.frame;
  896. }
  897. int opencl_init(){
  898. cl_int cl_result;
  899. cl_result = clGetPlatformIDs(1, &ri.cl_platform, &ri.cl_number_of_platforms);
  900. if (cl_result != CL_SUCCESS || ri.cl_number_of_platforms == 0) {
  901. debug("[OpenCL Init] Error! No OpenCL platforms");
  902. RI_stop(1);
  903. }
  904. cl_result = clGetDeviceIDs(ri.cl_platform, CL_DEVICE_TYPE_GPU, 1, &ri.cl_device, &ri.cl_number_of_devices);
  905. if (cl_result != CL_SUCCESS || ri.cl_number_of_devices == 0) {
  906. debug("[OpenCL Init] Error! No OpenCL devices");
  907. RI_stop(1);
  908. }
  909. ri.cl_context = clCreateContext(NULL, 1, &ri.cl_device, NULL, NULL, &cl_result);
  910. ri.cl_command_queue = clCreateCommandQueue(ri.cl_context, ri.cl_device, 0, &cl_result);
  911. return 0;
  912. }
  913. int sdl_init(int RI_window_width, int RI_window_height, char *RI_window_title){
  914. ri.window_width = RI_window_width;
  915. ri.window_height = RI_window_height;
  916. ri.window_title = RI_window_title;
  917. ri.frame_buffer = RI_malloc(sizeof(RI_texture));
  918. ri.frame_buffer->image_buffer = RI_malloc(sizeof(uint32_t) * ri.window_width * ri.window_height);
  919. ri.frame_buffer->resolution = (RI_vector_2){ri.window_width, ri.window_height};
  920. ri.z_buffer = RI_malloc(sizeof(double) * ri.window_width * ri.window_height);
  921. ri.z_buffer_resolution = (RI_vector_2){ri.window_width, ri.window_height};
  922. SDL_Init(SDL_INIT_VIDEO);
  923. ri.window = SDL_CreateWindow(RI_window_title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, ri.window_width, ri.window_height, SDL_WINDOW_OPENGL);
  924. ri.renderer = SDL_CreateRenderer(ri.window, -1, SDL_RENDERER_ACCELERATED);
  925. ri.texture = SDL_CreateTexture(ri.renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, ri.window_width, ri.window_height);
  926. return 0;
  927. }
  928. int RI_stop(int result){
  929. debug("[Notice] Stopping...");
  930. for (int scene_index = 0; scene_index < ri.scene_count; ++scene_index){
  931. RI_free(ri.scenes[scene_index].faces_to_render);
  932. RI_free(ri.scenes[scene_index].actors);
  933. }
  934. for (int mesh_index = 0; mesh_index < ri.loaded_mesh_count; ++mesh_index){
  935. RI_free(ri.loaded_meshes[mesh_index].faces);
  936. RI_free(ri.loaded_meshes[mesh_index].vertex_positions);
  937. RI_free(ri.loaded_meshes[mesh_index].normals);
  938. RI_free(ri.loaded_meshes[mesh_index].uvs);
  939. }
  940. for (int texture_index = 0; texture_index < ri.loaded_texture_count; ++texture_index){
  941. RI_free(ri.loaded_textures[texture_index].image_buffer);
  942. }
  943. RI_free(ri.loaded_meshes);
  944. RI_free(ri.loaded_textures);
  945. RI_free(ri.materials);
  946. RI_free(ri.actors);
  947. RI_free(ri.scenes);
  948. RI_free(ri.error_texture.image_buffer);
  949. RI_free(ri.frame_buffer->image_buffer);
  950. RI_free(ri.frame_buffer);
  951. RI_free(ri.z_buffer);
  952. RI_free(ri.error_mesh.faces);
  953. RI_free(ri.error_mesh.vertex_positions);
  954. RI_free(ri.error_mesh.normals);
  955. RI_free(ri.error_mesh.uvs);
  956. if (ri.debug_memory){
  957. size_t total_allocated = 0;
  958. size_t allocated = 0;
  959. size_t alloc_realloc = 0;
  960. size_t total_freed = 0;
  961. size_t freed = 0;
  962. size_t reallocated = 0;
  963. for (int i = 1; i < ri.allocation_table_length; ++i) {
  964. if (ri.allocation_table[i].allocated != 1) continue;
  965. else if (ri.allocation_table[i].freed)
  966. freed += ri.allocation_table[i].size;
  967. else debug("[Memory Manager] Memory allocated at line %d wasn't freed (%zu bytes)", ri.allocation_table[i].line, ri.allocation_table[i].size);
  968. if (!ri.allocation_table[i].reallocated_free && !ri.allocation_table[i].reallocated_alloc)
  969. allocated += ri.allocation_table[i].size;
  970. else if (ri.allocation_table[i].reallocated_alloc)
  971. alloc_realloc += ri.allocation_table[i].size;
  972. else if (ri.allocation_table[i].reallocated_free)
  973. reallocated += ri.allocation_table[i].size;
  974. }
  975. total_allocated = allocated + alloc_realloc;
  976. total_freed = freed + reallocated;
  977. debug("[Memory Manager] [Total Bytes Allocated] M(c)alloc & Realloc(): %zu -- M(c)alloc(): %zu -- Realloc(): %zu", total_allocated, allocated, alloc_realloc);
  978. debug("[Memory Manager] [Total Bytes Freed] Free() & Realloc(): %zu -- Free(): %zu -- Realloc(): %zu", total_freed, freed, reallocated);
  979. if (total_allocated != total_freed){
  980. debug("[Memory Manager] %zu bytes not freed", total_allocated - total_freed);
  981. }
  982. debug("[Memory Manager] Freeing allocation table...");
  983. RI_free(ri.allocation_table);
  984. }
  985. return 0;
  986. }
  987. void signal_interupt_handler(int signal) {
  988. debug("Recieved SIGINT");
  989. RI_stop(1);
  990. }
  991. double default_fragment_shader(int pixel_x, int pixel_y, RI_vector_3f v_pos_0, RI_vector_3f v_pos_1, RI_vector_3f v_pos_2, RI_vector_3f normal, RI_vector_2f uv, uint32_t color){
  992. return 1;
  993. }
  994. void perspective_vertex_shader(RI_vector_3f *v_pos_0, RI_vector_3f *v_pos_1, RI_vector_3f *v_pos_2, double horizontal_fov_factor, double vertical_fov_factor){
  995. v_pos_0->x = v_pos_0->x / v_pos_0->z * horizontal_fov_factor;
  996. v_pos_0->y = v_pos_0->y / v_pos_0->z * vertical_fov_factor;
  997. v_pos_1->x = v_pos_1->x / v_pos_1->z * horizontal_fov_factor;
  998. v_pos_1->y = v_pos_1->y / v_pos_1->z * vertical_fov_factor;
  999. v_pos_2->x = v_pos_2->x / v_pos_2->z * horizontal_fov_factor;
  1000. v_pos_2->y = v_pos_2->y / v_pos_2->z * vertical_fov_factor;
  1001. }
  1002. void orthographic_vertex_shader(RI_vector_3f *v_pos_0, RI_vector_3f *v_pos_1, RI_vector_3f *v_pos_2, double horizontal_fov_factor, double vertical_fov_factor){
  1003. }
  1004. int RI_init(int RI_window_width, int RI_window_height, char *RI_window_title){
  1005. signal(SIGINT, signal_interupt_handler);
  1006. ri.running = 1;
  1007. ri.prefix = "[RasterIver] ";
  1008. if (ri.debug_memory){
  1009. ri.current_allocation_index = 0;
  1010. ri.allocation_search_limit = 100;
  1011. ri.allocation_table_length = 100;
  1012. size_t __size = sizeof(RI_memory_allocation) * ri.allocation_table_length;
  1013. ri.allocation_table = malloc(__size);
  1014. debug("[Memory Manager] Allocated (malloc) %zu bytes", __size);
  1015. ri.allocation_table[ri.current_allocation_index].allocated = 1;
  1016. ri.allocation_table[ri.current_allocation_index].freed = 0;
  1017. ri.allocation_table[ri.current_allocation_index].reallocated_alloc = 0;
  1018. ri.allocation_table[ri.current_allocation_index].reallocated_free = 0;
  1019. ri.allocation_table[ri.current_allocation_index].pointer = ri.allocation_table;
  1020. ri.allocation_table[ri.current_allocation_index].size = __size;
  1021. ri.current_allocation_index++;
  1022. }
  1023. // opencl_init();
  1024. sdl_init(RI_window_width, RI_window_height, RI_window_title);
  1025. ri.loaded_mesh_count = 0;
  1026. ri.loaded_texture_count = 0;
  1027. ri.actor_count = 0;
  1028. char **error_cube_file = RI_malloc(sizeof(char *));
  1029. error_cube_file[0] = "objects/unit_cube.obj";
  1030. RI_mesh* error_mesh = RI_request_meshes(1, error_cube_file, 1);
  1031. ri.error_mesh = *error_mesh;
  1032. RI_free(error_mesh);
  1033. RI_free(error_cube_file);
  1034. ri.error_texture.image_buffer = RI_malloc(sizeof(uint32_t));
  1035. ri.error_texture.image_buffer[0] = 0xFFFF00FF;
  1036. ri.error_texture.resolution = (RI_vector_2){1, 1};
  1037. ri.default_fragment_shader = default_fragment_shader;
  1038. ri.default_vertex_shader = perspective_vertex_shader;
  1039. ri.perspective_vertex_shader = perspective_vertex_shader;
  1040. ri.orthographic_vertex_shader = orthographic_vertex_shader;
  1041. ri.error_material.bump_map_reference = NULL;
  1042. ri.error_material.normal_map_reference = NULL;
  1043. ri.error_material.uv_loop_multiplier = (RI_vector_2f){1.0, 1.0};
  1044. ri.error_material.wireframe_width = 0.2;
  1045. ri.error_material.texture_reference = &ri.error_texture;
  1046. ri.error_material.albedo = 0xFFFF00FF;
  1047. ri.error_material.fragment_shader = ri.default_fragment_shader;
  1048. ri.error_material.vertex_shader = ri.default_vertex_shader;
  1049. ri.error_material.flags = RI_MATERIAL_UNLIT | RI_MATERIAL_DONT_DEPTH_TEST | RI_MATERIAL_DONT_RECEIVE_SHADOW | RI_MATERIAL_HAS_TEXTURE | RI_MATERIAL_DOUBLE_SIDED;
  1050. return 0;
  1051. }