opengametools icon indicating copy to clipboard operation
opengametools copied to clipboard

ogt_vox: (teardown) custom chunk support

Open mgerhardy opened this issue 2 months ago • 2 comments

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

mgerhardy avatar Nov 02 '25 09:11 mgerhardy

after talking to @jpaver and @dougbinks:

So yes a deferred read with spans also sounds reasonable.

mgerhardy avatar Nov 02 '25 18:11 mgerhardy

I added #80 to track the custom XYZI chunk loading somewhat separately in case these don't get resolved in exactly the same way.

dougbinks avatar Nov 04 '25 10:11 dougbinks