Fix VoxelBuffer channel initialized with wrong value when allocated

This commit is contained in:
Marc Gilleron 2019-04-22 20:15:19 +01:00
parent 869b921ce8
commit 9108bfe0f6
2 changed files with 9 additions and 7 deletions

View File

@ -19,9 +19,10 @@ void VoxelBuffer::create(int sx, int sy, int sz) {
for (unsigned int i = 0; i < MAX_CHANNELS; ++i) { for (unsigned int i = 0; i < MAX_CHANNELS; ++i) {
Channel &channel = _channels[i]; Channel &channel = _channels[i];
if (channel.data) { if (channel.data) {
// Channel already contained data
// TODO Optimize with realloc // TODO Optimize with realloc
delete_channel(i); delete_channel(i);
create_channel(i, new_size); create_channel(i, new_size, channel.defval);
} }
} }
_size = new_size; _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.data == NULL) {
if (channel.defval != value) { 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; channel.data[index(x, y, z)] = value;
} }
} else { } 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.data == NULL) {
if (channel.defval != value) { if (channel.defval != value) {
create_channel(channel_index, _size); create_channel(channel_index, _size, channel.defval);
channel.data[index(x, y, z)] = value; channel.data[index(x, y, z)] = value;
} }
} else { } else {
@ -142,7 +144,7 @@ void VoxelBuffer::fill_area(int defval, Vector3i min, Vector3i max, unsigned int
if (channel.defval == defval) { if (channel.defval == defval) {
return; return;
} else { } 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 { } else {
if (other_channel.data) { if (other_channel.data) {
if (channel.data == NULL) { if (channel.data == NULL) {
create_channel(channel_index, _size); create_channel(channel_index, _size, channel.defval);
} }
// Copy row by row // Copy row by row
Vector3i pos; Vector3i pos;
@ -241,7 +243,7 @@ void VoxelBuffer::copy_from(const VoxelBuffer &other, Vector3i src_min, Vector3i
} }
} else if (channel.defval != other_channel.defval) { } else if (channel.defval != other_channel.defval) {
if (channel.data == NULL) { if (channel.data == NULL) {
create_channel(channel_index, _size); create_channel(channel_index, _size, channel.defval);
} }
// Set row by row // Set row by row
Vector3i pos; Vector3i pos;

View File

@ -98,7 +98,7 @@ public:
private: private:
void create_channel_noinit(int i, Vector3i size); 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); void delete_channel(int i);
protected: protected: