/************************************************************************************** Copyright (C) 2010 Autodesk, Inc. and/or its licensors. All Rights Reserved. The coded instructions, statements, computer programs, and/or related material (collectively the "Data") in these files contain unpublished information proprietary to Autodesk, Inc. and/or its licensors, which is protected by Canada and United States of America federal copyright law and by international treaties. The Data may not be disclosed or distributed to third parties, in whole or in part, without the prior written consent of Autodesk, Inc. ("Autodesk"). THE DATA IS PROVIDED "AS IS" AND WITHOUT WARRANTY. ALL WARRANTIES ARE EXPRESSLY EXCLUDED AND DISCLAIMED. AUTODESK MAKES NO WARRANTY OF ANY KIND WITH RESPECT TO THE DATA, EXPRESS, IMPLIED OR ARISING BY CUSTOM OR TRADE USAGE, AND DISCLAIMS ANY IMPLIED WARRANTIES OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE OR USE. WITHOUT LIMITING THE FOREGOING, AUTODESK DOES NOT WARRANT THAT THE OPERATION OF THE DATA WILL BE UNINTERRUPTED OR ERROR FREE. IN NO EVENT SHALL AUTODESK, ITS AFFILIATES, PARENT COMPANIES, LICENSORS OR SUPPLIERS ("AUTODESK GROUP") BE LIABLE FOR ANY LOSSES, DAMAGES OR EXPENSES OF ANY KIND (INCLUDING WITHOUT LIMITATION PUNITIVE OR MULTIPLE DAMAGES OR OTHER SPECIAL, DIRECT, INDIRECT, EXEMPLARY, INCIDENTAL, LOSS OF PROFITS, REVENUE OR DATA, COST OF COVER OR CONSEQUENTIAL LOSSES OR DAMAGES OF ANY KIND), HOWEVER CAUSED, AND REGARDLESS OF THE THEORY OF LIABILITY, WHETHER DERIVED FROM CONTRACT, TORT (INCLUDING, BUT NOT LIMITED TO, NEGLIGENCE), OR OTHERWISE, ARISING OUT OF OR RELATING TO THE DATA OR ITS USE OR ANY OTHER PERFORMANCE, WHETHER OR NOT AUTODESK HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH LOSS OR DAMAGE. **************************************************************************************/ // // The example illustrates how to: // 1) get normals of mesh. // 2) get smoothing info of mesh. // 3) compute smoothing info from normals. // 4) convert hard/soft edges info to smoothing group info. // //Background knowledge: //There are two kinds of smoothing info: //1. Smoothing groups info which is saved by polygon. It usually come from 3ds Max, because 3ds Max can set smoothing groups for polygon. //2. Hard/soft edges info which is saved by edge. It usually come from Maya, because Maya can set hard/soft edges. // //steps: // 1. initialize FBX sdk object. // 2. load fbx scene form the specified file. // 3. Get root node of the scene. // 4. Recursively traverse each node in the scene. // 5. Get normals of mesh, according to different mapping mode and reference mode. // 6. Recursively traverse each node in the scene. // 7. Computing smoothing info from normals or convert smoothing info // 8. Get smoothing info of mesh, according to different mapping mode and reference mode. // 9. Destroy all objects. // #include <fbxsdk.h> #include "../Common/Common.h" #define SAMPLE_FILENAME "Normals.fbx" //set pCompute true to compute smoothing from normals by default //set pConvertToSmoothingGroup true to convert hard/soft edge info to smoothing group info by default void GetSmoothing(KFbxSdkManager* pSdkManager, KFbxNode* pNode, bool pCompute = false, bool pConvertToSmoothingGroup = false); //get mesh normals info void GetNormals(KFbxNode* pNode); int main(int argc, char** argv) { KFbxSdkManager* lSdkManager = NULL; KFbxScene* lScene = NULL; bool lResult; // Prepare the FBX SDK. InitializeSdkObjects(lSdkManager, lScene); // Load the scene. // The example can take a FBX file as an argument. if(argc > 1) { printf("\n\nFile: %s\n\n", argv[1]); lResult = LoadScene(lSdkManager, lScene, argv[1]); } else { printf("\n\nFile: %s\n\n", SAMPLE_FILENAME); lResult = LoadScene(lSdkManager, lScene, SAMPLE_FILENAME); } if(lResult == false) { printf("\n\nAn error occurred while loading the scene..."); } else { if(!lScene) { K_ASSERT_MSG_NOW("null scene"); } //get root node of the fbx scene KFbxNode* lRootNode = lScene->GetRootNode(); //get normals info, if there're mesh in the scene GetNormals(lRootNode); //set me true to compute smoothing info from normals bool lComputeFromNormals = false; //set me true to convert hard/soft edges info to smoothing groups info bool lConvertToSmoothingGroup = false; //get smoothing info, if there're mesh in the scene GetSmoothing(lSdkManager, lRootNode, lComputeFromNormals, lConvertToSmoothingGroup); } //Destroy all objects created by the FBX SDK. DestroySdkObjects(lSdkManager); return 0; } //get mesh smoothing info //set pCompute true to compute smoothing from normals by default //set pConvertToSmoothingGroup true to convert hard/soft edge info to smoothing group info by default void GetSmoothing(KFbxSdkManager* pSdkManager, KFbxNode* pNode, bool pCompute, bool pConvertToSmoothingGroup) { if(!pNode || !pSdkManager) return; //get mesh KFbxMesh* lMesh = pNode->GetMesh(); if(lMesh) { //print mesh node name printf("current mesh node: %s\n", pNode->GetName()); //if there's no smoothing info in fbx file, but you still want to get smoothing info. //please compute smoothing info from normals. //Another case to recompute smoothing info from normals is: //If users edit normals manually in 3ds Max or Maya and export the scene to FBX with smoothing info, //The smoothing info may NOT match with normals. //the mesh called "fbx_customNormals" in Normals.fbx is the case. All edges are hard, but normals actually represent the "soft" looking. //Generally, the normals in fbx file holds the smoothing result you'd like to get. //If you want to get correct smoothing info(smoothing group or hard/soft edges) which match with normals, //please drop the original smoothing info of fbx file, and recompute smoothing info from normals. //if you want to get soft/hard edge info, please call KFbxGeometryConverter::ComputeEdgeSmoothingFromNormals(). //if you want to get smoothing group info, please get soft/hard edge info first by ComputeEdgeSmoothingFromNormals() //And then call KFbxGeometryConverter::ComputePolygonSmoothingFromEdgeSmoothing(). if(pCompute) { KFbxGeometryConverter lGeometryConverter(pSdkManager); lGeometryConverter.ComputeEdgeSmoothingFromNormals(lMesh); //convert soft/hard edge info to smoothing group info if(pConvertToSmoothingGroup) lGeometryConverter.ComputePolygonSmoothingFromEdgeSmoothing(lMesh); } //if there is smoothing groups info in your fbx file, but you want to get hard/soft edges info //please use following code: //KFbxGeometryConverter lGeometryConverter(lSdkManager); //lGeometryConverter.ComputeEdgeSmoothingFromPolygonSmoothing(lMesh); //get smoothing info KFbxGeometryElementSmoothing* lSmoothingElement = lMesh->GetElementSmoothing(); if(lSmoothingElement) { //mapping mode is by edge. The mesh usually come from Maya, because Maya can set hard/soft edges. //we can get smoothing info(which edges are soft, which edges are hard) by retrieving each edge. if( lSmoothingElement->GetMappingMode() == KFbxGeometryElement::eBY_EDGE ) { //Let's get smoothing of each edge, since the mapping mode of smoothing element is by edge for(int lEdgeIndex = 0; lEdgeIndex < lMesh->GetMeshEdgeCount(); lEdgeIndex++) { int lSmoothingIndex = 0; //reference mode is direct, the smoothing index is same as edge index. //get smoothing by the index of edge if( lSmoothingElement->GetReferenceMode() == KFbxGeometryElement::eDIRECT ) lSmoothingIndex = lEdgeIndex; //reference mode is index-to-direct, get smoothing by the index-to-direct if(lSmoothingElement->GetReferenceMode() == KFbxGeometryElement::eINDEX_TO_DIRECT) lSmoothingIndex = lSmoothingElement->GetIndexArray().GetAt(lEdgeIndex); //Got smoothing of each vertex. int lSmoothingFlag = lSmoothingElement->GetDirectArray().GetAt(lSmoothingIndex); printf("hard/soft value for edge[%d]: %d \n", lEdgeIndex, lSmoothingFlag); //add your custom code here, to output smoothing or get them into a list, such as KArrayTemplate<int> //. . . }//end for lEdgeIndex }//end eBY_EDGE //mapping mode is by polygon. The mesh usually come from 3ds Max, because 3ds Max can set smoothing groups for polygon. //we can get smoothing info(smoothing group ID for each polygon) by retrieving each polygon. else if(lSmoothingElement->GetMappingMode() == KFbxGeometryElement::eBY_POLYGON) { //Let's get smoothing of each polygon, since the mapping mode of smoothing element is by polygon. for(int lPolygonIndex = 0; lPolygonIndex < lMesh->GetPolygonCount(); lPolygonIndex++) { int lSmoothingIndex = 0; //reference mode is direct, the smoothing index is same as polygon index. if( lSmoothingElement->GetReferenceMode() == KFbxGeometryElement::eDIRECT ) lSmoothingIndex = lPolygonIndex; //reference mode is index-to-direct, get smoothing by the index-to-direct if(lSmoothingElement->GetReferenceMode() == KFbxGeometryElement::eINDEX_TO_DIRECT) lSmoothingIndex = lSmoothingElement->GetIndexArray().GetAt(lPolygonIndex); //Got smoothing of each polygon. int lSmoothingFlag = lSmoothingElement->GetDirectArray().GetAt(lSmoothingIndex); printf("smoothing group ID for polygon[%d]: %d \n", lPolygonIndex, lSmoothingFlag); //add your custom code here, to output normals or get them into a list, such as KArrayTemplate<int> //. . . }//end for lPolygonIndex //PolygonCount }//end eBY_POLYGON_VERTEX }//end if lSmoothingElement }//end if lMesh //recursively traverse each node in the scene int i, lCount = pNode->GetChildCount(); for (i = 0; i < lCount; i++) { GetSmoothing(pSdkManager, pNode->GetChild(i), pCompute, pConvertToSmoothingGroup); } } //get mesh normals info void GetNormals(KFbxNode* pNode) { if(!pNode) return; //get mesh KFbxMesh* lMesh = pNode->GetMesh(); if(lMesh) { //print mesh node name printf("current mesh node: %s\n", pNode->GetName()); //get the normal element KFbxGeometryElementNormal* lNormalElement = lMesh->GetElementNormal(); if(lNormalElement) { //mapping mode is by control points. The mesh should be smooth and soft. //we can get normals by retrieving each control point if( lNormalElement->GetMappingMode() == KFbxGeometryElement::eBY_CONTROL_POINT ) { //Let's get normals of each vertex, since the mapping mode of normal element is by control point for(int lVertexIndex = 0; lVertexIndex < lMesh->GetControlPointsCount(); lVertexIndex++) { int lNormalIndex = 0; //reference mode is direct, the normal index is same as vertex index. //get normals by the index of control vertex if( lNormalElement->GetReferenceMode() == KFbxGeometryElement::eDIRECT ) lNormalIndex = lVertexIndex; //reference mode is index-to-direct, get normals by the index-to-direct if(lNormalElement->GetReferenceMode() == KFbxGeometryElement::eINDEX_TO_DIRECT) lNormalIndex = lNormalElement->GetIndexArray().GetAt(lVertexIndex); //Got normals of each vertex. KFbxVector4 lNormal = lNormalElement->GetDirectArray().GetAt(lNormalIndex); printf("normals for vertex[%d]: %f %f %f %f \n", lVertexIndex, lNormal[0], lNormal[1], lNormal[2], lNormal[3]); //add your custom code here, to output normals or get them into a list, such as KArrayTemplate<KFbxVector4> //. . . }//end for lVertexIndex }//end eBY_CONTROL_POINT //mapping mode is by polygon-vertex. //we can get normals by retrieving polygon-vertex. else if(lNormalElement->GetMappingMode() == KFbxGeometryElement::eBY_POLYGON_VERTEX) { int lIndexByPolygonVertex = 0; //Let's get normals of each polygon, since the mapping mode of normal element is by polygon-vertex. for(int lPolygonIndex = 0; lPolygonIndex < lMesh->GetPolygonCount(); lPolygonIndex++) { //get polygon size, you know how many vertices in current polygon. int lPolygonSize = lMesh->GetPolygonSize(lPolygonIndex); //retrieve each vertex of current polygon. for(int i = 0; i < lPolygonSize; i++) { int lNormalIndex = 0; //reference mode is direct, the normal index is same as lIndexByPolygonVertex. if( lNormalElement->GetReferenceMode() == KFbxGeometryElement::eDIRECT ) lNormalIndex = lIndexByPolygonVertex; //reference mode is index-to-direct, get normals by the index-to-direct if(lNormalElement->GetReferenceMode() == KFbxGeometryElement::eINDEX_TO_DIRECT) lNormalIndex = lNormalElement->GetIndexArray().GetAt(lIndexByPolygonVertex); //Got normals of each polygon-vertex. KFbxVector4 lNormal = lNormalElement->GetDirectArray().GetAt(lNormalIndex); printf("normals for polygon[%d]vertex[%d]: %f %f %f %f \n", lPolygonIndex, i, lNormal[0], lNormal[1], lNormal[2], lNormal[3]); //add your custom code here, to output normals or get them into a list, such as KArrayTemplate<KFbxVector4> //. . . lIndexByPolygonVertex++; }//end for i //lPolygonSize }//end for lPolygonIndex //PolygonCount }//end eBY_POLYGON_VERTEX }//end if lNormalElement }//end if lMesh //recursively traverse each node in the scene int i, lCount = pNode->GetChildCount(); for (i = 0; i < lCount; i++) { GetNormals(pNode->GetChild(i)); } }