// Wild Magic Source Code
// David Eberly
// http://www.geometrictools.com
// Copyright (c) 1998-2008
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2.1 of the License, or (at
// your option) any later version.  The license is available for reading at
// either of the locations:
//     http://www.gnu.org/copyleft/lgpl.html
//     http://www.geometrictools.com/License/WildMagicLicense.pdf
//
// Version: 4.0.0 (2006/06/28)

#include "Wm4Dx9RendererPCH.h"
#include "Wm4Dx9Renderer.h"
#include "Wm4Camera.h"
using namespace Wm4;

//----------------------------------------------------------------------------
void Dx9Renderer::OnViewportChange ()
{
    float fPortL, fPortR, fPortT, fPortB;
    if (m_pkCamera)
    {
        // 3D applications use cameras.
        m_pkCamera->GetViewport(fPortL,fPortR,fPortT,fPortB);
    }
    else
    {
        // 2D applications do not use cameras.
        fPortL = 0.0f;
        fPortR = 1.0f;
        fPortT = 1.0f;
        fPortB = 0.0f;
    }

    // DirectX defines the full-sized viewport to have origin at the upper
    // left corner of the screen.  Wild Magic uses the OpenGL convention that
    // fB specifies the relative distance from the bottom of the screen.
    // DirectX needs a specification of relative distance from the top of
    // the screen, which is 1-fT.
    D3DVIEWPORT9 kViewport;
    kViewport.X = (DWORD)(fPortL*m_iWidth);
    kViewport.Y = (DWORD)((1.0f - fPortT)*m_iHeight);  // See note above.
    kViewport.Width = (DWORD)((fPortR - fPortL)*m_iWidth);
    kViewport.Height = (DWORD)((fPortT - fPortB)*m_iHeight);
    kViewport.MinZ = 0.0f;
    kViewport.MaxZ = 1.0f;

    ms_hResult = m_pqDevice->SetViewport(&kViewport);
    assert(SUCCEEDED(ms_hResult));
}
//----------------------------------------------------------------------------
void Dx9Renderer::OnDepthRangeChange ()
{
    D3DVIEWPORT9 kViewport;
    ms_hResult = m_pqDevice->GetViewport(&kViewport);
    assert(SUCCEEDED(ms_hResult));

    if (m_pkCamera)
    {
        m_pkCamera->GetDepthRange(kViewport.MinZ,kViewport.MaxZ);
    }
    else
    {
        kViewport.MinZ = 0.0f;
        kViewport.MaxZ = 1.0f;
    }

    ms_hResult = m_pqDevice->SetViewport(&kViewport);
    assert(SUCCEEDED(ms_hResult));
}
//----------------------------------------------------------------------------
