//----------------------------------------------------------------// void ZLFrustum::Init ( const ZLMatrix4x4& invViewProjMtx ) { // set up the homogenous coordinates of the canonical view volume ZLVec3D nlt ( -1.0f, 1.0f, -1.0f ); ZLVec3D nrt ( 1.0f, 1.0f, -1.0f ); ZLVec3D nrb ( 1.0f, -1.0f, -1.0f ); ZLVec3D nlb ( -1.0f, -1.0f, -1.0f ); ZLVec3D flt ( -1.0f, 1.0f, 1.0f ); ZLVec3D frt ( 1.0f, 1.0f, 1.0f ); ZLVec3D frb ( 1.0f, -1.0f, 1.0f ); ZLVec3D flb ( -1.0f, -1.0f, 1.0f ); // compute the corners of the frustum invViewProjMtx.Project ( nlt ); invViewProjMtx.Project ( nrt ); invViewProjMtx.Project ( nrb ); invViewProjMtx.Project ( nlb ); invViewProjMtx.Project ( flt ); invViewProjMtx.Project ( frt ); invViewProjMtx.Project ( frb ); invViewProjMtx.Project ( flb ); this->mPoints [ NEAR_LT_POINT ].Init ( nlt.mX, nlt.mY, nlt.mZ ); this->mPoints [ NEAR_RT_POINT ].Init ( nrt.mX, nrt.mY, nrt.mZ ); this->mPoints [ NEAR_RB_POINT ].Init ( nrb.mX, nrb.mY, nrb.mZ ); this->mPoints [ NEAR_LB_POINT ].Init ( nlb.mX, nlb.mY, nlb.mZ ); this->mPoints [ FAR_LT_POINT ].Init ( flt.mX, flt.mY, flt.mZ ); this->mPoints [ FAR_RT_POINT ].Init ( frt.mX, frt.mY, frt.mZ ); this->mPoints [ FAR_RB_POINT ].Init ( frb.mX, frb.mY, frb.mZ ); this->mPoints [ FAR_LB_POINT ].Init ( flb.mX, flb.mY, flb.mZ ); // Compute the frustum's axis-aligned bounding box this->mAABB.Init ( nlt ); this->mAABB.Grow ( nrt ); this->mAABB.Grow ( nrb ); this->mAABB.Grow ( nlb ); this->mAABB.Grow ( flt ); this->mAABB.Grow ( frt ); this->mAABB.Grow ( frb ); this->mAABB.Grow ( flb ); // intialize the planes this->mPlanes [ LEFT_PLANE ].Init ( nlt, flt, flb ); this->mPlanes [ RIGHT_PLANE ].Init ( nrb, frb, frt ); this->mPlanes [ TOP_PLANE ].Init ( nrt, frt, flt ); this->mPlanes [ BOTTOM_PLANE ].Init ( nlb, flb, frb ); this->mPlanes [ NEAR_PLANE ].Init ( nrt, nlt, nlb ); this->mPlanes [ FAR_PLANE ].Init ( flt, frt, frb ); double frustArea = _frustArea ( *this ); double boxArea = this->mAABB.Area (); this->mUsePlanesForCull = (( float )( frustArea / boxArea )) < MIN_FILL_RATIO; }
//----------------------------------------------------------------// void ZLPrism::Transform ( const ZLMatrix4x4& mtx ) { mtx.Transform ( mLoc ); mtx.TransformVec ( mXAxis ); mtx.TransformVec ( mYAxis ); mtx.TransformVec ( mZAxis ); }
//----------------------------------------------------------------// ZLRect MOAILayoutFrame::GetScissorRect () { ZLRect scissorRect = this->GetFrame (); ZLMatrix4x4 mtx; // TODO: mtx.Init ( this->mLocalToWorldMtx ); //mtx.Append ( MOAIGfxDevice::Get ().GetWorldToWndMtx ( 1.0f, 1.0f )); mtx.Transform ( scissorRect ); return scissorRect; }
//----------------------------------------------------------------// ZLRect MOAIScissorRect::GetScissorRect ( const ZLMatrix4x4& worldToWndMtx ) const { ZLVec3D vtx3D [ 4 ]; vtx3D [ 0 ].mX = this->mRect.mXMin; vtx3D [ 0 ].mY = this->mRect.mYMin; vtx3D [ 0 ].mZ = 0.0f; vtx3D [ 1 ].mX = this->mRect.mXMin; vtx3D [ 1 ].mY = this->mRect.mYMax; vtx3D [ 1 ].mZ = 0.0f; vtx3D [ 2 ].mX = this->mRect.mXMax; vtx3D [ 2 ].mY = this->mRect.mYMax; vtx3D [ 2 ].mZ = 0.0f; vtx3D [ 3 ].mX = this->mRect.mXMax; vtx3D [ 3 ].mY = this->mRect.mYMin; vtx3D [ 3 ].mZ = 0.0f; ZLMatrix4x4 mtx; mtx.Init ( this->GetLocalToWorldMtx ()); mtx.Append ( worldToWndMtx ); mtx.Project ( vtx3D [ 0 ]); mtx.Project ( vtx3D [ 1 ]); mtx.Project ( vtx3D [ 2 ]); mtx.Project ( vtx3D [ 3 ]); ZLRect scissorRect; scissorRect.Init ( vtx3D [ 0 ]); scissorRect.Grow ( vtx3D [ 1 ]); scissorRect.Grow ( vtx3D [ 2 ]); scissorRect.Grow ( vtx3D [ 3 ]); if ( this->mScissorRect ) { ZLRect parentRect = this->mScissorRect->GetScissorRect ( worldToWndMtx ); parentRect.Clip ( scissorRect ); } return scissorRect; }
//----------------------------------------------------------------// void MOAIDraw::DrawRay ( float x, float y, float dx, float dy ) { USVec2D loc ( x, y ); USVec2D vec ( dx, dy ); ZLMatrix4x4 mtx = MOAIGfxDevice::Get ().GetViewProjMtx (); ZLMatrix4x4 invMtx; invMtx.Inverse ( mtx ); mtx.Transform ( loc ); mtx.TransformVec ( vec ); ZLRect viewRect; viewRect.Init ( -1.0f, -1.0f, 1.0f, 1.0f ); USVec2D p0; USVec2D p1; if ( viewRect.GetIntersection ( loc, vec, p0, p1 )) { invMtx.Transform ( p0 ); invMtx.Transform ( p1 ); MOAIGfxDevice& gfxDevice = MOAIGfxDevice::Get (); gfxDevice.BeginPrim ( ZGL_PRIM_LINES ); gfxDevice.WriteVtx ( p0.mX, p0.mY, 0.0f ); gfxDevice.WriteFinalColor4b (); gfxDevice.WriteVtx ( p1.mX, p1.mY, 0.0f ); gfxDevice.WriteFinalColor4b (); gfxDevice.EndPrim (); } }
//----------------------------------------------------------------// void ZLQuaternion::Get ( ZLMatrix4x4& m ) const { ZLAffine3D affine; this->Get ( affine ); m.Init ( affine ); }
//----------------------------------------------------------------// void MOAIDraw::DrawAxisGrid ( USVec2D loc, USVec2D vec, float size ) { ZLMatrix4x4 mtx = MOAIGfxDevice::Get ().GetViewProjMtx (); ZLMatrix4x4 invMtx; invMtx.Inverse ( mtx ); // Set the axis to the grid length so we can get the length back post-transform vec.SetLength ( size ); mtx.Transform ( loc ); mtx.TransformVec ( vec ); // Get the axis unit vector USVec2D norm = vec; size = norm.NormSafe (); // Get the axis normal USVec2D perpNorm ( norm.mY, -norm.mX ); // Project the corners of the viewport onto the axis to get the mix/max bounds float dot; float min; float max; USVec2D corner; // left, top corner.Init ( -1.0f, 1.0f ); corner.Sub ( loc ); dot = norm.Dot ( corner ); min = dot; max = dot; // right, top corner.Init ( 1.0f, 1.0f ); corner.Sub ( loc ); dot = norm.Dot ( corner ); min = ( dot < min ) ? dot : min; max = ( dot > max ) ? dot : max; // right, bottom corner.Init ( 1.0f, -1.0f ); corner.Sub ( loc ); dot = norm.Dot ( corner ); min = ( dot < min ) ? dot : min; max = ( dot > max ) ? dot : max; // left, bottom corner.Init ( -1.0f, -1.0f ); corner.Sub ( loc ); dot = norm.Dot ( corner ); min = ( dot < min ) ? dot : min; max = ( dot > max ) ? dot : max; // Get the start andstop grids s32 start = ( s32 )( min / size ) - 1; s32 stop = ( s32 )( max / size ) + 1; // Set the pen to the first... USVec2D pen = norm; pen.Scale (( float )start * size ); pen.Add ( loc ); // Step along the axis to draw perpendicular grid lines ZLRect viewRect; viewRect.Init ( -1.0f, -1.0f, 1.0f, 1.0f ); for ( ; start < stop; ++start ) { USVec2D p0; USVec2D p1; if ( viewRect.GetIntersection ( pen, perpNorm, p0, p1 )) { invMtx.Transform ( p0 ); invMtx.Transform ( p1 ); MOAIDraw::DrawLine ( p0, p1 ); } pen.Add ( vec ); } }