rasteriver.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  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. int RI_add_actors_to_scene(int RI_number_of_actors_to_add_to_scene, RI_actor *actors, RI_scene *scene){
  24. int previous_actor_count = scene->actor_count;
  25. scene->actor_count += RI_number_of_actors_to_add_to_scene;
  26. scene->actors = realloc(scene->actors, sizeof(RI_actor *) * scene->actor_count);
  27. for (int i = 0; i < RI_number_of_actors_to_add_to_scene; ++i){
  28. scene->actors[i + previous_actor_count] = &actors[i];
  29. }
  30. return 0;
  31. }
  32. RI_scene* RI_request_scene(){
  33. RI_scene* new_scene = malloc(sizeof(RI_scene));
  34. new_scene->actor_count = 0;
  35. new_scene->actors = NULL;
  36. new_scene->faces_to_render = NULL;
  37. return new_scene;
  38. }
  39. RI_actor* RI_request_actors(int RI_number_of_requested_actors){
  40. int previous_actor_count = ri.actor_count;
  41. ri.actor_count += RI_number_of_requested_actors;
  42. ri.actors = realloc(ri.actors, sizeof(RI_actor) * ri.actor_count);
  43. for (int i = 0; i < RI_number_of_requested_actors; ++i){
  44. RI_actor new_actor = {0};
  45. new_actor.mesh_reference = NULL;
  46. new_actor.material_reference = NULL;
  47. ri.actors[i + previous_actor_count] = new_actor;
  48. }
  49. return ri.actors;
  50. }
  51. RI_material* RI_request_materials(int RI_number_of_requested_materials){
  52. ri.material_count += RI_number_of_requested_materials;
  53. ri.materials = realloc(ri.materials, sizeof(RI_material) * ri.material_count);
  54. return ri.materials;
  55. }
  56. RI_texture* RI_request_textures(int RI_number_of_requested_textures, RI_texture_creation_data *texture_creation_data){
  57. int previous_loaded_texture_count = ri.loaded_texture_count;
  58. ri.loaded_texture_count += RI_number_of_requested_textures;
  59. ri.loaded_textures = realloc(ri.loaded_textures, sizeof(RI_texture) * ri.loaded_texture_count);
  60. for (int i = 0; i < RI_number_of_requested_textures; i++){
  61. RI_texture new_texture = {0};
  62. char *current_texture_filename = texture_creation_data[i].filename;
  63. unsigned char* temp_texture = stbi_load(current_texture_filename, &new_texture.resolution.x, &new_texture.resolution.y, NULL, 4);
  64. if(stbi_failure_reason()){
  65. new_texture = ri.error_texture;
  66. }
  67. else {
  68. new_texture.image_buffer = malloc(sizeof(uint32_t) * new_texture.resolution.x * new_texture.resolution.y);
  69. for (int i = 0; i < new_texture.resolution.x * new_texture.resolution.y; ++i){
  70. unsigned char r = temp_texture[i * 4];
  71. unsigned char g = temp_texture[i * 4 + 1];
  72. unsigned char b = temp_texture[i * 4 + 2];
  73. unsigned char a = temp_texture[i * 4 + 3];
  74. new_texture.image_buffer[i] = (a << 24 | r << 16 | g << 8 | b);
  75. }
  76. }
  77. ri.loaded_textures[previous_loaded_texture_count + i] = new_texture;
  78. stbi_image_free(temp_texture);
  79. }
  80. return ri.loaded_textures;
  81. }
  82. RI_mesh* RI_request_meshes(int RI_number_of_requested_meshes, char **filenames, int RI_return_just_mesh){
  83. int meshes_already_loaded_count = ri.loaded_mesh_count;
  84. RI_mesh* mesh;
  85. if (!RI_return_just_mesh) {
  86. ri.loaded_mesh_count += RI_number_of_requested_meshes;
  87. ri.loaded_meshes = realloc(ri.loaded_meshes, sizeof(RI_mesh) * ri.loaded_mesh_count);
  88. }
  89. else {
  90. mesh = malloc(sizeof(RI_mesh));
  91. }
  92. for (int i = 0; i < RI_number_of_requested_meshes; i++){
  93. RI_mesh new_mesh_data_struct = {0};
  94. FILE *file = fopen(filenames[i], "r");
  95. if (!file){
  96. debug("Error! File \"%s\" not found", filenames[i]);
  97. RI_stop(1);
  98. }
  99. char line[512];
  100. while (fgets(line, sizeof(line), file)) {
  101. if (line[0] == 'f' && line[1] == ' ') { // face
  102. new_mesh_data_struct.face_count++;
  103. }
  104. else if (line[0] == 'v' && line[1] == ' ') { // vertex
  105. new_mesh_data_struct.vertex_count++;
  106. }
  107. else if (line[0] == 'v' && line[1] == 'n') { // normal
  108. new_mesh_data_struct.normal_count++;
  109. }
  110. else if (line[0] == 'v' && line[1] == 't') { // UV
  111. new_mesh_data_struct.uv_count++;
  112. }
  113. }
  114. fclose(file);
  115. new_mesh_data_struct.faces = malloc(sizeof(RI_face) * new_mesh_data_struct.face_count);
  116. new_mesh_data_struct.vertex_positions = malloc(sizeof(RI_vector_3f) * new_mesh_data_struct.vertex_count);
  117. new_mesh_data_struct.normals = malloc(sizeof(RI_vector_3f) * new_mesh_data_struct.normal_count);
  118. new_mesh_data_struct.uvs = malloc(sizeof(RI_vector_2f) * new_mesh_data_struct.uv_count);
  119. FILE *file_again = fopen(filenames[i], "r");
  120. int current_face_index = 0;
  121. int current_vertex_index = 0;
  122. int current_normal_index = 0;
  123. int current_uv_index = 0;
  124. int has_normals, has_uvs;
  125. has_normals = has_uvs = 0;
  126. while (fgets(line, sizeof(line), file_again)) {
  127. if (line[0] == 'f' && line[1] == ' ') {
  128. 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;
  129. int matches = sscanf(line, "f %d/%d/%d %d/%d/%d %d/%d/%d/",
  130. &vertex_0_index, &uv_0_index, &normal_0_index,
  131. &vertex_1_index, &uv_1_index, &normal_1_index,
  132. &vertex_2_index, &uv_2_index, &normal_2_index);
  133. if (matches != 9){
  134. vertex_0_index = -1;
  135. vertex_1_index = -1;
  136. vertex_2_index = -1;
  137. normal_0_index = -1;
  138. normal_1_index = -1;
  139. normal_2_index = -1;
  140. uv_0_index = -1;
  141. uv_1_index = -1;
  142. uv_2_index = -1;
  143. if (strchr(line, '/')){
  144. sscanf(line, "f %d//%d %d//%d %d//%d",
  145. &vertex_0_index, &normal_0_index,
  146. &vertex_1_index, &normal_1_index,
  147. &vertex_2_index, &normal_2_index);
  148. has_normals = 1;
  149. }
  150. else {
  151. sscanf(line, "f %d %d %d",
  152. &vertex_0_index,
  153. &vertex_1_index,
  154. &vertex_2_index);
  155. }
  156. }
  157. else {
  158. has_normals = has_uvs = 1;
  159. }
  160. new_mesh_data_struct.faces[current_face_index].position_0_index = vertex_0_index - 1;
  161. new_mesh_data_struct.faces[current_face_index].position_1_index = vertex_1_index - 1;
  162. new_mesh_data_struct.faces[current_face_index].position_2_index = vertex_2_index - 1;
  163. new_mesh_data_struct.faces[current_face_index].normal_0_index = normal_0_index - 1;
  164. new_mesh_data_struct.faces[current_face_index].normal_1_index = normal_1_index - 1;
  165. new_mesh_data_struct.faces[current_face_index].normal_2_index = normal_2_index - 1;
  166. new_mesh_data_struct.faces[current_face_index].uv_0_index = uv_0_index - 1;
  167. new_mesh_data_struct.faces[current_face_index].uv_1_index = uv_1_index - 1;
  168. new_mesh_data_struct.faces[current_face_index].uv_2_index = uv_2_index - 1;
  169. ++current_face_index;
  170. }
  171. else if (line[0] == 'v' && line[1] == ' ') {
  172. float x, y, z;
  173. sscanf(line, "v %f %f %f", &x, &y, &z);
  174. new_mesh_data_struct.vertex_positions[current_vertex_index].x = x;
  175. new_mesh_data_struct.vertex_positions[current_vertex_index].y = y;
  176. new_mesh_data_struct.vertex_positions[current_vertex_index].z = z;
  177. ++current_vertex_index;
  178. }
  179. else if (line[0] == 'v' && line[1] == 'n') {
  180. float x, y, z;
  181. sscanf(line, "vn %f %f %f", &x, &y, &z);
  182. new_mesh_data_struct.normals[current_normal_index].x = x;
  183. new_mesh_data_struct.normals[current_normal_index].y = y;
  184. new_mesh_data_struct.normals[current_normal_index].z = z;
  185. ++current_normal_index;
  186. }
  187. else if (line[0] == 'v' && line[1] == 't') {
  188. float x, y, z;
  189. sscanf(line, "vt %f %f %f", &x, &y, &z);
  190. new_mesh_data_struct.uvs[current_uv_index].x = x;
  191. new_mesh_data_struct.uvs[current_uv_index].y = y;
  192. // UVS are almost always 2D so we don't need Z (the type itself is a vector 2f, not 3f)
  193. ++current_uv_index;
  194. }
  195. }
  196. char* loading_mesh_notice_string;
  197. if (has_normals && !has_uvs) loading_mesh_notice_string = "normals";
  198. else if (!has_normals && has_uvs) loading_mesh_notice_string = "UVs";
  199. else if (!has_normals && !has_uvs) loading_mesh_notice_string = "normals and UVs";
  200. if (!has_normals || !has_uvs) debug("Notice! Mesh \"%s\" is missing %s", filenames[i], loading_mesh_notice_string);
  201. new_mesh_data_struct.has_normals = has_normals;
  202. new_mesh_data_struct.has_uvs = has_uvs;
  203. // fclose(file_again);
  204. if (!RI_return_just_mesh) {
  205. ri.loaded_meshes[meshes_already_loaded_count + i] = new_mesh_data_struct;
  206. debug("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);
  207. }
  208. else {
  209. *mesh = new_mesh_data_struct;
  210. }
  211. }
  212. if (!RI_return_just_mesh) return ri.loaded_meshes;
  213. else return mesh;
  214. }
  215. void quaternion_rotate(RI_vector_3f *position, RI_vector_4f rotation){
  216. RI_vector_4f pos_quat = {0, position->x, position->y, position->z};
  217. RI_vector_4f rotation_conjugation = rotation;
  218. quaternion_conjugate(&rotation_conjugation);
  219. quaternion_multiply(&rotation, pos_quat);
  220. quaternion_multiply(&rotation, rotation_conjugation);
  221. *position = (RI_vector_3f){rotation.x, rotation.y, rotation.z};
  222. }
  223. void RI_euler_rotation_to_quaternion(RI_vector_4f *quaternion, RI_vector_3f euler_rotation){
  224. float cx = cosf(euler_rotation.x * 0.5f);
  225. float sx = sinf(euler_rotation.x * 0.5f);
  226. float cy = cosf(euler_rotation.y * 0.5f);
  227. float sy = sinf(euler_rotation.y * 0.5f);
  228. float cz = cosf(euler_rotation.z * 0.5f);
  229. float sz = sinf(euler_rotation.z * 0.5f);
  230. quaternion->w = cx * cy * cz + sx * sy * sz;
  231. quaternion->x = sx * cy * cz - cx * sy * sz;
  232. quaternion->y = cx * sy * cz + sx * cy * sz;
  233. quaternion->z = cx * cy * sz - sx * sy * cz;
  234. }
  235. int RI_render(RI_scene *scene, RI_texture *target_texture){
  236. // do rendering stuff
  237. if (ri.running){
  238. float horizontal_fov_factor = target_texture->resolution.x / tanf(0.5 * scene->FOV);
  239. float vertical_fov_factor = target_texture->resolution.y / tanf(0.5 * scene->FOV);
  240. scene->min_clip = scene->minimum_clip_distance;
  241. if (!scene->faces_to_render){
  242. int total_faces = 0;
  243. for (int actor_index = 0; actor_index < scene->actor_count; ++actor_index){
  244. total_faces += scene->actors[actor_index]->mesh_reference->face_count;
  245. }
  246. scene->faces_to_render = malloc(sizeof(RI_renderable_face) * total_faces * 2); // x2 because faces can be split
  247. scene->face_count = total_faces;
  248. }
  249. memset(scene->faces_to_render, 0, sizeof(RI_renderable_face) * scene->face_count * 2);
  250. int current_renderable_face_index = 0;
  251. int current_split_renderable_face_index = 0;
  252. for (int actor_index = 0; actor_index < scene->actor_count; ++actor_index){
  253. RI_actor *current_actor = scene->actors[actor_index];
  254. for (int face_index = 0; face_index < current_actor->mesh_reference->face_count; ++face_index){
  255. RI_face *cur_face = &current_actor->mesh_reference->faces[face_index];
  256. if (!cur_face->should_render){
  257. continue;
  258. }
  259. int vert_pos_0_index = cur_face->position_0_index;
  260. int vert_pos_1_index = cur_face->position_1_index;
  261. int vert_pos_2_index = cur_face->position_2_index;
  262. int normal_0_index = cur_face->normal_0_index;
  263. int normal_1_index = cur_face->normal_1_index;
  264. int normal_2_index = cur_face->normal_2_index;
  265. int uv_0_index = cur_face->uv_0_index;
  266. int uv_1_index = cur_face->uv_1_index;
  267. int uv_2_index = cur_face->uv_2_index;
  268. RI_renderable_face *cur_r_face = &scene->faces_to_render[current_renderable_face_index];
  269. cur_r_face->material_reference = current_actor->material_reference;
  270. cur_r_face->position_0 = current_actor->mesh_reference->vertex_positions[vert_pos_0_index];
  271. cur_r_face->position_1 = current_actor->mesh_reference->vertex_positions[vert_pos_1_index];
  272. cur_r_face->position_2 = current_actor->mesh_reference->vertex_positions[vert_pos_2_index];
  273. if (current_actor->mesh_reference->has_uvs){
  274. cur_r_face->uv_0 = current_actor->mesh_reference->uvs[uv_0_index];
  275. cur_r_face->uv_1 = current_actor->mesh_reference->uvs[uv_1_index];
  276. cur_r_face->uv_2 = current_actor->mesh_reference->uvs[uv_2_index];
  277. }
  278. // scale
  279. vector_3f_hadamard(&cur_r_face->position_0, current_actor->transform.scale);
  280. vector_3f_hadamard(&cur_r_face->position_1, current_actor->transform.scale);
  281. vector_3f_hadamard(&cur_r_face->position_2, current_actor->transform.scale);
  282. // combine camera and object rotation
  283. RI_vector_4f combined_rotation = current_actor->transform.rotation;
  284. RI_vector_4f camera_rotation = scene->camera_rotation;
  285. quaternion_conjugate(&camera_rotation);
  286. quaternion_multiply(&combined_rotation, camera_rotation);
  287. // rotate
  288. quaternion_rotate(&cur_r_face->position_0, combined_rotation);
  289. quaternion_rotate(&cur_r_face->position_1, combined_rotation);
  290. quaternion_rotate(&cur_r_face->position_2, combined_rotation);
  291. // object position
  292. vector_3f_element_wise_add(&cur_r_face->position_0, current_actor->transform.position);
  293. vector_3f_element_wise_add(&cur_r_face->position_1, current_actor->transform.position);
  294. vector_3f_element_wise_add(&cur_r_face->position_2, current_actor->transform.position);
  295. // camera position
  296. vector_3f_element_wise_subtract(&cur_r_face->position_0, scene->camera_position);
  297. vector_3f_element_wise_subtract(&cur_r_face->position_1, scene->camera_position);
  298. vector_3f_element_wise_subtract(&cur_r_face->position_2, scene->camera_position);
  299. RI_vector_3f *pos_0 = &cur_r_face->position_0;
  300. RI_vector_3f *pos_1 = &cur_r_face->position_1;
  301. RI_vector_3f *pos_2 = &cur_r_face->position_2;
  302. int is_0_clipped = pos_0->z < scene->min_clip;
  303. int is_1_clipped = pos_1->z < scene->min_clip;
  304. int is_2_clipped = pos_2->z < scene->min_clip;
  305. int clip_count = is_0_clipped + is_1_clipped + is_2_clipped;
  306. cur_r_face->should_render = 1;
  307. switch(clip_count){
  308. case 3: // ignore polygon, it's behind the camera
  309. continue;
  310. break;
  311. case 2:{ // shrink poylgon
  312. RI_vector_3f *unclipped_point, *point_a, *point_b;
  313. RI_vector_3f *unclipped_normal, *normal_a, *normal_b;
  314. RI_vector_2f *unclipped_uv, *uv_a, *uv_b;
  315. if (!is_0_clipped){
  316. unclipped_point = &cur_r_face->position_0;
  317. point_a = &cur_r_face->position_1;
  318. point_b = &cur_r_face->position_2;
  319. unclipped_normal = &cur_r_face->normal_0;
  320. normal_a = &cur_r_face->normal_1;
  321. normal_b = &cur_r_face->normal_2;
  322. unclipped_uv = &cur_r_face->uv_0;
  323. uv_a = &cur_r_face->uv_1;
  324. uv_b = &cur_r_face->uv_2;
  325. }
  326. else if (!is_1_clipped){
  327. unclipped_point = &cur_r_face->position_1;
  328. point_a = &cur_r_face->position_2;
  329. point_b = &cur_r_face->position_0;
  330. unclipped_normal = &cur_r_face->normal_1;
  331. normal_a = &cur_r_face->normal_2;
  332. normal_b = &cur_r_face->normal_0;
  333. unclipped_uv = &cur_r_face->uv_1;
  334. uv_a = &cur_r_face->uv_2;
  335. uv_b = &cur_r_face->uv_0;
  336. }
  337. else if (!is_2_clipped){
  338. unclipped_point = &cur_r_face->position_2;
  339. point_a = &cur_r_face->position_0;
  340. point_b = &cur_r_face->position_1;
  341. unclipped_normal = &cur_r_face->normal_2;
  342. normal_a = &cur_r_face->normal_0;
  343. normal_b = &cur_r_face->normal_1;
  344. unclipped_uv = &cur_r_face->uv_2;
  345. uv_a = &cur_r_face->uv_0;
  346. uv_b = &cur_r_face->uv_1;
  347. }
  348. float fraction_a_to_unclip = (scene->min_clip - unclipped_point->z) / (point_a->z - unclipped_point->z);
  349. float fraction_b_to_unclip = (scene->min_clip - unclipped_point->z) / (point_b->z - unclipped_point->z);
  350. vector_3f_lerp(*unclipped_point, *point_a, point_a, fraction_a_to_unclip);
  351. vector_3f_lerp(*unclipped_point, *point_b, point_b, fraction_b_to_unclip);
  352. vector_3f_lerp(*unclipped_normal, *normal_a, normal_a, fraction_a_to_unclip);
  353. vector_3f_lerp(*unclipped_normal, *normal_b, normal_b, fraction_b_to_unclip);
  354. vector_2f_lerp(*unclipped_uv, *uv_a, uv_a, fraction_a_to_unclip);
  355. vector_2f_lerp(*unclipped_uv, *uv_b, uv_b, fraction_b_to_unclip);
  356. break;}
  357. case 1: // split polygon
  358. RI_vector_3f clipped_point, point_a, point_b;
  359. RI_vector_3f clipped_normal, normal_a, normal_b;
  360. RI_vector_2f clipped_uv, uv_a, uv_b;
  361. if (is_0_clipped){
  362. clipped_point = cur_r_face->position_0;
  363. point_a = cur_r_face->position_1;
  364. point_b = cur_r_face->position_2;
  365. clipped_normal = cur_r_face->normal_0;
  366. normal_a = cur_r_face->normal_1;
  367. normal_b = cur_r_face->normal_2;
  368. clipped_uv = cur_r_face->uv_0;
  369. uv_a = cur_r_face->uv_1;
  370. uv_b = cur_r_face->uv_2;
  371. }
  372. else if (is_1_clipped){
  373. clipped_point = cur_r_face->position_1;
  374. point_a = cur_r_face->position_2;
  375. point_b = cur_r_face->position_0;
  376. clipped_normal = cur_r_face->normal_1;
  377. normal_a = cur_r_face->normal_2;
  378. normal_b = cur_r_face->normal_0;
  379. clipped_uv = cur_r_face->uv_1;
  380. uv_a = cur_r_face->uv_2;
  381. uv_b = cur_r_face->uv_0;
  382. }
  383. else if (is_2_clipped){
  384. clipped_point = cur_r_face->position_2;
  385. point_a = cur_r_face->position_0;
  386. point_b = cur_r_face->position_1;
  387. clipped_normal = cur_r_face->normal_2;
  388. normal_a = cur_r_face->normal_0;
  389. normal_b = cur_r_face->normal_1;
  390. clipped_uv = cur_r_face->uv_2;
  391. uv_a = cur_r_face->uv_0;
  392. uv_b = cur_r_face->uv_1;
  393. }
  394. float fraction_a_to_clip = (scene->min_clip - clipped_point.z) / (point_a.z - clipped_point.z);
  395. float fraction_b_to_clip = (scene->min_clip - clipped_point.z) / (point_b.z - clipped_point.z);
  396. 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.
  397. RI_vector_3f new_normal_a, new_normal_b; // they come from the clipped point which was originally only 1
  398. RI_vector_2f new_uv_a, new_uv_b;
  399. vector_3f_lerp(clipped_point, point_a, &new_point_a, fraction_a_to_clip);
  400. vector_3f_lerp(clipped_point, point_b, &new_point_b, fraction_b_to_clip);
  401. vector_3f_lerp(clipped_normal, normal_a, &new_normal_a, fraction_a_to_clip);
  402. vector_3f_lerp(clipped_normal, normal_b, &new_normal_b, fraction_b_to_clip);
  403. vector_2f_lerp(clipped_uv, uv_a, &new_uv_a, fraction_a_to_clip);
  404. vector_2f_lerp(clipped_uv, uv_b, &new_uv_b, fraction_b_to_clip);
  405. // okay, now we have a quad (in clockwise order, point a, point b, new point b, new point a)
  406. // quads are easy to turn into tris >w<
  407. RI_renderable_face *cur_r_split_face = &scene->faces_to_render[scene->face_count + current_split_renderable_face_index];
  408. cur_r_split_face->should_render = 1;
  409. cur_r_split_face->material_reference = cur_r_face->material_reference;
  410. cur_r_face->position_0 = point_a;
  411. cur_r_face->position_1 = point_b;
  412. cur_r_face->position_2 = new_point_a;
  413. cur_r_face->normal_0 = normal_a;
  414. cur_r_face->normal_1 = normal_b;
  415. cur_r_face->normal_2 = new_normal_a;
  416. cur_r_face->uv_0 = uv_a;
  417. cur_r_face->uv_1 = uv_b;
  418. cur_r_face->uv_2 = new_uv_a;
  419. cur_r_split_face->position_0 = point_b;
  420. cur_r_split_face->position_1 = new_point_b;
  421. cur_r_split_face->position_2 = new_point_a;
  422. cur_r_split_face->normal_0 = normal_b;
  423. cur_r_split_face->normal_1 = new_normal_b;
  424. cur_r_split_face->normal_2 = new_normal_a;
  425. cur_r_split_face->uv_0 = uv_b;
  426. cur_r_split_face->uv_1 = new_uv_b;
  427. cur_r_split_face->uv_2 = new_uv_a;
  428. cur_r_split_face->position_0.x = cur_r_split_face->position_0.x / cur_r_split_face->position_0.z * horizontal_fov_factor;
  429. cur_r_split_face->position_0.y = cur_r_split_face->position_0.y / cur_r_split_face->position_0.z * vertical_fov_factor;
  430. cur_r_split_face->position_1.x = cur_r_split_face->position_1.x / cur_r_split_face->position_1.z * horizontal_fov_factor;
  431. cur_r_split_face->position_1.y = cur_r_split_face->position_1.y / cur_r_split_face->position_1.z * vertical_fov_factor;
  432. cur_r_split_face->position_2.x = cur_r_split_face->position_2.x / cur_r_split_face->position_2.z * horizontal_fov_factor;
  433. cur_r_split_face->position_2.y = cur_r_split_face->position_2.y / cur_r_split_face->position_2.z * vertical_fov_factor;
  434. cur_r_split_face->min_screen_x = cur_r_split_face->position_0.x;
  435. 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;
  436. 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;
  437. cur_r_split_face->min_screen_x = fmax(cur_r_split_face->min_screen_x, -target_texture->resolution.x / 2);
  438. cur_r_split_face->max_screen_x = cur_r_split_face->position_0.x;
  439. 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;
  440. 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;
  441. cur_r_split_face->max_screen_x = fmin(cur_r_split_face->max_screen_x, target_texture->resolution.x / 2);
  442. cur_r_split_face->min_screen_y = cur_r_split_face->position_0.y;
  443. 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;
  444. 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;
  445. cur_r_split_face->min_screen_y = fmax(cur_r_split_face->min_screen_y, -target_texture->resolution.y / 2);
  446. cur_r_split_face->max_screen_y = cur_r_split_face->position_0.y;
  447. 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;
  448. 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;
  449. cur_r_split_face->max_screen_y = fmin(cur_r_split_face->max_screen_y, target_texture->resolution.y / 2);
  450. ++current_split_renderable_face_index;
  451. break;
  452. case 0: // no issues, ignore
  453. break;
  454. }
  455. cur_r_face->position_0.x = cur_r_face->position_0.x / cur_r_face->position_0.z * horizontal_fov_factor;
  456. cur_r_face->position_0.y = cur_r_face->position_0.y / cur_r_face->position_0.z * vertical_fov_factor;
  457. cur_r_face->position_1.x = cur_r_face->position_1.x / cur_r_face->position_1.z * horizontal_fov_factor;
  458. cur_r_face->position_1.y = cur_r_face->position_1.y / cur_r_face->position_1.z * vertical_fov_factor;
  459. cur_r_face->position_2.x = cur_r_face->position_2.x / cur_r_face->position_2.z * horizontal_fov_factor;
  460. cur_r_face->position_2.y = cur_r_face->position_2.y / cur_r_face->position_2.z * vertical_fov_factor;
  461. cur_r_face->min_screen_x = pos_0->x;
  462. if (pos_1->x < cur_r_face->min_screen_x) cur_r_face->min_screen_x = pos_1->x;
  463. if (pos_2->x < cur_r_face->min_screen_x) cur_r_face->min_screen_x = pos_2->x;
  464. cur_r_face->min_screen_x = fmax(cur_r_face->min_screen_x, -target_texture->resolution.x / 2);
  465. cur_r_face->max_screen_x = pos_0->x;
  466. if (pos_1->x > cur_r_face->max_screen_x) cur_r_face->max_screen_x = pos_1->x;
  467. if (pos_2->x > cur_r_face->max_screen_x) cur_r_face->max_screen_x = pos_2->x;
  468. cur_r_face->max_screen_x = fmin(cur_r_face->max_screen_x, target_texture->resolution.x / 2);
  469. cur_r_face->min_screen_y = pos_0->y;
  470. if (pos_1->y < cur_r_face->min_screen_y) cur_r_face->min_screen_y = pos_1->y;
  471. if (pos_2->y < cur_r_face->min_screen_y) cur_r_face->min_screen_y = pos_2->y;
  472. cur_r_face->min_screen_y = fmax(cur_r_face->min_screen_y, -target_texture->resolution.y / 2);
  473. cur_r_face->max_screen_y = pos_0->y;
  474. if (pos_1->y > cur_r_face->max_screen_y) cur_r_face->max_screen_y = pos_1->y;
  475. if (pos_2->y > cur_r_face->max_screen_y) cur_r_face->max_screen_y = pos_2->y;
  476. cur_r_face->max_screen_y = fmin(cur_r_face->max_screen_y, target_texture->resolution.y / 2);
  477. ++current_renderable_face_index;
  478. }
  479. }
  480. scene->face_count = current_renderable_face_index;
  481. for (int pixel_index = 0; pixel_index < target_texture->resolution.x * target_texture->resolution.y; ++pixel_index){
  482. target_texture->image_buffer[pixel_index] = 0xFF333333;
  483. ri.z_buffer[pixel_index] = 999999999;
  484. }
  485. for (int face_index = 0; face_index < scene->face_count * 2; ++face_index){
  486. RI_renderable_face *current_face = &scene->faces_to_render[face_index];
  487. if (!current_face->should_render) continue;
  488. RI_material *mat = current_face->material_reference;
  489. RI_vector_2f *uv_0 = &current_face->uv_0;;
  490. RI_vector_2f *uv_1 = &current_face->uv_1;;
  491. RI_vector_2f *uv_2 = &current_face->uv_2;;
  492. if (mat == NULL){
  493. mat = &ri.error_material;
  494. }
  495. if(mat->flags & RI_MATERIAL_HAS_TEXTURE && mat->texture_reference == NULL){
  496. mat->texture_reference = &ri.error_texture;
  497. }
  498. if(mat->flags & RI_MATERIAL_HAS_BUMP_MAP && mat->bump_map_reference == NULL){
  499. mat->bump_map_reference = &ri.error_bump_map;
  500. }
  501. if(mat->flags & RI_MATERIAL_HAS_NORMAL_MAP && mat->normal_map_reference == NULL){
  502. mat->normal_map_reference = &ri.error_normal_map;
  503. }
  504. RI_vector_3f *pos_0 = &current_face->position_0;
  505. RI_vector_3f *pos_1 = &current_face->position_1;
  506. RI_vector_3f *pos_2 = &current_face->position_2;
  507. for (int pixel_y_index = current_face->min_screen_y; pixel_y_index < current_face->max_screen_y; ++pixel_y_index){
  508. for (int pixel_x_index = current_face->min_screen_x; pixel_x_index < current_face->max_screen_x; ++pixel_x_index){
  509. int x = pixel_x_index + target_texture->resolution.x / 2;
  510. int y = pixel_y_index + target_texture->resolution.y / 2;
  511. if (x < 0 || x >= target_texture->resolution.x || y < 0 || y >= target_texture->resolution.y) continue;
  512. float denominator, w0, w1, w2;
  513. 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);
  514. 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;
  515. 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;
  516. w2 = 1.0 - w0 - w1;
  517. if (!(mat->flags & RI_MATERIAL_DOUBLE_SIDED) && denominator > 0){
  518. continue;
  519. }
  520. float w_over_z = (w0 / pos_0->z + w1 / pos_1->z + w2 / pos_2->z);
  521. float interpolated_z = 1.0 / w_over_z;
  522. if (!(w0 >= 0 && w1 >= 0 && w2 >= 0) || (mat->flags & RI_MATERIAL_WIREFRAME && (w0 >= mat->wireframe_width && w1 >= mat->wireframe_width && w2 >= mat->wireframe_width))){
  523. continue;
  524. }
  525. if (!(mat->flags & RI_MATERIAL_DONT_DEPTH_TEST) && interpolated_z >= ri.z_buffer[y * target_texture->resolution.x + x]){
  526. continue;
  527. }
  528. if (!(mat->flags & RI_MATERIAL_DONT_DEPTH_WRITE)){
  529. ri.z_buffer[y * target_texture->resolution.x + x] = interpolated_z;
  530. }
  531. uint32_t pixel_color = 0xFF000000;
  532. if (mat->flags & RI_MATERIAL_HAS_TEXTURE && uv_0 && uv_1 && uv_2){
  533. 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;
  534. 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;
  535. RI_vector_2 texel_position = {mat->texture_reference->resolution.x * ux, mat->texture_reference->resolution.y * uy};
  536. 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)
  537. pixel_color = 0xFFFF00FF;
  538. else
  539. pixel_color = mat->texture_reference->image_buffer[texel_position.y * mat->texture_reference->resolution.x + texel_position.x];
  540. }
  541. else { // must be only an albedo
  542. if (mat->albedo) pixel_color = mat->albedo;
  543. else pixel_color = 0xFFFF77FF;
  544. }
  545. // tri culling debug
  546. if (face_index >= scene->face_count) pixel_color = 0xFF7777FF;
  547. // if (face_index < scene->face_count) pixel_color = 0xFF77FF77;
  548. // flip the texture
  549. // x = target_texture->resolution.x - 1 - x;
  550. // y = target_texture->resolution.y - 1 - y;
  551. if (x >= 0 && y >= 0 && x < target_texture->resolution.x && y < target_texture->resolution.y){
  552. target_texture->image_buffer[y * target_texture->resolution.x + x] = pixel_color;
  553. }
  554. }
  555. }
  556. }
  557. SDL_UpdateTexture(ri.texture, NULL, ri.frame_buffer->image_buffer, ri.window_width * sizeof(uint32_t));
  558. SDL_RenderClear(ri.renderer);
  559. SDL_RenderCopyEx(ri.renderer, ri.texture, NULL, NULL, 0, NULL, SDL_FLIP_VERTICAL);
  560. SDL_RenderPresent(ri.renderer);
  561. }
  562. else{
  563. RI_stop(0);
  564. }
  565. // handle SDL events
  566. while (SDL_PollEvent(&ri.event)){
  567. switch (ri.event.type){
  568. case SDL_QUIT:
  569. ri.running = 0;
  570. }
  571. }
  572. ++ri.frame;
  573. return 0;
  574. }
  575. int opencl_init(){
  576. return 0;
  577. }
  578. int sdl_init(int RI_window_width, int RI_window_height, char *RI_window_title){
  579. ri.window_width = RI_window_width;
  580. ri.window_height = RI_window_height;
  581. ri.window_title = RI_window_title;
  582. ri.frame_buffer = malloc(sizeof(RI_texture));
  583. ri.frame_buffer->image_buffer = malloc(sizeof(uint32_t) * ri.window_width * ri.window_height);
  584. ri.frame_buffer->resolution = (RI_vector_2){ri.window_width, ri.window_height};
  585. ri.z_buffer = malloc(sizeof(float) * ri.window_width * ri.window_height);
  586. SDL_Init(SDL_INIT_VIDEO);
  587. ri.window = SDL_CreateWindow(RI_window_title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, ri.window_width, ri.window_height, SDL_WINDOW_OPENGL);
  588. ri.renderer = SDL_CreateRenderer(ri.window, -1, SDL_RENDERER_ACCELERATED);
  589. ri.texture = SDL_CreateTexture(ri.renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, ri.window_width, ri.window_height);
  590. return 0;
  591. }
  592. int RI_init(int RI_window_width, int RI_window_height, char *RI_window_title){
  593. ri.running = 1;
  594. opencl_init();
  595. sdl_init(RI_window_width, RI_window_height, RI_window_title);
  596. ri.loaded_mesh_count = 0;
  597. ri.loaded_texture_count = 0;
  598. ri.actor_count = 0;
  599. ri.prefix = "[RasterIver] ";
  600. char **error_cube_file = malloc(sizeof(char *));
  601. error_cube_file[0] = "objects/unit_cube.obj";
  602. RI_mesh* error_mesh = RI_request_meshes(1, error_cube_file, 1);
  603. ri.error_mesh = *error_mesh;
  604. free(error_mesh);
  605. free(error_cube_file);
  606. ri.error_texture.image_buffer = malloc(sizeof(uint32_t));
  607. ri.error_texture.image_buffer[0] = 0xFFFF00FF;
  608. ri.error_texture.resolution = (RI_vector_2){1, 1};
  609. ri.error_material.texture_reference = &ri.error_texture;
  610. ri.error_material.albedo = 0xFF5522CC;
  611. 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;
  612. return 0;
  613. }
  614. int RI_stop(int result){
  615. debug("Stopping...");
  616. for (int mesh_index = 0; mesh_index < ri.loaded_mesh_count; ++mesh_index){
  617. free(ri.loaded_meshes[mesh_index].faces); // free face array
  618. }
  619. for (int texture_index = 0; texture_index < ri.loaded_texture_count; ++texture_index){
  620. free(ri.loaded_textures[texture_index].image_buffer);
  621. }
  622. exit(result);
  623. return 0;
  624. }