From 48730364a462898dc4e466de1a300e7a139334c4 Mon Sep 17 00:00:00 2001 From: Wenzel Jakob Date: Sat, 2 Aug 2014 23:09:46 +0200 Subject: [PATCH] =?UTF-8?q?TriMesh::fromBlender=20improvements=20contribut?= =?UTF-8?q?ed=20by=20Francesc=20Juh=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/librender/trimesh.cpp | 119 ++++++++++++++++++++++++++------------ 1 file changed, 82 insertions(+), 37 deletions(-) diff --git a/src/librender/trimesh.cpp b/src/librender/trimesh.cpp index c8214c5f..6adacbf9 100644 --- a/src/librender/trimesh.cpp +++ b/src/librender/trimesh.cpp @@ -864,6 +864,7 @@ void TriMesh::serialize(Stream *stream, InstanceManager *manager) const { ref TriMesh::fromBlender(const std::string &name, size_t faceCount, void *_facePtr, size_t vertexCount, void *_vertexPtr, void *_uvPtr, void *_colPtr, short mat_nr) { + const int ME_SMOOTH = 1; struct MFace { uint32_t v[4]; int16_t mat_nr; @@ -880,32 +881,46 @@ ref TriMesh::fromBlender(const std::string &name, uint8_t a, r, g, b; }; - struct MLoopUV { - float uv[2]; - int32_t flag; + struct MTFace { + float uv[4][2]; + void *tpage; + uint8_t flag, transp; + uint16_t mode, tile, unwrap; }; MFace *facePtr = (MFace *) _facePtr; MVert *vertexPtr = (MVert *) _vertexPtr; MCol *colPtr = (MCol *) _colPtr; - MLoopUV *uvPtr = (MLoopUV *) _uvPtr; + MTFace *uvPtr = (MTFace *) _uvPtr; - boost::unordered_map vertexMap; + boost::unordered_map vertexMap; + boost::unordered_map vertexFaceMap; uint32_t triangleCtr = 0, vertexCtr = 0; - for (uint32_t i=0; i triMesh = new TriMesh(name, triangleCtr, vertexCtr, true, uvPtr != NULL, colPtr != NULL); @@ -915,49 +930,79 @@ ref TriMesh::fromBlender(const std::string &name, Color3 *vertexColors = (Color3 *) triMesh->getVertexColors(); Point2 *vertexTexcoords = (Point2 *) triMesh->getVertexTexcoords(); - for (uint32_t i=0; i::iterator it = vertexMap.begin(); + for (boost::unordered_map::iterator it = vertexMap.begin(); it != vertexMap.end(); ++it) { - const MVert &vertex = vertexPtr[it->first]; + const MVert &vertex = vertexPtr[it->first & vertexMask]; + bool isSmooth = (it->first & faceMask) == 0; uint32_t idx = it->second; - vertexPositions[idx] = Point3(vertex.co[0], vertex.co[1], vertex.co[2]); - vertexNormals[idx] = normalize(Normal( - vertex.no[0] * normalScale, - vertex.no[1] * normalScale, - vertex.no[2] * normalScale - )); + vertexPositions[idx] = Point(vertex.co[0], vertex.co[1], vertex.co[2]); - if (uvPtr) { - const MLoopUV &uv = uvPtr[it->first]; - vertexTexcoords[idx] = Point2(uv.uv[0], uv.uv[1]); + if ((it->first & faceMask) != 0) { + uint32_t faceID = (uint32_t)((it->first >> 32ULL) & 0xFFFFFFFFULL) - 1; + const MFace &face = facePtr[faceID]; + isSmooth = (face.flag & ME_SMOOTH) != 0; + + if (uvPtr) { + const MTFace &uv = uvPtr[faceID]; + vertexTexcoords[idx] = Point2(uv.uv[vertexFaceMap[it->first]][0], 1 - uv.uv[vertexFaceMap[it->first]][1]); + } + + if (colPtr) { + const MCol &col = colPtr[(uint32_t) ( faceID * 4 ) + (uint32_t) vertexFaceMap[it->first]]; + vertexColors[idx] = Color3( + col.b * rgbScale, + col.g * rgbScale, + col.r * rgbScale + ); + } } - if (colPtr) { - const MCol &col = colPtr[it->first]; - vertexColors[idx] = Color3( - col.r * rgbScale, - col.g * rgbScale, - col.b * rgbScale - ); + if (isSmooth) { + vertexNormals[idx] = normalize(Normal( + vertex.no[0] * normalScale, + vertex.no[1] * normalScale, + vertex.no[2] * normalScale + )); } }