⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mgcturret.cpp

📁 《3D游戏引擎设计》的源码
💻 CPP
字号:
// Magic Software, Inc.
// http://www.magic-software.com
// Copyright (c) 2000, All Rights Reserved
//
// Source code from Magic Software is supplied under the terms of a license
// agreement and may not be copied or disclosed except in accordance with the
// terms of that agreement.  The various license agreements may be found at
// the Magic Software web site.  This file is subject to the license
//
// FREE SOURCE CODE
// http://www.magic-software.com/License/free.pdf

#include <windows.h>
#include "MgcCore.pkg"
#include "MgcTurret.h"

//---------------------------------------------------------------------------
MgcTurret::MgcTurret ()
    :
    m_kRotateComp(MgcMatrix3::IDENTITY),
    m_kTranslateComp(MgcVector3::ZERO)
{
    m_kRotate[0] = MgcMatrix3::IDENTITY;
    m_kRotate[1] = MgcMatrix3::IDENTITY;
    m_kRotate[2] = MgcMatrix3::IDENTITY;
    m_kTranslate[0] = MgcVector3::ZERO;
    m_kTranslate[1] = MgcVector3::ZERO;
    m_kTranslate[2] = MgcVector3::ZERO;

    for (int i = 0; i < 3; i++)
    {
        m_iTrnKeyM[i] = 0;
        m_iTrnKeyP[i] = 0;
        m_fTrnSpeed[i] = 0.0;
        m_iRotKeyM[i] = 0;
        m_iRotKeyP[i] = 0;
        m_fRotSpeed[i] = 0.0;
    }

    m_bUseAxis[0] = false;
    m_bUseAxis[1] = true;
    m_bUseAxis[2] = false;
    m_bUpdateAxis[0] = false;
    m_bUpdateAxis[1] = true;
    m_bUpdateAxis[2] = false;

    m_kAxis[0] = MgcVector3::UNIT_X;
    m_kAxis[1] = MgcVector3::UNIT_Y;
    m_kAxis[2] = MgcVector3::UNIT_Z;
}
//---------------------------------------------------------------------------
void MgcTurret::BindKeysToTranslateRight (int iKeyM, int iKeyP,
    MgcReal fSpeed)
{
    BindTrnKeys(0,iKeyM,iKeyP,fSpeed);
}
//---------------------------------------------------------------------------
void MgcTurret::BindKeysToTranslateUp (int iKeyM, int iKeyP,
    MgcReal fSpeed)
{
    BindTrnKeys(1,iKeyM,iKeyP,fSpeed);
}
//---------------------------------------------------------------------------
void MgcTurret::BindKeysToTranslateDirection (int iKeyM, int iKeyP,
    MgcReal fSpeed)
{
    BindTrnKeys(2,iKeyM,iKeyP,fSpeed);
}
//---------------------------------------------------------------------------
void MgcTurret::BindKeysToRotateRight (int iKeyM, int iKeyP, MgcReal fSpeed)
{
    BindRotKeys(0,iKeyM,iKeyP,fSpeed);
}
//---------------------------------------------------------------------------
void MgcTurret::BindKeysToRotateUp (int iKeyM, int iKeyP, MgcReal fSpeed)
{
    BindRotKeys(1,iKeyM,iKeyP,fSpeed);
}
//---------------------------------------------------------------------------
void MgcTurret::BindKeysToRotateDirection (int iKeyM, int iKeyP,
    MgcReal fSpeed)
{
    BindRotKeys(2,iKeyM,iKeyP,fSpeed);
}
//---------------------------------------------------------------------------
void MgcTurret::BindTrnKeys (int i, int iKeyM, int iKeyP, MgcReal fSpeed)
{
    m_iTrnKeyM[i] = iKeyM;
    m_iTrnKeyP[i] = iKeyP;
    m_fTrnSpeed[i] = fSpeed;
}
//---------------------------------------------------------------------------
void MgcTurret::BindRotKeys (int i, int iKeyM, int iKeyP, MgcReal fSpeed)
{
    m_iRotKeyM[i] = iKeyM;
    m_iRotKeyP[i] = iKeyP;
    m_fRotSpeed[i] = fSpeed;
}
//---------------------------------------------------------------------------
bool MgcTurret::ReadTrn (int i)
{
    if ( m_fTrnSpeed[i] != 0.0 )
    {
        MgcReal fSpeed;

        if ( GetAsyncKeyState(m_iTrnKeyM[i]) & 0x8000 )
            fSpeed = -m_fTrnSpeed[i];
        else if ( GetAsyncKeyState(m_iTrnKeyP[i]) & 0x8000 )
            fSpeed = +m_fTrnSpeed[i];
        else
            fSpeed = 0.0;

        if ( fSpeed != 0.0 )
        {
            m_kTranslate[i] += fSpeed*m_kAxis[i];
            return true;
        }
    }

    return false;
}
//---------------------------------------------------------------------------
bool MgcTurret::ReadRot (int i)
{
    if ( m_fRotSpeed[i] != 0.0 )
    {
        MgcReal fSpeed;

        if ( GetAsyncKeyState(m_iRotKeyM[i]) & 0x8000 )
        {
            fSpeed = -m_fRotSpeed[i];
        }
        else if ( GetAsyncKeyState(m_iRotKeyP[i]) & 0x8000 )
        {
            fSpeed = +m_fRotSpeed[i];
        }
        else
        {
            fSpeed = 0.0;
        }

        if ( fSpeed != 0.0 )
        {
            MgcMatrix3 kRotate = m_kRotate[i];
            MgcMatrix3 kIncrement;

            if ( m_bUseAxis[i] )
            {
                kIncrement.FromAxisAngle(m_kAxis[i],fSpeed);

                if ( m_bUpdateAxis[i] )
                {
                    for (int j = 0; j < 3; j++)
                    {
                        if ( j != i )
                            m_kAxis[j] = kIncrement*m_kAxis[j];
                    }
                }
            }
            else
            {
                MgcVector3 kAxis(kRotate[0][i],kRotate[1][i],kRotate[2][i]);
                kIncrement.FromAxisAngle(kAxis,fSpeed);
            }

            m_kRotate[i] = kIncrement*kRotate;
            return true;
        }
    }

    return false;
}
//---------------------------------------------------------------------------
bool MgcTurret::ReadKeyboard ()
{
    bool bKeyPressed =
        ReadTrn(0) || ReadTrn(1) || ReadTrn(2) ||
        ReadRot(0) || ReadRot(1) || ReadRot(2);

    if ( bKeyPressed )
    {
        m_kRotateComp = m_kRotate[1]*(m_kRotate[0]*m_kRotate[2]);
        m_kTranslateComp = m_kTranslate[0]+m_kTranslate[1]+m_kTranslate[2];
    }

    return bKeyPressed;
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -