diff --git a/CMakeLists.txt b/CMakeLists.txt index 0e57c3d..1774a51 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ -if(APPLE) - cmake_minimum_required(VERSION 3.9.6) -else() - cmake_minimum_required(VERSION 3.6) +if(APPLE) + cmake_minimum_required(VERSION 3.9.6) +else() + cmake_minimum_required(VERSION 3.6) endif() # Name of the project @@ -20,22 +20,23 @@ include_directories("ext/glad/include") # Set the executable. add_executable(${CMAKE_PROJECT_NAME} ${SOURCES} ${HEADERS} ${GLSL}) -# Set the default target for VS and Xcode -if(APPLE) - set(CMAKE_XCODE_GENERATE_SCHEME TRUE) -else() - set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT lab) -endif() - +# Set the default target for VS and Xcode +if(APPLE) + set(CMAKE_XCODE_GENERATE_SCHEME TRUE) +else() + set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT lab) +endif() + # Add GLFW # Get the GLFW environment variable. # There should be a CMakeLists.txt in the specified directory. -set(GLFW_DIR "$ENV{GLFW_DIR}") -# If there is no environment variable, search locally -if(NOT GLFW_DIR) - set(GLFW_DIR "ext/glfw-3.3-prerelease") +set(GLFW_DIR "$ENV{GLFW_DIR}") +set(FBX_INCLUDE_DIR "$ENV{FBX_INCLUDE_DIR}") +# If there is no environment variable, search locally +if(NOT GLFW_DIR) + set(GLFW_DIR "ext/glfw-3.3-prerelease") endif() if(GLFW_DIR) message(STATUS "GLFW environment variable found") @@ -73,10 +74,10 @@ endif() # Add GLM # Get the GLM environment variable. Since GLM is a header-only library, we # just need to add it to the include directory. -set(GLM_INCLUDE_DIR "$ENV{GLM_INCLUDE_DIR}") -# If there is no environment variable, search locally -if(NOT GLM_INCLUDE_DIR) - set(GLM_INCLUDE_DIR "ext/glm-0.9.8.3") +set(GLM_INCLUDE_DIR "$ENV{GLM_INCLUDE_DIR}") +# If there is no environment variable, search locally +if(NOT GLM_INCLUDE_DIR) + set(GLM_INCLUDE_DIR "ext/glm-0.9.8.3") endif() if(GLM_INCLUDE_DIR) include_directories(${GLM_INCLUDE_DIR}) @@ -95,6 +96,30 @@ if(WIN32) # -Wall produces way too many warnings. # -pedantic is not supported. target_link_libraries(${CMAKE_PROJECT_NAME} opengl32.lib) + # Add FBX + # If there is no environment variable, search locally + if(NOT FBX_INCLUDE_DIR) + set(FBX_INCLUDE_DIR "C:/Program\ Files/Autodesk/FBX/FBX\ SDK/2019.0/include") + endif() + set(FBX_LIBRARY_DIR "C:/Program\ Files/Autodesk/FBX/FBX\ SDK/2019.0/lib/vs2015/x86/debug") + + if(FBX_INCLUDE_DIR) + include_directories(${FBX_INCLUDE_DIR}) + target_link_libraries(${CMAKE_PROJECT_NAME} ${FBX_LIBRARY_DIR}/libfbxsdk.lib) + message(STATUS "FBX environment variable `FBX_INCLUDE_DIR` not found, FBX must be installed with the system") + else() + message(STATUS "FBX environment variable `FBX_INCLUDE_DIR` not found, FBX must be installed with the system") + endif() + + set(dllLoc ${FBX_LIBRARY_DIR}/libfbxsdk.dll) + set(dest ${CMAKE_CURRENT_BINARY_DIR}/libfbxsdk.dll) + add_custom_command( + TARGET ${CMAKE_PROJECT_NAME} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy ${dllLoc} ${dest} + DEPENDS ${dest} + COMMENT "copies over dll for fbx sdk to build directory so it can be used during execution" + ) + else() # Enable all pedantic warnings. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x -Wall -pedantic") @@ -105,5 +130,25 @@ else() else() #Link the Linux OpenGL library target_link_libraries(${CMAKE_PROJECT_NAME} "GL" "dl") + set(THREADS_PREFER_PTHREAD_FLAG ON) + find_package(Threads REQUIRED) + target_link_libraries(${CMAKE_PROJECT_NAME} Threads::Threads) + + # If there is no environment variable, search locally + if(NOT FBX_INCLUDE_DIR) + set(FBX_INCLUDE_DIR "/usr/include/fbxsdk") + endif() + set(FBX_LIBRARY "/usr/lib/gcc4/x64/release/libfbxsdk.a") + + + if(FBX_INCLUDE_DIR) + include_directories(${FBX_INCLUDE_DIR}) + target_link_libraries(${CMAKE_PROJECT_NAME} ${FBX_LIBRARY} ${CMAKE_DL_LIBS}) + message(STATUS "FBX environment variable found") + else() + # If the FBX_INCLUDE_DIR environment variable is not set, we assume + # the user has installed FBX properly on their system + message(STATUS "FBX environment variable `FBX_INCLUDE_DIR` not found, FBX must be installed with the system") + endif() endif() endif() diff --git a/resources/test.fbx b/resources/test.fbx new file mode 100644 index 0000000..dcbfba9 Binary files /dev/null and b/resources/test.fbx differ diff --git a/src/Program.cpp b/src/Program.cpp index 1e76eb1..909ae76 100755 --- a/src/Program.cpp +++ b/src/Program.cpp @@ -1,6 +1,7 @@ #include "Program.h" #include #include +#include #include "GLSL.h" void Program::setShaderNames(const std::string &v, const std::string &f, const std::string &g) { diff --git a/src/bone.h b/src/bone.h new file mode 100644 index 0000000..d035a3c --- /dev/null +++ b/src/bone.h @@ -0,0 +1,28 @@ +#pragma once +// value_ptr for glm +#include +#include +using namespace glm; +using namespace std; +class bone +{ +public: + string name; + vec3 pos; + quat q; + bone *parent = NULL; + vector kids; + int index; + mat4 *mat = NULL; + void write_to_VBO(vec3 origin,vector &vpos) + { + vpos.push_back(origin); + vec3 endp = origin + pos; + vpos.push_back(endp); + for (int i = 0; i < kids.size(); i++) + kids[i]->write_to_VBO(endp, vpos); + + } + +}; +int readtobone(bone **root); \ No newline at end of file diff --git a/src/fbx_convert.cpp b/src/fbx_convert.cpp new file mode 100644 index 0000000..f98e936 --- /dev/null +++ b/src/fbx_convert.cpp @@ -0,0 +1,807 @@ +#include +#include +#include +#include +//#define KFBX_DLLINFO +#define FBXSDK_SHARED +//#pragma comment(lib, "C:/Program\ Files/Autodesk/FBX/FBX\ SDK/2019.0/lib/vs2015/x64/debug/libfbxsdk.lib") +#include +using namespace std; + +#define PIe 3.141592654 + + +#include +#include "bone.h" +using namespace glm; + +/* Tab character ("\t") counter */ +int numTabs = 0; + +/** +* Print the required number of tabs. +*/ +void PrintTabs(FILE *file) { + for (int i = 0; i < numTabs; i++) + fprintf(file,"\t"); +} + +/** +* Return a string-based representation based on the attribute type. +*/ +FbxString GetAttributeTypeName(FbxNodeAttribute::EType type) { + + ifstream g; + switch (type) { + case FbxNodeAttribute::eUnknown: return "unidentified"; + case FbxNodeAttribute::eNull: return "null"; + case FbxNodeAttribute::eMarker: return "marker"; + case FbxNodeAttribute::eSkeleton: return "skeleton"; + case FbxNodeAttribute::eMesh: return "mesh"; + case FbxNodeAttribute::eNurbs: return "nurbs"; + case FbxNodeAttribute::ePatch: return "patch"; + case FbxNodeAttribute::eCamera: return "camera"; + case FbxNodeAttribute::eCameraStereo: return "stereo"; + case FbxNodeAttribute::eCameraSwitcher: return "camera switcher"; + case FbxNodeAttribute::eLight: return "light"; + case FbxNodeAttribute::eOpticalReference: return "optical reference"; + case FbxNodeAttribute::eOpticalMarker: return "marker"; + case FbxNodeAttribute::eNurbsCurve: return "nurbs curve"; + case FbxNodeAttribute::eTrimNurbsSurface: return "trim nurbs surface"; + case FbxNodeAttribute::eBoundary: return "boundary"; + case FbxNodeAttribute::eNurbsSurface: return "nurbs surface"; + case FbxNodeAttribute::eShape: return "shape"; + case FbxNodeAttribute::eLODGroup: return "lodgroup"; + case FbxNodeAttribute::eSubDiv: return "subdiv"; + default: return "unknown"; + } +} + +/** +* Print an attribute. +*/ +void PrintAttribute(FILE *file,FbxNodeAttribute* pAttribute) { + if (!pAttribute) return; + + FbxString typeName = GetAttributeTypeName(pAttribute->GetAttributeType()); + FbxString attrName = pAttribute->GetName(); + PrintTabs(file); + // Note: to retrieve the character array of a FbxString, use its Buffer() method. + fprintf(file,"\n", typeName.Buffer(), attrName.Buffer()); +} + +/** +* Print a node, its attributes, and all its children recursively. +*/ +bone *root=NULL; +int indexcount = 0; +void PrintNode(bone *actual, FbxNode* pNode, int lastlevel) +{ + + + actual->index = indexcount++; + //actual->mat = &modelmat[actual->index]; + + int level = lastlevel +1; + cout << "bonename: " << pNode->GetName() << endl; + cout << "bone level: " << level << endl; + + actual->name = pNode->GetName(); + + FbxDouble3 bone_vector = pNode->LclTranslation.Get(); + + float t1, t2, t3; + t1 = bone_vector[0]; + t2 = bone_vector[1]; + t3= bone_vector[2]; + cout << "bone vector (x,y,z): " << t1 << ", " << t2 << ", " << t3 << endl; + actual->pos.x = t1; + actual->pos.y = t2; + actual->pos.z = t3; + // Get the node's default local transformation matrix. + FbxAMatrix& lLocalTransform = pNode->EvaluateLocalTransform(); + + FbxDouble3 rotation = pNode->LclRotation.Get(); + FbxDouble3 scaling = pNode->LclScaling.Get(); + + float u0, u1, u2, u3; + float e1, e2, e3; + e1 = rotation[0]; + e2 = rotation[1]; + e3 = rotation[2]; + + u0 = sqrt(cos(e2*PIe / 180)*cos(e1*PIe / 180) + cos(e2*PIe / 180)*cos(e3*PIe / 180) - sin(e2*PIe / 180)*sin(e1*PIe / 180)*sin(e3*PIe / 180) + cos(e1*PIe / 180)* cos(e3*PIe / 180) + 1) / 2; + u1 = (cos(e1*PIe / 180)*sin(e3*PIe / 180) + cos(e2*PIe / 180)*sin(e3*PIe / 180) + sin(e2*PIe / 180)*sin(e1*PIe / 180)*cos(e3*PIe / 180)) / sqrt(cos(e2*PIe / 180)* cos(e1*PIe / 180) + cos(e2*PIe / 180)*cos(e3*PIe / 180) - sin(e2*PIe / 180)*sin(e1*PIe / 180)*sin(e3*PIe / 180) + cos(e1*PIe / 180)*cos(e3*PIe / 180) + 1) / 2; + u2 = (sin(e2*PIe / 180)*sin(e3*PIe / 180) - cos(e2*PIe / 180)*sin(e1*PIe / 180)*cos(e3*PIe / 180) - sin(e1*PIe / 180)) / sqrt(cos(e2*PIe / 180)*cos(e1*PIe / 180) + cos(e2*PIe / 180)*cos(e3*PIe / 180) - sin(e2*PIe / 180)*sin(e1*PIe / 180)*sin(e3*PIe / 180) + cos(e1*PIe / 180)*cos(e3*PIe / 180) + 1) / 2; + u3 = (sin(e2*PIe / 180)*cos(e1*PIe / 180) + sin(e2*PIe / 180)*cos(e3*PIe / 180) + cos(e2*PIe / 180)*sin(e1*PIe / 180)*sin(e3*PIe / 180)) / sqrt(cos(e2*PIe / 180)* cos(e1*PIe / 180) + cos(e2*PIe / 180)*cos(e3*PIe / 180) - sin(e2*PIe / 180)*sin(e1*PIe / 180)*sin(e3*PIe / 180) + cos(e1*PIe / 180)*cos(e3*PIe / 180) + 1) / 2; + + cout << "bone quaternion (i,j,k,re): " << u1 << ", " << u2 << ", " << u3 << ", " << u0 << endl; + actual->q.x = u1; + actual->q.y = u2; + actual->q.z = u3; + actual->q.w = u0; + + + + int child_count = pNode->GetChildCount(); + cout << "bone children count: " << child_count << endl; + cout << "bone children names:" << endl; + for (int j = 0; j < pNode->GetChildCount(); j++) + { + + cout << "\t" << pNode->GetChild(j)->GetName() << endl; + } + + //for (int i = 0; i < pNode->GetNodeAttributeCount(); i++) + // PrintAttribute(file,pNode->GetNodeAttributeByIndex(i)); + + // Recursively print the children. + for (int j = 0; j < pNode->GetChildCount(); j++) + { + bone *k = new bone; + actual->kids.push_back(k); + k->parent = actual; + PrintNode(k,pNode->GetChild(j), level); + } + + +} + +void CountBones(FbxNode* pNode, int &count) +{ + count++; + for (int j = 0; j < pNode->GetChildCount(); j++) + { + CountBones(pNode->GetChild(j), count); + } + +} + +void DisplayAnimation(FbxAnimStack* pAnimStack, FbxNode* pNode, bool isSwitcher = false); +void DisplayAnimation(FbxAnimLayer* pAnimLayer, FbxNode* pNode, bool isSwitcher = false); + +void DisplayChannels(FbxNode* pNode, FbxAnimLayer* pAnimLayer, void(*DisplayCurve) (FbxAnimCurve* pCurve), void(*DisplayListCurve) (FbxAnimCurve* pCurve, FbxProperty* pProperty), bool isSwitcher); +void DisplayCurveKeys(FbxAnimCurve* pCurve); +void DisplayListCurveKeys(FbxAnimCurve* pCurve, FbxProperty* pProperty); +void PrintAnimationData( FbxScene* lScene); +void CalcTransRotAnim(FbxScene* lScene, FbxNode* lNode, int animno) +{ + FbxAnimStack* currAnimStack = lScene->GetSrcObject(animno); + FbxString animStackName = currAnimStack->GetName(); + FbxString mAnimationName = animStackName.Buffer(); + FbxTakeInfo* takeInfo = lScene->GetTakeInfo(animStackName); + FbxTime start = takeInfo->mLocalTimeSpan.GetStart(); + FbxTime end = takeInfo->mLocalTimeSpan.GetStop(); + long long duration = end.GetMilliSeconds(); + int keyframecount = end.GetFrameCount(FbxTime::eFrames24) - start.GetFrameCount(FbxTime::eFrames24) + 1; + + const char* nodeName = lNode->GetName(); + cout << endl << "\t" << "bone name: " << nodeName << endl << endl; + + for (FbxLongLong i = start.GetFrameCount(FbxTime::eFrames24); i <= end.GetFrameCount(FbxTime::eFrames24); ++i) + { + FbxTime currTime; + currTime.SetFrame(i, FbxTime::eFrames24); + long long time_ms=currTime.GetMilliSeconds(); + cout << "\t" << "\t" << "frame time stamp (ms): " << time_ms << endl; + FbxDouble3 translation = lNode->EvaluateLocalTranslation(currTime); + float t1, t2, t3; + t1 = translation[0]; + t2 = translation[1]; + t3 = translation[2]; + cout << "\t" << "\t" << "translation (x,y,z): " << t1 << ", " << t2 << ", " << t3 << endl; + + FbxDouble3 rotation = lNode->EvaluateLocalRotation(currTime); + FbxAMatrix& loctraf = lNode->EvaluateLocalTransform(currTime); + + float e1, e2, e3; + e1 = rotation[0]; + e2 = rotation[1]; + e3 = rotation[2]; + + float u0, u1, u2, u3; + + u0 = sqrt(cos(e2*PIe / 180)*cos(e1*PIe / 180) + cos(e2*PIe / 180)*cos(e3*PIe / 180) - sin(e2*PIe / 180)*sin(e1*PIe / 180)*sin(e3*PIe / 180) + cos(e1*PIe / 180)* cos(e3*PIe / 180) + 1) / 2; + u1 = (cos(e1*PIe / 180)*sin(e3*PIe / 180) + cos(e2*PIe / 180)*sin(e3*PIe / 180) + sin(e2*PIe / 180)*sin(e1*PIe / 180)*cos(e3*PIe / 180)) / sqrt(cos(e2*PIe / 180)* cos(e1*PIe / 180) + cos(e2*PIe / 180)*cos(e3*PIe / 180) - sin(e2*PIe / 180)*sin(e1*PIe / 180)*sin(e3*PIe / 180) + cos(e1*PIe / 180)*cos(e3*PIe / 180) + 1) / 2; + u2 = (sin(e2*PIe / 180)*sin(e3*PIe / 180) - cos(e2*PIe / 180)*sin(e1*PIe / 180)*cos(e3*PIe / 180) - sin(e1*PIe / 180)) / sqrt(cos(e2*PIe / 180)*cos(e1*PIe / 180) + cos(e2*PIe / 180)*cos(e3*PIe / 180) - sin(e2*PIe / 180)*sin(e1*PIe / 180)*sin(e3*PIe / 180) + cos(e1*PIe / 180)*cos(e3*PIe / 180) + 1) / 2; + u3 = (sin(e2*PIe / 180)*cos(e1*PIe / 180) + sin(e2*PIe / 180)*cos(e3*PIe / 180) + cos(e2*PIe / 180)*sin(e1*PIe / 180)*sin(e3*PIe / 180)) / sqrt(cos(e2*PIe / 180)* cos(e1*PIe / 180) + cos(e2*PIe / 180)*cos(e3*PIe / 180) - sin(e2*PIe / 180)*sin(e1*PIe / 180)*sin(e3*PIe / 180) + cos(e1*PIe / 180)*cos(e3*PIe / 180) + 1) / 2; + + u3 = -u3; + u0 = -u0; + + e1 = e1*PIe / 180; + e2 = e2*PIe / 180; + e3 = e3*PIe / 180; + + float q0, q1, q2, q3; + q0 = -(cos(e1 / 2)*cos(e2 / 2)*cos(e3 / 2) + sin(e1 / 2)*sin(e2 / 2)*sin(e3 / 2)); + q1 = -(sin(e1 / 2)*cos(e2 / 2)*cos(e3 / 2) - cos(e1 / 2)*sin(e2 / 2)*sin(e3 / 2)); + q2 =-( cos(e1 / 2)*sin(e2 / 2)*cos(e3 / 2) + sin(e1 / 2)*cos(e2 / 2)*sin(e3 / 2)); + q3 =-( cos(e1 / 2)*cos(e2 / 2)*sin(e3 / 2) - sin(e1 / 2)*sin(e2 / 2)*cos(e3 / 2)); + + cout << "\t" << "\t" << "quaternion (i,j,k,re): " << q1 << ", " << q2 << ", " << q3 << ", " << q0 << endl; + +} +for (int k = 0; k < lNode->GetChildCount();k++) + CalcTransRotAnim( lScene, lNode->GetChild(k), animno); + +} + + + +//*************************************************************************************************************************************************************** +void PrintAnimationData(FbxScene* lScene) +{ + int i; + + int count_animations = lScene->GetSrcObjectCount(); + + ////falls es keine animation gibt + if (count_animations == 0) + { + cout << "no animations in file" << endl; + return; + } + + FbxNode* lNode = lScene->GetRootNode(); + for (int l = 0; l < count_animations; l++) + { + FbxAnimStack* currAnimStack = lScene->GetSrcObject(l); + FbxString animStackName = currAnimStack->GetName(); + FbxString mAnimationName = animStackName.Buffer(); + FbxTakeInfo* takeInfo = lScene->GetTakeInfo(animStackName); + FbxTime start = takeInfo->mLocalTimeSpan.GetStart(); + FbxTime end = takeInfo->mLocalTimeSpan.GetStop(); + long long duration = end.GetMilliSeconds(); + int keyframecount = end.GetFrameCount(FbxTime::eFrames24) - start.GetFrameCount(FbxTime::eFrames24) + 1; + + cout << endl; + cout << "animation name: " << mAnimationName << endl; + cout << "key frame count: " << keyframecount << endl; + cout << "animation duration (ms): " << duration << endl; + for (int k = 0; k < lNode->GetChildCount(); k++) + CalcTransRotAnim(lScene, lNode->GetChild(k), l); + } +} +//*************************************************************************************************************************************************************** +void DisplayAnimation(FbxScene* pScene) +{ + int i; + for (i = 0; i < pScene->GetSrcObjectCount(); i++) + { + FbxAnimStack* lAnimStack = pScene->GetSrcObject(i); + FbxString lOutputString = "Animation Stack Name: "; + lOutputString += lAnimStack->GetName(); + lOutputString += "\n\n"; + FBXSDK_printf(lOutputString); + DisplayAnimation(lAnimStack, pScene->GetRootNode(), true); + DisplayAnimation(lAnimStack, pScene->GetRootNode()); + } +} +void DisplayAnimation(FbxAnimStack* pAnimStack, FbxNode* pNode, bool isSwitcher) +{ + int l; + int nbAnimLayers = pAnimStack->GetMemberCount(); + FbxString lOutputString; + lOutputString = "Animation stack contains "; + lOutputString += nbAnimLayers; + lOutputString += " Animation Layer(s)\n"; + FBXSDK_printf(lOutputString); + for (l = 0; l < nbAnimLayers; l++) + { + FbxAnimLayer* lAnimLayer = pAnimStack->GetMember(l); + lOutputString = "AnimLayer "; + lOutputString += l; + lOutputString += "\n"; + FBXSDK_printf(lOutputString); + DisplayAnimation(lAnimLayer, pNode, isSwitcher); + } +} +void DisplayAnimation(FbxAnimLayer* pAnimLayer, FbxNode* pNode, bool isSwitcher) +{ + int lModelCount; + FbxString lOutputString; + lOutputString = " Node Name: "; + lOutputString += pNode->GetName(); + lOutputString += "\n\n"; + FBXSDK_printf(lOutputString); + DisplayChannels(pNode, pAnimLayer, DisplayCurveKeys, DisplayListCurveKeys, isSwitcher); + FBXSDK_printf("\n"); + for (lModelCount = 0; lModelCount < pNode->GetChildCount(); lModelCount++) + { + DisplayAnimation(pAnimLayer, pNode->GetChild(lModelCount), isSwitcher); + } +} +void DisplayChannels(FbxNode* pNode, FbxAnimLayer* pAnimLayer, void(*DisplayCurve) (FbxAnimCurve* pCurve), void(*DisplayListCurve) (FbxAnimCurve* pCurve, FbxProperty* pProperty), bool isSwitcher) +{ + FbxAnimCurve* lAnimCurve = NULL; + // Display general curves. + if (!isSwitcher) + { + lAnimCurve = pNode->LclTranslation.GetCurve(pAnimLayer, FBXSDK_CURVENODE_COMPONENT_X); + + if (lAnimCurve) + { + //FbxDouble3 rotation = pNode->Lcl.Get(); + //FbxDouble3 scaling = pNode->LclScaling.Get(); + FBXSDK_printf(" TX\n"); + DisplayCurve(lAnimCurve); + } + lAnimCurve = pNode->LclTranslation.GetCurve(pAnimLayer, FBXSDK_CURVENODE_COMPONENT_Y); + if (lAnimCurve) + { + FBXSDK_printf(" TY\n"); + DisplayCurve(lAnimCurve); + } + lAnimCurve = pNode->LclTranslation.GetCurve(pAnimLayer, FBXSDK_CURVENODE_COMPONENT_Z); + if (lAnimCurve) + { + FBXSDK_printf(" TZ\n"); + DisplayCurve(lAnimCurve); + } + lAnimCurve = pNode->LclRotation.GetCurve(pAnimLayer, FBXSDK_CURVENODE_COMPONENT_X); + if (lAnimCurve) + { + FBXSDK_printf(" RX\n"); + DisplayCurve(lAnimCurve); + } + lAnimCurve = pNode->LclRotation.GetCurve(pAnimLayer, FBXSDK_CURVENODE_COMPONENT_Y); + if (lAnimCurve) + { + FBXSDK_printf(" RY\n"); + DisplayCurve(lAnimCurve); + } + lAnimCurve = pNode->LclRotation.GetCurve(pAnimLayer, FBXSDK_CURVENODE_COMPONENT_Z); + if (lAnimCurve) + { + FBXSDK_printf(" RZ\n"); + DisplayCurve(lAnimCurve); + } + lAnimCurve = pNode->LclScaling.GetCurve(pAnimLayer, FBXSDK_CURVENODE_COMPONENT_X); + if (lAnimCurve) + { + FBXSDK_printf(" SX\n"); + DisplayCurve(lAnimCurve); + } + lAnimCurve = pNode->LclScaling.GetCurve(pAnimLayer, FBXSDK_CURVENODE_COMPONENT_Y); + if (lAnimCurve) + { + FBXSDK_printf(" SY\n"); + DisplayCurve(lAnimCurve); + } + lAnimCurve = pNode->LclScaling.GetCurve(pAnimLayer, FBXSDK_CURVENODE_COMPONENT_Z); + if (lAnimCurve) + { + FBXSDK_printf(" SZ\n"); + DisplayCurve(lAnimCurve); + } + } + // Display curves specific to a light or marker. + FbxNodeAttribute* lNodeAttribute = pNode->GetNodeAttribute(); + if (lNodeAttribute) + { + lAnimCurve = lNodeAttribute->Color.GetCurve(pAnimLayer, FBXSDK_CURVENODE_COLOR_RED); + if (lAnimCurve) + { + FBXSDK_printf(" Red\n"); + DisplayCurve(lAnimCurve); + } + lAnimCurve = lNodeAttribute->Color.GetCurve(pAnimLayer, FBXSDK_CURVENODE_COLOR_GREEN); + if (lAnimCurve) + { + FBXSDK_printf(" Green\n"); + DisplayCurve(lAnimCurve); + } + lAnimCurve = lNodeAttribute->Color.GetCurve(pAnimLayer, FBXSDK_CURVENODE_COLOR_BLUE); + if (lAnimCurve) + { + FBXSDK_printf(" Blue\n"); + DisplayCurve(lAnimCurve); + } + // Display curves specific to a light. + FbxLight* light = pNode->GetLight(); + if (light) + { + lAnimCurve = light->Intensity.GetCurve(pAnimLayer); + if (lAnimCurve) + { + FBXSDK_printf(" Intensity\n"); + DisplayCurve(lAnimCurve); + } + lAnimCurve = light->OuterAngle.GetCurve(pAnimLayer); + if (lAnimCurve) + { + FBXSDK_printf(" Outer Angle\n"); + DisplayCurve(lAnimCurve); + } + lAnimCurve = light->Fog.GetCurve(pAnimLayer); + if (lAnimCurve) + { + FBXSDK_printf(" Fog\n"); + DisplayCurve(lAnimCurve); + } + } + // Display curves specific to a camera. + FbxCamera* camera = pNode->GetCamera(); + if (camera) + { + lAnimCurve = camera->FieldOfView.GetCurve(pAnimLayer); + if (lAnimCurve) + { + FBXSDK_printf(" Field of View\n"); + DisplayCurve(lAnimCurve); + } + lAnimCurve = camera->FieldOfViewX.GetCurve(pAnimLayer); + if (lAnimCurve) + { + FBXSDK_printf(" Field of View X\n"); + DisplayCurve(lAnimCurve); + } + lAnimCurve = camera->FieldOfViewY.GetCurve(pAnimLayer); + if (lAnimCurve) + { + FBXSDK_printf(" Field of View Y\n"); + DisplayCurve(lAnimCurve); + } + lAnimCurve = camera->OpticalCenterX.GetCurve(pAnimLayer); + if (lAnimCurve) + { + FBXSDK_printf(" Optical Center X\n"); + DisplayCurve(lAnimCurve); + } + lAnimCurve = camera->OpticalCenterY.GetCurve(pAnimLayer); + if (lAnimCurve) + { + FBXSDK_printf(" Optical Center Y\n"); + DisplayCurve(lAnimCurve); + } + lAnimCurve = camera->Roll.GetCurve(pAnimLayer); + if (lAnimCurve) + { + FBXSDK_printf(" Roll\n"); + DisplayCurve(lAnimCurve); + } + } + // Display curves specific to a geometry. + if (lNodeAttribute->GetAttributeType() == FbxNodeAttribute::eMesh || + lNodeAttribute->GetAttributeType() == FbxNodeAttribute::eNurbs || + lNodeAttribute->GetAttributeType() == FbxNodeAttribute::ePatch) + { + FbxGeometry* lGeometry = (FbxGeometry*)lNodeAttribute; + int lBlendShapeDeformerCount = lGeometry->GetDeformerCount(FbxDeformer::eBlendShape); + for (int lBlendShapeIndex = 0; lBlendShapeIndexGetDeformer(lBlendShapeIndex, FbxDeformer::eBlendShape); + int lBlendShapeChannelCount = lBlendShape->GetBlendShapeChannelCount(); + for (int lChannelIndex = 0; lChannelIndexGetBlendShapeChannel(lChannelIndex); + const char* lChannelName = lChannel->GetName(); + lAnimCurve = lGeometry->GetShapeChannel(lBlendShapeIndex, lChannelIndex, pAnimLayer, true); + if (lAnimCurve) + { + FBXSDK_printf(" Shape %s\n", lChannelName); + DisplayCurve(lAnimCurve); + } + } + } + } + } + // Display curves specific to properties + FbxProperty lProperty = pNode->GetFirstProperty(); + while (lProperty.IsValid()) + { + if (lProperty.GetFlag(FbxPropertyFlags::eUserDefined)) + { + FbxString lFbxFCurveNodeName = lProperty.GetName(); + FbxAnimCurveNode* lCurveNode = lProperty.GetCurveNode(pAnimLayer); + if (!lCurveNode) { + lProperty = pNode->GetNextProperty(lProperty); + continue; + } + FbxDataType lDataType = lProperty.GetPropertyDataType(); + if (lDataType.GetType() == eFbxBool || lDataType.GetType() == eFbxDouble || lDataType.GetType() == eFbxFloat || lDataType.GetType() == eFbxInt) + { + FbxString lMessage; + lMessage = " Property "; + lMessage += lProperty.GetName(); + if (lProperty.GetLabel().GetLen() > 0) + { + lMessage += " (Label: "; + lMessage += lProperty.GetLabel(); + lMessage += ")"; + }; + //DisplayString(lMessage.Buffer()); + for (int c = 0; c < lCurveNode->GetCurveCount(0U); c++) + { + lAnimCurve = lCurveNode->GetCurve(0U, c); + if (lAnimCurve) + DisplayCurve(lAnimCurve); + } + } + else if (lDataType.GetType() == eFbxDouble3 || lDataType.GetType() == eFbxDouble4 || lDataType.Is(FbxColor3DT) || lDataType.Is(FbxColor4DT)) + { + char* lComponentName1 = (lDataType.Is(FbxColor3DT) || lDataType.Is(FbxColor4DT)) ? (char*)FBXSDK_CURVENODE_COLOR_RED : (char*)"X"; + char* lComponentName2 = (lDataType.Is(FbxColor3DT) || lDataType.Is(FbxColor4DT)) ? (char*)FBXSDK_CURVENODE_COLOR_GREEN : (char*)"Y"; + char* lComponentName3 = (lDataType.Is(FbxColor3DT) || lDataType.Is(FbxColor4DT)) ? (char*)FBXSDK_CURVENODE_COLOR_BLUE : (char*)"Z"; + FbxString lMessage; + + lMessage = " Property "; + lMessage += lProperty.GetName(); + if (lProperty.GetLabel().GetLen() > 0) + { + lMessage += " (Label: "; + lMessage += lProperty.GetLabel(); + lMessage += ")"; + } + //DisplayString(lMessage.Buffer()); + for (int c = 0; c < lCurveNode->GetCurveCount(0U); c++) + { + lAnimCurve = lCurveNode->GetCurve(0U, c); + if (lAnimCurve) + { + //DisplayString(" Component ", lComponentName1); + DisplayCurve(lAnimCurve); + } + } + for (int c = 0; c < lCurveNode->GetCurveCount(1U); c++) + { + lAnimCurve = lCurveNode->GetCurve(1U, c); + if (lAnimCurve) + { + //DisplayString(" Component ", lComponentName2); + DisplayCurve(lAnimCurve); + } + } + for (int c = 0; c < lCurveNode->GetCurveCount(2U); c++) + { + lAnimCurve = lCurveNode->GetCurve(2U, c); + if (lAnimCurve) + { + //DisplayString(" Component ", lComponentName3); + DisplayCurve(lAnimCurve); + } + } + } + else if (lDataType.GetType() == eFbxEnum) + { + FbxString lMessage; + lMessage = " Property "; + lMessage += lProperty.GetName(); + if (lProperty.GetLabel().GetLen() > 0) + { + lMessage += " (Label: "; + lMessage += lProperty.GetLabel(); + lMessage += ")"; + }; + //DisplayString(lMessage.Buffer()); + for (int c = 0; c < lCurveNode->GetCurveCount(0U); c++) + { + lAnimCurve = lCurveNode->GetCurve(0U, c); + if (lAnimCurve) + DisplayListCurve(lAnimCurve, &lProperty); + } + } + } + lProperty = pNode->GetNextProperty(lProperty); + } // while +} +static int InterpolationFlagToIndex(int flags) +{ + if ((flags & FbxAnimCurveDef::eInterpolationConstant) == FbxAnimCurveDef::eInterpolationConstant) return 1; + if ((flags & FbxAnimCurveDef::eInterpolationLinear) == FbxAnimCurveDef::eInterpolationLinear) return 2; + if ((flags & FbxAnimCurveDef::eInterpolationCubic) == FbxAnimCurveDef::eInterpolationCubic) return 3; + return 0; +} +static int ConstantmodeFlagToIndex(int flags) +{ + if ((flags & FbxAnimCurveDef::eConstantStandard) == FbxAnimCurveDef::eConstantStandard) return 1; + if ((flags & FbxAnimCurveDef::eConstantNext) == FbxAnimCurveDef::eConstantNext) return 2; + return 0; +} +static int TangentmodeFlagToIndex(int flags) +{ + if ((flags & FbxAnimCurveDef::eTangentAuto) == FbxAnimCurveDef::eTangentAuto) return 1; + if ((flags & FbxAnimCurveDef::eTangentAutoBreak) == FbxAnimCurveDef::eTangentAutoBreak) return 2; + if ((flags & FbxAnimCurveDef::eTangentTCB) == FbxAnimCurveDef::eTangentTCB) return 3; + if ((flags & FbxAnimCurveDef::eTangentUser) == FbxAnimCurveDef::eTangentUser) return 4; + if ((flags & FbxAnimCurveDef::eTangentGenericBreak) == FbxAnimCurveDef::eTangentGenericBreak) return 5; + if ((flags & FbxAnimCurveDef::eTangentBreak) == FbxAnimCurveDef::eTangentBreak) return 6; + return 0; +} +static int TangentweightFlagToIndex(int flags) +{ + if ((flags & FbxAnimCurveDef::eWeightedNone) == FbxAnimCurveDef::eWeightedNone) return 1; + if ((flags & FbxAnimCurveDef::eWeightedRight) == FbxAnimCurveDef::eWeightedRight) return 2; + if ((flags & FbxAnimCurveDef::eWeightedNextLeft) == FbxAnimCurveDef::eWeightedNextLeft) return 3; + return 0; +} +static int TangentVelocityFlagToIndex(int flags) +{ + if ((flags & FbxAnimCurveDef::eVelocityNone) == FbxAnimCurveDef::eVelocityNone) return 1; + if ((flags & FbxAnimCurveDef::eVelocityRight) == FbxAnimCurveDef::eVelocityRight) return 2; + if ((flags & FbxAnimCurveDef::eVelocityNextLeft) == FbxAnimCurveDef::eVelocityNextLeft) return 3; + return 0; +} +void DisplayCurveKeys(FbxAnimCurve* pCurve) +{ + static const char* interpolation[] = { "?", "constant", "linear", "cubic" }; + static const char* constantMode[] = { "?", "Standard", "Next" }; + static const char* cubicMode[] = { "?", "Auto", "Auto break", "Tcb", "User", "Break", "User break" }; + static const char* tangentWVMode[] = { "?", "None", "Right", "Next left" }; + FbxTime lKeyTime; + float lKeyValue; + char lTimeString[256]; + FbxString lOutputString; + int lCount; + int lKeyCount = pCurve->KeyGetCount(); + for (lCount = 0; lCount < lKeyCount; lCount++) + { + lKeyValue = static_cast(pCurve->KeyGetValue(lCount)); + lKeyTime = pCurve->KeyGetTime(lCount); + + lOutputString = " Key Time: "; + lOutputString += lKeyTime.GetTimeString(lTimeString, FbxUShort(256)); + lOutputString += ".... Key Value: "; + lOutputString += lKeyValue; + lOutputString += " [ "; + lOutputString += interpolation[InterpolationFlagToIndex(pCurve->KeyGetInterpolation(lCount))]; + if ((pCurve->KeyGetInterpolation(lCount)&FbxAnimCurveDef::eInterpolationConstant) == FbxAnimCurveDef::eInterpolationConstant) + { + lOutputString += " | "; + lOutputString += constantMode[ConstantmodeFlagToIndex(pCurve->KeyGetConstantMode(lCount))]; + } + else if ((pCurve->KeyGetInterpolation(lCount)&FbxAnimCurveDef::eInterpolationCubic) == FbxAnimCurveDef::eInterpolationCubic) + { + lOutputString += " | "; + lOutputString += cubicMode[TangentmodeFlagToIndex(pCurve->KeyGetTangentMode(lCount))]; + lOutputString += " | "; + lOutputString += tangentWVMode[TangentweightFlagToIndex(pCurve->KeyGet(lCount).GetTangentWeightMode())]; + lOutputString += " | "; + lOutputString += tangentWVMode[TangentVelocityFlagToIndex(pCurve->KeyGet(lCount).GetTangentVelocityMode())]; + } + lOutputString += " ]"; + lOutputString += "\n"; + FBXSDK_printf(lOutputString); + } +} +void DisplayListCurveKeys(FbxAnimCurve* pCurve, FbxProperty* pProperty) +{ + FbxTime lKeyTime; + int lKeyValue; + char lTimeString[256]; + FbxString lListValue; + FbxString lOutputString; + int lCount; + int lKeyCount = pCurve->KeyGetCount(); + for (lCount = 0; lCount < lKeyCount; lCount++) + { + lKeyValue = static_cast(pCurve->KeyGetValue(lCount)); + lKeyTime = pCurve->KeyGetTime(lCount); + lOutputString = " Key Time: "; + lOutputString += lKeyTime.GetTimeString(lTimeString, FbxUShort(256)); + lOutputString += ".... Key Value: "; + lOutputString += lKeyValue; + lOutputString += " ("; + lOutputString += pProperty->GetEnumValue(lKeyValue); + lOutputString += ")"; + lOutputString += "\n"; + FBXSDK_printf(lOutputString); + } +} + + + + + + + + + + +/** +* Main function - loads the hard-coded fbx file, +* and prints its contents in an xml format to stdout. +*/ + +int readtobone(bone **proot) +{ + + //ifstream fileHandle("fgdfg"); + string name_of_file; + cout << endl << "Enter filename:" << endl; + getline(cin,name_of_file); + const char* lFilename = name_of_file.c_str(); + FILE *checkfile=fopen(lFilename,"rb"); + if (!checkfile) + { + cout << endl << "file not found!" << endl; + return 0; + } + else + cout << endl << "file exists!" << endl; + fclose(checkfile); + + + // Initialize the SDK manager. This object handles all our memory management. + FbxManager* lSdkManager = FbxManager::Create(); + + // Create the IO settings object. + FbxIOSettings *ios = FbxIOSettings::Create(lSdkManager, IOSROOT); + lSdkManager->SetIOSettings(ios); + + // Create an importer using the SDK manager. + FbxImporter* lImporter = FbxImporter::Create(lSdkManager, ""); + + // Use the first argument as the filename for the importer. + if (!lImporter->Initialize(lFilename, -1, lSdkManager->GetIOSettings())) { + printf("Call to FbxImporter::Initialize() failed.\n"); + printf("Error returned: %s\n\n", lImporter->GetStatus().GetErrorString()); + FbxString error = lImporter->GetStatus().GetErrorString(); + exit(-1); + } + + // Create a new scene so that it can be populated by the imported file. + FbxScene* lScene = FbxScene::Create(lSdkManager, "myScene"); + + // Import the contents of the file into the scene. + lImporter->Import(lScene); + + // The file is imported; so get rid of the importer. + lImporter->Destroy(); + + string create_file_path; + + + /////////////////// + /// Model (Skeleton) + ///////////////// + // Print the nodes of the scene and their attributes recursively. + // Note that we are not printing the root node because it should + // not contain any attributes. + FbxNode* lRootNode = lScene->GetRootNode(); + int count_bones=0; + int child_count = lRootNode->GetChildCount(); + for (int i = 0; i < child_count; i++)//nur einen knochen machen + CountBones(lRootNode->GetChild(i),count_bones); + + cout << endl; + cout << "Skeleton" << endl; + cout << endl; + cout << "count bones: " << count_bones << endl; + + + bone *root = new bone; + *proot = root; + + if (lRootNode) + { + int anz = lRootNode->GetChildCount(); + for (int i = 0; i < lRootNode->GetChildCount(); i++)//nur einen knochen machen + { + PrintNode(root,lRootNode->GetChild(i), -1); + } + } + + cout << "----------------------------------------------------------------------------------------------------" << endl; + /////////////////// + /// Animation Data + ///////////////// + //extract animation stacks + /*int numStacks = lScene->GetSrcObjectCount(FBX_TYPE(FbxAnimStack));*/ + + //cout << endl; + //cout << "Animation" << endl; + //PrintAnimationData(lScene); + + ///////////////////// + ///// End + /////////////////// + //// Destroy the SDK manager and all the other objects it was handling. + //lSdkManager->Destroy(); + //system("pause"); + return 0; +} + + + diff --git a/src/main.cpp b/src/main.cpp index a7fc445..5626a7d 100755 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,20 +1,22 @@ // Core libraries -#include -#include - -// Third party libraries -#include -#include +#include +#include + +// Third party libraries +#include +#include #include #define STB_IMAGE_IMPLEMENTATION -#include "stb_image.h" - +#include "stb_image.h" + // Local headers #include "GLSL.h" #include "Program.h" #include "WindowManager.h" -#include "Shape.h" +#include "Shape.h" #include "Camera.h" +#include "bone.h" + using namespace std; using namespace glm; @@ -29,128 +31,131 @@ double get_last_elapsed_time() { class Application : public EventCallbacks { public: - WindowManager *windowManager = nullptr; + WindowManager *windowManager = nullptr; Camera *camera = nullptr; - + std::shared_ptr shape; - std::shared_ptr phongShader; - - double gametime = 0; - bool wireframeEnabled = false; - bool mousePressed = false; - bool mouseCaptured = false; - glm::vec2 mouseMoveOrigin = glm::vec2(0); - glm::vec3 mouseMoveInitialCameraRot; - - Application() { - camera = new Camera(); - } - - ~Application() { - delete camera; + std::shared_ptr phongShader; + + double gametime = 0; + bool wireframeEnabled = false; + bool mousePressed = false; + bool mouseCaptured = false; + glm::vec2 mouseMoveOrigin = glm::vec2(0); + glm::vec3 mouseMoveInitialCameraRot; + + Application() { + camera = new Camera(); + } + + ~Application() { + delete camera; } void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) { // Movement - if (key == GLFW_KEY_W && action != GLFW_REPEAT) camera->vel.z = (action == GLFW_PRESS) * -0.2f; - if (key == GLFW_KEY_S && action != GLFW_REPEAT) camera->vel.z = (action == GLFW_PRESS) * 0.2f; - if (key == GLFW_KEY_A && action != GLFW_REPEAT) camera->vel.x = (action == GLFW_PRESS) * -0.2f; - if (key == GLFW_KEY_D && action != GLFW_REPEAT) camera->vel.x = (action == GLFW_PRESS) * 0.2f; - // Rotation - if (key == GLFW_KEY_I && action != GLFW_REPEAT) camera->rotVel.x = (action == GLFW_PRESS) * 0.02f; - if (key == GLFW_KEY_K && action != GLFW_REPEAT) camera->rotVel.x = (action == GLFW_PRESS) * -0.02f; - if (key == GLFW_KEY_J && action != GLFW_REPEAT) camera->rotVel.y = (action == GLFW_PRESS) * 0.02f; - if (key == GLFW_KEY_L && action != GLFW_REPEAT) camera->rotVel.y = (action == GLFW_PRESS) * -0.02f; - if (key == GLFW_KEY_U && action != GLFW_REPEAT) camera->rotVel.z = (action == GLFW_PRESS) * 0.02f; - if (key == GLFW_KEY_O && action != GLFW_REPEAT) camera->rotVel.z = (action == GLFW_PRESS) * -0.02f; - // Polygon mode (wireframe vs solid) - if (key == GLFW_KEY_P && action == GLFW_PRESS) { - wireframeEnabled = !wireframeEnabled; - glPolygonMode(GL_FRONT_AND_BACK, wireframeEnabled ? GL_LINE : GL_FILL); - } - // Hide cursor (allows unlimited scrolling) - if (key == GLFW_KEY_SPACE && action == GLFW_PRESS) { - mouseCaptured = !mouseCaptured; - glfwSetInputMode(window, GLFW_CURSOR, mouseCaptured ? GLFW_CURSOR_DISABLED : GLFW_CURSOR_NORMAL); - resetMouseMoveInitialValues(window); - } + if (key == GLFW_KEY_W && action != GLFW_REPEAT) camera->vel.z = (action == GLFW_PRESS) * -0.2f; + if (key == GLFW_KEY_S && action != GLFW_REPEAT) camera->vel.z = (action == GLFW_PRESS) * 0.2f; + if (key == GLFW_KEY_A && action != GLFW_REPEAT) camera->vel.x = (action == GLFW_PRESS) * -0.2f; + if (key == GLFW_KEY_D && action != GLFW_REPEAT) camera->vel.x = (action == GLFW_PRESS) * 0.2f; + // Rotation + if (key == GLFW_KEY_I && action != GLFW_REPEAT) camera->rotVel.x = (action == GLFW_PRESS) * 0.02f; + if (key == GLFW_KEY_K && action != GLFW_REPEAT) camera->rotVel.x = (action == GLFW_PRESS) * -0.02f; + if (key == GLFW_KEY_J && action != GLFW_REPEAT) camera->rotVel.y = (action == GLFW_PRESS) * 0.02f; + if (key == GLFW_KEY_L && action != GLFW_REPEAT) camera->rotVel.y = (action == GLFW_PRESS) * -0.02f; + if (key == GLFW_KEY_U && action != GLFW_REPEAT) camera->rotVel.z = (action == GLFW_PRESS) * 0.02f; + if (key == GLFW_KEY_O && action != GLFW_REPEAT) camera->rotVel.z = (action == GLFW_PRESS) * -0.02f; + // Polygon mode (wireframe vs solid) + if (key == GLFW_KEY_P && action == GLFW_PRESS) { + wireframeEnabled = !wireframeEnabled; + glPolygonMode(GL_FRONT_AND_BACK, wireframeEnabled ? GL_LINE : GL_FILL); + } + // Hide cursor (allows unlimited scrolling) + if (key == GLFW_KEY_SPACE && action == GLFW_PRESS) { + mouseCaptured = !mouseCaptured; + glfwSetInputMode(window, GLFW_CURSOR, mouseCaptured ? GLFW_CURSOR_DISABLED : GLFW_CURSOR_NORMAL); + resetMouseMoveInitialValues(window); + } } - void mouseCallback(GLFWwindow *window, int button, int action, int mods) { - mousePressed = (action != GLFW_RELEASE); - if (action == GLFW_PRESS) { - resetMouseMoveInitialValues(window); - } - } - - void mouseMoveCallback(GLFWwindow *window, double xpos, double ypos) { - if (mousePressed || mouseCaptured) { - float yAngle = (xpos - mouseMoveOrigin.x) / windowManager->getWidth() * 3.14159f; - float xAngle = (ypos - mouseMoveOrigin.y) / windowManager->getHeight() * 3.14159f; - camera->setRotation(mouseMoveInitialCameraRot + glm::vec3(-xAngle, -yAngle, 0)); - } + void mouseCallback(GLFWwindow *window, int button, int action, int mods) { + mousePressed = (action != GLFW_RELEASE); + if (action == GLFW_PRESS) { + resetMouseMoveInitialValues(window); + } + } + + void mouseMoveCallback(GLFWwindow *window, double xpos, double ypos) { + if (mousePressed || mouseCaptured) { + float yAngle = (xpos - mouseMoveOrigin.x) / windowManager->getWidth() * 3.14159f; + float xAngle = (ypos - mouseMoveOrigin.y) / windowManager->getHeight() * 3.14159f; + camera->setRotation(mouseMoveInitialCameraRot + glm::vec3(-xAngle, -yAngle, 0)); + } } - void resizeCallback(GLFWwindow *window, int in_width, int in_height) { } - - // Reset mouse move initial position and rotation - void resetMouseMoveInitialValues(GLFWwindow *window) { - double mouseX, mouseY; - glfwGetCursorPos(window, &mouseX, &mouseY); - mouseMoveOrigin = glm::vec2(mouseX, mouseY); - mouseMoveInitialCameraRot = camera->rot; + void resizeCallback(GLFWwindow *window, int in_width, int in_height) { } + + // Reset mouse move initial position and rotation + void resetMouseMoveInitialValues(GLFWwindow *window) { + double mouseX, mouseY; + glfwGetCursorPos(window, &mouseX, &mouseY); + mouseMoveOrigin = glm::vec2(mouseX, mouseY); + mouseMoveInitialCameraRot = camera->rot; } + bone *root = NULL; + int size_stick = 0; void initGeom(const std::string& resourceDirectory) { - shape = make_shared(); - shape->loadMesh(resourceDirectory + "/sphere.obj"); - shape->resize(); - shape->init(); + readtobone(&root); + shape = make_shared(); + shape->loadMesh(resourceDirectory + "/sphere.obj"); + shape->resize(); + shape->init(); } void init(const std::string& resourceDirectory) { GLSL::checkVersion(); - + // Enable z-buffer test. - glEnable(GL_DEPTH_TEST); + glEnable(GL_DEPTH_TEST); // Initialize the GLSL programs - phongShader = std::make_shared(); - phongShader->setShaderNames(resourceDirectory + "/phong.vert", resourceDirectory + "/phong.frag"); - phongShader->init(); - } - - glm::mat4 getPerspectiveMatrix() { - float fov = 3.14159f / 4.0f; - float aspect = windowManager->getAspect(); - return glm::perspective(fov, aspect, 0.01f, 10000.0f); - } + phongShader = std::make_shared(); + phongShader->setShaderNames(resourceDirectory + "/phong.vert", resourceDirectory + "/phong.frag"); + phongShader->init(); + } + + glm::mat4 getPerspectiveMatrix() { + float fov = 3.14159f / 4.0f; + float aspect = windowManager->getAspect(); + return glm::perspective(fov, aspect, 0.01f, 10000.0f); + } void render() { double frametime = get_last_elapsed_time(); - gametime += frametime; + gametime += frametime; // Clear framebuffer. glClearColor(0.3f, 0.7f, 0.8f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Create the matrix stacks. - glm::mat4 V, M, P; + glm::mat4 V, M, P; P = getPerspectiveMatrix(); - V = camera->getViewMatrix(); - M = glm::mat4(1); - - /**************/ - /* DRAW SHAPE */ - /**************/ - M = glm::translate(glm::mat4(1), glm::vec3(0, 0, -3)); - phongShader->bind(); - phongShader->setMVP(&M[0][0], &V[0][0], &P[0][0]); - shape->draw(phongShader, false); - phongShader->unbind(); + V = camera->getViewMatrix(); + M = glm::mat4(1); + + /**************/ + /* DRAW SHAPE */ + /**************/ + M = glm::translate(glm::mat4(1), glm::vec3(0, 0, -3)); + phongShader->bind(); + phongShader->setMVP(&M[0][0], &V[0][0], &P[0][0]); + shape->draw(phongShader, false); + phongShader->unbind(); } }; - + int main(int argc, char **argv) { std::string resourceDir = "../resources"; if (argc >= 2) { @@ -167,11 +172,11 @@ int main(int argc, char **argv) { // Initialize scene. application->init(resourceDir); - application->initGeom(resourceDir); + application->initGeom(resourceDir); // Loop until the user closes the window. - while (!glfwWindowShouldClose(windowManager->getHandle())) { - // Update camera position. + while (!glfwWindowShouldClose(windowManager->getHandle())) { + // Update camera position. application->camera->update(); // Render scene. application->render();