VOXELFORMAT: use the node palette for the mesh format export, too

master
Martin Gerhardy 2022-05-11 23:28:49 +02:00
parent 96d7f8a6bb
commit c9a10ec425
5 changed files with 45 additions and 39 deletions

View File

@ -21,10 +21,10 @@ namespace voxelformat {
return false; \
}
bool FBXFormat::saveMeshes(const core::Map<int, int> &, const SceneGraph &, const Meshes &meshes,
bool FBXFormat::saveMeshes(const core::Map<int, int> &, const SceneGraph &sceneGraph, const Meshes &meshes,
const core::String &filename, io::SeekableWriteStream &stream, const glm::vec3 &scale,
bool quad, bool withColor, bool withTexCoords) {
return saveMeshesAscii(meshes, filename, stream, scale, quad, withColor, withTexCoords);
return saveMeshesAscii(meshes, filename, stream, scale, quad, withColor, withTexCoords, sceneGraph);
}
class FBXScopedHeader {
@ -51,7 +51,7 @@ public:
};
bool FBXFormat::saveMeshesBinary(const Meshes &meshes, const core::String &filename, io::SeekableWriteStream &stream, const glm::vec3 &scale, bool quad,
bool withColor, bool withTexCoords) {
bool withColor, bool withTexCoords, const SceneGraph &sceneGraph) {
wrapBool(stream.writeString("Kaydara FBX Binary ", true))
stream.writeUInt8(0x1A); // unknown
stream.writeUInt8(0x00); // unknown
@ -62,13 +62,7 @@ bool FBXFormat::saveMeshesBinary(const Meshes &meshes, const core::String &filen
// https://github.com/blender/blender/blob/00e219d8e97afcf3767a6d2b28a6d05bcc984279/release/io/export_fbx.py
bool FBXFormat::saveMeshesAscii(const Meshes &meshes, const core::String &filename, io::SeekableWriteStream &stream, const glm::vec3 &scale, bool quad,
bool withColor, bool withTexCoords) {
const voxel::Palette &palette = voxel::getPalette();
// 1 x 256 is the texture format that we are using for our palette
const float texcoord = 1.0f / (float)palette.colorCount;
// it is only 1 pixel high - sample the middle
const float v1 = 0.5f;
bool withColor, bool withTexCoords, const SceneGraph &sceneGraph) {
// TODO: support keyframes (takes)
stream.writeStringFormat(false, R"(FBXHeaderExtension: {
FBXHeaderVersion: 1003
@ -112,6 +106,8 @@ Objects: {
Log::error("Unexpected indices amount");
return false;
}
const SceneGraphNode &graphNode = sceneGraph.node(meshExt.nodeId);
const voxel::Palette &palette = graphNode.palette();
const glm::vec3 offset(mesh->getOffset());
const voxel::VoxelVertex *vertices = mesh->getRawVertexData();
const voxel::IndexType *indices = mesh->getRawIndexData();
@ -155,6 +151,10 @@ Objects: {
wrapBool(stream.writeString("\t\tGeometryVersion: 124\n", false))
if (withTexCoords) {
// 1 x 256 is the texture format that we are using for our palette
const float texcoord = 1.0f / (float)palette.colorCount;
// it is only 1 pixel high - sample the middle
const float v1 = 0.5f;
wrapBool(stream.writeString("\t\tLayerElementUV: 0 {\n", false))
wrapBool(stream.writeString("\t\t\tVersion: 101\n", false))
stream.writeStringFormat(false, "\t\t\tName: \"%sUV\"\n", objectName);

View File

@ -17,9 +17,9 @@ namespace voxelformat {
class FBXFormat : public MeshFormat {
private:
bool saveMeshesBinary(const Meshes &meshes, const core::String &filename, io::SeekableWriteStream &stream,
const glm::vec3 &scale, bool quad, bool withColor, bool withTexCoords);
const glm::vec3 &scale, bool quad, bool withColor, bool withTexCoords, const SceneGraph &sceneGraph);
bool saveMeshesAscii(const Meshes &meshes, const core::String &filename, io::SeekableWriteStream &stream,
const glm::vec3 &scale, bool quad, bool withColor, bool withTexCoords);
const glm::vec3 &scale, bool quad, bool withColor, bool withTexCoords, const SceneGraph &sceneGraph);
public:
bool saveMeshes(const core::Map<int, int> &meshIdxNodeMap, const SceneGraph &sceneGraph, const Meshes &meshes,

View File

@ -56,9 +56,6 @@ void GLTFFormat::processGltfNode(tinygltf::Model &m, tinygltf::Node &node, tinyg
bool GLTFFormat::saveMeshes(const core::Map<int, int> &meshIdxNodeMap, const SceneGraph &sceneGraph,
const Meshes &meshes, const core::String &filename, io::SeekableWriteStream &stream,
const glm::vec3 &scale, bool quad, bool withColor, bool withTexCoords) {
const voxel::Palette &palette = voxel::getPalette();
// 1 x 256 is the texture format that we are using for our palette
const float texcoord = 1.0f / (float)palette.colorCount;
unsigned char uvYVal[4] = {0, 0, 0, 63};
core::String palettename = core::string::stripExtension(filename);
@ -137,6 +134,9 @@ bool GLTFFormat::saveMeshes(const core::Map<int, int> &meshIdxNodeMap, const Sce
processGltfNode(m, node, scene, graphNode, stack);
continue;
}
const voxel::Palette &palette = graphNode.palette();
// 1 x 256 is the texture format that we are using for our palette
const float texcoord = 1.0f / (float)palette.colorCount;
int meshExtIdx = 0;
meshIdxNodeMap.get(nodeId, meshExtIdx);

View File

@ -57,25 +57,13 @@ bool OBJFormat::writeMtlFile(const core::String &mtlName, const core::String &pa
return true;
}
bool OBJFormat::saveMeshes(const core::Map<int, int> &, const SceneGraph &, const Meshes &meshes,
bool OBJFormat::saveMeshes(const core::Map<int, int> &, const SceneGraph &sceneGraph, const Meshes &meshes,
const core::String &filename, io::SeekableWriteStream &stream, const glm::vec3 &scale,
bool quad, bool withColor, bool withTexCoords) {
const voxel::Palette &palette = voxel::getPalette();
// 1 x 256 is the texture format that we are using for our palette
const float texcoord = 1.0f / (float)palette.colorCount;
// it is only 1 pixel high - sample the middle
const float v1 = 0.5f;
stream.writeStringFormat(false, "# version " PROJECT_VERSION " github.com/mgerhardy/vengi\n");
wrapBool(stream.writeStringFormat(false, "\n"))
wrapBool(stream.writeStringFormat(false, "g Model\n"))
core::String mtlname = core::string::stripExtension(filename);
mtlname.append(".mtl");
core::String palettename = core::string::stripExtension(filename);
palettename.append(".png");
Log::debug("Exporting %i layers", (int)meshes.size());
int idxOffset = 0;
@ -89,6 +77,21 @@ bool OBJFormat::saveMeshes(const core::Map<int, int> &, const SceneGraph &, cons
Log::error("Unexpected indices amount");
return false;
}
const SceneGraphNode &graphNode = sceneGraph.node(meshExt.nodeId);
const voxel::Palette &palette = graphNode.palette();
core::String mtlname = core::string::stripExtension(filename);
mtlname.append(core::String::format("%" PRIu64, palette.hash()));
mtlname.append(".mtl");
core::String palettename = core::string::stripExtension(filename);
palettename.append(core::String::format("%" PRIu64, palette.hash()));
palettename.append(".png");
// 1 x 256 is the texture format that we are using for our palette
const float texcoord = 1.0f / (float)palette.colorCount;
// it is only 1 pixel high - sample the middle
const float v1 = 0.5f;
const glm::vec3 offset(mesh->getOffset());
const voxel::VoxelVertex *vertices = mesh->getRawVertexData();
const voxel::IndexType *indices = mesh->getRawIndexData();
@ -170,12 +173,15 @@ bool OBJFormat::saveMeshes(const core::Map<int, int> &, const SceneGraph &, cons
texcoordOffset += ni;
}
idxOffset += nv;
}
if (!writeMtlFile(mtlname, palettename)) {
return false;
if (!writeMtlFile(mtlname, palettename)) {
return false;
}
if (!voxel::getPalette().save(palettename.c_str())) {
return false;
}
}
return voxel::getPalette().save(palettename.c_str());
return true;
}
#undef wrapBool

View File

@ -16,7 +16,7 @@
namespace voxelformat {
bool PLYFormat::saveMeshes(const core::Map<int, int> &, const SceneGraph &, const Meshes &meshes,
bool PLYFormat::saveMeshes(const core::Map<int, int> &, const SceneGraph &sceneGraph, const Meshes &meshes,
const core::String &filename, io::SeekableWriteStream &stream, const glm::vec3 &scale,
bool quad, bool withColor, bool withTexCoords) {
const char *paletteName = voxel::Palette::getDefaultPaletteName();
@ -57,17 +57,17 @@ bool PLYFormat::saveMeshes(const core::Map<int, int> &, const SceneGraph &, cons
stream.writeStringFormat(false, "property list uchar uint vertex_indices\n");
stream.writeStringFormat(false, "end_header\n");
const voxel::Palette& palette = voxel::getPalette();
// 1 x 256 is the texture format that we are using for our palette
const float texcoord = 1.0f / (float)palette.colorCount;
// it is only 1 pixel high - sample the middle
const float v1 = 0.5f;
for (const auto& meshExt : meshes) {
const voxel::Mesh& mesh = *meshExt.mesh;
const glm::vec3 offset(mesh.getOffset());
const int nv = (int)mesh.getNoOfVertices();
const voxel::VoxelVertex* vertices = mesh.getRawVertexData();
const SceneGraphNode &graphNode = sceneGraph.node(meshExt.nodeId);
const voxel::Palette &palette = graphNode.palette();
// 1 x 256 is the texture format that we are using for our palette
const float texcoord = 1.0f / (float)palette.colorCount;
// it is only 1 pixel high - sample the middle
const float v1 = 0.5f;
for (int i = 0; i < nv; ++i) {
const voxel::VoxelVertex& v = vertices[i];