[Plib-devel] Automatic normals in the OBJ loader.
Brought to you by:
sjbaker
From: <jeb...@fr...> - 2006-09-18 21:22:45
|
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, I need to write an application that will load objects from .obj files with no normal information included in the file. It would be nice if PLIB could test for this case and generate some reasonable normals automatically. I have implemented this function and it works on the files I have tested (without breaking those files that *include* normal information of course). In the function add_mesh (ssgLoadOBJ.cxx, line 310 for version 1.8.4), I added the following code: // Create the normals if (num_vert_normal == 0) { memset (vert_normal, 0, sizeof (vert_normal)); for (i = 0; i < num_face; ++i) { sgVec3 v1, v2, faceNormal; sgSubVec3 (v1, vert[face[i].vlist[1]], vert[face[i].vlist[0]]); sgSubVec3 (v2, vert[face[i].vlist[2]], vert[face[i].vlist[0]]); sgVectorProductVec3 (faceNormal, v1, v2); sgAddVec3 (vert_normal[face[i].vlist[0]], faceNormal); sgAddVec3 (vert_normal[face[i].vlist[1]], faceNormal); sgAddVec3 (vert_normal[face[i].vlist[2]], faceNormal); face[i].nlist[0] = face[i].vlist[0]; face[i].nlist[1] = face[i].vlist[1]; face[i].nlist[2] = face[i].vlist[2]; } num_vert_normal = num_vert; for (i = 0; i < num_vert_normal; ++i) sgNormalizeVec3 (vert_normal[i]); } Of course, the resulting normals aren't perfect, but they're still a lot better than what happens when there is no normal information at all. Jerome - -- +------------------------- Jerome M. BERGER ---------------------+ | mailto:jeb...@fr... | ICQ: 238062172 | | http://jeberger.free.fr/ | Jabber: jeb...@ja... | +---------------------------------+------------------------------+ -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2.2 (GNU/Linux) iD8DBQFFDw4ed0kWM4JG3k8RAjSkAJ9U16lS9mpl+NisA2J4LNwWPRS8DACgjdJv Q+42eEyAIuQpSOA1p6ILGE8= =JWHw -----END PGP SIGNATURE----- |