Browse Source

added shaders and updated example scene

Iver 4 months ago
parent
commit
23c7159542

BIN
build/librasteriver.so


BIN
build/main.bin


BIN
compiled_libs/librasteriver.so


+ 1 - 1
src/headers/custom_types.h

@@ -74,7 +74,7 @@ typedef struct {
     uint64_t flags;
     RI_vector_2f uv_loop_multiplier;
     RI_vector_2f texture_render_size;
-    int (*shader_function_pointer) (int pixel_x, int pixel_y, RI_vector_3f position, RI_vector_3f normal, RI_vector_2f uv, uint32_t color);
+    double (*shader_function_pointer) (int pixel_x, int pixel_y, RI_vector_3f position, RI_vector_3f normal, RI_vector_2f uv, uint32_t color);
 } RI_material;
 
 typedef struct { // An entity that has an mesh, transform, materials, etc

+ 23 - 8
src/launch_program/main.c

@@ -1,9 +1,9 @@
 #include "../headers/rasteriver.h"
 #include <time.h>
 
-int shader_function(int pixel_x, int pixel_y, RI_vector_3f position, RI_vector_3f normal, RI_vector_2f uv, uint32_t color){
-    if (uv.x > 0.5) return 1;
-    else return 0;
+double shader_function(int pixel_x, int pixel_y, RI_vector_3f position, RI_vector_3f normal, RI_vector_2f uv, uint32_t color){
+    if (color == 0xFFFFFFFF) return 0;
+    else return 1;
 }
 
 int main(){
@@ -24,14 +24,14 @@ int main(){
 
     // requesting assets
     RI_mesh* meshes = RI_request_meshes(1, filenames, 0);
-    RI_material* materials = RI_request_materials(1);
-    RI_actor* actors = RI_request_actors(1);
+    RI_material* materials = RI_request_materials(2);
+    RI_actor* actors = RI_request_actors(2);
     RI_scene* scenes = RI_request_scenes(1);
 
     RI_scene* scene = &scenes[0];
 
     // meshes
-    RI_mesh* text_plane_mesh = &meshes[0];
+    RI_mesh* plane_mesh = &meshes[0];
 
     // materials
     RI_material* text_plane_material = &materials[0];
@@ -40,18 +40,33 @@ int main(){
     text_plane_material->albedo = 0xFFFFFFFF;
     text_plane_material->shader_function_pointer = shader_function;
 
+    RI_material* bill_material = &materials[1];
+    bill_material->flags = RI_MATERIAL_HAS_TEXTURE | RI_MATERIAL_DOUBLE_SIDED;
+    RI_texture_creation_data tex_data[1] = {(RI_texture_creation_data){"textures/THIS IS THE WALL.png", {0, 0}}};
+    bill_material->texture_reference = RI_request_textures(1, tex_data);
+    bill_material->albedo = 0xFFFFFFFF;
+    bill_material->shader_function_pointer = NULL;
+
     // actors
     RI_actor* text_plane = &actors[0];
     text_plane->material_reference = text_plane_material;
-    text_plane->mesh_reference = text_plane_mesh;
+    text_plane->mesh_reference = plane_mesh;
     text_plane->transform.scale = (RI_vector_3f){100, 100, 100};
     text_plane->transform.position = (RI_vector_3f){0, 0, 400};
     text_plane->transform.rotation = (RI_vector_4f){0, 1, 0, 0};
     RI_euler_rotation_to_quaternion(&text_plane->transform.rotation, (RI_vector_3f){-3.1415926 / 2, 0, 0});
+    
+    RI_actor* bill_plane = &actors[1];
+    bill_plane->material_reference = bill_material;
+    bill_plane->mesh_reference = plane_mesh;
+    bill_plane->transform.scale = (RI_vector_3f){300, 300, 300};
+    bill_plane->transform.position = (RI_vector_3f){0, 0, 600};
+    bill_plane->transform.rotation = (RI_vector_4f){0, 1, 0, 0};
+    RI_euler_rotation_to_quaternion(&bill_plane->transform.rotation, (RI_vector_3f){3.1415926 / 2, 0, 0});
 
     RI_vector_4f rotation_delta;
 
-    RI_add_actors_to_scene(1, actors, scene);
+    RI_add_actors_to_scene(2, actors, scene);
 
     scene->FOV = 1.5; // 90 degrees in radians
     scene->minimum_clip_distance = 0.1;

+ 31 - 7
src/library/rasteriver.c

@@ -1027,10 +1027,14 @@ int RI_render(RI_scene *scene, RI_texture *target_texture, int clear_texture){
 
             RI_material *mat = current_face->material_reference;
         
-            RI_vector_2f *uv_0 = &current_face->uv_0;;
-            RI_vector_2f *uv_1 = &current_face->uv_1;;
-            RI_vector_2f *uv_2 = &current_face->uv_2;;
+            RI_vector_2f *uv_0 = &current_face->uv_0;
+            RI_vector_2f *uv_1 = &current_face->uv_1;
+            RI_vector_2f *uv_2 = &current_face->uv_2;
         
+            RI_vector_3f *normal_0 = &current_face->normal_0;
+            RI_vector_3f *normal_1 = &current_face->normal_1;
+            RI_vector_3f *normal_2 = &current_face->normal_2;
+
             if (mat == NULL){
                 mat = &ri.error_material;
             }
@@ -1110,10 +1114,21 @@ int RI_render(RI_scene *scene, RI_texture *target_texture, int clear_texture){
                     }
                     
                     uint32_t pixel_color = 0x00000000;
-                    
+         
+                    double ux, uy;
+                    ux = uy = -1;
+
+                    RI_vector_3f normal = {0};
+
+                    if (normal_0){
+                        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;
+                        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;      
+                        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;      
+                    }
+
                     if (mat->flags & RI_MATERIAL_HAS_TEXTURE && uv_0 && uv_1 && uv_2){                
-                        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;
-                        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;                
+                        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;
+                        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;                
                     
                         if (mat->flags & RI_MATERIAL_USE_UV_LOOP_MULTIPLIER){
                             ux *= mat->uv_loop_multiplier.x;
@@ -1147,7 +1162,6 @@ int RI_render(RI_scene *scene, RI_texture *target_texture, int clear_texture){
                     // flip the texture
                     // x = target_texture->resolution.x - 1 - x;
                     // y = target_texture->resolution.y - 1 - y;
-                
 
                     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)
                         if (current_face->shrunk) pixel_color = 0xFF7777FF;
@@ -1155,6 +1169,16 @@ int RI_render(RI_scene *scene, RI_texture *target_texture, int clear_texture){
                         else if (face_index >= current_renderable_face_index) pixel_color = 0xFFFF7777;
                         else pixel_color = 0xFF777777;
                     }
+                    
+                    double shader_result = 1;
+                    
+                    if (current_face->material_reference->shader_function_pointer != NULL) shader_result = current_face->material_reference->shader_function_pointer(x, y, (RI_vector_3f){0, 0, 0}, normal, (RI_vector_2f){ux, uy}, pixel_color);
+
+                    // set alpha after checking shader result becuase things with alpha 0 should still depth write
+
+                    if (shader_result <= 0) continue;
+                    
+                    alpha = shader_result;
 
                     if (!(mat->flags & RI_MATERIAL_DONT_DEPTH_WRITE)){
                         ri.z_buffer[y * target_texture->resolution.x + x] = interpolated_z;