// Tom Plick // 27 July 2005 // projector.cpp // Handle perspective of the board. Akin to Luna's Camera class (ch. 12), // but the Projector always points at the origin, and places // restriction on angles of view. #include "projector.h" #define RADIANS_PER_DEGREE ((float) (D3DX_PI / 180.0)) #define MIN_DISTANCE 3.0f #define MAX_DISTANCE 35.0f #define MAX_THETA (70.0f * RADIANS_PER_DEGREE) #define MIN_PHI (15.0f * RADIANS_PER_DEGREE) #define MAX_PHI (75.0f * RADIANS_PER_DEGREE) void Projector::recenter() { distance = 20.0f; theta = 0.0f * RADIANS_PER_DEGREE; phi = 40.0f * RADIANS_PER_DEGREE; calculateMatrix(); } Projector::Projector() { recenter(); } void Projector::checkBounds() { /* if (distance < MIN_DISTANCE) distance = MIN_DISTANCE; else if (distance > MAX_DISTANCE) distance = MAX_DISTANCE; if (theta > MAX_THETA) theta = MAX_THETA; else if (theta < -MAX_THETA) theta = -MAX_THETA; if (phi < MIN_PHI) phi = MIN_PHI; else if (phi > MAX_PHI) phi = MAX_PHI; */ } void Projector::calculateMatrix() { static D3DXVECTOR3 origin(0.0f, 0.0f, 0.0f); static D3DXMATRIX x_rotate, y_rotate; D3DXMatrixRotationX(&x_rotate, phi); D3DXMatrixRotationY(&y_rotate, theta); D3DXVECTOR3 viewer(0.0f, 0.0f, -distance); D3DXVECTOR3 up(0.0f, 1.0f, 0.0f); D3DXVec3TransformCoord(&viewer, &viewer, &x_rotate); D3DXVec3TransformCoord(&up, &up, &x_rotate); D3DXVec3TransformCoord(&viewer, &viewer, &y_rotate); D3DXVec3TransformCoord(&up, &up, &y_rotate); D3DXMatrixLookAtLH(&this->mat, &viewer, &origin, &up); } D3DXMATRIX Projector::getViewMatrix() { return mat; } void Projector::setDistance(float x) { distance = x; checkBounds(); calculateMatrix(); } float Projector::getDistance() { return distance; } void Projector::addDistance(float x) { distance += x; checkBounds(); calculateMatrix(); } void Projector::setTheta(float x) { theta = x; checkBounds(); calculateMatrix(); } float Projector::getTheta() { return theta; } void Projector::addTheta(float x) { theta += x; checkBounds(); calculateMatrix(); } void Projector::setPhi(float x) { phi = x; checkBounds(); calculateMatrix(); } float Projector::getPhi() { return phi; } void Projector::addPhi(float x) { phi += x; checkBounds(); calculateMatrix(); }