12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919 |
- //-----------------------------------------------------------------------------
- // Copyright (c) 2012 GarageGames, LLC
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to
- // deal in the Software without restriction, including without limitation the
- // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
- // sell copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
- // IN THE SOFTWARE.
- //-----------------------------------------------------------------------------
- #include "platform/platform.h"
- #include "math/util/frustum.h"
- #include "math/mathUtils.h"
- #include "math/mMath.h"
- #include "math/mRandom.h"
- #include "math/util/frustum.h"
- #include "platform/profiler.h"
- #include "core/tAlgorithm.h"
- #include "gfx/gfxDevice.h"
- namespace MathUtils
- {
- MRandomLCG sgRandom(0xdeadbeef); ///< Our random number generator.
- //-----------------------------------------------------------------------------
- bool capsuleCapsuleOverlap(const Point3F & a1, const Point3F & b1, F32 rad1, const Point3F & a2, const Point3F & b2, F32 rad2)
- {
- F32 s,t;
- Point3F c1,c2;
- F32 dist = segmentSegmentNearest(a1,b1,a2,b2,s,t,c1,c2);
- return dist <= (rad1+rad2)*(rad1+rad2);
- }
- //-----------------------------------------------------------------------------
- F32 segmentSegmentNearest(const Point3F & p1, const Point3F & q1, const Point3F & p2, const Point3F & q2, F32 & s, F32 & t, Point3F & c1, Point3F & c2)
- {
- Point3F d1 = q1-p1;
- Point3F d2 = q2-p2;
- Point3F r = p1-p2;
- F32 a = mDot(d1,d1);
- F32 e = mDot(d2,d2);
- F32 f = mDot(d2,r);
-
- const F32 EPSILON = 0.001f;
-
- if (a <= EPSILON && e <= EPSILON)
- {
- s = t = 0.0f;
- c1 = p1;
- c2 = p2;
- return mDot(c1-c2,c1-c2);
- }
-
- if (a <= EPSILON)
- {
- s = 0.0f;
- t = mClampF(f/e,0.0f,1.0f);
- }
- else
- {
- F32 c = mDot(d1,r);
- if (e <= EPSILON)
- {
- t = 0.0f;
- s = mClampF(-c/a,0.0f,1.0f);
- }
- else
- {
- F32 b = mDot(d1,d2);
- F32 denom = a*e-b*b;
- if (denom != 0.0f)
- s = mClampF((b*f-c*e)/denom,0.0f,1.0f);
- else
- s = 0.0f;
- F32 tnom = b*s+f;
- if (tnom < 0.0f)
- {
- t = 0.0f;
- s = mClampF(-c/a,0.0f,1.0f);
- }
- else if (tnom>e)
- {
- t = 1.0f;
- s = mClampF((b-c)/a,0.0f,1.0f);
- }
- else
- t = tnom/e;
- }
- }
-
- c1 = p1 + d1*s;
- c2 = p2 + d2*t;
- return mDot(c1-c2,c1-c2);
- }
- //-----------------------------------------------------------------------------
- bool capsuleSphereNearestOverlap(const Point3F & A0, const Point3F A1, F32 radA, const Point3F & B, F32 radB, F32 & t)
- {
- Point3F V = A1-A0;
- Point3F A0B = A0-B;
- F32 d1 = mDot(A0B,V);
- F32 d2 = mDot(A0B,A0B);
- F32 d3 = mDot(V,V);
- F32 R2 = (radA+radB)*(radA+radB);
- if (d2<R2)
- {
- // starting in collision state
- t=0;
- return true;
- }
- if (d3<0.01f)
- // no movement, and don't start in collision state, so no collision
- return false;
- F32 b24ac = mSqrt(d1*d1-d2*d3+d3*R2);
- F32 t1 = (-d1-b24ac)/d3;
- if (t1>0 && t1<1.0f)
- {
- t=t1;
- return true;
- }
- F32 t2 = (-d1+b24ac)/d3;
- if (t2>0 && t2<1.0f)
- {
- t=t2;
- return true;
- }
- if (t1<0 && t2>0)
- {
- t=0;
- return true;
- }
- return false;
- }
- //-----------------------------------------------------------------------------
- void vectorRotateZAxis( Point3F &vector, F32 radians )
- {
- F32 sin, cos;
- mSinCos(radians, sin, cos);
- F32 x = cos * vector.x - sin * vector.y;
- F32 y = sin * vector.x + cos * vector.y;
- vector.x = x;
- vector.y = y;
- }
- void vectorRotateZAxis( F32 radians, Point3F *vectors, U32 count )
- {
- F32 sin, cos;
- mSinCos(radians, sin, cos);
- F32 x, y;
- const Point3F *end = vectors + count;
- for ( ; vectors != end; vectors++ )
- {
- x = cos * vectors->x - sin * vectors->y;
- y = sin * vectors->x + cos * vectors->y;
- vectors->x = x;
- vectors->y = y;
- }
- }
- //-----------------------------------------------------------------------------
- void getZBiasProjectionMatrix( F32 bias, const Frustum &frustum, MatrixF *outMat, bool rotate )
- {
- Frustum temp(frustum);
- temp.setNearDist(frustum.getNearDist() + bias);
- temp.getProjectionMatrix(outMat, rotate);
- }
- //-----------------------------------------------------------------------------
- MatrixF createOrientFromDir( const Point3F &direction )
- {
- Point3F j = direction;
- Point3F k(0.0f, 0.0f, 1.0f);
- Point3F i;
-
- mCross( j, k, &i );
- if( i.magnitudeSafe() == 0.0f )
- {
- i.set( 0.0f, -1.0f, 0.0f );
- }
- i.normalizeSafe();
- mCross( i, j, &k );
- MatrixF mat( true );
- mat.setColumn( 0, i );
- mat.setColumn( 1, j );
- mat.setColumn( 2, k );
- return mat;
- }
- //-----------------------------------------------------------------------------
- void getMatrixFromUpVector( const VectorF &up, MatrixF *outMat )
- {
- AssertFatal( up.isUnitLength(), "MathUtils::getMatrixFromUpVector() - Up vector was not normalized!" );
- AssertFatal( outMat, "MathUtils::getMatrixFromUpVector() - Got null output matrix!" );
- AssertFatal( outMat->isAffine(), "MathUtils::getMatrixFromUpVector() - Got uninitialized matrix!" );
- VectorF forward = mPerp( up );
- VectorF right = mCross( forward, up );
- right.normalize();
- forward = mCross( up, right );
- forward.normalize();
- outMat->setColumn( 0, right );
- outMat->setColumn( 1, forward );
- outMat->setColumn( 2, up );
- }
- //-----------------------------------------------------------------------------
- void getMatrixFromForwardVector( const VectorF &forward, MatrixF *outMat )
- {
- AssertFatal( forward.isUnitLength(), "MathUtils::getMatrixFromForwardVector() - Forward vector was not normalized!" );
- AssertFatal( outMat, "MathUtils::getMatrixFromForwardVector() - Got null output matrix!" );
- AssertFatal( outMat->isAffine(), "MathUtils::getMatrixFromForwardVector() - Got uninitialized matrix!" );
- VectorF up = mPerp( forward );
- VectorF right = mCross( forward, up );
- right.normalize();
- up = mCross( right, forward );
- up.normalize();
- outMat->setColumn( 0, right );
- outMat->setColumn( 1, forward );
- outMat->setColumn( 2, up );
- }
- //-----------------------------------------------------------------------------
- Point3F randomDir( const Point3F &axis, F32 thetaAngleMin, F32 thetaAngleMax,
- F32 phiAngleMin, F32 phiAngleMax )
- {
- MatrixF orient = createOrientFromDir( axis );
- Point3F axisx;
- orient.getColumn( 0, &axisx );
- F32 theta = (thetaAngleMax - thetaAngleMin) * sgRandom.randF() + thetaAngleMin;
- F32 phi = (phiAngleMax - phiAngleMin) * sgRandom.randF() + phiAngleMin;
- // Both phi and theta are in degs. Create axis angles out of them, and create the
- // appropriate rotation matrix...
- AngAxisF thetaRot(axisx, theta * (M_PI_F / 180.0f));
- AngAxisF phiRot(axis, phi * (M_PI_F / 180.0f));
- Point3F ejectionAxis = axis;
- MatrixF temp(true);
- thetaRot.setMatrix(&temp);
- temp.mulP(ejectionAxis);
- phiRot.setMatrix(&temp);
- temp.mulP(ejectionAxis);
- return ejectionAxis;
- }
- //-----------------------------------------------------------------------------
- Point3F randomPointInSphere( F32 radius )
- {
- AssertFatal( radius > 0.0f, "MathUtils::randomPointInRadius - radius must be positive" );
- #define MAX_TRIES 20
- Point3F out;
- F32 radiusSq = radius * radius;
- for ( S32 i = 0; i < MAX_TRIES; i++ )
- {
- out.x = sgRandom.randF(-radius,radius);
- out.y = sgRandom.randF(-radius,radius);
- out.z = sgRandom.randF(-radius,radius);
- if ( out.lenSquared() < radiusSq )
- return out;
- }
- AssertFatal( false, "MathUtils::randomPointInRadius - something is wrong, should not fail this many times." );
- return Point3F::Zero;
- }
- //-----------------------------------------------------------------------------
- Point2F randomPointInCircle( F32 radius )
- {
- AssertFatal( radius > 0.0f, "MathUtils::randomPointInRadius - radius must be positive" );
- #define MAX_TRIES 20
- Point2F out;
- F32 radiusSq = radius * radius;
- for ( S32 i = 0; i < MAX_TRIES; i++ )
- {
- out.x = sgRandom.randF(-radius,radius);
- out.y = sgRandom.randF(-radius,radius);
- if ( out.lenSquared() < radiusSq )
- return out;
- }
- AssertFatal( false, "MathUtils::randomPointInRadius - something is wrong, should not fail this many times." );
- return Point2F::Zero;
- }
- //-----------------------------------------------------------------------------
- void getAnglesFromVector( const VectorF &vec, F32 &yawAng, F32 &pitchAng )
- {
- yawAng = mAtan2( vec.x, vec.y );
- if( yawAng < 0.0f )
- yawAng += M_2PI_F;
- if( mFabs(vec.x) > mFabs(vec.y) )
- pitchAng = mAtan2( mFabs(vec.z), mFabs(vec.x) );
- else
- pitchAng = mAtan2( mFabs(vec.z), mFabs(vec.y) );
- if( vec.z < 0.0f )
- pitchAng = -pitchAng;
- }
- //-----------------------------------------------------------------------------
- void getVectorFromAngles( VectorF &vec, F32 yawAng, F32 pitchAng )
- {
- VectorF pnt( 0.0f, 1.0f, 0.0f );
- EulerF rot( -pitchAng, 0.0f, 0.0f );
- MatrixF mat( rot );
- rot.set( 0.0f, 0.0f, yawAng );
- MatrixF mat2( rot );
- mat.mulV( pnt );
- mat2.mulV( pnt );
- vec = pnt;
- }
- F32 getAngleBetweenVectors(VectorF vecA, VectorF vecB)
- {
- F32 dot = mDot(vecA, vecB);
- F32 lenSq1 = vecA.lenSquared();
- F32 lenSq2 = vecB.lenSquared();
- F32 angle = mAcos(dot / mSqrt(lenSq1 * lenSq2));
- return angle;
- }
- F32 getSignedAngleBetweenVectors(VectorF vecA, VectorF vecB, VectorF norm)
- {
- // angle in 0-180
- F32 angle = getAngleBetweenVectors(vecA, vecB);
- F32 sign = mSign(mDot(norm, mCross(vecA, vecB)));
- // angle in -179-180
- F32 signed_angle = angle * sign;
- return signed_angle;
- }
- //-----------------------------------------------------------------------------
- void transformBoundingBox(const Box3F &sbox, const MatrixF &mat, const Point3F scale, Box3F &dbox)
- {
- Point3F center;
- // set transformed center...
- sbox.getCenter(¢er);
- center.convolve(scale);
- mat.mulP(center);
- dbox.minExtents = center;
- dbox.maxExtents = center;
- Point3F val;
- for(U32 ix=0; ix<2; ix++)
- {
- if(ix & 0x1)
- val.x = sbox.minExtents.x;
- else
- val.x = sbox.maxExtents.x;
- for(U32 iy=0; iy<2; iy++)
- {
- if(iy & 0x1)
- val.y = sbox.minExtents.y;
- else
- val.y = sbox.maxExtents.y;
- for(U32 iz=0; iz<2; iz++)
- {
- if(iz & 0x1)
- val.z = sbox.minExtents.z;
- else
- val.z = sbox.maxExtents.z;
- Point3F v1, v2;
- v1 = val;
- v1.convolve(scale);
- mat.mulP(v1, &v2);
- dbox.minExtents.setMin(v2);
- dbox.maxExtents.setMax(v2);
- }
- }
- }
- }
- //-----------------------------------------------------------------------------
- bool mProjectWorldToScreen( const Point3F &in,
- Point3F *out,
- const RectI &view,
- const MatrixF &world,
- const MatrixF &projection )
- {
- MatrixF worldProjection = projection;
- worldProjection.mul(world);
- return mProjectWorldToScreen( in, out, view, worldProjection );
- }
- //-----------------------------------------------------------------------------
- bool mProjectWorldToScreen( const Point3F &in,
- Point3F *out,
- const RectI &view,
- const MatrixF &worldProjection )
- {
- Point4F temp(in.x,in.y,in.z,1.0f);
- worldProjection.mul(temp);
- // Perform the perspective division. For orthographic
- // projections, temp.w will be 1.
- temp.x /= temp.w;
- temp.y /= temp.w;
- temp.z /= temp.w;
- // Take the normalized device coordinates (NDC) and transform them
- // into device coordinates.
- out->x = (temp.x + 1.0f) / 2.0f * view.extent.x + view.point.x;
- out->y = (1.0f - temp.y) / 2.0f * view.extent.y + view.point.y;
- out->z = temp.z;
- if ( out->z < 0.0f || out->z > 1.0f ||
- out->x < (F32)view.point.x || out->x > (F32)view.point.x + (F32)view.extent.x ||
- out->y < (F32)view.point.y || out->y > (F32)view.point.y + (F32)view.extent.y )
- return false;
- return true;
- }
- //-----------------------------------------------------------------------------
- void mProjectScreenToWorld( const Point3F &in,
- Point3F *out,
- const RectI &view,
- const MatrixF &world,
- const MatrixF &projection,
- F32 zfar,
- F32 znear )
- {
- MatrixF invWorldProjection = projection;
- invWorldProjection.mul(world);
- invWorldProjection.inverse();
- Point3F vec;
- vec.x = (in.x - view.point.x) * 2.0f / view.extent.x - 1.0f;
- vec.y = -(in.y - view.point.y) * 2.0f / view.extent.y + 1.0f;
- vec.z = (znear + in.z * (zfar - znear))/zfar;
- invWorldProjection.mulV(vec);
- vec *= 1.0f + in.z * zfar;
- invWorldProjection.getColumn(3, out);
- (*out) += vec;
- }
- //-----------------------------------------------------------------------------
- bool pointInPolygon( const Point2F *verts, U32 vertCount, const Point2F &testPt )
- {
- U32 i, j, c = 0;
- for ( i = 0, j = vertCount-1; i < vertCount; j = i++ )
- {
- if ( ( ( verts[i].y > testPt.y ) != ( verts[j].y > testPt.y ) ) &&
- ( testPt.x < ( verts[j].x - verts[i].x ) *
- ( testPt.y - verts[i].y ) /
- ( verts[j].y - verts[i].y ) + verts[i].x ) )
- c = !c;
- }
-
- return c != 0;
- }
- //-----------------------------------------------------------------------------
- F32 mTriangleDistance( const Point3F &A, const Point3F &B, const Point3F &C, const Point3F &P, IntersectInfo* info )
- {
- Point3F diff = A - P;
- Point3F edge0 = B - A;
- Point3F edge1 = C - A;
- F32 a00 = edge0.lenSquared();
- F32 a01 = mDot( edge0, edge1 );
- F32 a11 = edge1.lenSquared();
- F32 b0 = mDot( diff, edge0 );
- F32 b1 = mDot( diff, edge1 );
- F32 c = diff.lenSquared();
- F32 det = mFabs(a00*a11-a01*a01);
- F32 s = a01*b1-a11*b0;
- F32 t = a01*b0-a00*b1;
- F32 sqrDistance;
- if (s + t <= det)
- {
- if (s < 0.0f)
- {
- if (t < 0.0f) // region 4
- {
- if (b0 < 0.0f)
- {
- t = 0.0f;
- if (-b0 >= a00)
- {
- s = 1.0f;
- sqrDistance = a00 + (2.0f)*b0 + c;
- }
- else
- {
- s = -b0/a00;
- sqrDistance = b0*s + c;
- }
- }
- else
- {
- s = 0.0f;
- if (b1 >= 0.0f)
- {
- t = 0.0f;
- sqrDistance = c;
- }
- else if (-b1 >= a11)
- {
- t = 1.0f;
- sqrDistance = a11 + 2.0f*b1 + c;
- }
- else
- {
- t = -b1/a11;
- sqrDistance = b1*t + c;
- }
- }
- }
- else // region 3
- {
- s = 0.0f;
- if (b1 >= 0.0f)
- {
- t = 0.0f;
- sqrDistance = c;
- }
- else if (-b1 >= a11)
- {
- t = 1.0f;
- sqrDistance = a11 + 2.0f*b1 + c;
- }
- else
- {
- t = -b1/a11;
- sqrDistance = b1*t + c;
- }
- }
- }
- else if (t < 0.0f) // region 5
- {
- t = 0.0f;
- if (b0 >= 0.0f)
- {
- s = 0.0f;
- sqrDistance = c;
- }
- else if (-b0 >= a00)
- {
- s = 1.0f;
- sqrDistance = a00 + 2.0f*b0 + c;
- }
- else
- {
- s = -b0/a00;
- sqrDistance = b0*s + c;
- }
- }
- else // region 0
- {
- // minimum at interior point
- F32 invDet = 1.0f / det;
- s *= invDet;
- t *= invDet;
- sqrDistance = s * (a00*s + a01*t + 2.0f*b0) +
- t * (a01*s + a11*t + 2.0f*b1) + c;
- }
- }
- else
- {
- F32 tmp0, tmp1, numer, denom;
- if (s < 0.0f) // region 2
- {
- tmp0 = a01 + b0;
- tmp1 = a11 + b1;
- if (tmp1 > tmp0)
- {
- numer = tmp1 - tmp0;
- denom = a00 - 2.0f*a01 + a11;
- if (numer >= denom)
- {
- s = 1.0f;
- t = 0.0f;
- sqrDistance = a00 + 2.0f*b0 + c;
- }
- else
- {
- s = numer/denom;
- t = 1.0f - s;
- sqrDistance = s * (a00*s + a01*t + 2.0f*b0) +
- t * (a01*s + a11*t + 2.0f*b1) + c;
- }
- }
- else
- {
- s = 0.0f;
- if (tmp1 <= 0.0f)
- {
- t = 1.0f;
- sqrDistance = a11 + 2.0f*b1 + c;
- }
- else if (b1 >= 0.0f)
- {
- t = 0.0f;
- sqrDistance = c;
- }
- else
- {
- t = -b1/a11;
- sqrDistance = b1*t + c;
- }
- }
- }
- else if (t < 0.0f) // region 6
- {
- tmp0 = a01 + b1;
- tmp1 = a00 + b0;
- if (tmp1 > tmp0)
- {
- numer = tmp1 - tmp0;
- denom = a00 - 2.0f*a01 + a11;
- if (numer >= denom)
- {
- t = 1.0f;
- s = 0.0f;
- sqrDistance = a11 + 2.0f*b1 + c;
- }
- else
- {
- t = numer/denom;
- s = 1.0f - t;
- sqrDistance = s * (a00*s + a01*t + 2.0f*b0) +
- t * (a01*s + a11*t + 2.0f*b1) + c;
- }
- }
- else
- {
- t = 0.0f;
- if (tmp1 <= 0.0f)
- {
- s = 1.0f;
- sqrDistance = a00 + 2.0f*b0 + c;
- }
- else if (b0 >= 0.0f)
- {
- s = 0.0f;
- sqrDistance = c;
- }
- else
- {
- s = -b0/a00;
- sqrDistance = b0*s + c;
- }
- }
- }
- else // region 1
- {
- numer = a11 + b1 - a01 - b0;
- if (numer <= 0.0f)
- {
- s = 0.0f;
- t = 1.0f;
- sqrDistance = a11 + 2.0f*b1 + c;
- }
- else
- {
- denom = a00 - 2.0f*a01 + a11;
- if (numer >= denom)
- {
- s = 1.0f;
- t = 0.0f;
- sqrDistance = a00 + 2.0f*b0 + c;
- }
- else
- {
- s = numer/denom;
- t = 1.0f - s;
- sqrDistance = s * (a00*s + a01*t + 2.0f*b0) +
- t * (a01*s + a11*t + 2.0f*b1) + c;
- }
- }
- }
- }
- // account for numerical round-off error
- if (sqrDistance < 0.0f)
- sqrDistance = 0.0f;
- // This also calculates the barycentric coordinates and the closest point!
- //m_kClosestPoint0 = P;
- //m_kClosestPoint1 = A + s*edge0 + t*edge1;
- //m_afTriangleBary[1] = s;
- //m_afTriangleBary[2] = t;
- //m_afTriangleBary[0] = (Real)1.0 - fS - fT;
- if(info)
- {
- info->segment.p0 = P;
- info->segment.p1 = A + s*edge0 + t*edge1;
- info->bary.x = s;
- info->bary.y = t;
- info->bary.z = 1.0f - s - t;
- }
- return sqrDistance;
- }
- //-----------------------------------------------------------------------------
- Point3F mTriangleNormal( const Point3F &a, const Point3F &b, const Point3F &c )
- {
- // Vector from b to a.
- const F32 ax = a.x-b.x;
- const F32 ay = a.y-b.y;
- const F32 az = a.z-b.z;
- // Vector from b to c.
- const F32 cx = c.x-b.x;
- const F32 cy = c.y-b.y;
- const F32 cz = c.z-b.z;
- Point3F n;
- // This is an in-line cross product.
- n.x = ay*cz - az*cy;
- n.y = az*cx - ax*cz;
- n.z = ax*cy - ay*cx;
- m_point3F_normalize( (F32*)(&n) );
- return n;
- }
- //-----------------------------------------------------------------------------
- Point3F mClosestPointOnSegment( const Point3F &a, const Point3F &b, const Point3F &p )
- {
- Point3F c = p - a; // Vector from a to Point
- Point3F v = (b - a);
- F32 d = v.len(); // Length of the line segment
- v.normalize(); // Unit Vector from a to b
- F32 t = mDot( v, c ); // Intersection point Distance from a
- // Check to see if the point is on the line
- // if not then return the endpoint
- if(t < 0) return a;
- if(t > d) return b;
- // get the distance to move from point a
- v *= t;
- // move from point a to the nearest point on the segment
- return a + v;
- }
- //-----------------------------------------------------------------------------
- void mShortestSegmentBetweenLines( const Line &line0, const Line &line1, LineSegment *outSegment )
- {
- // compute intermediate parameters
- Point3F w0 = line0.origin - line1.origin;
- F32 a = mDot( line0.direction, line0.direction );
- F32 b = mDot( line0.direction, line1.direction );
- F32 c = mDot( line1.direction, line1.direction );
- F32 d = mDot( line0.direction, w0 );
- F32 e = mDot( line1.direction, w0 );
- F32 denom = a*c - b*b;
- if ( denom > -0.001f && denom < 0.001f )
- {
- outSegment->p0 = line0.origin;
- outSegment->p1 = line1.origin + (e/c)*line1.direction;
- }
- else
- {
- outSegment->p0 = line0.origin + ((b*e - c*d)/denom)*line0.direction;
- outSegment->p1 = line1.origin + ((a*e - b*d)/denom)*line1.direction;
- }
- }
- //-----------------------------------------------------------------------------
- U32 greatestCommonDivisor( U32 u, U32 v )
- {
- // http://en.wikipedia.org/wiki/Binary_GCD_algorithm
-
- S32 shift;
- /* GCD(0,x) := x */
- if (u == 0 || v == 0)
- return u | v;
- /* Left shift := lg K, where K is the greatest power of 2
- dividing both u and v. */
- for (shift = 0; ((u | v) & 1) == 0; ++shift) {
- u >>= 1;
- v >>= 1;
- }
- while ((u & 1) == 0)
- u >>= 1;
- /* From here on, u is always odd. */
- do {
- while ((v & 1) == 0) /* Loop X */
- v >>= 1;
- /* Now u and v are both odd, so diff(u, v) is even.
- Let u = min(u, v), v = diff(u, v)/2. */
- if (u < v) {
- v -= u;
- } else {
- U32 diff = u - v;
- u = v;
- v = diff;
- }
- v >>= 1;
- } while (v != 0);
- return u << shift;
- }
- //-----------------------------------------------------------------------------
- bool mLineTriangleCollide( const Point3F &p1, const Point3F &p2,
- const Point3F &t1, const Point3F &t2, const Point3F &t3,
- Point3F *outUVW, F32 *outT )
- {
- VectorF ab = t2 - t1;
- VectorF ac = t3 - t1;
- VectorF qp = p1 - p2;
- // Compute triangle normal. Can be precalculated or cached if
- // intersecting multiple segments against the same triangle
- VectorF n = mCross( ab, ac );
- // Compute denominator d. If d <= 0, segment is parallel to or points
- // away from triangle, so exit early
- F32 d = mDot( qp, n );
- if ( d <= 0.0f )
- return false;
- // Compute intersection t value of pq with plane of triangle. A ray
- // intersects if 0 <= t. Segment intersects iff 0 <= t <= 1. Delay
- // dividing by d until intersection has been found to pierce triangle
- VectorF ap = p1 - t1;
- F32 t = mDot( ap, n );
- if ( t < 0.0f )
- return false;
- if ( t > d )
- return false; // For segment; exclude this code line for a ray test
- // Compute barycentric coordinate components and test if within bounds
- VectorF e = mCross( qp, ap );
- F32 v = mDot( ac, e );
- if ( v < 0.0f || v > d )
- return false;
- F32 w = -mDot( ab, e );
- if ( w < 0.0f || v + w > d )
- return false;
- // Segment/ray intersects triangle. Perform delayed division and
- // compute the last barycentric coordinate component
- const F32 ood = 1.0f / d;
-
- if ( outT )
- *outT = t * ood;
-
- if ( outUVW )
- {
- v *= ood;
- w *= ood;
- outUVW->set( 1.0f - v - w, v, w );
- }
- return true;
- }
- //-----------------------------------------------------------------------------
- bool mRayQuadCollide( const Quad &quad,
- const Ray &ray,
- Point2F *outUV,
- F32 *outT )
- {
- static const F32 eps = F32(10e-6);
- // Rejects rays that are parallel to Q, and rays that intersect the plane of
- // Q either on the left of the line V00V01 or on the right of the line V00V10.
- // p01-----eXX-----p11
- // ^ . ^ |
- // | . |
- // e03 e02 eXX
- // | . |
- // | . |
- // p00-----e01---->p10
- VectorF e01 = quad.p10 - quad.p00;
- VectorF e03 = quad.p01 - quad.p00;
- // If the ray is perfectly perpendicular to e03, which
- // represents the entire planes tangent, then the
- // result of this cross product (P) will equal e01
- // If it is parallel it will result in a vector opposite e01.
- // If the ray is heading DOWN the cross product will point to the RIGHT
- // If the ray is heading UP the cross product will point to the LEFT
- // We do not reject based on this though...
- //
- // In either case cross product will be more parallel to e01 the more
- // perpendicular the ray is to e03, and it will be more perpendicular to
- // e01 the more parallel it is to e03.
- VectorF P = mCross(ray.direction, e03);
- // det can be seen as 'the amount of vector e01 in the direction P'
- F32 det = mDot(e01, P);
- // Take a Abs of the dot because we do not care if the ray is heading up or down,
- // but if it is perfectly parallel to the quad we want to reject it.
- if ( mFabs(det) < eps )
- return false;
- F32 inv_det = 1.0f / det;
- VectorF T = ray.origin - quad.p00;
- // alpha can be seen as 'the amount of vector T in the direction P'
- // T is a vector up from the quads corner point 00 to the ray's origin.
- // P is the cross product of the ray and e01, which should be "roughly"
- // parallel with e03 but might be of either positive or negative magnitude.
- F32 alpha = mDot(T, P) * inv_det;
- if ( alpha < 0.0f )
- return false;
- // if (alpha > real(1.0)) return false; // Uncomment if VR is used.
- // The cross product of T and e01 should be roughly parallel to e03
- // and of either positive or negative magnitude.
- VectorF Q = mCross(T, e01);
- F32 beta = mDot(ray.direction, Q) * inv_det;
- if ( beta < 0.0f )
- return false;
- // if (beta > real(1.0)) return false; // Uncomment if VR is used.
- if ( alpha + beta > 1.0f )
- //if ( false )
- {
- // Rejects rays that intersect the plane of Q either on the
- // left of the line V11V10 or on the right of the line V11V01.
- VectorF e23 = quad.p01 - quad.p11;
- VectorF e21 = quad.p10 - quad.p11;
- VectorF P_prime = mCross(ray.direction, e21);
- F32 det_prime = mDot(e23, P_prime);
- if ( mFabs(det_prime) < eps)
- return false;
- F32 inv_det_prime = 1.0f / det_prime;
- VectorF T_prime = ray.origin - quad.p11;
- F32 alpha_prime = mDot(T_prime, P_prime) * inv_det_prime;
- if (alpha_prime < 0.0f)
- return false;
- VectorF Q_prime = mCross(T_prime, e23);
- F32 beta_prime = mDot(ray.direction, Q_prime) * inv_det_prime;
- if (beta_prime < 0.0f)
- return false;
- }
- // Compute the ray parameter of the intersection point, and
- // reject the ray if it does not hit Q.
- F32 t = mDot(e03, Q) * inv_det;
- if ( t < 0.0f )
- return false;
- // Compute the barycentric coordinates of the fourth vertex.
- // These do not depend on the ray, and can be precomputed
- // and stored with the quadrilateral.
- F32 alpha_11, beta_11;
- VectorF e02 = quad.p11 - quad.p00;
- VectorF n = mCross(e01, e03);
- if ( mFabs(n.x) >= mFabs(n.y) &&
- mFabs(n.x) >= mFabs(n.z) )
- {
- alpha_11 = ( e02.y * e03.z - e02.z * e03.y ) / n.x;
- beta_11 = ( e01.y * e02.z - e01.z * e02.y ) / n.x;
- }
- else if ( mFabs(n.y) >= mFabs(n.x) &&
- mFabs(n.y) >= mFabs(n.z) )
- {
- alpha_11 = ((e02.z * e03.x) - (e02.x * e03.z)) / n.y;
- beta_11 = ((e01.z * e02.x) - (e01.x * e02.z)) / n.y;
- }
- else
- {
- alpha_11 = ((e02.x * e03.y) - (e02.y * e03.x)) / n.z;
- beta_11 = ((e01.x * e02.y) - (e01.y * e02.x)) / n.z;
- }
- // Compute the bilinear coordinates of the intersection point.
- F32 u,v;
- if ( mFabs(alpha_11 - 1.0f) < eps)
- {
- // Q is a trapezium.
- u = alpha;
- if ( mFabs(beta_11 - 1.0f) < eps)
- v = beta; // Q is a parallelogram.
- else
- v = beta / ((u * (beta_11 - 1.0f)) + 1.0f); // Q is a trapezium.
- }
- else if ( mFabs(beta_11 - 1.0f) < eps)
- {
- // Q is a trapezium.
- v = beta;
- u = alpha / ((v * (alpha_11 - 1.0f)) + 1.0f);
- }
- else
- {
- F32 A = 1.0f - beta_11;
- F32 B = (alpha * (beta_11 - 1.0f))
- - (beta * (alpha_11 - 1.0f)) - 1.0f;
- F32 C = alpha;
- F32 D = (B * B) - (4.0f * A * C);
- F32 F = -0.5f * (B + (B < 0.0f ? -1.0f : 1.0f) ) * mSqrt(D);
- u = F / A;
- if ((u < 0.0f) || (u > 1.0f)) u = C / F;
- v = beta / ((u * (beta_11 - 1.0f)) + 1.0f);
- }
- if ( outUV )
- outUV->set( u, v );
- if ( outT )
- *outT = t;
- return true;
- }
- //-----------------------------------------------------------------------------
- // Used by sortQuadWindingOrder.
- struct QuadSortPoint
- {
- U32 id;
- F32 theta;
- };
- // Used by sortQuadWindingOrder.
- S32 QSORT_CALLBACK cmpAngleAscending( const void *a, const void *b )
- {
- const QuadSortPoint *p0 = (const QuadSortPoint*)a;
- const QuadSortPoint *p1 = (const QuadSortPoint*)b;
- F32 diff = p1->theta - p0->theta;
- if ( diff > 0.0f )
- return -1;
- else if ( diff < 0.0f )
- return 1;
- else
- return 0;
- }
- // Used by sortQuadWindingOrder.
- S32 QSORT_CALLBACK cmpAngleDescending( const void *a, const void *b )
- {
- const QuadSortPoint *p0 = (const QuadSortPoint*)a;
- const QuadSortPoint *p1 = (const QuadSortPoint*)b;
- F32 diff = p1->theta - p0->theta;
- if ( diff > 0.0f )
- return 1;
- else if ( diff < 0.0f )
- return -1;
- else
- return 0;
- }
- void sortQuadWindingOrder( const MatrixF &quadMat, bool clockwise, const Point3F *verts, U32 *vertMap, U32 count )
- {
- PROFILE_SCOPE( MathUtils_sortQuadWindingOrder );
- if ( count == 0 )
- return;
-
- Point3F *quadPoints = new Point3F[count];
-
- for ( S32 i = 0; i < count; i++ )
- {
- quadMat.mulP( verts[i], &quadPoints[i] );
- quadPoints[i].normalizeSafe();
- }
- sortQuadWindingOrder( clockwise, quadPoints, vertMap, count );
- delete [] quadPoints;
- }
- void sortQuadWindingOrder( bool clockwise, const Point3F *verts, U32 *vertMap, U32 count )
- {
- QuadSortPoint *sortPoints = new QuadSortPoint[count];
- for ( S32 i = 0; i < count; i++ )
- {
- QuadSortPoint &sortPnt = sortPoints[i];
- const Point3F &vec = verts[i];
- sortPnt.id = i;
- F32 theta = mAtan2( vec.y, vec.x );
- if ( vec.y < 0.0f )
- theta = M_2PI_F + theta;
- sortPnt.theta = theta;
- }
- dQsort( sortPoints, count, sizeof( QuadSortPoint ), clockwise ? cmpAngleDescending : cmpAngleAscending );
- for ( S32 i = 0; i < count; i++ )
- vertMap[i] = sortPoints[i].id;
- delete [] sortPoints;
- }
- //-----------------------------------------------------------------------------
- void buildMatrix( const VectorF *rvec, const VectorF *fvec, const VectorF *uvec, const VectorF *pos, MatrixF *outMat )
- {
- /// Work in Progress
- /*
- AssertFatal( !rvec || rvec->isUnitLength(), "MathUtils::buildMatrix() - Right vector was not normalized!" );
- AssertFatal( !fvec || fvec->isUnitLength(), "MathUtils::buildMatrix() - Forward vector was not normalized!" );
- AssertFatal( !uvec || uvec->isUnitLength(), "MathUtils::buildMatrix() - Up vector was not normalized!" );
- // Note this relationship:
- //
- // Column0 Column1 Column2
- // Axis X Axis Y Axis Z
- // Rvec Fvec Uvec
- //
- enum
- {
- RVEC = 1,
- FVEC = 1 << 1,
- UVEC = 1 << 2,
- ALL = RVEC | FVEC | UVEC
- };
- U8 mask = 0;
- U8 count = 0;
- U8 axis0, axis1;
- if ( rvec )
- {
- mask |= RVEC;
- axis0 == 0;
- count++;
- }
- if ( fvec )
- {
- mask |= FVEC;
- if ( count == 0 )
- axis0 = 1;
- else
- axis1 = 1;
- count++;
- }
- if ( uvec )
- {
- mask |= UVEC;
- count++;
- }
- U8 bR = 1;
- U8 bF = 1 << 1;
- U8 bU = 1 << 2;
- U8 bRF = bR | bF;
- U8 bRU = bR | bU;
- U8 bFU = bF | bU;
- U8 bRFU = bR | bF | bU;
-
- // Cross product map.
- U8 cpdMap[3][2] =
- {
- { 1, 2 },
- { 2, 0 },
- { 0, 1 },
- }
- if ( count == 1 )
- {
- if ( mask == bR )
- {
-
- }
- else if ( mask == bF )
- {
- }
- else if ( mask == bU )
- {
- }
- }
- else if ( count == 2 )
- {
- if ( mask == bRF )
- {
- }
- else if ( mask == bRU )
- {
- }
- else if ( mask == bFU )
- {
- }
- }
- else // bRFU
- {
- }
- if ( rvec )
- {
- outMat->setColumn( 0, *rvec );
- if ( fvec )
- {
- outMat->setColumn( 1, *fvec );
- if ( uvec )
- outMat->setColumn( 2, *uvec );
- else
- {
- // Set uvec from rvec/fvec
- tmp = mCross( rvec, fvec );
- tmp.normalizeSafe();
- outMat->setColumn( 2, tmp );
- }
- }
- else if ( uvec )
- {
- // Set fvec from uvec/rvec
- tmp = mCross( uvec, rvec );
- tmp.normalizeSafe();
- outMat->setColumn( 1, tmp );
- }
- else
- {
- // Set fvec and uvec from rvec
- Point3F tempFvec = mPerp( rvec );
- Point3F tempUvec = mCross( )
- }
- }
- AssertFatal( rvec->isUnitLength(), "MathUtils::buildMatrix() - Right vector was not normalized!" );
- AssertFatal( fvec->isUnitLength(), "MathUtils::buildMatrix() - Forward vector was not normalized!" );
- AssertFatal( uvec->isUnitLength(), "MathUtils::buildMatrix() - UpVector vector was not normalized!" );
- AssertFatal( outMat, "MathUtils::buildMatrix() - Got null output matrix!" );
- AssertFatal( outMat->isAffine(), "MathUtils::buildMatrix() - Got uninitialized matrix!" );
- */
- }
- //-----------------------------------------------------------------------------
- bool reduceFrustum( const Frustum& frustum, const RectI& viewport, const RectF& area, Frustum& outFrustum )
- {
- // Just to be safe, clamp the area to the viewport.
- Point2F clampedMin;
- Point2F clampedMax;
- clampedMin.x = mClampF( area.extent.x, ( F32 ) viewport.point.x, ( F32 ) viewport.point.x + viewport.extent.x );
- clampedMin.y = mClampF( area.extent.y, ( F32 ) viewport.point.y, ( F32 ) viewport.point.y + viewport.extent.y );
- clampedMax.x = mClampF( area.extent.x, ( F32 ) viewport.point.x, ( F32 ) viewport.point.x + viewport.extent.x );
- clampedMax.y = mClampF( area.extent.y, ( F32 ) viewport.point.y, ( F32 ) viewport.point.y + viewport.extent.y );
- // If we have ended up without a visible region on the screen,
- // terminate now.
-
- if( mFloor( clampedMin.x ) == mFloor( clampedMax.x ) ||
- mFloor( clampedMin.y ) == mFloor( clampedMax.y ) )
- return false;
- // Get the extents of the frustum.
- const F32 frustumXExtent = mFabs( frustum.getNearRight() - frustum.getNearLeft() );
- const F32 frustumYExtent = mFabs( frustum.getNearTop() - frustum.getNearBottom() );
- // Now, normalize the screen-space pixel coordinates to lie within the screen-centered
- // -1 to 1 coordinate space that is used for the frustum planes.
- Point2F normalizedMin;
- Point2F normalizedMax;
- normalizedMin.x = ( ( clampedMin.x / viewport.extent.x ) * frustumXExtent ) - ( frustumXExtent / 2.f );
- normalizedMin.y = ( ( clampedMin.y / viewport.extent.y ) * frustumYExtent ) - ( frustumYExtent / 2.f );
- normalizedMax.x = ( ( clampedMax.x / viewport.extent.x ) * frustumXExtent ) - ( frustumXExtent / 2.f );
- normalizedMax.y = ( ( clampedMax.y / viewport.extent.y ) * frustumYExtent ) - ( frustumYExtent / 2.f );
- // Make sure the generated frustum metrics are somewhat sane.
- if( normalizedMax.x - normalizedMin.x < 0.001f ||
- normalizedMax.y - normalizedMin.y < 0.001f )
- return false;
-
- // Finally, create the new frustum using the original's frustum
- // information except its left/right/top/bottom planes.
- //
- // Note that screen-space coordinates go upside down on Y whereas
- // camera-space frustum coordinates go downside up on Y which is
- // why we are inverting Y here.
- outFrustum.set(
- frustum.isOrtho(),
- normalizedMin.x,
- normalizedMax.x,
- - normalizedMin.y,
- - normalizedMax.y,
- frustum.getNearDist(),
- frustum.getFarDist(),
- frustum.getTransform()
- );
- return true;
- }
- //-----------------------------------------------------------------------------
- void makeFrustum( F32 *outLeft,
- F32 *outRight,
- F32 *outTop,
- F32 *outBottom,
- F32 fovYInRadians,
- F32 aspectRatio,
- F32 nearPlane )
- {
- F32 top = nearPlane * mTan( fovYInRadians / 2.0 );
- if ( outTop ) *outTop = top;
- if ( outBottom ) *outBottom = -top;
- F32 left = top * aspectRatio;
- if ( outLeft ) *outLeft = -left;
- if ( outRight ) *outRight = left;
- }
- //-----------------------------------------------------------------------------
- void makeProjection( MatrixF *outMatrix,
- F32 fovYInRadians,
- F32 aspectRatio,
- F32 nearPlane,
- F32 farPlane,
- bool gfxRotate )
- {
- F32 left, right, top, bottom;
- makeFrustum( &left, &right, &top, &bottom, fovYInRadians, aspectRatio, nearPlane );
- makeProjection( outMatrix, left, right, top, bottom, nearPlane, farPlane, gfxRotate );
- }
- //-----------------------------------------------------------------------------
- void makeFovPortFrustum(
- Frustum *outFrustum,
- bool isOrtho,
- F32 nearDist,
- F32 farDist,
- const FovPort &inPort,
- const MatrixF &transform)
- {
- F32 leftSize = nearDist * inPort.leftTan;
- F32 rightSize = nearDist * inPort.rightTan;
- F32 upSize = nearDist * inPort.upTan;
- F32 downSize = nearDist * inPort.downTan;
- F32 left = -leftSize;
- F32 right = rightSize;
- F32 top = upSize;
- F32 bottom = -downSize;
- outFrustum->set(isOrtho, left, right, top, bottom, nearDist, farDist, transform);
- }
- //-----------------------------------------------------------------------------
- /// This is the special rotation matrix applied to
- /// projection matricies for GFX.
- ///
- /// It is a wart of the OGL to DX change over.
- ///
- static const MatrixF sGFXProjRotMatrix( EulerF( (M_PI_F / 2.0f), 0.0f, 0.0f ) );
- void makeProjection( MatrixF *outMatrix,
- F32 left,
- F32 right,
- F32 top,
- F32 bottom,
- F32 nearPlane,
- F32 farPlane,
- bool gfxRotate)
- {
- const bool isGL = GFX->getAdapterType() == OpenGL;
- Point4F row;
- row.x = 2.0f * nearPlane / (right - left);
- row.y = 0.0f;
- row.z = 0.0f;
- row.w = 0.0f;
- outMatrix->setRow(0, row);
- row.x = 0.0f;
- row.y = 2.0f * nearPlane / (top - bottom);
- row.z = 0.0f;
- row.w = 0.0f;
- outMatrix->setRow(1, row);
- row.x = (left + right) / (right - left);
- row.y = (top + bottom) / (top - bottom);
- row.z = isGL ? (nearPlane + farPlane) / (nearPlane - farPlane) : farPlane / (nearPlane - farPlane);
- row.w = -1.0f;
- outMatrix->setRow(2, row);
- row.x = 0.0f;
- row.y = 0.0f;
- row.z = isGL ? 2.0f * nearPlane * farPlane / (nearPlane - farPlane) : farPlane * nearPlane / (nearPlane - farPlane);
- row.w = 0.0f;
- outMatrix->setRow(3, row);
- outMatrix->transpose();
- if (gfxRotate)
- outMatrix->mul(sGFXProjRotMatrix);
- }
- //-----------------------------------------------------------------------------
- void makeOrthoProjection( MatrixF *outMatrix,
- F32 left,
- F32 right,
- F32 top,
- F32 bottom,
- F32 nearPlane,
- F32 farPlane,
- bool gfxRotate )
- {
- Point4F row;
- row.x = 2.0f / (right - left);
- row.y = 0.0f;
- row.z = 0.0f;
- row.w = 0.0f;
- outMatrix->setRow( 0, row );
- row.x = 0.0f;
- row.y = 2.0f / (top - bottom);
- row.z = 0.0f;
- row.w = 0.0f;
- outMatrix->setRow( 1, row );
- row.x = 0.0f;
- row.y = 0.0f;
- row.z = 1.0f / (nearPlane - farPlane);
- row.w = 0.0f;
- outMatrix->setRow( 2, row );
- row.x = (left + right) / (left - right);
- row.y = (top + bottom) / (bottom - top);
- row.z = nearPlane / (nearPlane - farPlane);
- row.w = 1.0f;
- outMatrix->setRow( 3, row );
- outMatrix->transpose();
- if ( gfxRotate )
- outMatrix->mul( sGFXProjRotMatrix );
- }
- //-----------------------------------------------------------------------------
- bool edgeFaceIntersect( const Point3F &edgeA, const Point3F &edgeB,
- const Point3F &faceA, const Point3F &faceB, const Point3F &faceC, const Point3F &faceD, Point3F *intersection )
- {
- VectorF edgeAB = edgeB - edgeA;
- VectorF edgeAFaceA = faceA - edgeA;
- VectorF edgeAFaceB = faceB - edgeA;
- VectorF edgeAFaceC = faceC - edgeA;
- VectorF m = mCross( edgeAFaceC, edgeAB );
- F32 v = mDot( edgeAFaceA, m );
- if ( v >= 0.0f )
- {
- F32 u = -mDot( edgeAFaceB, m );
- if ( u < 0.0f )
- return false;
- VectorF tmp = mCross( edgeAFaceB, edgeAB );
- F32 w = mDot( edgeAFaceA, tmp );
- if ( w < 0.0f )
- return false;
- F32 denom = 1.0f / (u + v + w );
- u *= denom;
- v *= denom;
- w *= denom;
- (*intersection) = u * faceA + v * faceB + w * faceC;
- }
- else
- {
- VectorF edgeAFaceD = faceD - edgeA;
- F32 u = mDot( edgeAFaceD, m );
- if ( u < 0.0f )
- return false;
- VectorF tmp = mCross( edgeAFaceA, edgeAB );
- F32 w = mDot( edgeAFaceD, tmp );
- if ( w < 0.0f )
- return false;
- v = -v;
- F32 denom = 1.0f / ( u + v + w );
- u *= denom;
- v *= denom;
- w *= denom;
- (*intersection) = u * faceA + v * faceD + w * faceC;
- }
- return true;
- }
- //-----------------------------------------------------------------------------
- bool isPlanarPolygon( const Point3F* vertices, U32 numVertices )
- {
- AssertFatal( vertices != NULL, "MathUtils::isPlanarPolygon - Received NULL pointer" );
- AssertFatal( numVertices >= 3, "MathUtils::isPlanarPolygon - Must have at least three vertices" );
- // Triangles are always planar. Letting smaller numVertices
- // slip through provides robustness for errors in release builds.
-
- if( numVertices <= 3 )
- return true;
- // Compute the normal of the first triangle in the polygon.
-
- Point3F triangle1Normal = mTriangleNormal( vertices[ 0 ], vertices[ 1 ], vertices[ 2 ] );
- // Now go through all the remaining vertices and build triangles
- // with the first two vertices. Then the normals of all these triangles
- // must be the same (minus some variance due to floating-point inaccuracies)
- // as the normal of the first triangle.
- for( U32 i = 3; i < numVertices; ++ i )
- {
- Point3F triangle2Normal = mTriangleNormal( vertices[ 0 ], vertices[ 1 ], vertices[ i ] );
- if( !triangle1Normal.equal( triangle2Normal ) )
- return false;
- }
- return true;
- }
- //-----------------------------------------------------------------------------
- bool isConvexPolygon( const Point3F* vertices, U32 numVertices )
- {
- AssertFatal( vertices != NULL, "MathUtils::isConvexPolygon - Received NULL pointer" );
- AssertFatal( numVertices >= 3, "MathUtils::isConvexPolygon - Must have at least three vertices" );
- // Triangles are always convex. Letting smaller numVertices
- // slip through provides robustness for errors in release builds.
- if( numVertices <= 3 )
- return true;
- U32 numPositive = 0;
- U32 numNegative = 0;
- for( U32 i = 0; i < numVertices; ++ i )
- {
- const Point3F& a = vertices[ i ];
- const Point3F& b = vertices[ ( i + 1 ) % numVertices ];
- const Point3F& c = vertices[ ( i + 2 ) % numVertices ];
- const F32 crossProductLength = mCross( b - a, c - b ).len();
-
- if( crossProductLength < 0.f )
- numNegative ++;
- else if( crossProductLength > 0.f )
- numPositive ++;
- if( numNegative && numPositive )
- return false;
- }
- return true;
- }
- //-----------------------------------------------------------------------------
- bool clipFrustumByPolygon( const Point3F* points, U32 numPoints, const RectI& viewport, const MatrixF& world,
- const MatrixF& projection, const Frustum& inFrustum, const Frustum& rootFrustum, Frustum& outFrustum )
- {
- enum
- {
- MAX_RESULT_VERTICES = 64,
- MAX_INPUT_VERTICES = MAX_RESULT_VERTICES - Frustum::PlaneCount // Clipping against each plane may add a vertex.
- };
- AssertFatal( numPoints <= MAX_INPUT_VERTICES, "MathUtils::clipFrustumByPolygon - Too many vertices!" );
- if( numPoints > MAX_INPUT_VERTICES )
- return false;
- // First, we need to clip the polygon against inFrustum.
- //
- // Use two buffers here in interchanging roles as sources and targets
- // in clipping against the frustum planes.
- Point3F polygonBuffer1[ MAX_RESULT_VERTICES ];
- Point3F polygonBuffer2[ MAX_RESULT_VERTICES ];
- Point3F* tempPolygon = polygonBuffer1;
- Point3F* clippedPolygon = polygonBuffer2;
- dMemcpy( clippedPolygon, points, numPoints * sizeof( points[ 0 ] ) );
- U32 numClippedPolygonVertices = numPoints;
- U32 numTempPolygonVertices = 0;
- for( U32 nplane = 0; nplane < Frustum::PlaneCount; ++ nplane )
- {
- // Make the output of the last iteration the
- // input of this iteration.
- T3D::swap( tempPolygon, clippedPolygon );
- numTempPolygonVertices = numClippedPolygonVertices;
- // Clip our current remainder of the original polygon
- // against the current plane.
- const PlaneF& plane = inFrustum.getPlanes()[ nplane ];
- numClippedPolygonVertices = plane.clipPolygon( tempPolygon, numTempPolygonVertices, clippedPolygon );
- // If the polygon was completely on the backside of the plane,
- // then polygon is outside the frustum. In this case, return false
- // to indicate we haven't clipped anything.
- if( !numClippedPolygonVertices )
- return false;
- }
- // Project the clipped polygon into screen space.
- MatrixF worldProjection = projection;
- worldProjection.mul( world ); // Premultiply world*projection so we don't have to do this over and over for each point.
- Point3F projectedPolygon[ 10 ];
- for( U32 i = 0; i < numClippedPolygonVertices; ++ i )
- mProjectWorldToScreen(
- clippedPolygon[ i ],
- &projectedPolygon[ i ],
- viewport,
- worldProjection
- );
- // Put an axis-aligned rectangle around our polygon.
- Point2F minPoint( projectedPolygon[ 0 ].x, projectedPolygon[ 0 ].y );
- Point2F maxPoint( projectedPolygon[ 0 ].x, projectedPolygon[ 0 ].y );
- for( U32 i = 1; i < numClippedPolygonVertices; ++ i )
- {
- minPoint.setMin( Point2F( projectedPolygon[ i ].x, projectedPolygon[ i ].y ) );
- maxPoint.setMax( Point2F( projectedPolygon[ i ].x, projectedPolygon[ i ].y ) );
- }
- RectF area( minPoint, maxPoint - minPoint );
- // Finally, reduce the input frustum to the given area. Note that we
- // use rootFrustum here instead of inFrustum as the latter does not necessarily
- // represent the full viewport we are using here which thus would skew the mapping.
- return reduceFrustum( rootFrustum, viewport, area, outFrustum );
- }
- //-----------------------------------------------------------------------------
- U32 extrudePolygonEdges( const Point3F* vertices, U32 numVertices, const Point3F& direction, PlaneF* outPlanes )
- {
- U32 numPlanes = 0;
- U32 lastVertex = numVertices - 1;
- bool invert = false;
- for( U32 i = 0; i < numVertices; lastVertex = i, ++ i )
- {
- const Point3F& v1 = vertices[ i ];
- const Point3F& v2 = vertices[ lastVertex ];
- // Skip the edge if it's length is really short.
- const Point3F edgeVector = v2 - v1;
- if( edgeVector.len() < 0.05 )
- continue;
- // Compute the plane normal. The direction and the edge vector
- // basically define the orientation of the plane so their cross
- // product is the plane normal.
- Point3F normal;
- if( !invert )
- normal = mCross( edgeVector, direction );
- else
- normal = mCross( direction, edgeVector );
- // Create a plane for the edge.
- outPlanes[ numPlanes ] = PlaneF( v1, normal );
- numPlanes ++;
- // If this is the first plane that we have created, find out whether
- // the vertex ordering is giving us the plane orientations that we want
- // (facing inside). If not, invert vertex order from now on.
- if( i == 0 )
- {
- const PlaneF& plane = outPlanes[ numPlanes - 1 ];
- for( U32 n = i + 1; n < numVertices; ++ n )
- {
- const PlaneF::Side side = plane.whichSide( vertices[ n ] );
- if( side == PlaneF::On )
- continue;
- if( side != PlaneF::Front )
- invert = true;
- break;
- }
- }
- }
- return numPlanes;
- }
- //-----------------------------------------------------------------------------
- U32 extrudePolygonEdgesFromPoint( const Point3F* vertices, U32 numVertices, const Point3F& fromPoint, PlaneF* outPlanes )
- {
- U32 numPlanes = 0;
- U32 lastVertex = numVertices - 1;
- bool invert = false;
- for( U32 i = 0; i < numVertices; lastVertex = i, ++ i )
- {
- const Point3F& v1 = vertices[ i ];
- const Point3F& v2 = vertices[ lastVertex ];
- // Skip the edge if it's length is really short.
- const Point3F edgeVector = v2 - v1;
- if( edgeVector.len() < 0.05 )
- continue;
- // Create a plane for the edge.
- if( !invert )
- outPlanes[ numPlanes ] = PlaneF( v1, fromPoint, v2 );
- else
- outPlanes[ numPlanes ] = PlaneF( v2, fromPoint, v1 );
-
- numPlanes ++;
- // If this is the first plane that we have created, find out whether
- // the vertex ordering is giving us the plane orientations that we want
- // (facing inside). If not, invert vertex order from now on.
- if( i == 0 )
- {
- const PlaneF& plane = outPlanes[ numPlanes - 1 ];
- for( U32 n = i + 1; n < numVertices; ++ n )
- {
- const PlaneF::Side side = plane.whichSide( vertices[ n ] );
- if( side == PlaneF::On )
- continue;
- if( side != PlaneF::Front )
- invert = true;
- break;
- }
- }
- }
- return numPlanes;
- }
- //-----------------------------------------------------------------------------
- void mBuildHull2D(const Vector<Point2F> _inPoints, Vector<Point2F> &hullPoints)
- {
- /// Andrew's monotone chain convex hull algorithm implementation
- struct Util
- {
- //compare by x and then by y
- static int CompareLexicographic( const Point2F *a, const Point2F *b)
- {
- return a->x < b->x || (a->x == b->x && a->y < b->y);
- }
- };
- hullPoints.clear();
- hullPoints.setSize( _inPoints.size()*2 );
- // sort in points by x and then by y
- Vector<Point2F> inSortedPoints = _inPoints;
- inSortedPoints.sort( &Util::CompareLexicographic );
- Point2F* lowerHullPtr = hullPoints.address();
- U32 lowerHullIdx = 0;
- //lower part of hull
- for( int i = 0; i < inSortedPoints.size(); ++i )
- {
- while( lowerHullIdx >= 2 && mCross( lowerHullPtr[ lowerHullIdx - 2], lowerHullPtr[lowerHullIdx - 1], inSortedPoints[i] ) <= 0 )
- --lowerHullIdx;
- lowerHullPtr[lowerHullIdx++] = inSortedPoints[i];
- }
- --lowerHullIdx; // last point are the same as first in upperHullPtr
- Point2F* upperHullPtr = hullPoints.address() + lowerHullIdx;
- U32 upperHullIdx = 0;
- //upper part of hull
- for( int i = inSortedPoints.size()-1; i >= 0; --i )
- {
- while( upperHullIdx >= 2 && mCross( upperHullPtr[ upperHullIdx - 2], upperHullPtr[upperHullIdx - 1], inSortedPoints[i] ) <= 0 )
- --upperHullIdx;
- upperHullPtr[upperHullIdx++] = inSortedPoints[i];
- }
- hullPoints.setSize( lowerHullIdx + upperHullIdx );
- }
- } // namespace MathUtils
|