diff --git a/animationController.cpp b/animationController.cpp index 8c8a11d..cb6d43e 100644 --- a/animationController.cpp +++ b/animationController.cpp @@ -99,12 +99,8 @@ namespace vb01{ break; } } - /* - swap(currentPos.y, currentPos.z); - currentPos.z = -currentPos.z; swap(currentRot.y, currentRot.z); currentRot.z = -currentRot.z; - */ channelBone->setPosePos(currentPos); channelBone->setPoseRot(currentRot); diff --git a/bone.cpp b/bone.cpp index a144170..00e4ca1 100644 --- a/bone.cpp +++ b/bone.cpp @@ -61,7 +61,15 @@ namespace vb01{ void Bone::setPoseRot(Quaternion r){ this->poseRot = r; setOrientation(restRot); - Quaternion parentSpacePoseRot = parent->globalToLocalOrientation(localToGlobalOrientation(r)); + + Quaternion parentSpacePoseRot = Quaternion::QUAT_W; + vector ancestors = getAncestors(this); + for(int i = 0; i < ancestors.size(); i++) + parentSpacePoseRot = parentSpacePoseRot * ancestors[i]->getOrientation(); + for(int i = ancestors.size() - 1; i > 0; i--) + parentSpacePoseRot = parentSpacePoseRot * ancestors[i]->getOrientation().conj(); + parentSpacePoseRot = r * parentSpacePoseRot; + setOrientation(parentSpacePoseRot); } diff --git a/light.cpp b/light.cpp index ed81648..51faa97 100755 --- a/light.cpp +++ b/light.cpp @@ -144,6 +144,11 @@ namespace vb01{ shader->setVec3(color,"light["+to_string(thisId)+"].color"); shader->setFloat(nearPlane,"light["+to_string(thisId)+"].near"); shader->setFloat(farPlane,"light["+to_string(thisId)+"].far"); + shader->setInt(0,"diffuseMap"); + shader->setInt(1,"normalMap"); + shader->setInt(2,"specularMap"); + shader->setInt(3,"parallaxMap"); + shader->setInt(4,"environmentMap"); shader->setInt(5,"light["+to_string(thisId)+"].depthMapCube"); shader->setInt(6,"light["+to_string(thisId)+"].depthMap"); depthMap->select(type==POINT?5:6); diff --git a/material.cpp b/material.cpp index 6777e50..d3fbd4c 100755 --- a/material.cpp +++ b/material.cpp @@ -66,7 +66,6 @@ namespace vb01{ shader->setInt(2,"specularMap"); shader->setInt(3,"parallaxMap"); shader->setInt(4,"environmentMap"); - for(Texture *t : diffuseMapTextures) t->update(0); for(Texture *t : normalMapTextures) diff --git a/node.cpp b/node.cpp index 16c41e3..c6521f2 100755 --- a/node.cpp +++ b/node.cpp @@ -191,7 +191,7 @@ namespace vb01{ float rotAngle = orientation.getAngle(); Vector3 rotAxis = orientation.getAxis(); Vector3 newAxis = (parGlobalAxis[0] * rotAxis.x + parGlobalAxis[1] * rotAxis.y + parGlobalAxis[2] * rotAxis.z).norm(); - Quaternion rotQuat=Quaternion(rotAngle,newAxis); + Quaternion rotQuat=Quaternion(rotAngle, newAxis); for(int i = 0; i < 3; i++) globalAxis[i] = (rotQuat * parGlobalAxis[i]).norm(); @@ -239,31 +239,35 @@ namespace vb01{ } Quaternion Node::adjustRot(vector ancestors, Quaternion adjustableRot, bool localToGlobal){ - Quaternion rOrigin = localToGlobal ? Quaternion::QUAT_W : adjustableRot; + Quaternion rOrigin = adjustableRot; while(!ancestors.empty()){ - int id = ancestors.size() - 1; - Vector3 parAxis[]{ - ancestors[id]->getGlobalAxis(0), - ancestors[id]->getGlobalAxis(1), - ancestors[id]->getGlobalAxis(2) - }; + int id = localToGlobal ? 0 : ancestors.size() - 1; float angle = ancestors[id]->getOrientation().getAngle(); Vector3 axis = ancestors[id]->getOrientation().getAxis(); - Vector3 rotAxis = (parAxis[0] * axis.x + parAxis[1] * axis.y + parAxis[2] * axis.z).norm(); Quaternion o; if(localToGlobal){ - o = Quaternion(angle, rotAxis); - rOrigin = o * rOrigin; + Node *par = ancestors[id]->getParent(); + Vector3 parAxis[]{ + par?par->getGlobalAxis(0):Vector3::VEC_I, + par?par->getGlobalAxis(1):Vector3::VEC_J, + par?par->getGlobalAxis(2):Vector3::VEC_K + }; + + axis = (parAxis[0] * axis.x + parAxis[1] * axis.y + parAxis[2] * axis.z).norm(); + + o = Quaternion(angle, axis); + rOrigin = rOrigin * o; + ancestors.erase(ancestors.begin()); } else{ - o = Quaternion(-angle, rotAxis); + o = Quaternion(angle, axis).conj(); rOrigin = rOrigin * o; + ancestors.pop_back(); } - ancestors.pop_back(); } return rOrigin; diff --git a/skeleton.cpp b/skeleton.cpp index ef840bd..dd84535 100644 --- a/skeleton.cpp +++ b/skeleton.cpp @@ -22,11 +22,12 @@ namespace vb01{ void Skeleton::solveIk(Bone *ikBone){ const int chainLength = ikBone->getIkChainLength(); + Bone *rootBone = getRootBone(); Bone *ikTarget = ikBone->getIkTarget(); Bone *boneChain[chainLength]; Bone *ikBoneAncestor = ikBone; Vector3 boneIkPos[chainLength]; - Vector3 targetPos = ikTarget->getPosition(); + Vector3 targetPos = rootBone->globalToLocalPosition(ikTarget->localToGlobalPosition(Vector3::VEC_ZERO)); float sumLengths = 0; @@ -45,7 +46,7 @@ namespace vb01{ void Skeleton::calculateFabrik(int chainLength, Bone *boneChain[], Vector3 boneIkPos[], Vector3 targetPos, float sumLengths){ Vector3 startPos = boneChain[chainLength - 1]->getModelSpacePos(); if(startPos.getDistanceFrom(targetPos) < sumLengths){ - int numIterations = 2; + int numIterations = 200; for(int i = 0 ; i < numIterations; i++){ bool backward = (i % 2 == 0); @@ -94,11 +95,19 @@ namespace vb01{ } void Skeleton::transformIkChain(int chainLength, Bone *boneChain[], Vector3 boneIkPos[], Vector3 targetPos){ + /* + Bone *rootBone = getRootBone(); + for(int i = chainLength - 1; i >= 0; i--){ + Vector3 dir = ((i == 0 ? targetPos : boneIkPos[i - 1]) - boneChain[i]->getModelSpacePos()).norm(); + boneChain[i]->lookAt(dir, rootBone); + } + */ + float boneAngles[chainLength]; Vector3 axis[chainLength]; - for(int i = chainLength - 1; i >= 0; i--){ Vector3 dir = ((i == 0 ? targetPos : boneIkPos[i - 1]) - boneIkPos[i]).norm(); + Vector3 boneAxis = boneChain[i]->getInitAxis(1); Vector3 rotAxis = boneAxis.cross(dir).norm(); float angle = boneAxis.getAngleBetween(dir); @@ -108,8 +117,6 @@ namespace vb01{ rotAxis = Vector3::VEC_I; } - //rotAxis = (boneChain[i]->getInitAxis(0) * rotAxis.x + boneChain[i]->getInitAxis(1) * rotAxis.y + boneChain[i]->getInitAxis(2) * rotAxis.z).norm(); - boneAngles[i] = angle; axis[i] = rotAxis; } diff --git a/vbModelReader.cpp b/vbModelReader.cpp index bd0a672..cc2aa40 100644 --- a/vbModelReader.cpp +++ b/vbModelReader.cpp @@ -101,14 +101,12 @@ namespace vb01{ Node *parent = skeleton->getBone(parName); if(!parent){ parent = model; - swap(xAxis.y, xAxis.z); - xAxis.z = -xAxis.z; + swap(pos.y, pos.z); + pos.z = -pos.z; swap(yAxis.y, yAxis.z); yAxis.z = -yAxis.z; swap(zAxis.y, zAxis.z); zAxis.z = -zAxis.z; - swap(pos.y, pos.z); - pos.z = -pos.z; /* */ }