From 51623b48f24d4cfdb6525345e73632c1eace53eb Mon Sep 17 00:00:00 2001 From: Martin Gerhardy Date: Tue, 25 Oct 2016 20:52:45 +0200 Subject: [PATCH] VOXEL: implemented rawvolume loading for voxloader and implemented the load command for voxedit --- data/tests/magicavoxel.vox | Bin 1656 -> 2688 bytes src/modules/voxel/VoxLoader.cpp | 224 +++++++++++++++++++++++++++++--- src/tools/voxedit/VoxEdit.cpp | 15 ++- 3 files changed, 216 insertions(+), 23 deletions(-) diff --git a/data/tests/magicavoxel.vox b/data/tests/magicavoxel.vox index 56e986c94ad0dbb8f68649de00d038ea71736ec7..3ec10066ed65dc189a86b0f093fcacf74c588ff2 100644 GIT binary patch literal 2688 zcmXZdv2G+;6^HRV5~{Jwwv z;@>ajSO33$|JNTr{`T;-TFO7(`7h#2i6-@>#Psqm^2@u3|GbMPRVF#SypR6neS~Y1 z`uaX5*}q)qyU@3b`1Q``T~ycU$6Xv`FFU!A_wr84kNdbh?PGr3$3;8zd_DA#_Ik)J zK4*PBSU-gCVLhyQ=OK1K9hzFx(|oVis?W8(PIF3#&%H|?rykOzo~AyzUVNUsV@kbg zEv;o0Yk821Pm^~};ogYT6xPFfSdYW!DfVp&&%iLXJWb6rTKmn^YEI4lZ7-fttyj;e z)~jb!�bJ-8<#WG{sB3jMsYEq`p?{b&g}5n|%E~9V>^*p>n7kYOnIOevpe#HRsow z{a8~Em&&DbK_Yr`uV*KE{gyTK@~WYiSLIcC)zBMz*?q2Y;rhN+bE@W4oBNvkn)|rc z=Dt?XwM|!*Z*AVuyn}PC{Zu*E8m`u=8rRCTacx{1&&IQvQJW9P#<6j192>{htM8NR z*R_k!E$36?*f=(hjbr23I5v(g_j-1sH}rCB92>{Rv2ko18^^}6#ZL4#y}TN)#;fsa zjUBJn4*zP+bDQTjy}XR~=tmKKX4rVy2rt1acm=QE6}*C1@CshRD|iL3;1#^W?1ESD z3SPl0cm=QE6}-ai!tBEAf>-bgUcoDP1+U;0yn(9oRU-8m&q%6rG1(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Z$GlhSQ>h*k8GSPlwGNa5iN|B3BaTc=V zgd=BDnQiui=j3u`9b(purMj9NLfuWOf5aF1M)~Z~z1oM9p7)cfBbkl<=G4ixiw^IO zc*b)TsAnzkM31cZ^ncX7a_#{L;Dz(9p#>gRGnv@HfPzT$00tCL6whb^6hNA1x&pK= z70DbREYARqk{KPP2c-{kP#I7_536qS$UvI{G1g>`L7wZ3&3o()+POCDnLYTe@BqTO zfu z-?sq1GK(wUXKbl&9 %i. children => %i", numBytesMainChunk, numBytesChildrenChunks); - - if (stream.remaining() < numBytesChildrenChunks) { + if (stream.remaining() < numBytesMainChildrenChunks) { Log::error("Could not load vox file: Incomplete file"); return nullptr; } + RawVolume *volume = nullptr; + uint32_t numModels = 1; + + // 8. Default Palette : if chunk 'RGBA' is absent + // ------------------------------------------------------------------------------- + uint32_t palette[256] = { + 0x00000000, 0xffffffff, 0xffccffff, 0xff99ffff, 0xff66ffff, 0xff33ffff, 0xff00ffff, 0xffffccff, 0xffccccff, 0xff99ccff, 0xff66ccff, 0xff33ccff, 0xff00ccff, 0xffff99ff, 0xffcc99ff, 0xff9999ff, + 0xff6699ff, 0xff3399ff, 0xff0099ff, 0xffff66ff, 0xffcc66ff, 0xff9966ff, 0xff6666ff, 0xff3366ff, 0xff0066ff, 0xffff33ff, 0xffcc33ff, 0xff9933ff, 0xff6633ff, 0xff3333ff, 0xff0033ff, 0xffff00ff, + 0xffcc00ff, 0xff9900ff, 0xff6600ff, 0xff3300ff, 0xff0000ff, 0xffffffcc, 0xffccffcc, 0xff99ffcc, 0xff66ffcc, 0xff33ffcc, 0xff00ffcc, 0xffffcccc, 0xffcccccc, 0xff99cccc, 0xff66cccc, 0xff33cccc, + 0xff00cccc, 0xffff99cc, 0xffcc99cc, 0xff9999cc, 0xff6699cc, 0xff3399cc, 0xff0099cc, 0xffff66cc, 0xffcc66cc, 0xff9966cc, 0xff6666cc, 0xff3366cc, 0xff0066cc, 0xffff33cc, 0xffcc33cc, 0xff9933cc, + 0xff6633cc, 0xff3333cc, 0xff0033cc, 0xffff00cc, 0xffcc00cc, 0xff9900cc, 0xff6600cc, 0xff3300cc, 0xff0000cc, 0xffffff99, 0xffccff99, 0xff99ff99, 0xff66ff99, 0xff33ff99, 0xff00ff99, 0xffffcc99, + 0xffcccc99, 0xff99cc99, 0xff66cc99, 0xff33cc99, 0xff00cc99, 0xffff9999, 0xffcc9999, 0xff999999, 0xff669999, 0xff339999, 0xff009999, 0xffff6699, 0xffcc6699, 0xff996699, 0xff666699, 0xff336699, + 0xff006699, 0xffff3399, 0xffcc3399, 0xff993399, 0xff663399, 0xff333399, 0xff003399, 0xffff0099, 0xffcc0099, 0xff990099, 0xff660099, 0xff330099, 0xff000099, 0xffffff66, 0xffccff66, 0xff99ff66, + 0xff66ff66, 0xff33ff66, 0xff00ff66, 0xffffcc66, 0xffcccc66, 0xff99cc66, 0xff66cc66, 0xff33cc66, 0xff00cc66, 0xffff9966, 0xffcc9966, 0xff999966, 0xff669966, 0xff339966, 0xff009966, 0xffff6666, + 0xffcc6666, 0xff996666, 0xff666666, 0xff336666, 0xff006666, 0xffff3366, 0xffcc3366, 0xff993366, 0xff663366, 0xff333366, 0xff003366, 0xffff0066, 0xffcc0066, 0xff990066, 0xff660066, 0xff330066, + 0xff000066, 0xffffff33, 0xffccff33, 0xff99ff33, 0xff66ff33, 0xff33ff33, 0xff00ff33, 0xffffcc33, 0xffcccc33, 0xff99cc33, 0xff66cc33, 0xff33cc33, 0xff00cc33, 0xffff9933, 0xffcc9933, 0xff999933, + 0xff669933, 0xff339933, 0xff009933, 0xffff6633, 0xffcc6633, 0xff996633, 0xff666633, 0xff336633, 0xff006633, 0xffff3333, 0xffcc3333, 0xff993333, 0xff663333, 0xff333333, 0xff003333, 0xffff0033, + 0xffcc0033, 0xff990033, 0xff660033, 0xff330033, 0xff000033, 0xffffff00, 0xffccff00, 0xff99ff00, 0xff66ff00, 0xff33ff00, 0xff00ff00, 0xffffcc00, 0xffcccc00, 0xff99cc00, 0xff66cc00, 0xff33cc00, + 0xff00cc00, 0xffff9900, 0xffcc9900, 0xff999900, 0xff669900, 0xff339900, 0xff009900, 0xffff6600, 0xffcc6600, 0xff996600, 0xff666600, 0xff336600, 0xff006600, 0xffff3300, 0xffcc3300, 0xff993300, + 0xff663300, 0xff333300, 0xff003300, 0xffff0000, 0xffcc0000, 0xff990000, 0xff660000, 0xff330000, 0xff0000ee, 0xff0000dd, 0xff0000bb, 0xff0000aa, 0xff000088, 0xff000077, 0xff000055, 0xff000044, + 0xff000022, 0xff000011, 0xff00ee00, 0xff00dd00, 0xff00bb00, 0xff00aa00, 0xff008800, 0xff007700, 0xff005500, 0xff004400, 0xff002200, 0xff001100, 0xffee0000, 0xffdd0000, 0xffbb0000, 0xffaa0000, + 0xff880000, 0xff770000, 0xff550000, 0xff440000, 0xff220000, 0xff110000, 0xffeeeeee, 0xffdddddd, 0xffbbbbbb, 0xffaaaaaa, 0xff888888, 0xff777777, 0xff555555, 0xff444444, 0xff222222, 0xff111111 + }; + do { - uint8_t byte; - if (stream.readByte(byte) != 0) { - break; + uint32_t chunkId; + wrap(stream.readInt(chunkId)) + uint32_t numBytesChunk; + wrap(stream.readInt(numBytesChunk)) + uint32_t numBytesChildrenChunks; + wrap(stream.readInt(numBytesChildrenChunks)) + + if (chunkId == FourCC('P','A','C','K')) { + // 4. Chunk id 'PACK' : if it is absent, only one model in the file + // ------------------------------------------------------------------------------- + // # Bytes | Type | Value + // ------------------------------------------------------------------------------- + // 4 | int | numModels : num of SIZE and XYZI chunks + // ------------------------------------------------------------------------------- + wrap(stream.readInt(numModels)) + if (numModels > 1) { + Log::warn("We are right now only loading the first model of a vox file"); + } + } else if (chunkId == FourCC('S','I','Z','E')) { + // 5. Chunk id 'SIZE' : model size + // ------------------------------------------------------------------------------- + // # Bytes | Type | Value + // ------------------------------------------------------------------------------- + // 4 | int | size x + // 4 | int | size y + // 4 | int | size z : gravity direction + // ------------------------------------------------------------------------------- + uint32_t x, y, z; + wrap(stream.readInt(x)) + wrap(stream.readInt(z)) + wrap(stream.readInt(y)) + glm::ivec3 maxs(x, y, z); + Log::debug("Found size chunk: (%u:%u:%u)", x, y, z); + Region region(glm::ivec3(0), maxs); + if (volume != nullptr) { + delete volume; + } + volume = new RawVolume(region); + } else if (chunkId == FourCC('X','Y','Z','I')) { + // 6. Chunk id 'XYZI' : model voxels + // ------------------------------------------------------------------------------- + // # Bytes | Type | Value + // ------------------------------------------------------------------------------- + // 4 | int | numVoxels (N) + // 4 x N | int | (x, y, z, colorIndex) : 1 byte for each component + // ------------------------------------------------------------------------------- + uint32_t numVoxels; + wrap(stream.readInt(numVoxels)) + if (volume == nullptr) { + Log::error("Could not load vox file: Wrong order of chunks"); + return nullptr; + } + Log::debug("Found voxel chunk with %u voxels", numVoxels); + for (uint32_t i = 0; i < numVoxels; ++i) { + uint8_t x, y, z, colorIndex; + wrap(stream.readByte(x)) + wrap(stream.readByte(z)) + wrap(stream.readByte(y)) + wrap(stream.readByte(colorIndex)) + // TODO: palette support + volume->setVoxel(x, y, z, createVoxel(Grass1)); + } + } else if (chunkId == FourCC('R','G','B','A')) { + Log::debug("Found palette chunk"); + // 7. Chunk id 'RGBA' : palette + // ------------------------------------------------------------------------------- + // # Bytes | Type | Value + // ------------------------------------------------------------------------------- + // 4 x 256 | int | (R, G, B, A) : 1 byte for each component + // | * + // | * color [0-254] are mapped to palette index [1-255], e.g : + // | + // | for ( int i = 0; i <= 254; i++ ) { + // | palette[i + 1] = ReadRGBA(); + // | } + // ------------------------------------------------------------------------------- + for (int i = 0; i <= 254; i++) { + uint32_t rgba; + wrap(stream.readInt(rgba)) + palette[i + 1] = rgba; + } + } else if (chunkId == FourCC('M','A','T','T')) { + Log::debug("Found material chunk"); + // 9. Chunk id 'MATT' : material, if it is absent, it is diffuse material + // ------------------------------------------------------------------------------- + // # Bytes | Type | Value + // ------------------------------------------------------------------------------- + // 4 | int | id [1-255] + // + // 4 | int | material type + // | 0 : diffuse + // | 1 : metal + // | 2 : glass + // | 3 : emissive + // + // 4 | float | material weight + // | diffuse : 1.0 + // | metal : (0.0 - 1.0] : blend between metal and diffuse material + // | glass : (0.0 - 1.0] : blend between glass and diffuse material + // | emissive : (0.0 - 1.0] : self-illuminated material + // + // 4 | int | property bits : set if value is saved in next section + // | bit(0) : Plastic + // | bit(1) : Roughness + // | bit(2) : Specular + // | bit(3) : IOR + // | bit(4) : Attenuation + // | bit(5) : Power + // | bit(6) : Glow + // | bit(7) : isTotalPower (*no value) + // + // 4 * N | float | normalized property value : (0.0 - 1.0] + // | * need to map to real range + // | * Plastic material only accepts {0.0, 1.0} for this version + // ------------------------------------------------------------------------------- + // TODO: + uint32_t materialId; + wrap(stream.readInt(materialId)) + uint32_t materialType; + wrap(stream.readInt(materialType)) + float materialWeight; + wrap(stream.readFloat(materialWeight)) + uint32_t materialProperties; + wrap(stream.readInt(materialProperties)) + for (uint32_t i = 0; i < numBytesChunk; ++i) { + float materialPropertyValue; + wrap(stream.readFloat(materialPropertyValue)) + } } } while (stream.remaining() > 0); - return nullptr; + return volume; } } diff --git a/src/tools/voxedit/VoxEdit.cpp b/src/tools/voxedit/VoxEdit.cpp index 3967d58ed..d6a613518 100644 --- a/src/tools/voxedit/VoxEdit.cpp +++ b/src/tools/voxedit/VoxEdit.cpp @@ -13,6 +13,7 @@ #include "frontend/Movement.h" #include "voxel/polyvox/Picking.h" #include "video/ScopedPolygonMode.h" +#include "voxel/VoxLoader.h" VoxEdit::VoxEdit(const io::FilesystemPtr& filesystem, const core::EventBusPtr& eventBus, const core::TimeProviderPtr& timeProvider) : ui::UIApp(filesystem, eventBus, timeProvider), _rawVolumeRenderer(true) { @@ -30,10 +31,20 @@ bool VoxEdit::saveFile(std::string_view file) { } bool VoxEdit::loadFile(std::string_view file) { - // TODO + const io::FilePtr& filePtr = core::App::getInstance()->filesystem()->open(std::string(file)); + if (!(bool)filePtr) { + return false; + } + voxel::VoxLoader loader; + voxel::RawVolume* newVolume = loader.load(filePtr); + if (newVolume == nullptr) { + return false; + } + voxel::RawVolume* old = _rawVolumeRenderer.setVolume(newVolume); + delete old; _extract = true; _dirty = false; - return false; + return true; } bool VoxEdit::newFile(bool force) {