rasteriver.c 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415
  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, float size, uint32_t color, int bezier_resolution, float 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 = (float)glyph->x_coords[cur] / units_per_em * size + position.x;
  172. new_points[new_point_count].y = (float)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){(float)glyph->x_coords[cur] / units_per_em * size + position.x, (float)glyph->y_coords[cur] / units_per_em * size + position.y}, (RI_vector_2f){(float)glyph->x_coords[next] / units_per_em * size + position.x, (float)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. float w = (float)(i + 1) / (float)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)((float)glyph->y_min / (float)units_per_em * size) + position.y, 0.0); y < fmin((int)((float)glyph->y_max / (float)units_per_em * size) + position.y, target_texture->resolution.y); ++y){
  205. for (int x = fmax((int)((float)glyph->x_min / (float)units_per_em * size) + position.x, 0.0); x < fmin((int)((float)glyph->x_max / (float)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, float 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 + 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. float offset_x = (float)current_glyph->components[k].arg1;
  241. float offset_y = (float)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. position.x += (float)font->h_metrics[glyph].advance_width / font->units_per_em * size;
  250. }
  251. }
  252. int RI_add_actors_to_scene(int RI_number_of_actors_to_add_to_scene, RI_actor *actors, RI_scene *scene){
  253. int previous_actor_count = scene->actor_count;
  254. scene->actor_count += RI_number_of_actors_to_add_to_scene;
  255. scene->actors = RI_realloc(scene->actors, sizeof(RI_actor *) * scene->actor_count);
  256. for (int i = 0; i < RI_number_of_actors_to_add_to_scene; ++i){
  257. scene->actors[i + previous_actor_count] = &actors[i];
  258. }
  259. return 0;
  260. }
  261. RI_scene* RI_request_scenes(int RI_number_of_requested_scenes){
  262. int previous_scene_count = ri.scene_count;
  263. ri.scene_count += RI_number_of_requested_scenes;
  264. ri.scenes = RI_realloc(ri.scenes, sizeof(RI_scene) * ri.scene_count);
  265. for (int i = 0; i < RI_number_of_requested_scenes; ++i){
  266. RI_scene new_scene = {0};
  267. new_scene.actor_count = 0;
  268. new_scene.actors = NULL;
  269. new_scene.faces_to_render = NULL;
  270. new_scene.antialiasing_subsample_resolution = 4;
  271. ri.scenes[i + previous_scene_count] = new_scene;
  272. }
  273. return ri.scenes;
  274. }
  275. RI_actor* RI_request_actors(int RI_number_of_requested_actors){
  276. int previous_actor_count = ri.actor_count;
  277. ri.actor_count += RI_number_of_requested_actors;
  278. ri.actors = RI_realloc(ri.actors, sizeof(RI_actor) * ri.actor_count);
  279. for (int i = 0; i < RI_number_of_requested_actors; ++i){
  280. RI_actor new_actor = {0};
  281. new_actor.mesh_reference = NULL;
  282. new_actor.material_reference = NULL;
  283. ri.actors[i + previous_actor_count] = new_actor;
  284. }
  285. return ri.actors;
  286. }
  287. RI_material* RI_request_materials(int RI_number_of_requested_materials){
  288. ri.material_count += RI_number_of_requested_materials;
  289. ri.materials = RI_realloc(ri.materials, sizeof(RI_material) * ri.material_count);
  290. return ri.materials;
  291. }
  292. RI_texture* RI_request_textures(int RI_number_of_requested_textures, RI_texture_creation_data *texture_creation_data){
  293. int previous_loaded_texture_count = ri.loaded_texture_count;
  294. ri.loaded_texture_count += RI_number_of_requested_textures;
  295. ri.loaded_textures = RI_realloc(ri.loaded_textures, sizeof(RI_texture) * ri.loaded_texture_count);
  296. for (int i = 0; i < RI_number_of_requested_textures; i++){
  297. RI_texture new_texture = {0};
  298. char *current_texture_filename = texture_creation_data[i].filename;
  299. unsigned char* temp_texture = stbi_load(current_texture_filename, &new_texture.resolution.x, &new_texture.resolution.y, NULL, 4);
  300. if(stbi_failure_reason()){
  301. new_texture = ri.error_texture;
  302. }
  303. else {
  304. new_texture.image_buffer = RI_malloc(sizeof(uint32_t) * new_texture.resolution.x * new_texture.resolution.y);
  305. for (int i = 0; i < new_texture.resolution.x * new_texture.resolution.y; ++i){
  306. unsigned char r = temp_texture[i * 4];
  307. unsigned char g = temp_texture[i * 4 + 1];
  308. unsigned char b = temp_texture[i * 4 + 2];
  309. unsigned char a = temp_texture[i * 4 + 3];
  310. new_texture.image_buffer[i] = (a << 24 | r << 16 | g << 8 | b);
  311. }
  312. }
  313. ri.loaded_textures[previous_loaded_texture_count + i] = new_texture;
  314. stbi_image_free(temp_texture);
  315. }
  316. return ri.loaded_textures;
  317. }
  318. RI_mesh* RI_request_meshes(int RI_number_of_requested_meshes, char **filenames, int RI_return_just_mesh){
  319. int meshes_already_loaded_count = ri.loaded_mesh_count;
  320. RI_mesh* mesh;
  321. if (!RI_return_just_mesh) {
  322. ri.loaded_mesh_count += RI_number_of_requested_meshes;
  323. ri.loaded_meshes = RI_realloc(ri.loaded_meshes, sizeof(RI_mesh) * ri.loaded_mesh_count);
  324. }
  325. else {
  326. mesh = RI_malloc(sizeof(RI_mesh));
  327. }
  328. for (int i = 0; i < RI_number_of_requested_meshes; i++){
  329. RI_mesh new_mesh_data_struct = {0};
  330. FILE *file = fopen(filenames[i], "r");
  331. if (!file){
  332. debug("[Mesh Loader] Error! File \"%s\" not found", filenames[i]);
  333. RI_stop(1);
  334. }
  335. char line[512];
  336. while (fgets(line, sizeof(line), file)) {
  337. if (line[0] == 'f' && line[1] == ' ') { // face
  338. new_mesh_data_struct.face_count++;
  339. }
  340. else if (line[0] == 'v' && line[1] == ' ') { // vertex
  341. new_mesh_data_struct.vertex_count++;
  342. }
  343. else if (line[0] == 'v' && line[1] == 'n') { // normal
  344. new_mesh_data_struct.normal_count++;
  345. }
  346. else if (line[0] == 'v' && line[1] == 't') { // UV
  347. new_mesh_data_struct.uv_count++;
  348. }
  349. }
  350. fclose(file);
  351. new_mesh_data_struct.faces = RI_malloc(sizeof(RI_face) * new_mesh_data_struct.face_count);
  352. new_mesh_data_struct.vertex_positions = RI_malloc(sizeof(RI_vector_3f) * new_mesh_data_struct.vertex_count);
  353. new_mesh_data_struct.normals = RI_malloc(sizeof(RI_vector_3f) * new_mesh_data_struct.normal_count);
  354. new_mesh_data_struct.uvs = RI_malloc(sizeof(RI_vector_2f) * new_mesh_data_struct.uv_count);
  355. FILE *file_again = fopen(filenames[i], "r");
  356. int current_face_index = 0;
  357. int current_vertex_index = 0;
  358. int current_normal_index = 0;
  359. int current_uv_index = 0;
  360. int has_normals, has_uvs;
  361. has_normals = has_uvs = 0;
  362. while (fgets(line, sizeof(line), file_again)) {
  363. if (line[0] == 'f' && line[1] == ' ') {
  364. 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;
  365. int matches = sscanf(line, "f %d/%d/%d %d/%d/%d %d/%d/%d/",
  366. &vertex_0_index, &uv_0_index, &normal_0_index,
  367. &vertex_1_index, &uv_1_index, &normal_1_index,
  368. &vertex_2_index, &uv_2_index, &normal_2_index);
  369. if (matches != 9){
  370. vertex_0_index = -1;
  371. vertex_1_index = -1;
  372. vertex_2_index = -1;
  373. normal_0_index = -1;
  374. normal_1_index = -1;
  375. normal_2_index = -1;
  376. uv_0_index = -1;
  377. uv_1_index = -1;
  378. uv_2_index = -1;
  379. if (strchr(line, '/')){
  380. sscanf(line, "f %d//%d %d//%d %d//%d",
  381. &vertex_0_index, &normal_0_index,
  382. &vertex_1_index, &normal_1_index,
  383. &vertex_2_index, &normal_2_index);
  384. has_normals = 1;
  385. }
  386. else {
  387. sscanf(line, "f %d %d %d",
  388. &vertex_0_index,
  389. &vertex_1_index,
  390. &vertex_2_index);
  391. }
  392. }
  393. else {
  394. has_normals = has_uvs = 1;
  395. }
  396. new_mesh_data_struct.faces[current_face_index].position_0_index = vertex_0_index - 1;
  397. new_mesh_data_struct.faces[current_face_index].position_1_index = vertex_1_index - 1;
  398. new_mesh_data_struct.faces[current_face_index].position_2_index = vertex_2_index - 1;
  399. new_mesh_data_struct.faces[current_face_index].normal_0_index = normal_0_index - 1;
  400. new_mesh_data_struct.faces[current_face_index].normal_1_index = normal_1_index - 1;
  401. new_mesh_data_struct.faces[current_face_index].normal_2_index = normal_2_index - 1;
  402. new_mesh_data_struct.faces[current_face_index].uv_0_index = uv_0_index - 1;
  403. new_mesh_data_struct.faces[current_face_index].uv_1_index = uv_1_index - 1;
  404. new_mesh_data_struct.faces[current_face_index].uv_2_index = uv_2_index - 1;
  405. new_mesh_data_struct.faces[current_face_index].should_render = 1;
  406. ++current_face_index;
  407. }
  408. else if (line[0] == 'v' && line[1] == ' ') {
  409. double x, y, z;
  410. sscanf(line, "v %lf %lf %lf", &x, &y, &z);
  411. new_mesh_data_struct.vertex_positions[current_vertex_index].x = x;
  412. new_mesh_data_struct.vertex_positions[current_vertex_index].y = y;
  413. new_mesh_data_struct.vertex_positions[current_vertex_index].z = z;
  414. ++current_vertex_index;
  415. }
  416. else if (line[0] == 'v' && line[1] == 'n') {
  417. double x, y, z;
  418. sscanf(line, "vn %lf %lf %lf", &x, &y, &z);
  419. new_mesh_data_struct.normals[current_normal_index].x = x;
  420. new_mesh_data_struct.normals[current_normal_index].y = y;
  421. new_mesh_data_struct.normals[current_normal_index].z = z;
  422. ++current_normal_index;
  423. }
  424. else if (line[0] == 'v' && line[1] == 't') {
  425. double x, y, z;
  426. sscanf(line, "vt %lf %lf %lf", &x, &y, &z);
  427. new_mesh_data_struct.uvs[current_uv_index].x = x;
  428. new_mesh_data_struct.uvs[current_uv_index].y = y;
  429. // UVS are almost always 2D so we don't need Z (the type itself is a vector 2f, not 3f)
  430. ++current_uv_index;
  431. }
  432. }
  433. char* loading_mesh_notice_string;
  434. if (has_normals && !has_uvs) loading_mesh_notice_string = "normals";
  435. else if (!has_normals && has_uvs) loading_mesh_notice_string = "UVs";
  436. else if (!has_normals && !has_uvs) loading_mesh_notice_string = "normals and UVs";
  437. if (!has_normals || !has_uvs) debug("[Mesh Loader] Notice! Mesh \"%s\" is missing %s", filenames[i], loading_mesh_notice_string);
  438. new_mesh_data_struct.has_normals = has_normals;
  439. new_mesh_data_struct.has_uvs = has_uvs;
  440. // fclose(file_again);
  441. if (!RI_return_just_mesh) {
  442. ri.loaded_meshes[meshes_already_loaded_count + i] = new_mesh_data_struct;
  443. 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);
  444. }
  445. else {
  446. *mesh = new_mesh_data_struct;
  447. }
  448. }
  449. if (!RI_return_just_mesh) return ri.loaded_meshes;
  450. else return mesh;
  451. }
  452. void quaternion_rotate(RI_vector_3f *position, RI_vector_4f rotation){
  453. RI_vector_4f pos_quat = {0, position->x, position->y, position->z};
  454. RI_vector_4f rotation_conjugation = rotation;
  455. quaternion_conjugate(&rotation_conjugation);
  456. quaternion_multiply(&rotation, pos_quat);
  457. quaternion_multiply(&rotation, rotation_conjugation);
  458. *position = (RI_vector_3f){rotation.x, rotation.y, rotation.z};
  459. }
  460. void RI_euler_rotation_to_quaternion(RI_vector_4f *quaternion, RI_vector_3f euler_rotation){
  461. double cx = cosf(euler_rotation.x * 0.5f);
  462. double sx = sinf(euler_rotation.x * 0.5f);
  463. double cy = cosf(euler_rotation.y * 0.5f);
  464. double sy = sinf(euler_rotation.y * 0.5f);
  465. double cz = cosf(euler_rotation.z * 0.5f);
  466. double sz = sinf(euler_rotation.z * 0.5f);
  467. quaternion->w = cx * cy * cz + sx * sy * sz;
  468. quaternion->x = sx * cy * cz - cx * sy * sz;
  469. quaternion->y = cx * sy * cz + sx * cy * sz;
  470. quaternion->z = cx * cy * sz - sx * sy * cz;
  471. }
  472. double mod(double a, double b){
  473. if(b < 0.0)
  474. return -mod(-a, -b);
  475. double ret = fmod(a, b);
  476. if(ret < 0.0)
  477. ret+=b;
  478. return ret;
  479. }
  480. uint32_t multiply_rgb(uint32_t color, float factor) {
  481. uint8_t a = (color >> 24) & 0xFF;
  482. uint8_t r = (color >> 16) & 0xFF;
  483. uint8_t g = (color >> 8) & 0xFF;
  484. uint8_t b = color & 0xFF;
  485. r = (uint8_t)fminf(fmaxf(r * factor, 0.0f), 255.0f);
  486. g = (uint8_t)fminf(fmaxf(g * factor, 0.0f), 255.0f);
  487. b = (uint8_t)fminf(fmaxf(b * factor, 0.0f), 255.0f);
  488. return (a << 24) | (r << 16) | (g << 8) | b;
  489. }
  490. int RI_render(RI_scene *scene, RI_texture *target_texture, int clear_texture){
  491. // do rendering stuff
  492. if (ri.running){
  493. double horizontal_fov_factor = target_texture->resolution.x / tanf(0.5 * scene->FOV);
  494. double vertical_fov_factor = target_texture->resolution.y / tanf(0.5 * scene->FOV);
  495. scene->min_clip = scene->minimum_clip_distance;
  496. if (!scene->faces_to_render){
  497. int total_faces = 0;
  498. for (int actor_index = 0; actor_index < scene->actor_count; ++actor_index){
  499. total_faces += scene->actors[actor_index]->mesh_reference->face_count;
  500. }
  501. scene->faces_to_render = RI_malloc(sizeof(RI_renderable_face) * total_faces * 2); // x2 because faces can be split
  502. scene->face_count = total_faces;
  503. }
  504. memset(scene->faces_to_render, 0, sizeof(RI_renderable_face) * scene->face_count * 2);
  505. int current_renderable_face_index = 0;
  506. int current_split_renderable_face_index = 0;
  507. for (int actor_index = 0; actor_index < scene->actor_count; ++actor_index){
  508. RI_actor *current_actor = scene->actors[actor_index];
  509. for (int face_index = 0; face_index < current_actor->mesh_reference->face_count; ++face_index){
  510. RI_face *cur_face = &current_actor->mesh_reference->faces[face_index];
  511. if (!cur_face->should_render){
  512. continue;
  513. }
  514. int vert_pos_0_index = cur_face->position_0_index;
  515. int vert_pos_1_index = cur_face->position_1_index;
  516. int vert_pos_2_index = cur_face->position_2_index;
  517. int normal_0_index = cur_face->normal_0_index;
  518. int normal_1_index = cur_face->normal_1_index;
  519. int normal_2_index = cur_face->normal_2_index;
  520. int uv_0_index = cur_face->uv_0_index;
  521. int uv_1_index = cur_face->uv_1_index;
  522. int uv_2_index = cur_face->uv_2_index;
  523. RI_renderable_face *cur_r_face = &scene->faces_to_render[current_renderable_face_index];
  524. cur_r_face->parent_actor = current_actor;
  525. cur_r_face->shrunk = 0;
  526. cur_r_face->split = 0;
  527. cur_r_face->material_reference = current_actor->material_reference;
  528. cur_r_face->position_0 = current_actor->mesh_reference->vertex_positions[vert_pos_0_index];
  529. cur_r_face->position_1 = current_actor->mesh_reference->vertex_positions[vert_pos_1_index];
  530. cur_r_face->position_2 = current_actor->mesh_reference->vertex_positions[vert_pos_2_index];
  531. if (current_actor->mesh_reference->has_uvs){
  532. cur_r_face->uv_0 = current_actor->mesh_reference->uvs[uv_0_index];
  533. cur_r_face->uv_1 = current_actor->mesh_reference->uvs[uv_1_index];
  534. cur_r_face->uv_2 = current_actor->mesh_reference->uvs[uv_2_index];
  535. }
  536. // scale
  537. vector_3f_hadamard(&cur_r_face->position_0, current_actor->transform.scale);
  538. vector_3f_hadamard(&cur_r_face->position_1, current_actor->transform.scale);
  539. vector_3f_hadamard(&cur_r_face->position_2, current_actor->transform.scale);
  540. // actor rotation
  541. quaternion_rotate(&cur_r_face->position_0, current_actor->transform.rotation);
  542. quaternion_rotate(&cur_r_face->position_1, current_actor->transform.rotation);
  543. quaternion_rotate(&cur_r_face->position_2, current_actor->transform.rotation);
  544. // object position
  545. vector_3f_element_wise_add(&cur_r_face->position_0, current_actor->transform.position);
  546. vector_3f_element_wise_add(&cur_r_face->position_1, current_actor->transform.position);
  547. vector_3f_element_wise_add(&cur_r_face->position_2, current_actor->transform.position);
  548. // camera rotation
  549. vector_3f_element_wise_subtract(&cur_r_face->position_0, scene->camera_position);
  550. vector_3f_element_wise_subtract(&cur_r_face->position_1, scene->camera_position);
  551. vector_3f_element_wise_subtract(&cur_r_face->position_2, scene->camera_position);
  552. quaternion_rotate(&cur_r_face->position_0, scene->camera_rotation);
  553. quaternion_rotate(&cur_r_face->position_1, scene->camera_rotation);
  554. quaternion_rotate(&cur_r_face->position_2, scene->camera_rotation);
  555. // camera position
  556. // vector_3f_element_wise_subtract(&cur_r_face->position_0, scene->camera_position);
  557. // vector_3f_element_wise_subtract(&cur_r_face->position_1, scene->camera_position);
  558. // vector_3f_element_wise_subtract(&cur_r_face->position_2, scene->camera_position);
  559. RI_vector_3f *pos_0 = &cur_r_face->position_0;
  560. RI_vector_3f *pos_1 = &cur_r_face->position_1;
  561. RI_vector_3f *pos_2 = &cur_r_face->position_2;
  562. int is_0_clipped = pos_0->z < scene->min_clip;
  563. int is_1_clipped = pos_1->z < scene->min_clip;
  564. int is_2_clipped = pos_2->z < scene->min_clip;
  565. int clip_count = is_0_clipped + is_1_clipped + is_2_clipped;
  566. cur_r_face->should_render = 1;
  567. switch(clip_count){
  568. case 3: // ignore polygon, it's behind the camera
  569. continue;
  570. break;
  571. case 2:{ // shrink poylgon
  572. RI_vector_3f *unclipped_point, *point_a, *point_b;
  573. RI_vector_3f *unclipped_normal, *normal_a, *normal_b;
  574. RI_vector_2f *unclipped_uv, *uv_a, *uv_b;
  575. if (!is_0_clipped){
  576. unclipped_point = &cur_r_face->position_0;
  577. point_a = &cur_r_face->position_1;
  578. point_b = &cur_r_face->position_2;
  579. unclipped_normal = &cur_r_face->normal_0;
  580. normal_a = &cur_r_face->normal_1;
  581. normal_b = &cur_r_face->normal_2;
  582. unclipped_uv = &cur_r_face->uv_0;
  583. uv_a = &cur_r_face->uv_1;
  584. uv_b = &cur_r_face->uv_2;
  585. }
  586. else if (!is_1_clipped){
  587. unclipped_point = &cur_r_face->position_1;
  588. point_a = &cur_r_face->position_2;
  589. point_b = &cur_r_face->position_0;
  590. unclipped_normal = &cur_r_face->normal_1;
  591. normal_a = &cur_r_face->normal_2;
  592. normal_b = &cur_r_face->normal_0;
  593. unclipped_uv = &cur_r_face->uv_1;
  594. uv_a = &cur_r_face->uv_2;
  595. uv_b = &cur_r_face->uv_0;
  596. }
  597. else if (!is_2_clipped){
  598. unclipped_point = &cur_r_face->position_2;
  599. point_a = &cur_r_face->position_0;
  600. point_b = &cur_r_face->position_1;
  601. unclipped_normal = &cur_r_face->normal_2;
  602. normal_a = &cur_r_face->normal_0;
  603. normal_b = &cur_r_face->normal_1;
  604. unclipped_uv = &cur_r_face->uv_2;
  605. uv_a = &cur_r_face->uv_0;
  606. uv_b = &cur_r_face->uv_1;
  607. }
  608. double fraction_a_to_unclip = (scene->min_clip - unclipped_point->z) / (point_a->z - unclipped_point->z);
  609. double fraction_b_to_unclip = (scene->min_clip - unclipped_point->z) / (point_b->z - unclipped_point->z);
  610. vector_3f_lerp(*unclipped_point, *point_a, point_a, fraction_a_to_unclip);
  611. vector_3f_lerp(*unclipped_point, *point_b, point_b, fraction_b_to_unclip);
  612. vector_3f_lerp(*unclipped_normal, *normal_a, normal_a, fraction_a_to_unclip);
  613. vector_3f_lerp(*unclipped_normal, *normal_b, normal_b, fraction_b_to_unclip);
  614. vector_2f_lerp(*unclipped_uv, *uv_a, uv_a, fraction_a_to_unclip);
  615. vector_2f_lerp(*unclipped_uv, *uv_b, uv_b, fraction_b_to_unclip);
  616. cur_r_face->shrunk = 1;
  617. break;}
  618. case 1: // split polygon
  619. RI_vector_3f clipped_point, point_a, point_b;
  620. RI_vector_3f clipped_normal, normal_a, normal_b;
  621. RI_vector_2f clipped_uv, uv_a, uv_b;
  622. cur_r_face->split = 1;
  623. if (is_0_clipped){
  624. clipped_point = cur_r_face->position_0;
  625. point_a = cur_r_face->position_1;
  626. point_b = cur_r_face->position_2;
  627. clipped_normal = cur_r_face->normal_0;
  628. normal_a = cur_r_face->normal_1;
  629. normal_b = cur_r_face->normal_2;
  630. clipped_uv = cur_r_face->uv_0;
  631. uv_a = cur_r_face->uv_1;
  632. uv_b = cur_r_face->uv_2;
  633. }
  634. else if (is_1_clipped){
  635. clipped_point = cur_r_face->position_1;
  636. point_a = cur_r_face->position_2;
  637. point_b = cur_r_face->position_0;
  638. clipped_normal = cur_r_face->normal_1;
  639. normal_a = cur_r_face->normal_2;
  640. normal_b = cur_r_face->normal_0;
  641. clipped_uv = cur_r_face->uv_1;
  642. uv_a = cur_r_face->uv_2;
  643. uv_b = cur_r_face->uv_0;
  644. }
  645. else if (is_2_clipped){
  646. clipped_point = cur_r_face->position_2;
  647. point_a = cur_r_face->position_0;
  648. point_b = cur_r_face->position_1;
  649. clipped_normal = cur_r_face->normal_2;
  650. normal_a = cur_r_face->normal_0;
  651. normal_b = cur_r_face->normal_1;
  652. clipped_uv = cur_r_face->uv_2;
  653. uv_a = cur_r_face->uv_0;
  654. uv_b = cur_r_face->uv_1;
  655. }
  656. double fraction_a_to_clip = (scene->min_clip - clipped_point.z) / (point_a.z - clipped_point.z);
  657. double fraction_b_to_clip = (scene->min_clip - clipped_point.z) / (point_b.z - clipped_point.z);
  658. 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.
  659. RI_vector_3f new_normal_a, new_normal_b; // they come from the clipped point which was originally only 1
  660. RI_vector_2f new_uv_a, new_uv_b;
  661. vector_3f_lerp(clipped_point, point_a, &new_point_a, fraction_a_to_clip);
  662. vector_3f_lerp(clipped_point, point_b, &new_point_b, fraction_b_to_clip);
  663. vector_3f_lerp(clipped_normal, normal_a, &new_normal_a, fraction_a_to_clip);
  664. vector_3f_lerp(clipped_normal, normal_b, &new_normal_b, fraction_b_to_clip);
  665. vector_2f_lerp(clipped_uv, uv_a, &new_uv_a, fraction_a_to_clip);
  666. vector_2f_lerp(clipped_uv, uv_b, &new_uv_b, fraction_b_to_clip);
  667. // okay, now we have a quad (in clockwise order, point a, point b, new point b, new point a)
  668. // quads are easy to turn into tris >w<
  669. RI_renderable_face *cur_r_split_face = &scene->faces_to_render[scene->face_count + current_split_renderable_face_index];
  670. cur_r_split_face->parent_actor = current_actor;
  671. cur_r_split_face->should_render = 1;
  672. cur_r_split_face->material_reference = cur_r_face->material_reference;
  673. cur_r_face->position_0 = point_a;
  674. cur_r_face->position_1 = point_b;
  675. cur_r_face->position_2 = new_point_a;
  676. cur_r_face->normal_0 = normal_a;
  677. cur_r_face->normal_1 = normal_b;
  678. cur_r_face->normal_2 = new_normal_a;
  679. cur_r_face->uv_0 = uv_a;
  680. cur_r_face->uv_1 = uv_b;
  681. cur_r_face->uv_2 = new_uv_a;
  682. cur_r_split_face->position_0 = point_b;
  683. cur_r_split_face->position_1 = new_point_b;
  684. cur_r_split_face->position_2 = new_point_a;
  685. cur_r_split_face->normal_0 = normal_b;
  686. cur_r_split_face->normal_1 = new_normal_b;
  687. cur_r_split_face->normal_2 = new_normal_a;
  688. cur_r_split_face->uv_0 = uv_b;
  689. cur_r_split_face->uv_1 = new_uv_b;
  690. cur_r_split_face->uv_2 = new_uv_a;
  691. cur_r_split_face->position_0.x = cur_r_split_face->position_0.x / cur_r_split_face->position_0.z * horizontal_fov_factor;
  692. cur_r_split_face->position_0.y = cur_r_split_face->position_0.y / cur_r_split_face->position_0.z * vertical_fov_factor;
  693. cur_r_split_face->position_1.x = cur_r_split_face->position_1.x / cur_r_split_face->position_1.z * horizontal_fov_factor;
  694. cur_r_split_face->position_1.y = cur_r_split_face->position_1.y / cur_r_split_face->position_1.z * vertical_fov_factor;
  695. cur_r_split_face->position_2.x = cur_r_split_face->position_2.x / cur_r_split_face->position_2.z * horizontal_fov_factor;
  696. cur_r_split_face->position_2.y = cur_r_split_face->position_2.y / cur_r_split_face->position_2.z * vertical_fov_factor;
  697. cur_r_split_face->min_screen_x = cur_r_split_face->position_0.x;
  698. 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;
  699. 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;
  700. cur_r_split_face->min_screen_x = fmax(cur_r_split_face->min_screen_x, -target_texture->resolution.x / 2);
  701. cur_r_split_face->max_screen_x = cur_r_split_face->position_0.x;
  702. 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;
  703. 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;
  704. cur_r_split_face->max_screen_x = fmin(cur_r_split_face->max_screen_x, target_texture->resolution.x / 2);
  705. cur_r_split_face->min_screen_y = cur_r_split_face->position_0.y;
  706. 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;
  707. 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;
  708. cur_r_split_face->min_screen_y = fmax(cur_r_split_face->min_screen_y, -target_texture->resolution.y / 2);
  709. cur_r_split_face->max_screen_y = cur_r_split_face->position_0.y;
  710. 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;
  711. 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;
  712. cur_r_split_face->max_screen_y = fmin(cur_r_split_face->max_screen_y, target_texture->resolution.y / 2);
  713. ++current_split_renderable_face_index;
  714. break;
  715. case 0: // no issues, ignore
  716. break;
  717. }
  718. cur_r_face->position_0.x = cur_r_face->position_0.x / cur_r_face->position_0.z * horizontal_fov_factor;
  719. cur_r_face->position_0.y = cur_r_face->position_0.y / cur_r_face->position_0.z * vertical_fov_factor;
  720. cur_r_face->position_1.x = cur_r_face->position_1.x / cur_r_face->position_1.z * horizontal_fov_factor;
  721. cur_r_face->position_1.y = cur_r_face->position_1.y / cur_r_face->position_1.z * vertical_fov_factor;
  722. cur_r_face->position_2.x = cur_r_face->position_2.x / cur_r_face->position_2.z * horizontal_fov_factor;
  723. cur_r_face->position_2.y = cur_r_face->position_2.y / cur_r_face->position_2.z * vertical_fov_factor;
  724. cur_r_face->min_screen_x = pos_0->x;
  725. if (pos_1->x < cur_r_face->min_screen_x) cur_r_face->min_screen_x = pos_1->x;
  726. if (pos_2->x < cur_r_face->min_screen_x) cur_r_face->min_screen_x = pos_2->x;
  727. cur_r_face->min_screen_x = fmax(cur_r_face->min_screen_x, -target_texture->resolution.x / 2);
  728. cur_r_face->max_screen_x = pos_0->x;
  729. if (pos_1->x > cur_r_face->max_screen_x) cur_r_face->max_screen_x = pos_1->x;
  730. if (pos_2->x > cur_r_face->max_screen_x) cur_r_face->max_screen_x = pos_2->x;
  731. cur_r_face->max_screen_x = fmin(cur_r_face->max_screen_x, target_texture->resolution.x / 2);
  732. cur_r_face->min_screen_y = pos_0->y;
  733. if (pos_1->y < cur_r_face->min_screen_y) cur_r_face->min_screen_y = pos_1->y;
  734. if (pos_2->y < cur_r_face->min_screen_y) cur_r_face->min_screen_y = pos_2->y;
  735. cur_r_face->min_screen_y = fmax(cur_r_face->min_screen_y, -target_texture->resolution.y / 2);
  736. cur_r_face->max_screen_y = pos_0->y;
  737. if (pos_1->y > cur_r_face->max_screen_y) cur_r_face->max_screen_y = pos_1->y;
  738. if (pos_2->y > cur_r_face->max_screen_y) cur_r_face->max_screen_y = pos_2->y;
  739. cur_r_face->max_screen_y = fmin(cur_r_face->max_screen_y, target_texture->resolution.y / 2);
  740. ++current_renderable_face_index;
  741. }
  742. }
  743. if (ri.z_buffer_resolution.x * ri.z_buffer_resolution.y < target_texture->resolution.x * target_texture->resolution.y){
  744. ri.z_buffer = RI_realloc(ri.z_buffer, sizeof(double) * target_texture->resolution.x * target_texture->resolution.y);
  745. }
  746. for (int pixel_index = 0; pixel_index < target_texture->resolution.x * target_texture->resolution.y; ++pixel_index){
  747. if (clear_texture)
  748. target_texture->image_buffer[pixel_index] = 0xFF333333;
  749. ri.z_buffer[pixel_index] = 999999999;
  750. }
  751. for (int face_index = 0; face_index < current_renderable_face_index * 2; ++face_index){
  752. RI_renderable_face *current_face = &scene->faces_to_render[face_index];
  753. if (!current_face->should_render) continue;
  754. RI_material *mat = current_face->material_reference;
  755. RI_vector_2f *uv_0 = &current_face->uv_0;
  756. RI_vector_2f *uv_1 = &current_face->uv_1;
  757. RI_vector_2f *uv_2 = &current_face->uv_2;
  758. RI_vector_3f *normal_0 = &current_face->normal_0;
  759. RI_vector_3f *normal_1 = &current_face->normal_1;
  760. RI_vector_3f *normal_2 = &current_face->normal_2;
  761. if (mat == NULL){
  762. mat = &ri.error_material;
  763. }
  764. if(mat->flags & RI_MATERIAL_HAS_TEXTURE && mat->texture_reference == NULL){
  765. mat->texture_reference = &ri.error_texture;
  766. }
  767. if(mat->flags & RI_MATERIAL_HAS_BUMP_MAP && mat->bump_map_reference == NULL){
  768. mat->bump_map_reference = &ri.error_bump_map;
  769. }
  770. if(mat->flags & RI_MATERIAL_HAS_NORMAL_MAP && mat->normal_map_reference == NULL){
  771. mat->normal_map_reference = &ri.error_normal_map;
  772. }
  773. RI_vector_3f *pos_0 = &current_face->position_0;
  774. RI_vector_3f *pos_1 = &current_face->position_1;
  775. RI_vector_3f *pos_2 = &current_face->position_2;
  776. for (int pixel_y_index = current_face->min_screen_y; pixel_y_index < current_face->max_screen_y; ++pixel_y_index){
  777. for (int pixel_x_index = current_face->min_screen_x; pixel_x_index < current_face->max_screen_x; ++pixel_x_index){
  778. int x = pixel_x_index + target_texture->resolution.x / 2;
  779. int y = pixel_y_index + target_texture->resolution.y / 2;
  780. if (x < 0 || x >= target_texture->resolution.x || y < 0 || y >= target_texture->resolution.y) continue;
  781. double denominator, w0, w1, w2;
  782. 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);
  783. 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;
  784. 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;
  785. w2 = 1.0 - w0 - w1;
  786. if (!(mat->flags & RI_MATERIAL_DOUBLE_SIDED || scene->flags & RI_SCENE_DOUBLE_SIDED) && denominator > 0){
  787. continue;
  788. }
  789. double w_over_z = (w0 / pos_0->z + w1 / pos_1->z + w2 / pos_2->z);
  790. double interpolated_z = 1.0 / w_over_z;
  791. if (scene->flags & RI_SCENE_DEBUG_AABB) {
  792. target_texture->image_buffer[y * target_texture->resolution.x + x] += 0x0F0F0707;
  793. continue;
  794. }
  795. 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))){
  796. continue;
  797. }
  798. if (!(mat->flags & RI_MATERIAL_DONT_DEPTH_TEST) && interpolated_z >= ri.z_buffer[y * target_texture->resolution.x + x]){
  799. continue;
  800. }
  801. if (scene->flags & RI_SCENE_DEBUG_OVERDRAW) {
  802. target_texture->image_buffer[y * target_texture->resolution.x + x] += 0x0F070F07;
  803. continue;
  804. }
  805. double alpha = 1;
  806. if (!(scene->flags & RI_SCENE_DONT_USE_AA) || !(mat->flags & RI_MATERIAL_DONT_USE_AA)){
  807. float total_inside = 0;
  808. for (float 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)){
  809. for (float 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)){
  810. 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;
  811. 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;
  812. w2 = 1.0 - w0 - w1;
  813. if(!(w0 >= 0 && w1 >= 0 && w2 >= 0)) total_inside++;
  814. }
  815. }
  816. alpha = 1.0 - total_inside / (scene->antialiasing_subsample_resolution * scene->antialiasing_subsample_resolution);
  817. }
  818. uint32_t pixel_color = 0x00000000;
  819. double ux, uy;
  820. ux = uy = -1;
  821. RI_vector_3f normal = {0};
  822. if (normal_0){
  823. 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;
  824. 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;
  825. 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;
  826. }
  827. if (mat->flags & RI_MATERIAL_HAS_TEXTURE && uv_0 && uv_1 && uv_2){
  828. 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;
  829. 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;
  830. if (mat->flags & RI_MATERIAL_USE_UV_LOOP_MULTIPLIER){
  831. ux *= mat->uv_loop_multiplier.x;
  832. uy *= mat->uv_loop_multiplier.y;
  833. }
  834. if (mat->flags & RI_MATERIAL_USE_UV_RENDER_RESOLUTION){
  835. ux *= (current_face->parent_actor->transform.scale.x / mat->texture_render_size.x);
  836. uy *= (current_face->parent_actor->transform.scale.y / mat->texture_render_size.y);
  837. }
  838. if (mat->flags & RI_MATERIAL_USE_UV_LOOP_MULTIPLIER || mat->flags & RI_MATERIAL_USE_UV_RENDER_RESOLUTION){
  839. ux = mod(ux, 1.0);
  840. uy = mod(-uy, 1.0);
  841. } else{
  842. ux = fmax(fmin(ux, 1.0), 0.0);
  843. uy = fmax(fmin(1.0 - uy, 1.0), 0.0);
  844. }
  845. RI_vector_2 texel_position = {mat->texture_reference->resolution.x * ux, mat->texture_reference->resolution.y * uy};
  846. 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)
  847. pixel_color = 0xFFFF00FF;
  848. else
  849. pixel_color = mat->texture_reference->image_buffer[texel_position.y * mat->texture_reference->resolution.x + texel_position.x];
  850. }
  851. else { // must be only an albedo
  852. if (mat->albedo) pixel_color = mat->albedo;
  853. else pixel_color = 0xFFFF77FF;
  854. }
  855. // tri culling debug
  856. // if (face_index >= current_renderable_face_index) pixel_color = 0xFF7777FF;
  857. // if (face_index < scene->face_count) pixel_color = 0xFF77FF77;
  858. // flip the texture
  859. // x = target_texture->resolution.x - 1 - x;
  860. // y = target_texture->resolution.y - 1 - y;
  861. 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)
  862. if (current_face->shrunk) pixel_color = 0xFF7777FF;
  863. else if (current_face->split) pixel_color = 0xFF77FF77;
  864. else if (face_index >= current_renderable_face_index) pixel_color = 0xFFFF7777;
  865. else pixel_color = 0xFF777777;
  866. }
  867. double shader_result = 1;
  868. if (current_face->material_reference->shader_function_pointer != NULL) shader_result = current_face->material_reference->shader_function_pointer(x, y, *pos_0, *pos_1, *pos_2, normal, (RI_vector_2f){ux, uy}, pixel_color);
  869. // set alpha after checking shader result becuase things with alpha 0 should still depth write
  870. if (shader_result <= 0) continue;
  871. alpha = shader_result;
  872. if (!(mat->flags & RI_MATERIAL_DONT_DEPTH_WRITE)){
  873. ri.z_buffer[y * target_texture->resolution.x + x] = interpolated_z;
  874. }
  875. if (x >= 0 && y >= 0 && x < target_texture->resolution.x && y < target_texture->resolution.y){
  876. target_texture->image_buffer[y * target_texture->resolution.x + x] = pixel_color;
  877. }
  878. }
  879. }
  880. }
  881. }
  882. else{
  883. RI_stop(0);
  884. }
  885. return 0;
  886. }
  887. void RI_tick(int clear_window_texture_after_rendering){
  888. SDL_UpdateTexture(ri.texture, NULL, ri.frame_buffer->image_buffer, ri.window_width * sizeof(uint32_t));
  889. SDL_RenderClear(ri.renderer);
  890. SDL_RenderCopyEx(ri.renderer, ri.texture, NULL, NULL, 0, NULL, SDL_FLIP_VERTICAL);
  891. SDL_RenderPresent(ri.renderer);
  892. // handle SDL events
  893. while (SDL_PollEvent(&ri.event)){
  894. switch (ri.event.type){
  895. case SDL_QUIT:
  896. ri.running = 0;
  897. }
  898. }
  899. if (clear_window_texture_after_rendering){
  900. RI_clear_texture(ri.frame_buffer);
  901. }
  902. ++ri.frame;
  903. }
  904. int opencl_init(){
  905. cl_int cl_result;
  906. cl_result = clGetPlatformIDs(1, &ri.cl_platform, &ri.cl_number_of_platforms);
  907. if (cl_result != CL_SUCCESS || ri.cl_number_of_platforms == 0) {
  908. debug("[OpenCL Init] Error! No OpenCL platforms");
  909. RI_stop(1);
  910. }
  911. cl_result = clGetDeviceIDs(ri.cl_platform, CL_DEVICE_TYPE_GPU, 1, &ri.cl_device, &ri.cl_number_of_devices);
  912. if (cl_result != CL_SUCCESS || ri.cl_number_of_devices == 0) {
  913. debug("[OpenCL Init] Error! No OpenCL devices");
  914. RI_stop(1);
  915. }
  916. ri.cl_context = clCreateContext(NULL, 1, &ri.cl_device, NULL, NULL, &cl_result);
  917. ri.cl_command_queue = clCreateCommandQueue(ri.cl_context, ri.cl_device, 0, &cl_result);
  918. return 0;
  919. }
  920. int sdl_init(int RI_window_width, int RI_window_height, char *RI_window_title){
  921. ri.window_width = RI_window_width;
  922. ri.window_height = RI_window_height;
  923. ri.window_title = RI_window_title;
  924. ri.frame_buffer = RI_malloc(sizeof(RI_texture));
  925. ri.frame_buffer->image_buffer = RI_malloc(sizeof(uint32_t) * ri.window_width * ri.window_height);
  926. ri.frame_buffer->resolution = (RI_vector_2){ri.window_width, ri.window_height};
  927. ri.z_buffer = RI_malloc(sizeof(double) * ri.window_width * ri.window_height);
  928. ri.z_buffer_resolution = (RI_vector_2){ri.window_width, ri.window_height};
  929. SDL_Init(SDL_INIT_VIDEO);
  930. ri.window = SDL_CreateWindow(RI_window_title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, ri.window_width, ri.window_height, SDL_WINDOW_OPENGL);
  931. ri.renderer = SDL_CreateRenderer(ri.window, -1, SDL_RENDERER_ACCELERATED);
  932. ri.texture = SDL_CreateTexture(ri.renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, ri.window_width, ri.window_height);
  933. return 0;
  934. }
  935. int RI_stop(int result){
  936. debug("[Notice] Stopping...");
  937. for (int scene_index = 0; scene_index < ri.scene_count; ++scene_index){
  938. RI_free(ri.scenes[scene_index].faces_to_render);
  939. RI_free(ri.scenes[scene_index].actors);
  940. }
  941. for (int mesh_index = 0; mesh_index < ri.loaded_mesh_count; ++mesh_index){
  942. RI_free(ri.loaded_meshes[mesh_index].faces);
  943. RI_free(ri.loaded_meshes[mesh_index].vertex_positions);
  944. RI_free(ri.loaded_meshes[mesh_index].normals);
  945. RI_free(ri.loaded_meshes[mesh_index].uvs);
  946. }
  947. for (int texture_index = 0; texture_index < ri.loaded_texture_count; ++texture_index){
  948. RI_free(ri.loaded_textures[texture_index].image_buffer);
  949. }
  950. RI_free(ri.loaded_meshes);
  951. RI_free(ri.loaded_textures);
  952. RI_free(ri.materials);
  953. RI_free(ri.actors);
  954. RI_free(ri.scenes);
  955. RI_free(ri.error_texture.image_buffer);
  956. RI_free(ri.frame_buffer->image_buffer);
  957. RI_free(ri.frame_buffer);
  958. RI_free(ri.z_buffer);
  959. RI_free(ri.error_mesh.faces);
  960. RI_free(ri.error_mesh.vertex_positions);
  961. RI_free(ri.error_mesh.normals);
  962. RI_free(ri.error_mesh.uvs);
  963. if (ri.debug_memory){
  964. size_t total_allocated = 0;
  965. size_t allocated = 0;
  966. size_t alloc_realloc = 0;
  967. size_t total_freed = 0;
  968. size_t freed = 0;
  969. size_t reallocated = 0;
  970. for (int i = 1; i < ri.allocation_table_length; ++i) {
  971. if (ri.allocation_table[i].allocated != 1) continue;
  972. else if (ri.allocation_table[i].freed)
  973. freed += ri.allocation_table[i].size;
  974. else debug("[Memory Manager] Memory allocated at line %d wasn't freed (%zu bytes)", ri.allocation_table[i].line, ri.allocation_table[i].size);
  975. if (!ri.allocation_table[i].reallocated_free && !ri.allocation_table[i].reallocated_alloc)
  976. allocated += ri.allocation_table[i].size;
  977. else if (ri.allocation_table[i].reallocated_alloc)
  978. alloc_realloc += ri.allocation_table[i].size;
  979. else if (ri.allocation_table[i].reallocated_free)
  980. reallocated += ri.allocation_table[i].size;
  981. }
  982. total_allocated = allocated + alloc_realloc;
  983. total_freed = freed + reallocated;
  984. debug("[Memory Manager] [Total Bytes Allocated] M(c)alloc & Realloc(): %zu -- M(c)alloc(): %zu -- Realloc(): %zu", total_allocated, allocated, alloc_realloc);
  985. debug("[Memory Manager] [Total Bytes Freed] Free() & Realloc(): %zu -- Free(): %zu -- Realloc(): %zu", total_freed, freed, reallocated);
  986. if (total_allocated != total_freed){
  987. debug("[Memory Manager] %zu bytes not freed", total_allocated - total_freed);
  988. }
  989. debug("[Memory Manager] Freeing allocation table...");
  990. RI_free(ri.allocation_table);
  991. }
  992. return 0;
  993. }
  994. void signal_interupt_handler(int signal) {
  995. debug("Recieved SIGINT");
  996. RI_stop(1);
  997. }
  998. int RI_init(int RI_window_width, int RI_window_height, char *RI_window_title){
  999. signal(SIGINT, signal_interupt_handler);
  1000. ri.running = 1;
  1001. ri.prefix = "[RasterIver] ";
  1002. if (ri.debug_memory){
  1003. ri.current_allocation_index = 0;
  1004. ri.allocation_search_limit = 100;
  1005. ri.allocation_table_length = 100;
  1006. size_t __size = sizeof(RI_memory_allocation) * ri.allocation_table_length;
  1007. ri.allocation_table = malloc(__size);
  1008. debug("[Memory Manager] Allocated (malloc) %zu bytes", __size);
  1009. ri.allocation_table[ri.current_allocation_index].allocated = 1;
  1010. ri.allocation_table[ri.current_allocation_index].freed = 0;
  1011. ri.allocation_table[ri.current_allocation_index].reallocated_alloc = 0;
  1012. ri.allocation_table[ri.current_allocation_index].reallocated_free = 0;
  1013. ri.allocation_table[ri.current_allocation_index].pointer = ri.allocation_table;
  1014. ri.allocation_table[ri.current_allocation_index].size = __size;
  1015. ri.current_allocation_index++;
  1016. }
  1017. // opencl_init();
  1018. sdl_init(RI_window_width, RI_window_height, RI_window_title);
  1019. ri.loaded_mesh_count = 0;
  1020. ri.loaded_texture_count = 0;
  1021. ri.actor_count = 0;
  1022. char **error_cube_file = RI_malloc(sizeof(char *));
  1023. error_cube_file[0] = "objects/unit_cube.obj";
  1024. RI_mesh* error_mesh = RI_request_meshes(1, error_cube_file, 1);
  1025. ri.error_mesh = *error_mesh;
  1026. RI_free(error_mesh);
  1027. RI_free(error_cube_file);
  1028. ri.error_texture.image_buffer = RI_malloc(sizeof(uint32_t));
  1029. ri.error_texture.image_buffer[0] = 0xFFFF00FF;
  1030. ri.error_texture.resolution = (RI_vector_2){1, 1};
  1031. ri.error_material.texture_reference = &ri.error_texture;
  1032. ri.error_material.albedo = 0xFF5522CC;
  1033. 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;
  1034. return 0;
  1035. }