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

📄 camera.cpp

📁 浙江大学 RoboCup3D 2006 源代码
💻 CPP
字号:
/*************************************************************************** *   Copyright (C) 2004 - 2006 by ZJUBase                                  *
 *                                National Lab of Industrial Control Tech. * *                                Zhejiang University, China               *
*                                                                         * *   Team members:                                                         *
 *    Currently the team leader is,                                        * *           Hao JIANG (jianghao@iipc.zju.edu.cn; riveria@gmail.com)       *
 *    In the next season, the leader will be                               * *           Yifeng ZHANG (yfzhang@iipc.zju.edu.cn)                        *
 *    ZJUBase 3D agent is created by                                       * *           Dijun LUO (djluo@iipc.zju.edu.cn)                             *
 *    All the members who has ever contributed:                            * *           Jun JIANG                                                     *
 *           Xinfeng DU (xfdu@iipc.zju.edu.cn)                             *
 *           Yang ZHOU (yzhou@iipc.zju.edu.cn)                             *
 *           Zhipeng YANG                                                  *
 *           Xiang FAN                                                     *
 *                                                                         *
 *   Team Manager:                                                          *
 *      Ms. Rong XIONG (rxiong@iipc.zju.edu.cn)                            *
 *                                                                         *
 *   If you met any problems or you have something to discuss about        * *   ZJUBase. Please feel free to contact us through EMails given below.   * *                                                                         * *   This program is free software; you can redistribute it and/or modify  * *   it under the terms of the GNU General Public License as published by  * *   the Free Software Foundation; either version 2 of the License, or     * *   (at your option) any later version.                                   * *                                                                         * *   This program is distributed in the hope that it will be useful,       * *   but WITHOUT ANY WARRANTY; without even the implied warranty of        * *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         * *   GNU General Public License for more details.                          * *                                                                         * *   You should have received a copy of the GNU General Public License     * *   along with this program; if not, write to the                         * *   Free Software Foundation, Inc.,                                       * *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             * ***************************************************************************/
#include "camera.h"

Camera::Camera(const Vector3& camPos,
               const Vector3& lookAtPos,
               const Vector3& upVector)
{
  mPosition  = camPos;
  mLookAtPos = lookAtPos;
  mUpVector  = upVector;

  //resets the camera
  RefreshCam();

  mFixDist = 8.0;
  mMouseSensitivity = 0.01;
}

Vector3
Camera::GetCameraPos()
{
  return mPosition;
}

Vector3
Camera::GetLookAtPos()
{
  return mLookAtPos;
}

void
Camera::SetCameraPos(const Vector3& newCamPos)
{
  mPosition = newCamPos;
  RefreshCam();
}

void
Camera::SetLookAtPos(const Vector3& lookAtPos)
{
  mLookAtPos = lookAtPos;
  RefreshCam();
}

void
Camera::SetUpVector(const Vector3& upVector)
{
  mUpVector = upVector;
  RefreshCam();
}

void
Camera::Look()
{
  gluLookAt( mPosition[0], mPosition[1], mPosition[2],
             mLookAtPos[0], mLookAtPos[1], mLookAtPos[2],
             mUpVector[0], mUpVector[1], mUpVector[2]);
}

void
Camera::RefreshCam()
{
  //resets the camera attributes mTheta and mPhi
  Vector3 tmp = mLookAtPos - mPosition;
  mTheta = acos(tmp[2] / tmp.mod());
  Vector2 tmp2(mLookAtPos[0] - mPosition[0], mLookAtPos[1] - mPosition[1]);
  mPhi = tmp2.ang() / 180. * pi;
}

void
Camera::MoveCamForward(double steps)
{
  //move camera 'steps' meters into direction we are facing
  Vector3 dir = (mLookAtPos - mPosition).Normalized()*steps;
  mPosition   += dir;
  mLookAtPos  += dir;
}

void
Camera::MoveCamStrafeForward(double steps)
{
  //move camera 'steps' meters into direction we are facing
  Vector3 dir = (mLookAtPos - mPosition).Normalized()*steps;
  //remove z-coord
  dir[2]=0.0f;
  
  mPosition   += dir;
  mLookAtPos  += dir;
}

void Camera::MoveCamUp(double steps)
{
    mPosition[2] += steps;
    mLookAtPos[2] += steps;
}

void
Camera::MoveCamStrafe(double steps)
{
  //move cam perpendicular to our current direction
  Vector3 tmp = (mLookAtPos - mPosition);
  Vector3 dir = (mUpVector.Cross(tmp)).Normalized()*steps;
  mPosition   += dir;
  mLookAtPos  += dir;
}

//reset the Camera by taking the difference between the current
//mousePos and the given reference pos, a simple refPos could be the
//middle of the screen
void
Camera::SetViewByMouse(const Vector2& mousePos,
                       const Vector2& refPos)
{
  double deltaX = mousePos.x - refPos.x;
  double deltaY = mousePos.y - refPos.y;

  mTheta += mMouseSensitivity * deltaY;
  if(mTheta < mMouseSensitivity) mTheta = mMouseSensitivity;
  else if (mTheta >  pi - mMouseSensitivity) mTheta = pi - mMouseSensitivity;

  mPhi -= mMouseSensitivity * deltaX;
  if(mPhi < 0) mPhi += 2*pi;
  else if (mPhi > 2*pi) mPhi -= 2*pi;

  mLookAtPos[0] = mPosition[0] + mFixDist * sin(mTheta) * cos(mPhi);
  mLookAtPos[1] = mPosition[1] + mFixDist * sin(mTheta) * sin(mPhi);
  mLookAtPos[2] = mPosition[2] + mFixDist * cos(mTheta);
}

⌨️ 快捷键说明

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