From 9108bfe0f64acb75a7bb5fe45a19636005ffaa95 Mon Sep 17 00:00:00 2001 From: Marc Gilleron Date: Mon, 22 Apr 2019 20:15:19 +0100 Subject: [PATCH] Fix VoxelBuffer channel initialized with wrong value when allocated --- voxel_buffer.cpp | 14 ++++++++------ voxel_buffer.h | 2 +- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/voxel_buffer.cpp b/voxel_buffer.cpp index 1f0f119..835b23c 100644 --- a/voxel_buffer.cpp +++ b/voxel_buffer.cpp @@ -19,9 +19,10 @@ void VoxelBuffer::create(int sx, int sy, int sz) { for (unsigned int i = 0; i < MAX_CHANNELS; ++i) { Channel &channel = _channels[i]; if (channel.data) { + // Channel already contained data // TODO Optimize with realloc delete_channel(i); - create_channel(i, new_size); + create_channel(i, new_size, channel.defval); } } _size = new_size; @@ -71,7 +72,8 @@ void VoxelBuffer::set_voxel(int value, int x, int y, int z, unsigned int channel if (channel.data == NULL) { if (channel.defval != value) { - create_channel(channel_index, _size); + // Allocate channel with same initial values as defval + create_channel(channel_index, _size, channel.defval); channel.data[index(x, y, z)] = value; } } else { @@ -90,7 +92,7 @@ void VoxelBuffer::try_set_voxel(int x, int y, int z, int value, unsigned int cha if (channel.data == NULL) { if (channel.defval != value) { - create_channel(channel_index, _size); + create_channel(channel_index, _size, channel.defval); channel.data[index(x, y, z)] = value; } } else { @@ -142,7 +144,7 @@ void VoxelBuffer::fill_area(int defval, Vector3i min, Vector3i max, unsigned int if (channel.defval == defval) { return; } else { - create_channel(channel_index, _size); + create_channel(channel_index, _size, channel.defval); } } @@ -227,7 +229,7 @@ void VoxelBuffer::copy_from(const VoxelBuffer &other, Vector3i src_min, Vector3i } else { if (other_channel.data) { if (channel.data == NULL) { - create_channel(channel_index, _size); + create_channel(channel_index, _size, channel.defval); } // Copy row by row Vector3i pos; @@ -241,7 +243,7 @@ void VoxelBuffer::copy_from(const VoxelBuffer &other, Vector3i src_min, Vector3i } } else if (channel.defval != other_channel.defval) { if (channel.data == NULL) { - create_channel(channel_index, _size); + create_channel(channel_index, _size, channel.defval); } // Set row by row Vector3i pos; diff --git a/voxel_buffer.h b/voxel_buffer.h index e4a0204..796f66c 100644 --- a/voxel_buffer.h +++ b/voxel_buffer.h @@ -98,7 +98,7 @@ public: private: void create_channel_noinit(int i, Vector3i size); - void create_channel(int i, Vector3i size, uint8_t defval = 0); + void create_channel(int i, Vector3i size, uint8_t defval); void delete_channel(int i); protected: