Browse Source

added animated textures

Iver 1 month ago
parent
commit
bb590addd0

BIN
builds/libpitmap.so


BIN
builds/librasteriver.so


BIN
builds/main.bin


+ 2 - 3
changelog.txt

@@ -1,3 +1,2 @@
--added split faces (cur_r_split_face) to the triangle binning (or tiling? i havent picked what i want to call it yet) implementation
--fixed (?) issue where shrunk faces would get glitched positions and flash on screen
--removed font stuff from roadmap
+-added animated textures
+-latest version of PitMap (8bpp images that have no compression, debug mode)

+ 0 - 2
objects/plane_subdivided.mtl

@@ -1,2 +0,0 @@
-# Blender 4.3.2 MTL File: 'None'
-# www.blender.org

+ 0 - 12
objects/terrain.mtl

@@ -1,12 +0,0 @@
-# Blender 4.3.2 MTL File: 'terrain.blend'
-# www.blender.org
-
-newmtl Material.001
-Ns 250.000000
-Ka 0.372727 0.372727 0.372727
-Kd 0.800000 0.800000 0.800000
-Ks 0.500000 0.500000 0.500000
-Ke 0.000000 0.000000 0.000000
-Ni 1.500000
-d 1.000000
-illum 3

+ 3 - 0
src/headers/functions.h

@@ -34,4 +34,7 @@ RI_texture *RI_new_texture(int width, int height);
 // loads an image file as a texture
 // loads an image file as a texture
 RI_texture* RI_load_image(char* filename);
 RI_texture* RI_load_image(char* filename);
 
 
+// loads an image file as an animated texture
+RI_texture* RI_load_animation(char* filename, uint16_t frame_count);
+
 #endif
 #endif

+ 1 - 1
src/headers/pitmap.h

@@ -8,6 +8,6 @@ typedef struct {
     int width, height;
     int width, height;
 } PM_image;
 } PM_image;
 
 
-PM_image* PM_load_image(char* filename);
+PM_image* PM_load_image(char* filename, uint16_t debug_mode);
 
 
 #endif
 #endif

+ 9 - 3
src/headers/types.h

@@ -13,6 +13,9 @@ enum {
 enum {
 enum {
     RI_DEBUG_NONE = 0,
     RI_DEBUG_NONE = 0,
 
 
+    // PitMap
+    RI_DEBUG_PITMAP = 1 << 29,
+
     // frame-level
     // frame-level
     RI_DEBUG_FRAME_START_END_MARKERS     = 1 << 0,
     RI_DEBUG_FRAME_START_END_MARKERS     = 1 << 0,
     RI_DEBUG_TICK_TIME                   = 1 << 1,
     RI_DEBUG_TICK_TIME                   = 1 << 1,
@@ -112,8 +115,11 @@ typedef enum {
 
 
 typedef struct {
 typedef struct {
     uint16_t width;
     uint16_t width;
-    uint16_t height;
+    uint16_t height; // actual height of the image INCLUDING all frames
     uint32_t index;
     uint32_t index;
+    uint32_t frame_count;
+    uint16_t current_frame;
+    uint16_t frame_height; // height of each frame
 } RI_texture;
 } RI_texture;
 
 
 typedef struct {
 typedef struct {
@@ -169,6 +175,7 @@ typedef struct {
     RI_texture* texture;
     RI_texture* texture;
     int active;
     int active;
     int material_index;
     int material_index;
+    uint16_t texture_frame;
 } RI_actor;
 } RI_actor;
 
 
 typedef struct {
 typedef struct {
@@ -180,8 +187,7 @@ typedef struct {
     unsigned char is_split;
     unsigned char is_split;
     unsigned char is_transformed;
     unsigned char is_transformed;
     unsigned char is_shrunk;
     unsigned char is_shrunk;
-    uint16_t texture_width, height;
-    uint32_t texture_index;
+    RI_texture texture;
 } RI_renderable_face;
 } RI_renderable_face;
 
 
 typedef struct {
 typedef struct {

+ 33 - 15
src/kernels/kernels.cl

@@ -28,6 +28,15 @@ typedef struct {
     double y;
     double y;
 } RI_vector_2;
 } RI_vector_2;
 
 
+typedef struct {
+    ushort width;
+    ushort height; // actual height of the image INCLUDING all frames
+    uint index;
+    ushort frame_count;
+    ushort current_frame;
+    ushort frame_height; // height of each frame
+} RI_texture;
+
 typedef struct {
 typedef struct {
     RI_vector_3 position_0, position_1, position_2;
     RI_vector_3 position_0, position_1, position_2;
     RI_vector_3 normal_0, normal_1, normal_2;
     RI_vector_3 normal_0, normal_1, normal_2;
@@ -37,8 +46,7 @@ typedef struct {
     uchar is_split;
     uchar is_split;
     uchar is_transformed;
     uchar is_transformed;
     uchar is_shrunk;
     uchar is_shrunk;
-    ushort texture_width, texture_height;
-    uint texture_index;
+    RI_texture texture;
 } RI_renderable_face;
 } RI_renderable_face;
 
 
 typedef struct {
 typedef struct {
@@ -312,7 +320,7 @@ __kernel void clear_tile_array(__global uint* tiles){
     return;
     return;
 }
 }
 
 
-__kernel void transformer(__global RI_face *faces, __global RI_renderable_face *renderable_faces, double actor_x, double actor_y, double actor_z, double actor_r_w, double actor_r_x, double actor_r_y, double actor_r_z, double actor_s_x, double actor_s_y, double actor_s_z, int has_normals, int has_uvs, int face_array_offset_index, int face_count, int width, int height, double horizontal_fov_factor, double vertical_fov_factor, float min_clip, float max_clip, double camera_x, double camera_y, double camera_z, double camera_r_w, double camera_r_x, double camera_r_y, double camera_r_z, int renderable_face_offset, int face_sqrt, ushort texture_width, ushort texture_height, uint texture_index, __global uint* tiles){
+__kernel void transformer(__global RI_face *faces, __global RI_renderable_face *renderable_faces, double actor_x, double actor_y, double actor_z, double actor_r_w, double actor_r_x, double actor_r_y, double actor_r_z, double actor_s_x, double actor_s_y, double actor_s_z, int has_normals, int has_uvs, int face_array_offset_index, int face_count, int width, int height, double horizontal_fov_factor, double vertical_fov_factor, float min_clip, float max_clip, double camera_x, double camera_y, double camera_z, double camera_r_w, double camera_r_x, double camera_r_y, double camera_r_z, int renderable_face_offset, int face_sqrt, ushort texture_width, ushort texture_height, uint texture_index, __global uint* tiles, ushort frame_count, ushort frame_height, ushort current_frame){
     int face_index = get_global_id(1) * face_sqrt + get_global_id(0); if (face_index >= face_count) return;
     int face_index = get_global_id(1) * face_sqrt + get_global_id(0); if (face_index >= face_count) return;
 
 
     RI_vector_3 current_actor_position = (RI_vector_3){actor_x, actor_y, actor_z};
     RI_vector_3 current_actor_position = (RI_vector_3){actor_x, actor_y, actor_z};
@@ -356,9 +364,19 @@ __kernel void transformer(__global RI_face *faces, __global RI_renderable_face *
         cur_r_face->uv_2 = cur_face->uv_2;
         cur_r_face->uv_2 = cur_face->uv_2;
     }
     }
 
 
-    cur_r_face->texture_width = texture_width;
-    cur_r_face->texture_height = texture_height;
-    cur_r_face->texture_index = texture_index;
+    cur_r_face->texture.width = texture_width;
+    cur_r_face->texture.height = texture_height;
+    cur_r_face->texture.index = texture_index;
+    cur_r_face->texture.frame_count = frame_count;
+    cur_r_face->texture.current_frame = current_frame;
+    cur_r_face->texture.frame_height = frame_height;
+
+    cur_r_split_face->texture.width = texture_width;
+    cur_r_split_face->texture.height = texture_height;
+    cur_r_split_face->texture.index = texture_index;
+    cur_r_split_face->texture.frame_count = frame_count;
+    cur_r_split_face->texture.current_frame = current_frame;
+    cur_r_split_face->texture.frame_height = frame_height;
 
 
     // scale
     // scale
     global_vector_3_hadamard(&cur_r_face->position_0, current_actor_scale);
     global_vector_3_hadamard(&cur_r_face->position_0, current_actor_scale);
@@ -463,7 +481,7 @@ __kernel void transformer(__global RI_face *faces, __global RI_renderable_face *
                 n_result_a = &cur_r_face->normal_0;
                 n_result_a = &cur_r_face->normal_0;
                 normal_b = cur_r_face->normal_1;
                 normal_b = cur_r_face->normal_1;
                 n_result_b = &cur_r_face->normal_1;
                 n_result_b = &cur_r_face->normal_1;
-                
+
                 unclipped_uv = cur_r_face->uv_2;                
                 unclipped_uv = cur_r_face->uv_2;                
                 u_result_a = &cur_r_face->uv_0;
                 u_result_a = &cur_r_face->uv_0;
                 uv_a = cur_r_face->uv_0;
                 uv_a = cur_r_face->uv_0;
@@ -615,9 +633,9 @@ __kernel void transformer(__global RI_face *faces, __global RI_renderable_face *
             cur_r_split_face->should_render = 1;
             cur_r_split_face->should_render = 1;
             cur_r_face->should_render = 1;
             cur_r_face->should_render = 1;
 
 
-            cur_r_split_face->texture_width = texture_width;
-            cur_r_split_face->texture_height = texture_height;
-            cur_r_split_face->texture_index = texture_index;
+            cur_r_split_face->texture.width = texture_width;
+            cur_r_split_face->texture.height = texture_height;
+            cur_r_split_face->texture.index = texture_index;
 
 
             cur_r_split_face->is_split = 1;
             cur_r_split_face->is_split = 1;
             cur_r_face->is_transformed = 1;
             cur_r_face->is_transformed = 1;
@@ -773,7 +791,7 @@ __kernel void rasterizer(__global RI_renderable_face *renderable_faces, __global
     uint num_faces_in_cur_tile = tiles[tile_array_index];
     uint num_faces_in_cur_tile = tiles[tile_array_index];
 
 
     // debug tiles
     // debug tiles
-    // if (num_faces_in_cur_tile > 0) pixel_color = 0x00AA00FF;
+    if (num_faces_in_cur_tile > 0) pixel_color = 0x00AA00FF;
 
 
     for (int face_i = 0; face_i < num_faces_in_cur_tile; ++face_i){
     for (int face_i = 0; face_i < num_faces_in_cur_tile; ++face_i){
         __global RI_renderable_face *current_face = &renderable_faces[tiles[tile_array_index + face_i + 1]];
         __global RI_renderable_face *current_face = &renderable_faces[tiles[tile_array_index + face_i + 1]];
@@ -823,11 +841,11 @@ __kernel void rasterizer(__global RI_renderable_face *renderable_faces, __global
 
 
         RI_vector_3 interpolated_normal = {0};
         RI_vector_3 interpolated_normal = {0};
         
         
-        uint texel_x = current_face->texture_width * ux;
-        uint texel_y = current_face->texture_height * uy;
+        uint texel_x = current_face->texture.width * ux;
+        uint texel_y = current_face->texture.frame_height * uy + current_face->texture.frame_height * (current_face->texture.current_frame % current_face->texture.frame_count);
 
 
-        uint texel_index = current_face->texture_index + 
-            texel_y * current_face->texture_width + texel_x;
+        uint texel_index = current_face->texture.index + 
+            texel_y * current_face->texture.width + texel_x;
 
 
         pixel_color = textures[texel_index];
         pixel_color = textures[texel_index];
 
 

+ 13 - 10
src/launch program/main.c

@@ -15,7 +15,7 @@ int main(){
     }
     }
     
     
     RI_scene *scene = RI_new_scene();
     RI_scene *scene = RI_new_scene();
-    context->debug_flags |= RI_DEBUG_TRANSFORMER_TIME | RI_DEBUG_RASTERIZER_TIME;
+    context->debug_flags |= RI_DEBUG_TRANSFORMER_TIME | RI_DEBUG_RASTERIZER_TIME | RI_DEBUG_PITMAP | RI_DEBUG_FRAME_START_END_MARKERS;
 
 
     scene->camera.FOV = 1.5;
     scene->camera.FOV = 1.5;
     scene->camera.min_clip = 0.1;
     scene->camera.min_clip = 0.1;
@@ -33,7 +33,7 @@ int main(){
 
 
     RI_texture* skybox_texture = RI_load_image("textures/alley_skybox_3072x3072.bmp");
     RI_texture* skybox_texture = RI_load_image("textures/alley_skybox_3072x3072.bmp");
     RI_texture* gordon_texture = RI_load_image("textures/terrain_texture.bmp");
     RI_texture* gordon_texture = RI_load_image("textures/terrain_texture.bmp");
-    RI_texture* gordon_face_texture = RI_load_image("textures/gordon_face.bmp");
+    RI_texture* gordon_face_texture = RI_load_animation("textures/gordon_face_animated.bmp", 3);
     RI_texture* emoji_texture = RI_load_image("textures/gordon_body.bmp");
     RI_texture* emoji_texture = RI_load_image("textures/gordon_body.bmp");
 
 
     scene->actors[0] = RI_new_actor();
     scene->actors[0] = RI_new_actor();
@@ -52,19 +52,19 @@ scene->actors[0]->active = 0;
 
 
     scene->actors[1]->mesh = gordon_head_mesh;
     scene->actors[1]->mesh = gordon_head_mesh;
     scene->actors[1]->texture = gordon_face_texture;
     scene->actors[1]->texture = gordon_face_texture;
-    scene->actors[1]->scale = (RI_vector_3){1, 1, 1};
+    scene->actors[1]->scale = (RI_vector_3){3, 1, 1};
     scene->actors[1]->position = (RI_vector_3){-100, -40, 50};
     scene->actors[1]->position = (RI_vector_3){-100, -40, 50};
 // scene->actors[1]->active = 0;
 // scene->actors[1]->active = 0;
 
 
-    scene->actors[2]->mesh = plane_mesh;
+    scene->actors[2]->mesh = gordon_mesh;
     scene->actors[2]->texture = gordon_texture;
     scene->actors[2]->texture = gordon_texture;
-    scene->actors[2]->scale = (RI_vector_3){2000, 5000, 2000};
-    scene->actors[2]->position = (RI_vector_3){0, -50, 2000};
+    scene->actors[2]->scale = (RI_vector_3){2000, 1000, 2000};
+    scene->actors[2]->position = (RI_vector_3){0, -150, 0};
     // scene->actors[2]->active = 0;
     // scene->actors[2]->active = 0;
 
 
     scene->actors[3]->mesh = text_mesh;
     scene->actors[3]->mesh = text_mesh;
     scene->actors[3]->texture = emoji_texture;
     scene->actors[3]->texture = emoji_texture;
-    scene->actors[3]->scale = (RI_vector_3){1, 1, 1};
+    scene->actors[3]->scale = (RI_vector_3){3, 1, 1};
     scene->actors[3]->position = (RI_vector_3){-100, -40, 50};
     scene->actors[3]->position = (RI_vector_3){-100, -40, 50};
 // scene->actors[3]->active = 0;
 // scene->actors[3]->active = 0;
 
 
@@ -75,7 +75,7 @@ scene->actors[0]->active = 0;
 scene->actors[4]->active = 0;
 scene->actors[4]->active = 0;
 
 
     RI_euler_rotation_to_quaternion(&scene->actors[0]->rotation, (RI_vector_3){0, 0, 0});
     RI_euler_rotation_to_quaternion(&scene->actors[0]->rotation, (RI_vector_3){0, 0, 0});
-    RI_euler_rotation_to_quaternion(&scene->actors[1]->rotation, (RI_vector_3){0, 3.14159, 0});
+    // RI_euler_rotation_to_quaternion(&scene->actors[1]->rotation, (RI_vector_3){0, 3.14159, 0});
     RI_euler_rotation_to_quaternion(&scene->actors[2]->rotation, (RI_vector_3){0, 0.78539816339, 0});
     RI_euler_rotation_to_quaternion(&scene->actors[2]->rotation, (RI_vector_3){0, 0.78539816339, 0});
 
 
     scene->length_of_actors_array = 5;
     scene->length_of_actors_array = 5;
@@ -96,8 +96,11 @@ scene->actors[4]->active = 0;
         
         
         // scene->camera.FOV = context->current_frame;
         // scene->camera.FOV = context->current_frame;
         
         
-        RI_euler_rotation_to_quaternion(&scene->camera.rotation, (RI_vector_3){0, rotation / 4, 0});
-        // RI_euler_rotation_to_quaternion(&scene->actors[2]->rotation, (RI_vector_3){0, rotation / 4, 0});
+        scene->actors[1]->texture_frame++;
+
+        RI_euler_rotation_to_quaternion(&scene->camera.rotation, (RI_vector_3){0, .7 + rotation / 10, 0});
+        RI_euler_rotation_to_quaternion(&scene->actors[1]->rotation, (RI_vector_3){0, rotation / 4, 0});
+        RI_euler_rotation_to_quaternion(&scene->actors[3]->rotation, (RI_vector_3){0, rotation / 4, 0});
 // scene->actors[2]->position.z += delta_time;
 // scene->actors[2]->position.z += delta_time;
 
 
         rotation += delta_time;
         rotation += delta_time;

BIN
src/libraries/libpitmap.so


+ 34 - 1
src/main/main.c

@@ -40,7 +40,7 @@ void debug(char *string, int debug_flag, ...){
 }
 }
 
 
 RI_texture* RI_load_image(char* filename){
 RI_texture* RI_load_image(char* filename){
-    PM_image* image = PM_load_image(filename);
+    PM_image* image = PM_load_image(filename, context.debug_flags & RI_DEBUG_PITMAP == 0 ? 0 : 1);
 
 
     RI_texture* texture = RI_malloc(sizeof(RI_texture));
     RI_texture* texture = RI_malloc(sizeof(RI_texture));
 
 
@@ -50,6 +50,10 @@ RI_texture* RI_load_image(char* filename){
     texture->height = image->height;
     texture->height = image->height;
     texture->index = previous_length_of_textures_array;
     texture->index = previous_length_of_textures_array;
 
 
+    texture->frame_count = 1;
+    texture->frame_height = image->height;
+    texture->current_frame = 0;
+
     context.opencl.length_of_textures_array += image->width * image->height;
     context.opencl.length_of_textures_array += image->width * image->height;
 
 
     context.opencl.textures = RI_realloc(context.opencl.textures, context.opencl.length_of_textures_array * sizeof(uint32_t));
     context.opencl.textures = RI_realloc(context.opencl.textures, context.opencl.length_of_textures_array * sizeof(uint32_t));
@@ -90,6 +94,20 @@ RI_texture* RI_load_image(char* filename){
     return texture;
     return texture;
 }
 }
 
 
+RI_texture* RI_load_animation(char* filename, uint16_t frame_count){
+    RI_texture* texture = RI_load_image(filename);
+
+    if (texture->height % frame_count != 0){
+        printf("frame count is invalid, frame height doesn't divide evenly by %d", frame_count);
+        return NULL;
+    }
+
+    texture->frame_count = frame_count;
+    texture->frame_height = texture->height / frame_count;
+
+    return texture;
+}
+
 RI_material *RI_new_material(){
 RI_material *RI_new_material(){
     RI_material *new_material = RI_malloc(sizeof(RI_material));
     RI_material *new_material = RI_malloc(sizeof(RI_material));
 
 
@@ -729,6 +747,13 @@ void RI_render(RI_scene *scene){
         // 33: uint32_t texture_index
         // 33: uint32_t texture_index
         clSetKernelArg(context.opencl.transformation_kernel, 33, sizeof(uint32_t), &actor->texture->index);
         clSetKernelArg(context.opencl.transformation_kernel, 33, sizeof(uint32_t), &actor->texture->index);
 
 
+        // 35: uint16_t frame_count
+        clSetKernelArg(context.opencl.transformation_kernel, 35, sizeof(uint16_t), &actor->texture->frame_count);
+        // 36: uint16_t frame_height
+        clSetKernelArg(context.opencl.transformation_kernel, 36, sizeof(uint16_t), &actor->texture->frame_height);
+        // 37: uint32_t current_frame
+        clSetKernelArg(context.opencl.transformation_kernel, 37, sizeof(uint16_t), &actor->texture_frame);
+
         debug("running actor #%d's transformation kernel...", 
         debug("running actor #%d's transformation kernel...", 
             RI_DEBUG_TRANSFORMER_MESSAGE, 
             RI_DEBUG_TRANSFORMER_MESSAGE, 
             actor_index
             actor_index
@@ -1246,6 +1271,13 @@ int RI_init(){
     // // 34: uint32_t tiles mem buffer
     // // 34: uint32_t tiles mem buffer
     // clSetKernelArg(context.opencl.transformation_kernel, 34, sizeof(cl_mem), &context.opencl.tiles_mem_buffer);
     // clSetKernelArg(context.opencl.transformation_kernel, 34, sizeof(cl_mem), &context.opencl.tiles_mem_buffer);
     
     
+    // // 35: uint16_t frame_count
+    // clSetKernelArg(context.opencl.transformation_kernel, 35, sizeof(uint16_t), &actor->texture->frame_count);
+    // // 36: uint16_t frame_height
+    // clSetKernelArg(context.opencl.transformation_kernel, 36, sizeof(uint16_t), &actor->texture->frame_height);
+    // // 37: uint32_t current_frame
+    // clSetKernelArg(context.opencl.transformation_kernel, 37, sizeof(uint32_t), &actor->texture->current_frame);
+
     context.defaults.default_actor = RI_malloc(sizeof(RI_actor));
     context.defaults.default_actor = RI_malloc(sizeof(RI_actor));
 
 
     context.defaults.default_actor->mesh = RI_load_mesh("objects/error_object.obj");
     context.defaults.default_actor->mesh = RI_load_mesh("objects/error_object.obj");
@@ -1255,6 +1287,7 @@ int RI_init(){
     context.defaults.default_actor->rotation = (RI_vector_4){1, 0, 0, 0};
     context.defaults.default_actor->rotation = (RI_vector_4){1, 0, 0, 0};
     context.defaults.default_actor->scale = (RI_vector_3){1, 1, 1};
     context.defaults.default_actor->scale = (RI_vector_3){1, 1, 1};
     context.defaults.default_actor->texture = RI_load_image("textures/missing_texture.bmp");                                                                                                                                                   
     context.defaults.default_actor->texture = RI_load_image("textures/missing_texture.bmp");                                                                                                                                                   
+    context.defaults.default_actor->texture_frame = 0;
 
 
     return 0;
     return 0;
 }
 }