//----------------------------------------------------------------// USMatrix4x4 MOAIGfxDevice::GetWndToWorldMtx () const { USMatrix4x4 wndToWorld = MOAIGfxDevice::GetWndToNormMtx (); // inv viewproj USMatrix4x4 mtx = this->GetViewProjMtx (); mtx.Inverse (); wndToWorld.Append ( mtx ); return wndToWorld; }
//----------------------------------------------------------------// USQuad MOAIGfxDevice::GetViewQuad () const { USQuad quad; USMatrix4x4 invMtx; invMtx.Inverse ( this->GetViewProjMtx ()); quad.mV [ 0 ].Init ( -1.0f, 1.0f ); quad.mV [ 1 ].Init ( 1.0f, 1.0f ); quad.mV [ 2 ].Init ( 1.0f, -1.0f ); quad.mV [ 3 ].Init ( -1.0f, -1.0f ); invMtx.TransformQuad ( quad.mV ); return quad; }
//----------------------------------------------------------------// USMatrix4x4 MOAIGfxDevice::GetWndToWorldMtx () const { USMatrix4x4 wndToWorld; USMatrix4x4 mtx; USRect rect = this->GetViewRect (); float hWidth = rect.Width () * 0.5f; float hHeight = rect.Height () * 0.5f; // Inv Wnd wndToWorld.Translate ( -hWidth - rect.mXMin, -hHeight - rect.mYMin, 0.0f ); mtx.Scale (( 1.0f / hWidth ), -( 1.0f / hHeight ), 1.0f ); wndToWorld.Append ( mtx ); // inv viewproj mtx = this->GetViewProjMtx (); mtx.Inverse (); wndToWorld.Append ( mtx ); return wndToWorld; }
//----------------------------------------------------------------// void MOAIDraw::DrawRay ( float x, float y, float dx, float dy ) { USVec2D loc ( x, y ); USVec2D vec ( dx, dy ); USMatrix4x4 mtx = MOAIGfxDevice::Get ().GetViewProjMtx (); USMatrix4x4 invMtx; invMtx.Inverse ( mtx ); mtx.Transform ( loc ); mtx.TransformVec ( vec ); USRect 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 ( GL_LINES ); gfxDevice.WriteVtx ( p0.mX, p0.mY, 0.0f ); gfxDevice.WriteFinalColor4b (); gfxDevice.WriteVtx ( p1.mX, p1.mY, 0.0f ); gfxDevice.WriteFinalColor4b (); gfxDevice.EndPrim (); } }
//----------------------------------------------------------------// void MOAIDraw::DrawAxisGrid ( USVec2D loc, USVec2D vec, float size ) { USMatrix4x4 mtx = MOAIGfxDevice::Get ().GetViewProjMtx (); USMatrix4x4 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 USRect 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 ); } }
//----------------------------------------------------------------// void MOAIGfxDevice::UpdateViewVolume () { USMatrix4x4 invViewProj; invViewProj.Inverse ( this->GetViewProjMtx ()); this->mViewVolume.Init ( invViewProj ); }
//----------------------------------------------------------------// USMatrix4x4 MOAIGfxDevice::GetWorldToModelMtx () const { USMatrix4x4 worldToModel; worldToModel.Inverse ( this->mVertexTransforms [ VTX_WORLD_TRANSFORM ]); return worldToModel; }
//----------------------------------------------------------------// USMatrix4x4 MOAIGfxDevice::GetWndToModelMtx () const { USMatrix4x4 wndToModel; wndToModel.Inverse ( this->GetModelToWndMtx ()); return wndToModel; }