void FBXScene::ProcessAnimation(FbxNode* pNode, const char* strTakeName, float fFrameRate, float fStart, float fStop) { FbxNodeAttribute* pNodeAttribute = pNode->GetNodeAttribute(); if (pNodeAttribute) { if (pNodeAttribute->GetAttributeType() == FbxNodeAttribute::EType::eSkeleton) { if( m_pSkeleton ) { SkeletonBone* pBone = m_pSkeleton->FindBone(pNode->GetName()); if( pBone ) { AnimationKeyFrames* pAnimationKeyFrames = new AnimationKeyFrames(strTakeName); double fTime = 0; while( fTime <= fStop ) { FbxTime takeTime; takeTime.SetSecondDouble(fTime); FbxMatrix matAbsoluteTransform2 = GetAbsoluteTransformFromCurrentTake2(pNode, takeTime); FbxMatrix matParentAbsoluteTransform2 = GetAbsoluteTransformFromCurrentTake2(pNode->GetParent(), takeTime); FbxMatrix matInvParentAbsoluteTransform2 = matParentAbsoluteTransform2.Inverse(); FbxMatrix matTransform2 = matInvParentAbsoluteTransform2 * matAbsoluteTransform2; pAnimationKeyFrames->AddKeyFrame(matTransform2); fTime += 1.0f / fFrameRate; } pBone->AddAnimationKeyFrames(pAnimationKeyFrames); } } } else if (pNodeAttribute->GetAttributeType() == FbxNodeAttribute::EType::eMesh) { Model* pModel = m_Models.Find(pNode->GetName(), NULL); if( pModel ) { AnimationKeyFrames* pAnimationKeyFrames = new AnimationKeyFrames(strTakeName); double fTime = 0; while( fTime <= fStop ) { FbxTime takeTime; takeTime.SetSecondDouble(fTime); FbxMatrix matAbsoluteTransform2 = GetAbsoluteTransformFromCurrentTake2(pNode, takeTime); pAnimationKeyFrames->AddKeyFrame(matAbsoluteTransform2); fTime += 1.0f/fFrameRate; } pModel->AddAnimationKeyFrames(pAnimationKeyFrames); } } } for( int i = 0; i < pNode->GetChildCount(); ++i ) { ProcessAnimation(pNode->GetChild(i), strTakeName, fFrameRate, fStart, fStop); } }
//-------------------------------------------------------------- void testApp::draw(){ ofDisableAlphaBlending(); ofSetColor(255); kinect.draw(0, 0); ofEnableAlphaBlending(); for(int i=0; i<circles.size(); i++) { ofFill(); ofSetHexColor(0xf6c738); circles[i].get()->draw(); } for(int i=0; i<boxes.size(); i++) { ofFill(); ofSetHexColor(0xBF2545); boxes[i].get()->draw(); } for(int i=0; i<vehicles.size(); i++) { ofFill(); ofSetColor(50,200,50); vehicles[i].get()->draw(); } for(int i = 0 ; i < tracks.size() ; i++){ ofFill(); ofSetColor(40,180,70); tracks[i].get()->draw(); } LHumerusTrack.draw(); RHumerusTrack.draw(); LRadiusTrack.draw(); RRadiusTrack.draw(); ofSetColor(50,50,200); //train.draw(); // draw the ground box2d.drawGround(); string info = ""; /*info += "Press [c] for circles\n"; info += "Press [b] for blocks\n"; info += "Total Bodies: "+ofToString(box2d.getBodyCount())+"\n"; info += "Total Joints: "+ofToString(box2d.getJointCount())+"\n\n";*/ info += "FPS: "+ofToString(ofGetFrameRate(), 1)+"\n"; info += "Time: "+ofToString(ofGetElapsedTimef())+"\n"; info += "Score: "+ofToString(score)+"\n"; info += "Level: "+ofToString(level)+"\n"; ofFill(); ofSetColor(255); ofDrawBitmapString(info, 30, 30); //SCENARIO ELEMENTS ofSetColor(50,100,230,180); ofRect(0,ofGetHeight()-50,ofGetWidth(),50); //THE RIVER!!! ofSetColor(255); ambientCanyon.draw(0,0); //THE VALLEY!! if(kinect.isNewSkeleton()) { ofSetColor(255); for(int i = 0 ; i < kinect.getSkeletons().size(); i++) { if(kinect.getSkeletons().at(i).find(NUI_SKELETON_POSITION_HEAD) != kinect.getSkeletons().at(i).end()) { SkeletonBone headBone = kinect.getSkeletons().at(i).find(NUI_SKELETON_POSITION_HEAD)->second; ofVec2f headScreenPosition(headBone.getScreenPosition().x, headBone.getScreenPosition().y); if(started == false){ ofSetColor(255,255,100); ofCircle(headScreenPosition.x+50, headScreenPosition.y-30, 25); ofSetColor(0); ofDrawBitmapString("START",headScreenPosition.x+30, headScreenPosition.y-30); } //RIGHT SHOULDER ofSetColor(100,80,40); if(kinect.getSkeletons().at(i).find(NUI_SKELETON_POSITION_SHOULDER_RIGHT) != kinect.getSkeletons().at(i).end()) { SkeletonBone RShoulderBone = kinect.getSkeletons().at(i).find(NUI_SKELETON_POSITION_SHOULDER_RIGHT)->second; ofVec2f partScreenPosition(RShoulderBone.getScreenPosition().x, RShoulderBone.getScreenPosition().y); ofCircle(partScreenPosition.x, partScreenPosition.y, 10); //LEFT SHOULDER if(kinect.getSkeletons().at(i).find(NUI_SKELETON_POSITION_SHOULDER_LEFT) != kinect.getSkeletons().at(i).end()) { SkeletonBone LShoulderBone = kinect.getSkeletons().at(i).find(NUI_SKELETON_POSITION_SHOULDER_LEFT)->second; ofVec2f partScreenPosition(LShoulderBone.getScreenPosition().x, LShoulderBone.getScreenPosition().y); ofCircle(partScreenPosition.x, partScreenPosition.y, 10); //RIGHT HAND if(kinect.getSkeletons().at(i).find(NUI_SKELETON_POSITION_WRIST_RIGHT) != kinect.getSkeletons().at(i).end()) { SkeletonBone RHandBone = kinect.getSkeletons().at(i).find(NUI_SKELETON_POSITION_WRIST_RIGHT)->second; ofVec2f partScreenPosition(RHandBone.getScreenPosition().x, RHandBone.getScreenPosition().y); ofCircle(partScreenPosition.x, partScreenPosition.y, 10); //LEFT HAND if(kinect.getSkeletons().at(i).find(NUI_SKELETON_POSITION_WRIST_LEFT) != kinect.getSkeletons().at(i).end()) { SkeletonBone LHandBone = kinect.getSkeletons().at(i).find(NUI_SKELETON_POSITION_WRIST_LEFT)->second; ofVec2f partScreenPosition(LHandBone.getScreenPosition().x, LHandBone.getScreenPosition().y); ofCircle(partScreenPosition.x, partScreenPosition.y, 10); //RIGHT ELBOW if(kinect.getSkeletons().at(i).find(NUI_SKELETON_POSITION_ELBOW_RIGHT) != kinect.getSkeletons().at(i).end()) { SkeletonBone RElbowBone = kinect.getSkeletons().at(i).find(NUI_SKELETON_POSITION_ELBOW_RIGHT)->second; ofVec2f partScreenPosition(RElbowBone.getScreenPosition().x, RElbowBone.getScreenPosition().y); ofCircle(partScreenPosition.x, partScreenPosition.y, 10); //LEFT ELBOW if(kinect.getSkeletons().at(i).find(NUI_SKELETON_POSITION_ELBOW_LEFT) != kinect.getSkeletons().at(i).end()) { SkeletonBone LElbowBone = kinect.getSkeletons().at(i).find(NUI_SKELETON_POSITION_ELBOW_LEFT)->second; ofVec2f partScreenPosition(LElbowBone.getScreenPosition().x, LElbowBone.getScreenPosition().y); ofCircle(partScreenPosition.x, partScreenPosition.y, 10); //Starting the game... if(started == false){ if( RHandBone.getScreenPosition().x > (headScreenPosition.x+50) && RHandBone.getScreenPosition().x < (headScreenPosition.x+80) && RHandBone.getScreenPosition().y > (headScreenPosition.y-30) && RHandBone.getScreenPosition().y < (headScreenPosition.y )) { started = true; explosion.play(); } } //Con todos los bones guardados... float LHumerusX = LElbowBone.getScreenPosition().x - LShoulderBone.getScreenPosition().x; float LHumerusY = LElbowBone.getScreenPosition().y - LShoulderBone.getScreenPosition().y; float LRadiusX = LHandBone.getScreenPosition().x - LElbowBone.getScreenPosition().x; float LRadiusY = LHandBone.getScreenPosition().y - LElbowBone.getScreenPosition().y; float RHumerusX = RElbowBone.getScreenPosition().x - RShoulderBone.getScreenPosition().x; float RHumerusY = RElbowBone.getScreenPosition().y - RShoulderBone.getScreenPosition().y; float RRadiusX = RHandBone.getScreenPosition().x - RElbowBone.getScreenPosition().x; float RRadiusY = RHandBone.getScreenPosition().y - RElbowBone.getScreenPosition().y; float LHumerusAngle = 90 - atan2(LHumerusX,LHumerusY)*180/M_PI; LHumerusTrack.setPosition((LElbowBone.getScreenPosition().x + LShoulderBone.getScreenPosition().x) / 2, (LElbowBone.getScreenPosition().y + LShoulderBone.getScreenPosition().y) / 2); LHumerusTrack.setRotation(LHumerusAngle); float RHumerusAngle = 90 - atan2(RHumerusX,RHumerusY)*180/M_PI; RHumerusTrack.setPosition((RElbowBone.getScreenPosition().x + RShoulderBone.getScreenPosition().x) / 2, (RElbowBone.getScreenPosition().y + RShoulderBone.getScreenPosition().y) / 2); RHumerusTrack.setRotation(RHumerusAngle); float LRadiusAngle = 90 - atan2(LRadiusX,LRadiusY)*180/M_PI; LRadiusTrack.setPosition((LHandBone.getScreenPosition().x + LElbowBone.getScreenPosition().x) / 2, (LHandBone.getScreenPosition().y + LElbowBone.getScreenPosition().y) / 2); LRadiusTrack.setRotation(LRadiusAngle); float RRadiusAngle = 90 - atan2(RRadiusX,RRadiusY)*180/M_PI; RRadiusTrack.setPosition((RHandBone.getScreenPosition().x + RElbowBone.getScreenPosition().x) / 2, (RHandBone.getScreenPosition().y + RElbowBone.getScreenPosition().y) / 2); RRadiusTrack.setRotation(RRadiusAngle); } } } } } } return; } } } }
BTHFBX_RAY_BOX_RESULT FBXSceneInstance::RayVsScene(const BTHFBX_RAY& ray, BTHFBX_MATRIX* worldMatrix) { BTHFBX_RAY_BOX_RESULT result; result.DistanceToHit = -1.0f; result.JointIndex = -1; FbxVector4 o(ray.Origin.x, ray.Origin.y, ray.Origin.z); FbxVector4 d(ray.Direction.x, ray.Direction.y, ray.Direction.z); FbxMatrix world, worldInv; for(int y = 0; y < 4; y++) for(int x = 0; x < 4; x++) world.Set(y, x, worldMatrix->f[y*4+x]); worldInv = world.Inverse(); FbxVector4 no, nd; FBXVec3TransformCoord(&no, &o, &worldInv); FBXVec3TransformNormal(&nd, &d, &worldInv); nd.Normalize(); float hitDistance = -1.0f; if(!m_pSkeleton) { // FUNKAR INTE?=!?!??!?!!? hitDistance = BoxVsRay(no, nd, m_BoundingBox); result.DistanceToHit = hitDistance; } else { hitDistance = BoxVsRay(no, nd, m_pSkeleton->GetCombinedBoundingBox()); result.DistanceToHit = hitDistance; if(hitDistance > -1.0f && m_pSkeleton) { SkeletonBone* bone = NULL; int jointIndex = -1; float tmpDist, closestDist = FLT_MAX; for(unsigned int i = 0; i < m_pSkeleton->GetBoneCount(); i++) { bone = (SkeletonBone*)m_pSkeleton->GetBone(i); worldInv = (world * bone->GetSkinTransform()).Inverse(); FBXVec3TransformCoord(&no, &o, &worldInv); FBXVec3TransformNormal(&nd, &d, &worldInv); nd.Normalize(); tmpDist = BoxVsRay(no, nd, bone->GetAABB()); if(tmpDist > -1.0f) { if(tmpDist < closestDist) { closestDist = tmpDist; jointIndex = i; } } } if(jointIndex != -1) { result.DistanceToHit = closestDist; result.JointIndex = jointIndex; } } } return result; }
//-------------------------------------------------------------- void testApp::update() { kinect.update(); if(kinect.isNewSkeleton()) { for( int i = 0; i < kinect.getSkeletons().size(); i++) { if(kinect.getSkeletons().at(i).find(NUI_SKELETON_POSITION_HEAD) != kinect.getSkeletons().at(i).end()) { // just get the first one SkeletonBone headBone = kinect.getSkeletons().at(i).find(NUI_SKELETON_POSITION_HEAD)->second; SkeletonBone lHandBone = kinect.getSkeletons().at(i).find(NUI_SKELETON_POSITION_HAND_LEFT)->second; SkeletonBone rHandBone = kinect.getSkeletons().at(i).find(NUI_SKELETON_POSITION_HAND_RIGHT)->second; ofVec3f hb( headBone.getScreenPosition().x, headBone.getScreenPosition().y, 0 ); head = head.getInterpolated(hb, 0.5); head.z = ofInterpolateCosine( head.z, headBone.getStartPosition().x, 0.5) + 0.1; ofVec3f lhb(lHandBone.getScreenPosition().x, lHandBone.getScreenPosition().y, 0); lHand = lHand.getInterpolated( lhb, 0.5); lHand.z = ofInterpolateCosine( lHand.z, lHandBone.getStartPosition().x, 0.5); ofVec3f rhb(rHandBone.getScreenPosition().x, rHandBone.getScreenPosition().y, 0); rHand = rHand.getInterpolated( rhb, 0.5); rHand.z = ofInterpolateCosine( rHand.z, rHandBone.getStartPosition().x, 0.5); cout << headBone.getScreenPosition() << endl; cout << rHandBone.getScreenPosition() << endl; cout << lHandBone.getScreenPosition() << endl; //cout << kinect.getSkeletons().at(i).find(NUI_SKELETON_POSITION_HEAD)->second.getScreenPosition() << endl; //cout << kinect.getSkeletons().at(i).find(NUI_SKELETON_POSITION_HAND_LEFT)->second.getScreenPosition() << endl; //cout << kinect.getSkeletons().at(i).find(NUI_SKELETON_POSITION_HAND_RIGHT)->second.getScreenPosition() << endl; jointDistance = head.distance(rHand); jointDistance += lHand.distance(rHand); jointDistance += lHand.distance(head); hasSkeleton = true; return; } } } }
//-------------------------------------------------------------- void ofApp::update() { //kinect stuff kinect.update(); if(kinect.isNewSkeleton()) { for( int i = 0; i < kinect.getSkeletons().size(); i++) { // has a head? probably working ok then :) if(kinect.getSkeletons().at(i).find(NUI_SKELETON_POSITION_HEAD) != kinect.getSkeletons().at(i).end()) { // just get the first one //SkeletonBone headBone = kinect.getSkeletons().at(i).find(NUI_SKELETON_POSITION_HEAD)->second; //ofVec2f headScrenPosition( headBone.getScreenPosition().x, headBone.getScreenPosition().y); SkeletonBone lHand = kinect.getSkeletons().at(i).find(NUI_SKELETON_POSITION_HAND_LEFT)->second; ofVec2f lHandScrenPosition( lHand.getScreenPosition().x, lHand.getScreenPosition().y); SkeletonBone rHand = kinect.getSkeletons().at(i).find(NUI_SKELETON_POSITION_HAND_RIGHT)->third; ofVec2f rHandScreenPosition( rHand.getScreenPosition().x, rHand.getScreenPosition().y); //cerr << headBone; handDist = ofDistSquared(lHand.getScreenPosition().x, lHand.getScreenPosition().y, rHand.getScreenPosition().x, rHand.getScreenPosition().y); return; } } } vidGrabber.update(); float time = ofGetElapsedTimef(); //Get time float oldValue3d = ofNoise(meshRadius, time); //Change vertices for (int i=0; i<mesh.getNumVertices(); i++) { //---------------------------------// //set mesh color from video (even though it's not working right now) //---------------------------------// // Map i to the width and height of the image ( or video ) so that color can be sampled from the corresponding pixel int widthSampleIndex = ofMap(i, 0, mesh.getNumVertices(), 0, vidGrabber.getWidth()); int heightSampleIndex = ofMap(i, 0, mesh.getNumVertices(), 0, vidGrabber.getHeight()); if(vidGrabber.isFrameNew()) { vidPixels = vidGrabber.getPixelsRef(); //Grab color from pixel and set vertex color to it //c = vidPixels.getColor(widthSampleIndex, heightSampleIndex); image.setFromPixels(vidGrabber.getPixelsRef()); c = image.getColor(widthSampleIndex, heightSampleIndex); cerr << ( c )<< endl ; } mesh.addColor(c); //---------------------------------// //displace vertice here using prelin noise ( After attempting this approach I think I would go with GLSL shaders next time. While each point follows the noise with this there's no continuity from point to point so it looks random. //---------------------------------// ofVec3f p = mesh.getVertex( i ); //Get Perlin noise value ( not sure which is best float value = ofNoise(100*i); float value2d = ofNoise(100 * i, time); float value3d = ofNoise(100 * i, i, time); ofVec3f center = mesh.getCentroid(); float dist = center.distance(p); //find scale value so that the vertice can be moved float scaleValue = (dist*(oldValue3d))/dist; float oldValue3d = value3d; //Change coordinate of vertex in relation to the center of the sphere ofVec3f newP = p.scale(scaleValue*(10*handDist)); //mesh.setVertex(i, newP); } }