Camera 헤더
//=============================================================================
// Camera.h by Frank Luna (C) 2004 All Rights Reserved.
//=============================================================================
#ifndef CAMERA_H
#define CAMERA_H
#include <d3dx9.h>
class Camera
{
public:
Camera();
// Read access only.
const D3DXMATRIX& view();
const D3DXMATRIX& proj();
const D3DXMATRIX& viewProj();
float fov();
float aspect();
float nearZ();
float farZ();
// Read and write access.
D3DXVECTOR3& pos();
void lookAt(D3DXVECTOR3& pos, D3DXVECTOR3& target, D3DXVECTOR3& up);
void setLens(float fov, float aspect, float nearZ, float farZ);
virtual void update(float dt) = 0;
protected:
void buildView();
protected:
D3DXMATRIX mView;
D3DXMATRIX mProj;
D3DXMATRIX mViewProj;
// Lens info.
float mFOV;
float mAspect;
float mNearZ;
float mFarZ;
// Frame info.
D3DXVECTOR3 mPos;
D3DXVECTOR3 mRight;
D3DXVECTOR3 mUp;
D3DXVECTOR3 mLook;
// Speed (m/s) or (rad/s)
float mLinearSpeed;
float mAngularSpeed;
};
class FreeCamera : public Camera
{
public:
void update(float dt);
};
#endif // CAMERA_H
Camera 구현부
//=============================================================================
// Camera.cpp by Frank Luna (C) 2004 All Rights Reserved.
//=============================================================================
#include "Camera.h"
#include "d3dAfx.h"
Camera::Camera()
{
D3DXMatrixIdentity(&mView);
D3DXMatrixIdentity(&mProj);
D3DXMatrixIdentity(&mViewProj);
mFOV = 0.0f;
mAspect = 0.0f;
mNearZ = 0.0f;
mFarZ = 0.0f;
mPos = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
mRight = D3DXVECTOR3(1.0f, 0.0f, 0.0f);
mUp = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
mLook = D3DXVECTOR3(0.0f, 0.0f, 1.0f);
mLinearSpeed = 10.0f;
mAngularSpeed = 1.0f;
}
const D3DXMATRIX& Camera::view()
{
return mView;
}
const D3DXMATRIX& Camera::proj()
{
return mProj;
}
const D3DXMATRIX& Camera::viewProj()
{
return mViewProj;
}
float Camera::fov()
{
return mFOV;
}
float Camera::aspect()
{
return mAspect;
}
float Camera::nearZ()
{
return mNearZ;
}
float Camera::farZ()
{
return mFarZ;
}
D3DXVECTOR3& Camera::pos()
{
return mPos;
}
void Camera::lookAt(D3DXVECTOR3& pos, D3DXVECTOR3& target, D3DXVECTOR3& up)
{
D3DXVECTOR3 L = target - pos;
D3DXVec3Normalize(&L, &L);
D3DXVECTOR3 R;
D3DXVec3Cross(&R, &up, &L);
// Since 'up' and 'L' are unit vectors, 'R' will be a
// unit vector also.
D3DXVECTOR3 U;
D3DXVec3Cross(&U, &L, &R);
// Since 'L' and 'right' are unit vectors, 'U' will be a
// unit vector also.
mPos = pos;
mRight = R;
mUp = U;
mLook = L;
}
void Camera::setLens(float fov, float aspect, float nearZ, float farZ)
{
mFOV = fov;
mAspect = aspect;
mNearZ = nearZ;
mFarZ = farZ;
D3DXMatrixPerspectiveFovLH(&mProj, fov, aspect, nearZ, farZ);
}
void Camera::buildView()
{
D3DXVECTOR3& P = mPos;
D3DXVECTOR3& L = mLook;
D3DXVECTOR3& U = mUp;
D3DXVECTOR3& R = mRight;
// Keep camera's axes orthogonal to each other and of unit length.
D3DXVec3Normalize(&L, &L);
D3DXVec3Cross(&U, &L, &R);
D3DXVec3Normalize(&U, &U);
D3DXVec3Cross(&R, &U, &L);
D3DXVec3Normalize(&R, &R);
float x = -D3DXVec3Dot(&P, &R);
float y = -D3DXVec3Dot(&P, &U);
float z = -D3DXVec3Dot(&P, &L);
mView(0,0) = R.x;
mView(1,0) = R.y;
mView(2,0) = R.z;
mView(3,0) = x;
mView(0,1) = U.x;
mView(1,1) = U.y;
mView(2,1) = U.z;
mView(3,1) = y;
mView(0,2) = L.x;
mView(1,2) = L.y;
mView(2,2) = L.z;
mView(3,2) = z;
mView(0,3) = 0.0f;
mView(1,3) = 0.0f;
mView(2,3) = 0.0f;
mView(3,3) = 1.0f;
}
void FreeCamera::update(float dt)
{
// Forwards/Backwards
if( d3d::gDInput->keyDown(DIK_W) )
mPos += mLook * mLinearSpeed * dt;
if( d3d::gDInput->keyDown(DIK_S) )
mPos -= mLook * mLinearSpeed * dt;
// Strafe Left/Right
if( d3d::gDInput->keyDown(DIK_D) )
mPos += mRight * mLinearSpeed * dt;
if( d3d::gDInput->keyDown(DIK_A) )
mPos -= mRight * mLinearSpeed * dt;
// Mouse displacement is already in differential form (we obtain
// the mouse displacement between frames every frame), so we do not
// need to multiply by dt. However, we do need to scale the
// hardware device units down to a scale that makes more sense
// for our program.
float deltaX = (float)d3d::gDInput->mouseDX() * 0.005f;
float deltaY = (float)d3d::gDInput->mouseDY() * 0.005f;
// Pitch--rotate about "right" vector.
D3DXMATRIX R;
D3DXMatrixRotationAxis(&R, &mRight, deltaY);
D3DXVec3TransformCoord(&mLook, &mLook, &R);
D3DXVec3TransformCoord(&mUp, &mUp, &R);
// Rotate about fixed y-axis.
D3DXMatrixRotationY(&R, deltaX);
D3DXVec3TransformCoord(&mLook, &mLook, &R);
D3DXVec3TransformCoord(&mRight, &mRight, &R);
buildView();
mViewProj = mView * mProj;
}
'Programming' 카테고리의 다른 글
| ID3DXSprite::Begin (0) | 2007/10/02 |
|---|---|
| D3DXCreateSprite (0) | 2007/09/30 |
| 연결 리스트 ( Linked List ) 링크드 리스트 (0) | 2007/09/23 |
| 순열(next_permutation)과 문자열 변환 (0) | 2007/09/16 |
| next_permutation (0) | 2007/09/13 |
| DICamera (0) | 2007/06/19 |
| Camera Class 구현 (0) | 2007/06/19 |
| DirectX Study (3) (0) | 2007/06/19 |
| DirectX Study (2) (0) | 2007/06/18 |
| DirectX Study (1) (0) | 2007/06/18 |
| 구조체 (0) | 2007/06/16 |






DICamera.zip
댓글을 달아 주세요