rasteriver.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168
  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. RasterIver ri = {NULL};
  10. void debug(char *string, ...){
  11. va_list args;
  12. va_start(args, string);
  13. char message[500];
  14. strcpy(message, ri.prefix);
  15. strcat(message, string);
  16. vprintf(message, args);
  17. printf("\n");
  18. va_end(args);
  19. }
  20. RasterIver* RI_get_ri(){
  21. return &ri;
  22. }
  23. #define RI_realloc(__ptr, __size) written_RI_realloc(__ptr, __size, __func__, __LINE__)
  24. #define RI_malloc(__size) written_RI_malloc(__size, __func__, __LINE__)
  25. #define RI_calloc(__nmemb, __size) written_RI_calloc(__nmemb, __size, __func__, __LINE__)
  26. #define RI_free(__ptr) written_RI_free(__ptr, __func__, __LINE__)
  27. void* written_RI_realloc(void *__ptr, size_t __size, const char *caller, int line){
  28. void *pointer = realloc(__ptr, __size);
  29. if (ri.debug_memory) {
  30. int current_allocation_index = 0;
  31. int checking = 1;
  32. while (checking){
  33. if (!ri.allocation_table[current_allocation_index].reallocated_free && ri.allocation_table[current_allocation_index].pointer == __ptr){
  34. ri.allocation_table[current_allocation_index].reallocated_free = 1;
  35. checking = 0;
  36. }
  37. current_allocation_index++;
  38. if (current_allocation_index >= ri.allocation_search_limit){
  39. checking = 0;
  40. }
  41. }
  42. debug("[Memory Manager] Allocated (realloc) %zu bytes (func \"%s\":%d)", __size, caller, line);
  43. if (ri.current_allocation_index >= ri.allocation_table_length){
  44. ri.allocation_table_length += 50;
  45. ri.allocation_search_limit += 50;
  46. ri.allocation_table = RI_realloc(ri.allocation_table, sizeof(RI_memory_allocation) * ri.allocation_table_length);
  47. }
  48. ri.allocation_table[ri.current_allocation_index].allocated = 1;
  49. ri.allocation_table[ri.current_allocation_index].reallocated_alloc = 1;
  50. ri.allocation_table[ri.current_allocation_index].reallocated_free = 0;
  51. ri.allocation_table[ri.current_allocation_index].freed = 0;
  52. ri.allocation_table[ri.current_allocation_index].line = line;
  53. ri.allocation_table[ri.current_allocation_index].pointer = pointer;
  54. ri.allocation_table[ri.current_allocation_index].size = __size;
  55. ri.current_allocation_index++;
  56. }
  57. return pointer;
  58. }
  59. void* written_RI_malloc(size_t __size, const char *caller, int line){
  60. void *pointer = malloc(__size);
  61. if (ri.debug_memory) {
  62. debug("[Memory Manager] Allocated (malloc) %zu bytes (func \"%s\":%d)", __size, caller, line);
  63. if (ri.current_allocation_index >= ri.allocation_table_length){
  64. ri.allocation_table_length += 50;
  65. ri.allocation_search_limit += 50;
  66. ri.allocation_table = RI_realloc(ri.allocation_table, sizeof(RI_memory_allocation) * ri.allocation_table_length);
  67. }
  68. ri.allocation_table[ri.current_allocation_index].allocated = 1;
  69. ri.allocation_table[ri.current_allocation_index].reallocated_free = 0;
  70. ri.allocation_table[ri.current_allocation_index].reallocated_alloc = 0;
  71. ri.allocation_table[ri.current_allocation_index].freed = 0;
  72. ri.allocation_table[ri.current_allocation_index].line = line;
  73. ri.allocation_table[ri.current_allocation_index].pointer = pointer;
  74. ri.allocation_table[ri.current_allocation_index].size = __size;
  75. ri.current_allocation_index++;
  76. }
  77. return pointer;
  78. }
  79. void* written_RI_calloc(size_t __nmemb, size_t __size, const char *caller, int line){
  80. void *pointer = calloc(__nmemb, __size);
  81. if (ri.debug_memory) {
  82. debug("[Memory Manager] Allocated (calloc) %zu bytes (func \"%s\":%d)", __size * __nmemb, caller, line);
  83. if (ri.current_allocation_index >= ri.allocation_table_length){
  84. ri.allocation_table_length += 50;
  85. ri.allocation_search_limit += 50;
  86. ri.allocation_table = RI_realloc(ri.allocation_table, sizeof(RI_memory_allocation) * ri.allocation_table_length);
  87. }
  88. ri.allocation_table[ri.current_allocation_index].allocated = 1;
  89. ri.allocation_table[ri.current_allocation_index].reallocated_free = 0;
  90. ri.allocation_table[ri.current_allocation_index].reallocated_alloc = 0;
  91. ri.allocation_table[ri.current_allocation_index].freed = 0;
  92. ri.allocation_table[ri.current_allocation_index].line = line;
  93. ri.allocation_table[ri.current_allocation_index].pointer = pointer;
  94. ri.allocation_table[ri.current_allocation_index].size = __size * __nmemb;
  95. ri.current_allocation_index++;
  96. }
  97. return pointer;
  98. }
  99. void written_RI_free(void *__ptr, const char *caller, int line){
  100. if (ri.debug_memory) {
  101. size_t size = 0;
  102. int current_allocation_index = 0;
  103. int checking = 1;
  104. while (checking){
  105. if (!ri.allocation_table[current_allocation_index].reallocated_free && ri.allocation_table[current_allocation_index].pointer == __ptr){
  106. size = ri.allocation_table[current_allocation_index].size;
  107. ri.allocation_table[current_allocation_index].freed = 1;
  108. checking = 0;
  109. }
  110. current_allocation_index++;
  111. if (current_allocation_index >= ri.allocation_search_limit){
  112. checking = 0;
  113. }
  114. }
  115. debug("[Memory Manager] Freed %zu bytes (func \"%s\":%d)", size, caller, line);
  116. }
  117. free(__ptr);
  118. }
  119. int RI_add_actors_to_scene(int RI_number_of_actors_to_add_to_scene, RI_actor *actors, RI_scene *scene){
  120. int previous_actor_count = scene->actor_count;
  121. scene->actor_count += RI_number_of_actors_to_add_to_scene;
  122. scene->actors = RI_realloc(scene->actors, sizeof(RI_actor *) * scene->actor_count);
  123. for (int i = 0; i < RI_number_of_actors_to_add_to_scene; ++i){
  124. scene->actors[i + previous_actor_count] = &actors[i];
  125. }
  126. return 0;
  127. }
  128. RI_scene* RI_request_scenes(int RI_number_of_requested_scenes){
  129. int previous_scene_count = ri.scene_count;
  130. ri.scene_count += RI_number_of_requested_scenes;
  131. ri.scenes = RI_realloc(ri.scenes, sizeof(RI_scene) * ri.scene_count);
  132. for (int i = 0; i < RI_number_of_requested_scenes; ++i){
  133. RI_scene new_scene = {0};
  134. new_scene.actor_count = 0;
  135. new_scene.actors = NULL;
  136. new_scene.faces_to_render = NULL;
  137. new_scene.antialiasing_subsample_resolution = 4;
  138. ri.scenes[i + previous_scene_count] = new_scene;
  139. }
  140. return ri.scenes;
  141. }
  142. RI_actor* RI_request_actors(int RI_number_of_requested_actors){
  143. int previous_actor_count = ri.actor_count;
  144. ri.actor_count += RI_number_of_requested_actors;
  145. ri.actors = RI_realloc(ri.actors, sizeof(RI_actor) * ri.actor_count);
  146. for (int i = 0; i < RI_number_of_requested_actors; ++i){
  147. RI_actor new_actor = {0};
  148. new_actor.mesh_reference = NULL;
  149. new_actor.material_reference = NULL;
  150. ri.actors[i + previous_actor_count] = new_actor;
  151. }
  152. return ri.actors;
  153. }
  154. RI_material* RI_request_materials(int RI_number_of_requested_materials){
  155. ri.material_count += RI_number_of_requested_materials;
  156. ri.materials = RI_realloc(ri.materials, sizeof(RI_material) * ri.material_count);
  157. return ri.materials;
  158. }
  159. RI_texture* RI_request_textures(int RI_number_of_requested_textures, RI_texture_creation_data *texture_creation_data){
  160. int previous_loaded_texture_count = ri.loaded_texture_count;
  161. ri.loaded_texture_count += RI_number_of_requested_textures;
  162. ri.loaded_textures = RI_realloc(ri.loaded_textures, sizeof(RI_texture) * ri.loaded_texture_count);
  163. for (int i = 0; i < RI_number_of_requested_textures; i++){
  164. RI_texture new_texture = {0};
  165. char *current_texture_filename = texture_creation_data[i].filename;
  166. unsigned char* temp_texture = stbi_load(current_texture_filename, &new_texture.resolution.x, &new_texture.resolution.y, NULL, 4);
  167. if(stbi_failure_reason()){
  168. new_texture = ri.error_texture;
  169. }
  170. else {
  171. new_texture.image_buffer = RI_malloc(sizeof(uint32_t) * new_texture.resolution.x * new_texture.resolution.y);
  172. for (int i = 0; i < new_texture.resolution.x * new_texture.resolution.y; ++i){
  173. unsigned char r = temp_texture[i * 4];
  174. unsigned char g = temp_texture[i * 4 + 1];
  175. unsigned char b = temp_texture[i * 4 + 2];
  176. unsigned char a = temp_texture[i * 4 + 3];
  177. new_texture.image_buffer[i] = (a << 24 | r << 16 | g << 8 | b);
  178. }
  179. }
  180. ri.loaded_textures[previous_loaded_texture_count + i] = new_texture;
  181. stbi_image_free(temp_texture);
  182. }
  183. return ri.loaded_textures;
  184. }
  185. RI_mesh* RI_request_meshes(int RI_number_of_requested_meshes, char **filenames, int RI_return_just_mesh){
  186. int meshes_already_loaded_count = ri.loaded_mesh_count;
  187. RI_mesh* mesh;
  188. if (!RI_return_just_mesh) {
  189. ri.loaded_mesh_count += RI_number_of_requested_meshes;
  190. ri.loaded_meshes = RI_realloc(ri.loaded_meshes, sizeof(RI_mesh) * ri.loaded_mesh_count);
  191. }
  192. else {
  193. mesh = RI_malloc(sizeof(RI_mesh));
  194. }
  195. for (int i = 0; i < RI_number_of_requested_meshes; i++){
  196. RI_mesh new_mesh_data_struct = {0};
  197. FILE *file = fopen(filenames[i], "r");
  198. if (!file){
  199. debug("[Mesh Loader] Error! File \"%s\" not found", filenames[i]);
  200. RI_stop(1);
  201. }
  202. char line[512];
  203. while (fgets(line, sizeof(line), file)) {
  204. if (line[0] == 'f' && line[1] == ' ') { // face
  205. new_mesh_data_struct.face_count++;
  206. }
  207. else if (line[0] == 'v' && line[1] == ' ') { // vertex
  208. new_mesh_data_struct.vertex_count++;
  209. }
  210. else if (line[0] == 'v' && line[1] == 'n') { // normal
  211. new_mesh_data_struct.normal_count++;
  212. }
  213. else if (line[0] == 'v' && line[1] == 't') { // UV
  214. new_mesh_data_struct.uv_count++;
  215. }
  216. }
  217. fclose(file);
  218. new_mesh_data_struct.faces = RI_malloc(sizeof(RI_face) * new_mesh_data_struct.face_count);
  219. new_mesh_data_struct.vertex_positions = RI_malloc(sizeof(RI_vector_3f) * new_mesh_data_struct.vertex_count);
  220. new_mesh_data_struct.normals = RI_malloc(sizeof(RI_vector_3f) * new_mesh_data_struct.normal_count);
  221. new_mesh_data_struct.uvs = RI_malloc(sizeof(RI_vector_2f) * new_mesh_data_struct.uv_count);
  222. FILE *file_again = fopen(filenames[i], "r");
  223. int current_face_index = 0;
  224. int current_vertex_index = 0;
  225. int current_normal_index = 0;
  226. int current_uv_index = 0;
  227. int has_normals, has_uvs;
  228. has_normals = has_uvs = 0;
  229. while (fgets(line, sizeof(line), file_again)) {
  230. if (line[0] == 'f' && line[1] == ' ') {
  231. 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;
  232. int matches = sscanf(line, "f %d/%d/%d %d/%d/%d %d/%d/%d/",
  233. &vertex_0_index, &uv_0_index, &normal_0_index,
  234. &vertex_1_index, &uv_1_index, &normal_1_index,
  235. &vertex_2_index, &uv_2_index, &normal_2_index);
  236. if (matches != 9){
  237. vertex_0_index = -1;
  238. vertex_1_index = -1;
  239. vertex_2_index = -1;
  240. normal_0_index = -1;
  241. normal_1_index = -1;
  242. normal_2_index = -1;
  243. uv_0_index = -1;
  244. uv_1_index = -1;
  245. uv_2_index = -1;
  246. if (strchr(line, '/')){
  247. sscanf(line, "f %d//%d %d//%d %d//%d",
  248. &vertex_0_index, &normal_0_index,
  249. &vertex_1_index, &normal_1_index,
  250. &vertex_2_index, &normal_2_index);
  251. has_normals = 1;
  252. }
  253. else {
  254. sscanf(line, "f %d %d %d",
  255. &vertex_0_index,
  256. &vertex_1_index,
  257. &vertex_2_index);
  258. }
  259. }
  260. else {
  261. has_normals = has_uvs = 1;
  262. }
  263. new_mesh_data_struct.faces[current_face_index].position_0_index = vertex_0_index - 1;
  264. new_mesh_data_struct.faces[current_face_index].position_1_index = vertex_1_index - 1;
  265. new_mesh_data_struct.faces[current_face_index].position_2_index = vertex_2_index - 1;
  266. new_mesh_data_struct.faces[current_face_index].normal_0_index = normal_0_index - 1;
  267. new_mesh_data_struct.faces[current_face_index].normal_1_index = normal_1_index - 1;
  268. new_mesh_data_struct.faces[current_face_index].normal_2_index = normal_2_index - 1;
  269. new_mesh_data_struct.faces[current_face_index].uv_0_index = uv_0_index - 1;
  270. new_mesh_data_struct.faces[current_face_index].uv_1_index = uv_1_index - 1;
  271. new_mesh_data_struct.faces[current_face_index].uv_2_index = uv_2_index - 1;
  272. new_mesh_data_struct.faces[current_face_index].should_render = 1;
  273. ++current_face_index;
  274. }
  275. else if (line[0] == 'v' && line[1] == ' ') {
  276. double x, y, z;
  277. sscanf(line, "v %lf %lf %lf", &x, &y, &z);
  278. new_mesh_data_struct.vertex_positions[current_vertex_index].x = x;
  279. new_mesh_data_struct.vertex_positions[current_vertex_index].y = y;
  280. new_mesh_data_struct.vertex_positions[current_vertex_index].z = z;
  281. ++current_vertex_index;
  282. }
  283. else if (line[0] == 'v' && line[1] == 'n') {
  284. double x, y, z;
  285. sscanf(line, "vn %lf %lf %lf", &x, &y, &z);
  286. new_mesh_data_struct.normals[current_normal_index].x = x;
  287. new_mesh_data_struct.normals[current_normal_index].y = y;
  288. new_mesh_data_struct.normals[current_normal_index].z = z;
  289. ++current_normal_index;
  290. }
  291. else if (line[0] == 'v' && line[1] == 't') {
  292. double x, y, z;
  293. sscanf(line, "vt %lf %lf %lf", &x, &y, &z);
  294. new_mesh_data_struct.uvs[current_uv_index].x = x;
  295. new_mesh_data_struct.uvs[current_uv_index].y = y;
  296. // UVS are almost always 2D so we don't need Z (the type itself is a vector 2f, not 3f)
  297. ++current_uv_index;
  298. }
  299. }
  300. char* loading_mesh_notice_string;
  301. if (has_normals && !has_uvs) loading_mesh_notice_string = "normals";
  302. else if (!has_normals && has_uvs) loading_mesh_notice_string = "UVs";
  303. else if (!has_normals && !has_uvs) loading_mesh_notice_string = "normals and UVs";
  304. if (!has_normals || !has_uvs) debug("[Mesh Loader] Notice! Mesh \"%s\" is missing %s", filenames[i], loading_mesh_notice_string);
  305. new_mesh_data_struct.has_normals = has_normals;
  306. new_mesh_data_struct.has_uvs = has_uvs;
  307. // fclose(file_again);
  308. if (!RI_return_just_mesh) {
  309. ri.loaded_meshes[meshes_already_loaded_count + i] = new_mesh_data_struct;
  310. 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);
  311. }
  312. else {
  313. *mesh = new_mesh_data_struct;
  314. }
  315. }
  316. if (!RI_return_just_mesh) return ri.loaded_meshes;
  317. else return mesh;
  318. }
  319. void quaternion_rotate(RI_vector_3f *position, RI_vector_4f rotation){
  320. RI_vector_4f pos_quat = {0, position->x, position->y, position->z};
  321. RI_vector_4f rotation_conjugation = rotation;
  322. quaternion_conjugate(&rotation_conjugation);
  323. quaternion_multiply(&rotation, pos_quat);
  324. quaternion_multiply(&rotation, rotation_conjugation);
  325. *position = (RI_vector_3f){rotation.x, rotation.y, rotation.z};
  326. }
  327. void RI_euler_rotation_to_quaternion(RI_vector_4f *quaternion, RI_vector_3f euler_rotation){
  328. double cx = cosf(euler_rotation.x * 0.5f);
  329. double sx = sinf(euler_rotation.x * 0.5f);
  330. double cy = cosf(euler_rotation.y * 0.5f);
  331. double sy = sinf(euler_rotation.y * 0.5f);
  332. double cz = cosf(euler_rotation.z * 0.5f);
  333. double sz = sinf(euler_rotation.z * 0.5f);
  334. quaternion->w = cx * cy * cz + sx * sy * sz;
  335. quaternion->x = sx * cy * cz - cx * sy * sz;
  336. quaternion->y = cx * sy * cz + sx * cy * sz;
  337. quaternion->z = cx * cy * sz - sx * sy * cz;
  338. }
  339. double mod(double a, double b){
  340. if(b < 0.0)
  341. return -mod(-a, -b);
  342. double ret = fmod(a, b);
  343. if(ret < 0.0)
  344. ret+=b;
  345. return ret;
  346. }
  347. uint32_t multiply_rgb(uint32_t color, float factor) {
  348. uint8_t a = (color >> 24) & 0xFF;
  349. uint8_t r = (color >> 16) & 0xFF;
  350. uint8_t g = (color >> 8) & 0xFF;
  351. uint8_t b = color & 0xFF;
  352. r = (uint8_t)fminf(fmaxf(r * factor, 0.0f), 255.0f);
  353. g = (uint8_t)fminf(fmaxf(g * factor, 0.0f), 255.0f);
  354. b = (uint8_t)fminf(fmaxf(b * factor, 0.0f), 255.0f);
  355. return (a << 24) | (r << 16) | (g << 8) | b;
  356. }
  357. int RI_render(RI_scene *scene, RI_texture *target_texture){
  358. // do rendering stuff
  359. if (ri.running){
  360. double horizontal_fov_factor = target_texture->resolution.x / tanf(0.5 * scene->FOV);
  361. double vertical_fov_factor = target_texture->resolution.y / tanf(0.5 * scene->FOV);
  362. scene->min_clip = scene->minimum_clip_distance;
  363. if (!scene->faces_to_render){
  364. int total_faces = 0;
  365. for (int actor_index = 0; actor_index < scene->actor_count; ++actor_index){
  366. total_faces += scene->actors[actor_index]->mesh_reference->face_count;
  367. }
  368. scene->faces_to_render = RI_malloc(sizeof(RI_renderable_face) * total_faces * 2); // x2 because faces can be split
  369. scene->face_count = total_faces;
  370. }
  371. memset(scene->faces_to_render, 0, sizeof(RI_renderable_face) * scene->face_count * 2);
  372. int current_renderable_face_index = 0;
  373. int current_split_renderable_face_index = 0;
  374. for (int actor_index = 0; actor_index < scene->actor_count; ++actor_index){
  375. RI_actor *current_actor = scene->actors[actor_index];
  376. for (int face_index = 0; face_index < current_actor->mesh_reference->face_count; ++face_index){
  377. RI_face *cur_face = &current_actor->mesh_reference->faces[face_index];
  378. if (!cur_face->should_render){
  379. continue;
  380. }
  381. int vert_pos_0_index = cur_face->position_0_index;
  382. int vert_pos_1_index = cur_face->position_1_index;
  383. int vert_pos_2_index = cur_face->position_2_index;
  384. int normal_0_index = cur_face->normal_0_index;
  385. int normal_1_index = cur_face->normal_1_index;
  386. int normal_2_index = cur_face->normal_2_index;
  387. int uv_0_index = cur_face->uv_0_index;
  388. int uv_1_index = cur_face->uv_1_index;
  389. int uv_2_index = cur_face->uv_2_index;
  390. RI_renderable_face *cur_r_face = &scene->faces_to_render[current_renderable_face_index];
  391. cur_r_face->parent_actor = current_actor;
  392. cur_r_face->shrunk = 0;
  393. cur_r_face->split = 0;
  394. cur_r_face->material_reference = current_actor->material_reference;
  395. cur_r_face->position_0 = current_actor->mesh_reference->vertex_positions[vert_pos_0_index];
  396. cur_r_face->position_1 = current_actor->mesh_reference->vertex_positions[vert_pos_1_index];
  397. cur_r_face->position_2 = current_actor->mesh_reference->vertex_positions[vert_pos_2_index];
  398. if (current_actor->mesh_reference->has_uvs){
  399. cur_r_face->uv_0 = current_actor->mesh_reference->uvs[uv_0_index];
  400. cur_r_face->uv_1 = current_actor->mesh_reference->uvs[uv_1_index];
  401. cur_r_face->uv_2 = current_actor->mesh_reference->uvs[uv_2_index];
  402. }
  403. // scale
  404. vector_3f_hadamard(&cur_r_face->position_0, current_actor->transform.scale);
  405. vector_3f_hadamard(&cur_r_face->position_1, current_actor->transform.scale);
  406. vector_3f_hadamard(&cur_r_face->position_2, current_actor->transform.scale);
  407. // actor rotation
  408. quaternion_rotate(&cur_r_face->position_0, current_actor->transform.rotation);
  409. quaternion_rotate(&cur_r_face->position_1, current_actor->transform.rotation);
  410. quaternion_rotate(&cur_r_face->position_2, current_actor->transform.rotation);
  411. // object position
  412. vector_3f_element_wise_add(&cur_r_face->position_0, current_actor->transform.position);
  413. vector_3f_element_wise_add(&cur_r_face->position_1, current_actor->transform.position);
  414. vector_3f_element_wise_add(&cur_r_face->position_2, current_actor->transform.position);
  415. // camera rotation
  416. vector_3f_element_wise_subtract(&cur_r_face->position_0, scene->camera_position);
  417. vector_3f_element_wise_subtract(&cur_r_face->position_1, scene->camera_position);
  418. vector_3f_element_wise_subtract(&cur_r_face->position_2, scene->camera_position);
  419. quaternion_rotate(&cur_r_face->position_0, scene->camera_rotation);
  420. quaternion_rotate(&cur_r_face->position_1, scene->camera_rotation);
  421. quaternion_rotate(&cur_r_face->position_2, scene->camera_rotation);
  422. // camera position
  423. // vector_3f_element_wise_subtract(&cur_r_face->position_0, scene->camera_position);
  424. // vector_3f_element_wise_subtract(&cur_r_face->position_1, scene->camera_position);
  425. // vector_3f_element_wise_subtract(&cur_r_face->position_2, scene->camera_position);
  426. RI_vector_3f *pos_0 = &cur_r_face->position_0;
  427. RI_vector_3f *pos_1 = &cur_r_face->position_1;
  428. RI_vector_3f *pos_2 = &cur_r_face->position_2;
  429. int is_0_clipped = pos_0->z < scene->min_clip;
  430. int is_1_clipped = pos_1->z < scene->min_clip;
  431. int is_2_clipped = pos_2->z < scene->min_clip;
  432. int clip_count = is_0_clipped + is_1_clipped + is_2_clipped;
  433. cur_r_face->should_render = 1;
  434. switch(clip_count){
  435. case 3: // ignore polygon, it's behind the camera
  436. continue;
  437. break;
  438. case 2:{ // shrink poylgon
  439. RI_vector_3f *unclipped_point, *point_a, *point_b;
  440. RI_vector_3f *unclipped_normal, *normal_a, *normal_b;
  441. RI_vector_2f *unclipped_uv, *uv_a, *uv_b;
  442. if (!is_0_clipped){
  443. unclipped_point = &cur_r_face->position_0;
  444. point_a = &cur_r_face->position_1;
  445. point_b = &cur_r_face->position_2;
  446. unclipped_normal = &cur_r_face->normal_0;
  447. normal_a = &cur_r_face->normal_1;
  448. normal_b = &cur_r_face->normal_2;
  449. unclipped_uv = &cur_r_face->uv_0;
  450. uv_a = &cur_r_face->uv_1;
  451. uv_b = &cur_r_face->uv_2;
  452. }
  453. else if (!is_1_clipped){
  454. unclipped_point = &cur_r_face->position_1;
  455. point_a = &cur_r_face->position_2;
  456. point_b = &cur_r_face->position_0;
  457. unclipped_normal = &cur_r_face->normal_1;
  458. normal_a = &cur_r_face->normal_2;
  459. normal_b = &cur_r_face->normal_0;
  460. unclipped_uv = &cur_r_face->uv_1;
  461. uv_a = &cur_r_face->uv_2;
  462. uv_b = &cur_r_face->uv_0;
  463. }
  464. else if (!is_2_clipped){
  465. unclipped_point = &cur_r_face->position_2;
  466. point_a = &cur_r_face->position_0;
  467. point_b = &cur_r_face->position_1;
  468. unclipped_normal = &cur_r_face->normal_2;
  469. normal_a = &cur_r_face->normal_0;
  470. normal_b = &cur_r_face->normal_1;
  471. unclipped_uv = &cur_r_face->uv_2;
  472. uv_a = &cur_r_face->uv_0;
  473. uv_b = &cur_r_face->uv_1;
  474. }
  475. double fraction_a_to_unclip = (scene->min_clip - unclipped_point->z) / (point_a->z - unclipped_point->z);
  476. double fraction_b_to_unclip = (scene->min_clip - unclipped_point->z) / (point_b->z - unclipped_point->z);
  477. vector_3f_lerp(*unclipped_point, *point_a, point_a, fraction_a_to_unclip);
  478. vector_3f_lerp(*unclipped_point, *point_b, point_b, fraction_b_to_unclip);
  479. vector_3f_lerp(*unclipped_normal, *normal_a, normal_a, fraction_a_to_unclip);
  480. vector_3f_lerp(*unclipped_normal, *normal_b, normal_b, fraction_b_to_unclip);
  481. vector_2f_lerp(*unclipped_uv, *uv_a, uv_a, fraction_a_to_unclip);
  482. vector_2f_lerp(*unclipped_uv, *uv_b, uv_b, fraction_b_to_unclip);
  483. cur_r_face->shrunk = 1;
  484. break;}
  485. case 1: // split polygon
  486. RI_vector_3f clipped_point, point_a, point_b;
  487. RI_vector_3f clipped_normal, normal_a, normal_b;
  488. RI_vector_2f clipped_uv, uv_a, uv_b;
  489. cur_r_face->split = 1;
  490. if (is_0_clipped){
  491. clipped_point = cur_r_face->position_0;
  492. point_a = cur_r_face->position_1;
  493. point_b = cur_r_face->position_2;
  494. clipped_normal = cur_r_face->normal_0;
  495. normal_a = cur_r_face->normal_1;
  496. normal_b = cur_r_face->normal_2;
  497. clipped_uv = cur_r_face->uv_0;
  498. uv_a = cur_r_face->uv_1;
  499. uv_b = cur_r_face->uv_2;
  500. }
  501. else if (is_1_clipped){
  502. clipped_point = cur_r_face->position_1;
  503. point_a = cur_r_face->position_2;
  504. point_b = cur_r_face->position_0;
  505. clipped_normal = cur_r_face->normal_1;
  506. normal_a = cur_r_face->normal_2;
  507. normal_b = cur_r_face->normal_0;
  508. clipped_uv = cur_r_face->uv_1;
  509. uv_a = cur_r_face->uv_2;
  510. uv_b = cur_r_face->uv_0;
  511. }
  512. else if (is_2_clipped){
  513. clipped_point = cur_r_face->position_2;
  514. point_a = cur_r_face->position_0;
  515. point_b = cur_r_face->position_1;
  516. clipped_normal = cur_r_face->normal_2;
  517. normal_a = cur_r_face->normal_0;
  518. normal_b = cur_r_face->normal_1;
  519. clipped_uv = cur_r_face->uv_2;
  520. uv_a = cur_r_face->uv_0;
  521. uv_b = cur_r_face->uv_1;
  522. }
  523. double fraction_a_to_clip = (scene->min_clip - clipped_point.z) / (point_a.z - clipped_point.z);
  524. double fraction_b_to_clip = (scene->min_clip - clipped_point.z) / (point_b.z - clipped_point.z);
  525. 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.
  526. RI_vector_3f new_normal_a, new_normal_b; // they come from the clipped point which was originally only 1
  527. RI_vector_2f new_uv_a, new_uv_b;
  528. vector_3f_lerp(clipped_point, point_a, &new_point_a, fraction_a_to_clip);
  529. vector_3f_lerp(clipped_point, point_b, &new_point_b, fraction_b_to_clip);
  530. vector_3f_lerp(clipped_normal, normal_a, &new_normal_a, fraction_a_to_clip);
  531. vector_3f_lerp(clipped_normal, normal_b, &new_normal_b, fraction_b_to_clip);
  532. vector_2f_lerp(clipped_uv, uv_a, &new_uv_a, fraction_a_to_clip);
  533. vector_2f_lerp(clipped_uv, uv_b, &new_uv_b, fraction_b_to_clip);
  534. // okay, now we have a quad (in clockwise order, point a, point b, new point b, new point a)
  535. // quads are easy to turn into tris >w<
  536. RI_renderable_face *cur_r_split_face = &scene->faces_to_render[scene->face_count + current_split_renderable_face_index];
  537. cur_r_split_face->parent_actor = current_actor;
  538. cur_r_split_face->should_render = 1;
  539. cur_r_split_face->material_reference = cur_r_face->material_reference;
  540. cur_r_face->position_0 = point_a;
  541. cur_r_face->position_1 = point_b;
  542. cur_r_face->position_2 = new_point_a;
  543. cur_r_face->normal_0 = normal_a;
  544. cur_r_face->normal_1 = normal_b;
  545. cur_r_face->normal_2 = new_normal_a;
  546. cur_r_face->uv_0 = uv_a;
  547. cur_r_face->uv_1 = uv_b;
  548. cur_r_face->uv_2 = new_uv_a;
  549. cur_r_split_face->position_0 = point_b;
  550. cur_r_split_face->position_1 = new_point_b;
  551. cur_r_split_face->position_2 = new_point_a;
  552. cur_r_split_face->normal_0 = normal_b;
  553. cur_r_split_face->normal_1 = new_normal_b;
  554. cur_r_split_face->normal_2 = new_normal_a;
  555. cur_r_split_face->uv_0 = uv_b;
  556. cur_r_split_face->uv_1 = new_uv_b;
  557. cur_r_split_face->uv_2 = new_uv_a;
  558. cur_r_split_face->position_0.x = cur_r_split_face->position_0.x / cur_r_split_face->position_0.z * horizontal_fov_factor;
  559. cur_r_split_face->position_0.y = cur_r_split_face->position_0.y / cur_r_split_face->position_0.z * vertical_fov_factor;
  560. cur_r_split_face->position_1.x = cur_r_split_face->position_1.x / cur_r_split_face->position_1.z * horizontal_fov_factor;
  561. cur_r_split_face->position_1.y = cur_r_split_face->position_1.y / cur_r_split_face->position_1.z * vertical_fov_factor;
  562. cur_r_split_face->position_2.x = cur_r_split_face->position_2.x / cur_r_split_face->position_2.z * horizontal_fov_factor;
  563. cur_r_split_face->position_2.y = cur_r_split_face->position_2.y / cur_r_split_face->position_2.z * vertical_fov_factor;
  564. cur_r_split_face->min_screen_x = cur_r_split_face->position_0.x;
  565. 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;
  566. 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;
  567. cur_r_split_face->min_screen_x = fmax(cur_r_split_face->min_screen_x, -target_texture->resolution.x / 2);
  568. cur_r_split_face->max_screen_x = cur_r_split_face->position_0.x;
  569. 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;
  570. 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;
  571. cur_r_split_face->max_screen_x = fmin(cur_r_split_face->max_screen_x, target_texture->resolution.x / 2);
  572. cur_r_split_face->min_screen_y = cur_r_split_face->position_0.y;
  573. 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;
  574. 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;
  575. cur_r_split_face->min_screen_y = fmax(cur_r_split_face->min_screen_y, -target_texture->resolution.y / 2);
  576. cur_r_split_face->max_screen_y = cur_r_split_face->position_0.y;
  577. 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;
  578. 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;
  579. cur_r_split_face->max_screen_y = fmin(cur_r_split_face->max_screen_y, target_texture->resolution.y / 2);
  580. ++current_split_renderable_face_index;
  581. break;
  582. case 0: // no issues, ignore
  583. break;
  584. }
  585. cur_r_face->position_0.x = cur_r_face->position_0.x / cur_r_face->position_0.z * horizontal_fov_factor;
  586. cur_r_face->position_0.y = cur_r_face->position_0.y / cur_r_face->position_0.z * vertical_fov_factor;
  587. cur_r_face->position_1.x = cur_r_face->position_1.x / cur_r_face->position_1.z * horizontal_fov_factor;
  588. cur_r_face->position_1.y = cur_r_face->position_1.y / cur_r_face->position_1.z * vertical_fov_factor;
  589. cur_r_face->position_2.x = cur_r_face->position_2.x / cur_r_face->position_2.z * horizontal_fov_factor;
  590. cur_r_face->position_2.y = cur_r_face->position_2.y / cur_r_face->position_2.z * vertical_fov_factor;
  591. cur_r_face->min_screen_x = pos_0->x;
  592. if (pos_1->x < cur_r_face->min_screen_x) cur_r_face->min_screen_x = pos_1->x;
  593. if (pos_2->x < cur_r_face->min_screen_x) cur_r_face->min_screen_x = pos_2->x;
  594. cur_r_face->min_screen_x = fmax(cur_r_face->min_screen_x, -target_texture->resolution.x / 2);
  595. cur_r_face->max_screen_x = pos_0->x;
  596. if (pos_1->x > cur_r_face->max_screen_x) cur_r_face->max_screen_x = pos_1->x;
  597. if (pos_2->x > cur_r_face->max_screen_x) cur_r_face->max_screen_x = pos_2->x;
  598. cur_r_face->max_screen_x = fmin(cur_r_face->max_screen_x, target_texture->resolution.x / 2);
  599. cur_r_face->min_screen_y = pos_0->y;
  600. if (pos_1->y < cur_r_face->min_screen_y) cur_r_face->min_screen_y = pos_1->y;
  601. if (pos_2->y < cur_r_face->min_screen_y) cur_r_face->min_screen_y = pos_2->y;
  602. cur_r_face->min_screen_y = fmax(cur_r_face->min_screen_y, -target_texture->resolution.y / 2);
  603. cur_r_face->max_screen_y = pos_0->y;
  604. if (pos_1->y > cur_r_face->max_screen_y) cur_r_face->max_screen_y = pos_1->y;
  605. if (pos_2->y > cur_r_face->max_screen_y) cur_r_face->max_screen_y = pos_2->y;
  606. cur_r_face->max_screen_y = fmin(cur_r_face->max_screen_y, target_texture->resolution.y / 2);
  607. ++current_renderable_face_index;
  608. }
  609. }
  610. if (ri.z_buffer_resolution.x * ri.z_buffer_resolution.y < target_texture->resolution.x * target_texture->resolution.y){
  611. ri.z_buffer = RI_realloc(ri.z_buffer, sizeof(double) * target_texture->resolution.x * target_texture->resolution.y);
  612. }
  613. for (int pixel_index = 0; pixel_index < target_texture->resolution.x * target_texture->resolution.y; ++pixel_index){
  614. target_texture->image_buffer[pixel_index] = 0xFF333333;
  615. ri.z_buffer[pixel_index] = 999999999;
  616. }
  617. for (int face_index = 0; face_index < current_renderable_face_index * 2; ++face_index){
  618. RI_renderable_face *current_face = &scene->faces_to_render[face_index];
  619. if (!current_face->should_render) continue;
  620. RI_material *mat = current_face->material_reference;
  621. RI_vector_2f *uv_0 = &current_face->uv_0;;
  622. RI_vector_2f *uv_1 = &current_face->uv_1;;
  623. RI_vector_2f *uv_2 = &current_face->uv_2;;
  624. if (mat == NULL){
  625. mat = &ri.error_material;
  626. }
  627. if(mat->flags & RI_MATERIAL_HAS_TEXTURE && mat->texture_reference == NULL){
  628. mat->texture_reference = &ri.error_texture;
  629. }
  630. if(mat->flags & RI_MATERIAL_HAS_BUMP_MAP && mat->bump_map_reference == NULL){
  631. mat->bump_map_reference = &ri.error_bump_map;
  632. }
  633. if(mat->flags & RI_MATERIAL_HAS_NORMAL_MAP && mat->normal_map_reference == NULL){
  634. mat->normal_map_reference = &ri.error_normal_map;
  635. }
  636. RI_vector_3f *pos_0 = &current_face->position_0;
  637. RI_vector_3f *pos_1 = &current_face->position_1;
  638. RI_vector_3f *pos_2 = &current_face->position_2;
  639. for (int pixel_y_index = current_face->min_screen_y; pixel_y_index < current_face->max_screen_y; ++pixel_y_index){
  640. for (int pixel_x_index = current_face->min_screen_x; pixel_x_index < current_face->max_screen_x; ++pixel_x_index){
  641. int x = pixel_x_index + target_texture->resolution.x / 2;
  642. int y = pixel_y_index + target_texture->resolution.y / 2;
  643. if (x < 0 || x >= target_texture->resolution.x || y < 0 || y >= target_texture->resolution.y) continue;
  644. double denominator, w0, w1, w2;
  645. 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);
  646. 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;
  647. 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;
  648. w2 = 1.0 - w0 - w1;
  649. if (!(mat->flags & RI_MATERIAL_DOUBLE_SIDED || scene->flags & RI_SCENE_DOUBLE_SIDED) && denominator > 0){
  650. continue;
  651. }
  652. double w_over_z = (w0 / pos_0->z + w1 / pos_1->z + w2 / pos_2->z);
  653. double interpolated_z = 1.0 / w_over_z;
  654. if (scene->flags & RI_SCENE_DEBUG_AABB) {
  655. target_texture->image_buffer[y * target_texture->resolution.x + x] += 0x0F0F0707;
  656. continue;
  657. }
  658. 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))){
  659. continue;
  660. }
  661. if (!(mat->flags & RI_MATERIAL_DONT_DEPTH_TEST) && interpolated_z >= ri.z_buffer[y * target_texture->resolution.x + x]){
  662. continue;
  663. }
  664. if (scene->flags & RI_SCENE_DEBUG_OVERDRAW) {
  665. target_texture->image_buffer[y * target_texture->resolution.x + x] += 0x0F070F07;
  666. continue;
  667. }
  668. if (!(mat->flags & RI_MATERIAL_DONT_DEPTH_WRITE)){
  669. ri.z_buffer[y * target_texture->resolution.x + x] = interpolated_z;
  670. }
  671. double alpha = 1;
  672. if (!(scene->flags & RI_SCENE_DONT_USE_AA) || !(mat->flags & RI_MATERIAL_DONT_USE_AA)){
  673. float total_inside = 0;
  674. 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)){
  675. 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)){
  676. 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;
  677. 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;
  678. w2 = 1.0 - w0 - w1;
  679. if(!(w0 >= 0 && w1 >= 0 && w2 >= 0)) total_inside++;
  680. }
  681. }
  682. alpha = 1.0 - total_inside / (scene->antialiasing_subsample_resolution * scene->antialiasing_subsample_resolution);
  683. }
  684. uint32_t pixel_color = 0x00000000;
  685. if (mat->flags & RI_MATERIAL_HAS_TEXTURE && uv_0 && uv_1 && uv_2){
  686. double 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;
  687. double 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;
  688. if (mat->flags & RI_MATERIAL_USE_UV_LOOP_MULTIPLIER){
  689. ux *= mat->uv_loop_multiplier.x;
  690. uy *= mat->uv_loop_multiplier.y;
  691. }
  692. if (mat->flags & RI_MATERIAL_USE_UV_RENDER_RESOLUTION){
  693. ux *= (current_face->parent_actor->transform.scale.x / mat->texture_render_size.x);
  694. uy *= (current_face->parent_actor->transform.scale.y / mat->texture_render_size.y);
  695. }
  696. ux = mod(ux, 1.0);
  697. uy = mod(-uy, 1.0);
  698. RI_vector_2 texel_position = {mat->texture_reference->resolution.x * ux, mat->texture_reference->resolution.y * uy};
  699. 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)
  700. pixel_color = 0xFFFF00FF;
  701. else
  702. pixel_color = mat->texture_reference->image_buffer[texel_position.y * mat->texture_reference->resolution.x + texel_position.x];
  703. }
  704. else { // must be only an albedo
  705. if (mat->albedo) pixel_color = mat->albedo;
  706. else pixel_color = 0xFFFF77FF;
  707. }
  708. // tri culling debug
  709. // if (face_index >= current_renderable_face_index) pixel_color = 0xFF7777FF;
  710. // if (face_index < scene->face_count) pixel_color = 0xFF77FF77;
  711. // flip the texture
  712. // x = target_texture->resolution.x - 1 - x;
  713. // y = target_texture->resolution.y - 1 - y;
  714. 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)
  715. if (current_face->shrunk) pixel_color = 0xFF7777FF;
  716. else if (current_face->split) pixel_color = 0xFF77FF77;
  717. else if (face_index >= current_renderable_face_index) pixel_color = 0xFFFF7777;
  718. else pixel_color = 0xFF777777;
  719. }
  720. if (x >= 0 && y >= 0 && x < target_texture->resolution.x && y < target_texture->resolution.y){
  721. target_texture->image_buffer[y * target_texture->resolution.x + x] = pixel_color;
  722. }
  723. }
  724. }
  725. }
  726. }
  727. else{
  728. RI_stop(0);
  729. }
  730. return 0;
  731. }
  732. void RI_tick(){
  733. SDL_UpdateTexture(ri.texture, NULL, ri.frame_buffer->image_buffer, ri.window_width * sizeof(uint32_t));
  734. SDL_RenderClear(ri.renderer);
  735. SDL_RenderCopyEx(ri.renderer, ri.texture, NULL, NULL, 0, NULL, SDL_FLIP_VERTICAL);
  736. SDL_RenderPresent(ri.renderer);
  737. // handle SDL events
  738. while (SDL_PollEvent(&ri.event)){
  739. switch (ri.event.type){
  740. case SDL_QUIT:
  741. ri.running = 0;
  742. }
  743. }
  744. ++ri.frame;
  745. }
  746. int opencl_init(){
  747. return 0;
  748. }
  749. int sdl_init(int RI_window_width, int RI_window_height, char *RI_window_title){
  750. ri.window_width = RI_window_width;
  751. ri.window_height = RI_window_height;
  752. ri.window_title = RI_window_title;
  753. ri.frame_buffer = RI_malloc(sizeof(RI_texture));
  754. ri.frame_buffer->image_buffer = RI_malloc(sizeof(uint32_t) * ri.window_width * ri.window_height);
  755. ri.frame_buffer->resolution = (RI_vector_2){ri.window_width, ri.window_height};
  756. ri.z_buffer = RI_malloc(sizeof(double) * ri.window_width * ri.window_height);
  757. ri.z_buffer_resolution = (RI_vector_2){ri.window_width, ri.window_height};
  758. SDL_Init(SDL_INIT_VIDEO);
  759. ri.window = SDL_CreateWindow(RI_window_title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, ri.window_width, ri.window_height, SDL_WINDOW_OPENGL);
  760. ri.renderer = SDL_CreateRenderer(ri.window, -1, SDL_RENDERER_ACCELERATED);
  761. ri.texture = SDL_CreateTexture(ri.renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, ri.window_width, ri.window_height);
  762. return 0;
  763. }
  764. int RI_init(int RI_window_width, int RI_window_height, char *RI_window_title){
  765. ri.running = 1;
  766. ri.prefix = "[RasterIver] ";
  767. if (ri.debug_memory){
  768. ri.current_allocation_index = 0;
  769. ri.allocation_search_limit = 100;
  770. ri.allocation_table_length = 100;
  771. size_t __size = sizeof(RI_memory_allocation) * ri.allocation_table_length;
  772. ri.allocation_table = malloc(__size);
  773. debug("[Memory Manager] Allocated (malloc) %zu bytes", __size);
  774. ri.allocation_table[ri.current_allocation_index].allocated = 1;
  775. ri.allocation_table[ri.current_allocation_index].freed = 0;
  776. ri.allocation_table[ri.current_allocation_index].reallocated_alloc = 0;
  777. ri.allocation_table[ri.current_allocation_index].reallocated_free = 0;
  778. ri.allocation_table[ri.current_allocation_index].pointer = ri.allocation_table;
  779. ri.allocation_table[ri.current_allocation_index].size = __size;
  780. ri.current_allocation_index++;
  781. }
  782. opencl_init();
  783. sdl_init(RI_window_width, RI_window_height, RI_window_title);
  784. ri.loaded_mesh_count = 0;
  785. ri.loaded_texture_count = 0;
  786. ri.actor_count = 0;
  787. char **error_cube_file = RI_malloc(sizeof(char *));
  788. error_cube_file[0] = "objects/unit_cube.obj";
  789. RI_mesh* error_mesh = RI_request_meshes(1, error_cube_file, 1);
  790. ri.error_mesh = *error_mesh;
  791. RI_free(error_mesh);
  792. RI_free(error_cube_file);
  793. ri.error_texture.image_buffer = RI_malloc(sizeof(uint32_t));
  794. ri.error_texture.image_buffer[0] = 0xFFFF00FF;
  795. ri.error_texture.resolution = (RI_vector_2){1, 1};
  796. ri.error_material.texture_reference = &ri.error_texture;
  797. ri.error_material.albedo = 0xFF5522CC;
  798. 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;
  799. return 0;
  800. }
  801. int RI_stop(int result){
  802. debug("[Notice] Stopping...");
  803. for (int scene_index = 0; scene_index < ri.scene_count; ++scene_index){
  804. RI_free(ri.scenes[scene_index].faces_to_render);
  805. RI_free(ri.scenes[scene_index].actors);
  806. }
  807. for (int mesh_index = 0; mesh_index < ri.loaded_mesh_count; ++mesh_index){
  808. RI_free(ri.loaded_meshes[mesh_index].faces);
  809. RI_free(ri.loaded_meshes[mesh_index].vertex_positions);
  810. RI_free(ri.loaded_meshes[mesh_index].normals);
  811. RI_free(ri.loaded_meshes[mesh_index].uvs);
  812. }
  813. for (int texture_index = 0; texture_index < ri.loaded_texture_count; ++texture_index){
  814. RI_free(ri.loaded_textures[texture_index].image_buffer);
  815. }
  816. RI_free(ri.loaded_meshes);
  817. RI_free(ri.loaded_textures);
  818. RI_free(ri.materials);
  819. RI_free(ri.actors);
  820. RI_free(ri.scenes);
  821. RI_free(ri.error_texture.image_buffer);
  822. RI_free(ri.frame_buffer->image_buffer);
  823. RI_free(ri.frame_buffer);
  824. RI_free(ri.z_buffer);
  825. RI_free(ri.error_mesh.faces);
  826. RI_free(ri.error_mesh.vertex_positions);
  827. RI_free(ri.error_mesh.normals);
  828. RI_free(ri.error_mesh.uvs);
  829. if (ri.debug_memory){
  830. size_t total_allocated = 0;
  831. size_t allocated = 0;
  832. size_t alloc_realloc = 0;
  833. size_t total_freed = 0;
  834. size_t freed = 0;
  835. size_t reallocated = 0;
  836. for (int i = 1; i < ri.allocation_table_length; ++i) {
  837. if (ri.allocation_table[i].allocated != 1) continue;
  838. else if (ri.allocation_table[i].freed)
  839. freed += ri.allocation_table[i].size;
  840. else debug("[Memory Manager] Memory allocated at line %d wasn't freed (%zu bytes)", ri.allocation_table[i].line, ri.allocation_table[i].size);
  841. if (!ri.allocation_table[i].reallocated_free && !ri.allocation_table[i].reallocated_alloc)
  842. allocated += ri.allocation_table[i].size;
  843. else if (ri.allocation_table[i].reallocated_alloc)
  844. alloc_realloc += ri.allocation_table[i].size;
  845. else if (ri.allocation_table[i].reallocated_free)
  846. reallocated += ri.allocation_table[i].size;
  847. }
  848. total_allocated = allocated + alloc_realloc;
  849. total_freed = freed + reallocated;
  850. debug("[Memory Manager] [Total Bytes Allocated] M(c)alloc & Realloc(): %zu -- M(c)alloc(): %zu -- Realloc(): %zu", total_allocated, allocated, alloc_realloc);
  851. debug("[Memory Manager] [Total Bytes Freed] Free() & Realloc(): %zu -- Free(): %zu -- Realloc(): %zu", total_freed, freed, reallocated);
  852. if (total_allocated != total_freed){
  853. debug("[Memory Manager] %zu bytes not freed", total_allocated - total_freed);
  854. }
  855. debug("[Memory Manager] Freeing allocation table...");
  856. RI_free(ri.allocation_table);
  857. }
  858. exit(result);
  859. return 0;
  860. }