bool intersection(Vector4 start1, Vector4 end1, Vector4 start2, Vector4 end2, Vector4 *out_intersection) { Vector4 dir1 = Vector4Subtract(end1, start1); Vector4 dir2 = Vector4Subtract(end2, start2); //считаем уравнения прямых проходящих через отрезки float a1 = -dir1.y; float b1 = +dir1.x; float d1 = -(a1*start1.x + b1*start1.y); float a2 = -dir2.y; float b2 = +dir2.x; float d2 = -(a2*start2.x + b2*start2.y); //подставляем концы отрезков, для выяснения в каких полуплоскотях они float seg1_line2_start = a2*start1.x + b2*start1.y + d2; float seg1_line2_end = a2*end1.x + b2*end1.y + d2; float seg2_line1_start = a1*start2.x + b1*start2.y + d1; float seg2_line1_end = a1*end2.x + b1*end2.y + d1; //если концы одного отрезка имеют один знак, значит он в одной полуплоскости и пересечения нет. if (seg1_line2_start * seg1_line2_end >= 0 || seg2_line1_start * seg2_line1_end >= 0) return false; float u = seg1_line2_start / (seg1_line2_start - seg1_line2_end); *out_intersection = Vector4Add(start1, Vector4MultiplyScalar(dir1, u)); return true; }
void gamerRenderUpdateAllObjects( float fTime ) { double fElapsed4 = 0.0f; double fElapsed6 = 0.0f; double fElapsed7 = 0.0f; double fElapsed8 = 0.0f; double fElapsed9 = 0.0f; double fElapsed10 = 0.0f; double fStart, fEnd; double fPartStart, fPartEnd; int iNumJoints = saAnimHierarchies[0].miNumJoints; /*tVector4* aPositions = (tVector4 *)malloc( sizeof( tVector4 ) * iNumJoints ); tVector4* aScalings = (tVector4 *)malloc( sizeof( tVector4 ) * iNumJoints ); tQuaternion* aRotations = (tQuaternion *)malloc( sizeof( tQuaternion ) * iNumJoints ); tVector4* aRotationVecs = (tVector4 *)malloc( sizeof( tVector4 ) * iNumJoints ); tMatrix44* aAnimMatrices = (tMatrix44 *)malloc( sizeof( tMatrix44 ) * iNumJoints ); */ // allocate for all the objects and their joints tVector4** aaPositions = (tVector4 **)malloc( sizeof( tVector4* ) * siNumRenderObjects ); tVector4** aaScalings = (tVector4 **)malloc( sizeof( tVector4* ) * siNumRenderObjects ); tQuaternion** aaRotations = (tQuaternion **)malloc( sizeof( tQuaternion* ) * siNumRenderObjects ); tVector4** aaRotationVecs = (tVector4 **)malloc( sizeof( tVector4* ) * siNumRenderObjects ); tMatrix44** aaAnimMatrices = (tMatrix44 **)malloc( sizeof( tMatrix44* ) * siNumRenderObjects ); for( int i = 0; i < siNumRenderObjects; i++ ) { aaPositions[i] = (tVector4 *)malloc( sizeof( tVector4 ) * iNumJoints ); aaScalings[i] = (tVector4 *)malloc( sizeof( tVector4 ) * iNumJoints ); aaRotations[i] = (tQuaternion *)malloc( sizeof( tQuaternion ) * iNumJoints ); aaRotationVecs[i] = (tVector4 *)malloc( sizeof( tVector4 ) * iNumJoints ); aaAnimMatrices[i] = (tMatrix44 *)malloc( sizeof( tMatrix44 ) * iNumJoints ); for( int j = 0; j < iNumJoints; j++ ) { Matrix44Identity( &aaAnimMatrices[i][j] ); } } /*for( int i = 0; i < iNumJoints; i++ ) { Matrix44Identity( &aAnimMatrices[i] ); }*/ fPartStart = getTime(); #if 0 // update anim model instances' transform for( int i = 0; i < siNumRenderObjects; i++ ) { fStart = getTime(); //animSequencePlay( spAnimSequence, aPositions, aScalings, aRotations, aRotationVecs, aAnimMatrices, sfTime ); tGameRenderObject* pGameRenderObject = &saRenderObjects[i]; tAnimModelInstance* pAnimModelInstance = pGameRenderObject->mpAnimModelInstance; // y rotation angle float fAngle = atan2f( pGameRenderObject->mpHeading->fZ, pGameRenderObject->mpHeading->fX ) - 3.14159f * 0.5f; pAnimModelInstance->mRotation.fY = fAngle; // update position tVector4 dPos; Vector4MultScalar( &dPos, pGameRenderObject->mpHeading, pGameRenderObject->mfSpeed ); tVector4* pPos = pGameRenderObject->mpPosition; tVector4 newPos; Vector4Add( &newPos, pPos, &dPos ); memcpy( pGameRenderObject->mpPosition, &newPos, sizeof( tVector4 ) ); memcpy( &pAnimModelInstance->mPosition, &newPos, sizeof( tVector4 ) ); memcpy( &pAnimModelInstance->mScaling, pGameRenderObject->mpSize, sizeof( tVector4 ) ); fEnd = getTime(); fElapsed0 += ( fEnd - fStart ); } #endif // #if 0 fPartEnd = getTime(); fElapsed7 = fPartEnd - fPartEnd; fPartStart = fPartEnd; tVector4** paPositions = aaPositions; tVector4** paScalings = aaScalings; tVector4** paRotationVecs = aaRotationVecs; tQuaternion** paRotations = aaRotations; tMatrix44** paAnimMatrices = aaAnimMatrices; // update joint in hierarchy matrices for( int i = 0; i < siNumRenderObjects; i++ ) { tJob job; tJointJobData jobData; jobData.mpSequence = spAnimSequence; jobData.mpHierarchy = &saAnimHierarchies[0]; jobData.maPositions = *paPositions; //aPositions; jobData.maScalings = *paScalings; //aScalings; jobData.maRotations = *paRotations; //aRotations; jobData.maRotationVecs = *paRotationVecs; //aRotationVecs; jobData.mfTime = fTime * saRenderObjects[i].mfAnimMult; jobData.maAnimMatrices = *paAnimMatrices; //aAnimMatrices; jobData.mpResultAnimHiearchy = &saAnimHierarchies[i]; job.mpfnFunc = updateJointValuesAndMatrices; job.miDataSize = sizeof( tJointJobData ); job.mpData = &jobData; jobManagerAddJob( gpJobManager, &job ); ++paPositions; ++paScalings; ++paRotations; ++paRotationVecs; ++paAnimMatrices; } // for i = 0 to num render objects jobManagerWait( gpJobManager ); // update all the joint's matrices /*for( int i = 0; i < siNumRenderObjects; i++ ) { fStart = getTime(); animSequenceUpdateJointAnimValues( spAnimSequence, &saAnimHierarchies[0], aaPositions[i], aaScalings[i], aaRotations[i], aaRotationVecs[i], fTime * saRenderObjects[i].mfAnimMult ); jobManagerWait( gpJobManager ); fEnd = getTime(); fElapsed1 += ( fEnd - fStart ); fStart = fEnd; animSequenceUpdateAnimMatrices( spAnimSequence, &saAnimHierarchies[0], aaPositions[i], aaScalings[i], aaRotations[i], aaRotationVecs[i], aaAnimMatrices[i] ); jobManagerWait( gpJobManager ); fElapsed2 += ( fEnd - fStart ); fStart = fEnd; updateJointMatrices( &saAnimHierarchies[i], aaAnimMatrices[i] ); jobManagerWait( gpJobManager ); fElapsed3 += ( fEnd - fStart ); fStart = fEnd; } jobManagerWait( gpJobManager ); */ fPartEnd = getTime(); fElapsed8 = fPartEnd - fPartStart; // update the xform of the total model for( int i = 0; i < siNumRenderObjects; i++ ) { animModelInstanceUpdateXForm( &saAnimModelInstances[i] ); } // transform position and normal fPartStart = getTime(); fStart = getTime(); for( int i = 0; i < siNumRenderObjects; i++ ) { animModelInstanceUpdate( &saAnimModelInstances[i] ); } jobManagerWait( gpJobManager ); fEnd = getTime(); fElapsed4 += ( fEnd - fStart ); fStart = fEnd; fPartEnd = getTime(); fElapsed9 = fPartEnd - fPartStart; WTFASSERT2( fPartEnd >= fPartStart, "WTF" ); fStart = getTime(); // TODO: OPTIMIZE COPY VB DATA for( int i = 0; i < siNumRenderObjects; i++ ) { animModelInstanceCopyVBData( &saAnimModelInstances[i] ); } fEnd = getTime(); fElapsed6 = fEnd - fStart; fPartEnd = getTime(); fElapsed10 = fPartEnd - fPartStart; fPartStart = fPartEnd; #if 0 OUTPUT( "********************\n" ); OUTPUT( "elapsed 0 = %.4f\n", fElapsed0 ); OUTPUT( "elapsed 1 = %.4f\n", fElapsed1 ); OUTPUT( "elapsed 2 = %.4f\n", fElapsed2 ); OUTPUT( "elapsed 3 = %.4f\n", fElapsed3 ); OUTPUT( "elapsed 4 = %.4f\n", fElapsed4 ); OUTPUT( "elapsed 5 = %.4f\n", fElapsed5 ); OUTPUT( "elapsed 6 = %.4f\n", fElapsed6 ); OUTPUT( "elapsed 7 = %.4f\n", fElapsed7 ); OUTPUT( "elapsed 8 = %.4f\n", fElapsed8 ); OUTPUT( "elapsed 9 = %.4f\n", fElapsed9 ); OUTPUT( "elapsed 10 = %.4f\n", fElapsed10 ); OUTPUT( "total elapsed = %.4f\n", fTotalElapsed ); OUTPUT( "********************\n" ); #endif // #if 0 /*free( aPositions ); free( aScalings ); free( aRotations ); free( aRotationVecs ); free( aAnimMatrices );*/ for( int i = 0; i < siNumRenderObjects; i++ ) { free( aaPositions[i] ); free( aaScalings[i] ); free( aaRotations[i] ); free( aaRotationVecs[i] ); free( aaAnimMatrices[i] ); } free( aaPositions ); free( aaScalings ); free( aaRotations ); free( aaRotationVecs ); free( aaAnimMatrices ); }