Skip to content

ogt_vox: (teardown) custom chunk support #79

@mgerhardy

Description

@mgerhardy

it looks like teardown has a custom chunk for compressed XYZI - called TDCZ - which is basically just a zlib compressed XYZI chunk.

i am not sure if a general support outside the default magicavoxel chunks is the best option, but e.g. having a way to inject own chunk readers (and maybe also writers) would be a nice way to use the power of RIFF based formats.

https://github.com/TTFH/Teardown-Converter/blob/a75787ac6ca943a9838bf2fc746fe08aaab3c51f/src/vox_writer.cpp#L115C1-L128C2

diff --git a/src/ogt_vox.h b/src/ogt_vox.h
index fbf24ee..5ac2976 100644
--- a/src/ogt_vox.h
+++ b/src/ogt_vox.h
@@ -636,6 +636,20 @@
         uint32_t        offset;       // current offset in the buffer data.
     };
 
+    // custom chunk loader function. Returns false to skip the chunk, true if it was handled properly
+    typedef bool (*ogt_vox_custom_chunk_reader_func)(_vox_file* fp, uint32_t chunk_id, uint32_t chunk_size, uint32_t chunk_child_size, void* user_data);
+
+    static ogt_vox_custom_chunk_reader_func g_custom_chunk_reader_func = NULL;
+    static void* g_custom_chunk_reader_user_data = NULL;
+
+    // set the custom chunk reader callback function and user data to pass to it - this can only happen in the implementation because we need
+    // the reading functions to be available, too
+    // you can extend the library with support for own chunk types by providing a custom chunk reader function.
+    static inline void ogt_vox_set_custom_chunk_reader_func(ogt_vox_custom_chunk_reader_func custom_chunk_reader_func, void* user_data) {
+        g_custom_chunk_reader_func = custom_chunk_reader_func;
+        g_custom_chunk_reader_user_data = user_data;
+    }
+
     static uint32_t _vox_file_bytes_remaining(const _vox_file* fp) {
         if (fp->offset < fp->buffer_size) {
             return fp->buffer_size - fp->offset;
@@ -2054,7 +2068,9 @@
                 }
                 default:
                 {
-                    _vox_file_seek_forwards(fp, chunk_size);
+                    if (!g_custom_chunk_reader_func || !g_custom_chunk_reader_func(fp, chunk_id, chunk_size, chunk_child_size, g_custom_chunk_reader_user_data)) {
+                        _vox_file_seek_forwards(fp, chunk_size);
+                    }
                     break;
                 }
             } // end switch

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions