My Simple Camera

Dev.Write 2010. 2. 14. 05:49 Posted by zetz
SimpleCamera.h
#pragma once


#include 

class Camera
{
public:
	Camera(D3DXVECTOR3 eye = D3DXVECTOR3(0.f, 0.0f, -50.f), 
		   D3DXVECTOR3 look= D3DXVECTOR3(0.f, 0.0f, 0.f),
		   D3DXVECTOR3 up  = D3DXVECTOR3(0.f, 1.0f, 0.f));
	~Camera(void);
	
	D3DXMATRIX		GetViewMatrix(){	return m_matView;	};

	void			Move(UINT camera_direction);
	void			Rotate(float relative_x, float relative_y);
	void			Zoom(float zDelta, float Scale = 1000);

	enum	{ FORWARD, BACKWARD, LEFTWARD, RIGHTWARD };
private:
	D3DXVECTOR3		m_vEyePt;
	D3DXVECTOR3		m_vLookAtPt;
	D3DXVECTOR3		m_vUp;
	
	float			m_fSpeed;
	D3DXMATRIX		m_matView;
};

SimpleCamera.cpp
#include "StdAfx.h"
#include "Camera.h"

Camera::Camera(D3DXVECTOR3 eye, D3DXVECTOR3 look, D3DXVECTOR3 up)
:m_vEyePt(eye), m_vLookAtPt(look), m_vUp(up)
{
	m_fSpeed = 0.25f;
	D3DXMatrixLookAtLH(&m_matView,&m_vEyePt,&m_vLookAtPt,&m_vUp);
}

Camera::~Camera(void)
{
}


void	Camera::Move(UINT camera_direction)
{
	D3DXVECTOR3 vDirection;
	D3DXVec3Normalize(&vDirection, &(m_vLookAtPt - m_vEyePt));

	if( camera_direction == FORWARD )
	{
		m_vEyePt += vDirection * m_fSpeed;
		m_vLookAtPt += vDirection * m_fSpeed;
	}
	if( camera_direction == BACKWARD )
	{
		m_vEyePt -= vDirection * m_fSpeed;
		m_vLookAtPt -= vDirection * m_fSpeed;
	}
	if( camera_direction == LEFTWARD )
	{
		D3DXVec3Cross(&vDirection,&vDirection,&m_vUp);
		D3DXVec3Normalize(&vDirection,&vDirection);

		m_vEyePt += vDirection * m_fSpeed;
		m_vLookAtPt += vDirection * m_fSpeed;
	}
	if( camera_direction == RIGHTWARD )
	{
		D3DXVec3Cross(&vDirection,&vDirection,&m_vUp);
		D3DXVec3Normalize(&vDirection,&vDirection);

		m_vEyePt -= vDirection * m_fSpeed;
		m_vLookAtPt -= vDirection * m_fSpeed;
	}
	
	D3DXMatrixLookAtLH(&m_matView,&m_vEyePt,&m_vLookAtPt,&m_vUp);
}
void	Camera::Rotate(float relative_x, float relative_y)
{
	D3DXVECTOR3 vDirection,vRotAxis;
	D3DXMATRIX matRotAxis, matRotZ;

	D3DXVec3Normalize(&vDirection, &(m_vLookAtPt - m_vEyePt));

	D3DXVec3Cross(&vRotAxis,&vDirection,&m_vUp);
	D3DXVec3Normalize(&vRotAxis,&vRotAxis);

	D3DXMatrixRotationAxis(&matRotAxis,&vRotAxis, relative_y / -360.0f);

	D3DXMatrixRotationY(&matRotZ,relative_x /360.0f);
	D3DXVec3TransformCoord(&vDirection,&vDirection,&(matRotAxis * matRotZ));
	D3DXVec3TransformCoord(&m_vUp,&m_vUp,&(matRotAxis * matRotZ));
	m_vLookAtPt = vDirection + m_vEyePt;		

	D3DXMatrixLookAtLH(&m_matView,&m_vEyePt,&m_vLookAtPt,&m_vUp);
}
void	Camera::Zoom(float zDelta, float Scale)
{
	D3DXVECTOR3 vDirection;

	D3DXVec3Normalize(&vDirection, &(m_vLookAtPt - m_vEyePt));

	m_vEyePt += (vDirection * zDelta/Scale);

	D3DXMatrixLookAtLH(&m_matView,&m_vEyePt,&m_vLookAtPt,&m_vUp);
}

'Dev.Write' 카테고리의 다른 글

파일 존재 확인 함수  (0) 2011.03.24
[Crypto] RSA Example  (0) 2010.05.17
높이 맵 제작 툴 만들기(2)  (0) 2010.01.20
높이 맵 제작 툴 만들기 (1)  (0) 2010.01.18
MFC 기반의 Direct3DX Window  (0) 2010.01.15