From f6e4c054a7f338f912de2640d156f90c35b9efb8 Mon Sep 17 00:00:00 2001 From: Leonardo Zide Date: Sat, 9 Sep 2017 11:33:46 -0700 Subject: [PATCH] Export normals to pov and obj. Fixes #117 . --- common/lc_mesh.cpp | 14 +++++++++----- common/project.cpp | 20 ++++++++++++++++++++ 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/common/lc_mesh.cpp b/common/lc_mesh.cpp index 1a41309b..953436f1 100644 --- a/common/lc_mesh.cpp +++ b/common/lc_mesh.cpp @@ -270,11 +270,15 @@ void lcMesh::ExportPOVRay(lcFile& File, const char* MeshName, const char** Color for (int Idx = 0; Idx < Section->NumIndices; Idx += 3) { - lcVector3& v1 = Verts[Indices[Idx]].Position; - lcVector3& v2 = Verts[Indices[Idx + 1]].Position; - lcVector3& v3 = Verts[Indices[Idx + 2]].Position; + const lcVector3 v1 = Verts[Indices[Idx]].Position / 25.0f; + const lcVector3 v2 = Verts[Indices[Idx + 1]].Position / 25.0f; + const lcVector3 v3 = Verts[Indices[Idx + 2]].Position / 25.0f; + const lcVector3 n1 = lcUnpackNormal(Verts[Indices[Idx]].Normal); + const lcVector3 n2 = lcUnpackNormal(Verts[Indices[Idx + 1]].Normal); + const lcVector3 n3 = lcUnpackNormal(Verts[Indices[Idx + 2]].Normal); - sprintf(Line, " triangle { <%.2f, %.2f, %.2f>, <%.2f, %.2f, %.2f>, <%.2f, %.2f, %.2f> }\n", -v1.y / 25.0f, -v1.x / 25.0f, v1.z / 25.0f, -v2.y / 25.0f, -v2.x / 25.0f, v2.z / 25.0f, -v3.y / 25.0f, -v3.x / 25.0f, v3.z / 25.0f); + sprintf(Line, " smooth_triangle { <%.2f, %.2f, %.2f>, <%.2f, %.2f, %.2f>, <%.2f, %.2f, %.2f>, <%.2f, %.2f, %.2f>, <%.2f, %.2f, %.2f>, <%.2f, %.2f, %.2f> }\n", + -v1.y, -v1.x, v1.z, -n1.y, -n1.x, n1.z, -v2.y, -v2.x, v2.z, -n2.y, -n2.x, n2.z, -v3.y, -v3.x, v3.z, -n3.y, -n3.x, n3.z); File.WriteLine(Line); } @@ -323,7 +327,7 @@ void lcMesh::ExportWavefrontIndices(lcFile& File, int DefaultColorIndex, int Ver long int idx3 = Indices[Idx + 2] + VertexOffset; if (idx1 != idx2 && idx1 != idx3 && idx2 != idx3) - sprintf(Line, "f %ld %ld %ld\n", idx1, idx2, idx3); + sprintf(Line, "f %ld//%ld %ld//%ld %ld//%ld\n", idx1, idx1, idx2, idx2, idx3, idx3); File.WriteLine(Line); } } diff --git a/common/project.cpp b/common/project.cpp index 722f1185..431adfbe 100644 --- a/common/project.cpp +++ b/common/project.cpp @@ -2282,6 +2282,26 @@ void Project::ExportWavefront(const QString& FileName) OBJFile.WriteLine("#\n\n"); } + for (int PartIdx = 0; PartIdx < ModelParts.GetSize(); PartIdx++) + { + lcMesh* Mesh = ModelParts[PartIdx].Info->GetMesh(); + + if (!Mesh) + continue; + + const lcMatrix44& ModelWorld = ModelParts[PartIdx].WorldMatrix; + lcVertex* Verts = (lcVertex*)Mesh->mVertexData; + + for (int VertexIdx = 0; VertexIdx < Mesh->mNumVertices; VertexIdx++) + { + lcVector3 Normal = lcMul30(lcUnpackNormal(Verts[VertexIdx].Normal), ModelWorld); + sprintf(Line, "vn %.2f %.2f %.2f\n", Normal[0], Normal[1], Normal[2]); + OBJFile.WriteLine(Line); + } + + OBJFile.WriteLine("#\n\n"); + } + for (int PartIdx = 0; PartIdx < ModelParts.GetSize(); PartIdx++) { PieceInfo* Info = ModelParts[PartIdx].Info;