LiFeng_Camera.cpp
资源名称:虚拟地形建模.rar [点击查看]
上传用户:dfjhuyju
上传日期:2013-03-13
资源大小:11035k
文件大小:5k
源码类别:
OpenGL
开发平台:
Visual C++
- #pragma comment(lib, "winmm.lib")
- #include "LiFeng.h"
- #include "StdAfx.h"
- #include "LiFeng_Camera.h"
- #include <math.h>
- LiFeng_Camera::Init(HWND wnd)
- {
- hwnd = wnd;
- }
- LiFeng_Camera::LiFeng_Camera()
- {
- CVector3 vZero = CVector3(0.0, 0.0, 0.0);
- CVector3 vView = CVector3(0.0, 1.0, 0.5);
- CVector3 vUp = CVector3(0.0, 0.0, 1.0);
- m_vPosition = vZero;
- m_vView = vView;
- m_vUpVector = vUp;
- m_Role = false;
- Role_Flag = false;
- }
- void LiFeng_Camera::PositionCamera(float positionX, float positionY, float positionZ,
- float viewX, float viewY, float viewZ,
- float upVectorX, float upVectorY, float upVectorZ)
- {
- CVector3 vPosition = CVector3(positionX, positionY, positionZ);
- CVector3 vView = CVector3(viewX, viewY, viewZ);
- CVector3 vUpVector = CVector3(upVectorX, upVectorY, upVectorZ);
- m_vPosition = vPosition;
- m_vView = vView;
- m_vUpVector = vUpVector;
- }
- void LiFeng_Camera::SetViewByMouse(int w,int h,BOOL rol)
- {
- POINT mousePos;
- int middleX = w/2;
- int middleY = h/2;
- float angleY = 0.0f;
- float angleZ = 0.0f;
- static float currentRotX = 0.0f;
- GetCursorPos(&mousePos);
- if( (mousePos.x == middleX) && (mousePos.y == middleY) ) return;
- SetCursorPos(middleX, middleY);
- angleY = (float)( (middleX - mousePos.x) ) / 500.0f;
- angleZ = (float)( (middleY - mousePos.y) ) / 500.0f;
- currentRotX -= angleZ;
- if(rol!=m_Role)
- {
- m_Role = rol;
- }
- else
- {
- if(currentRotX > 2.0f)
- currentRotX = 2.0f;
- else if(currentRotX < -2.0f)
- currentRotX = -2.0f;
- else
- {
- CVector3 vAxis = Cross(m_vView - m_vPosition, m_vUpVector);
- vAxis = Normalize(vAxis);
- if(fabs(angleZ)>=fabs(angleY))
- {
- RotateView(angleZ, vAxis.x, vAxis.y, vAxis.z);
- }
- else
- {
- RotateView(angleY, 0, 1, 0);
- }
- }
- }
- }
- void LiFeng_Camera::RotateView(float angle, float x, float y, float z)
- {
- CVector3 vNewView;
- CVector3 vView = m_vView - m_vPosition;
- float cosTheta = (float)cos(angle);
- float sinTheta = (float)sin(angle);
- vNewView.x = (cosTheta + (1 - cosTheta) * x * x) * vView.x;
- vNewView.x += ((1 - cosTheta) * x * y - z * sinTheta) * vView.y;
- vNewView.x += ((1 - cosTheta) * x * z + y * sinTheta) * vView.z;
- vNewView.y = ((1 - cosTheta) * x * y + z * sinTheta) * vView.x;
- vNewView.y += (cosTheta + (1 - cosTheta) * y * y) * vView.y;
- vNewView.y += ((1 - cosTheta) * y * z - x * sinTheta) * vView.z;
- vNewView.z = ((1 - cosTheta) * x * z - y * sinTheta) * vView.x;
- vNewView.z += ((1 - cosTheta) * y * z + x * sinTheta) * vView.y;
- vNewView.z += (cosTheta + (1 - cosTheta) * z * z) * vView.z;
- m_vView = m_vPosition + vNewView;
- }
- void LiFeng_Camera::StrafeCamera(float speed)
- {
- m_vPosition.x += m_vStrafe.x * speed;
- m_vPosition.z += m_vStrafe.z * speed;
- // Add the strafe vector to our view
- m_vView.x += m_vStrafe.x * speed;
- m_vView.z += m_vStrafe.z * speed;
- }
- void LiFeng_Camera::MoveCamera(float speed)
- {
- CVector3 vVector = m_vView - m_vPosition;
- vVector = Normalize(vVector);
- m_vPosition.x += vVector.x * speed;
- m_vPosition.y += vVector.y * speed;
- m_vPosition.z += vVector.z * speed;
- m_vView.x += vVector.x * speed;
- m_vView.y += vVector.y * speed;
- m_vView.z += vVector.z * speed;
- }
- void LiFeng_Camera::CheckForMovement(float speed)
- {
- if(GetKeyState(VK_UP) & 0x80 || GetKeyState('W') & 0x80) {
- MoveCamera(speed);
- }
- if(GetKeyState(VK_DOWN) & 0x80 || GetKeyState('S') & 0x80) {
- MoveCamera(-speed);
- }
- if(GetKeyState(VK_LEFT) & 0x80 || GetKeyState('A') & 0x80) {
- StrafeCamera(-speed);
- }
- if(GetKeyState(VK_RIGHT) & 0x80 || GetKeyState('D') & 0x80) {
- StrafeCamera(speed);
- }
- if(GetKeyState('M') & 0x80)
- {
- Role_Flag = true;
- ShowCursor(false);
- }
- if(GetKeyState('N') & 0x80)
- {
- Role_Flag = false;
- ShowCursor(true);
- }
- }
- void LiFeng_Camera::Update(int w,int h,float speed)
- {
- CalculateHorizonVec();
- CalculateVerticalVec();
- ViewDerection = m_vView - m_vPosition;
- ViewDerection = Normalize(ViewDerection);
- CVector3 vCross = Cross(m_vView - m_vPosition, m_vUpVector);
- m_vStrafe = Normalize(vCross);
- if(Role_Flag)
- {
- SetViewByMouse(w,h,Role_Flag);
- }
- else
- {
- m_Role = false;
- }
- CheckForMovement(speed);
- }
- void LiFeng_Camera::Look()
- {
- gluLookAt(m_vPosition.x, m_vPosition.y, m_vPosition.z,
- m_vView.x, m_vView.y, m_vView.z,
- m_vUpVector.x, m_vUpVector.y, m_vUpVector.z);
- }
- void LiFeng_Camera::CalculateHorizonVec()
- {
- CVector3 frontderection;
- CVector3 horizonderection;
- CVector3 ViewVector=m_vPosition-m_vView;
- frontderection=CVector3(ViewVector.x,ViewVector.y,0);
- m_vStrafe=Cross(ViewVector,frontderection);
- Normalize(m_vStrafe);
- }
- void LiFeng_Camera::CalculateVerticalVec()
- {
- CVector3 ViewVector=m_vPosition-m_vView;
- VerticalVector=Cross(ViewVector,m_vStrafe);
- Normalize(VerticalVector);
- }