#include "camera.h" const double PI = 3.14159265358979; Camera::Camera() { eye = Point3D(0, 0, 5); n = Vector3D(0, 0, 1); u = Vector3D(1, 0, 0); v = Vector3D(0, 1, 0); viewAngle = 60; aspect = 4.0/3.0; nearDist = 0.1; farDist = 100.0; speed = 1.0; // units per second return; } Camera::Camera(Point3D e, Point3D look, Vector3D up) { eye = e; n.set(eye.x - look.x, eye.y - look.y, eye.z - look.z); n = n.normalize(); u = up.cross(n); u = u.normalize(); v = n.cross(u); viewAngle = 60.0; aspect = 4.0/3.0; nearDist = 0.1; farDist = 100.0; speed = 1.0; // units per second return; } void Camera::set(Point3D e, Point3D look, Vector3D up) { eye = e; n.set(eye.x - look.x, eye.y - look.y, eye.z - look.z); n = n.normalize(); u = up.cross(n); u = u.normalize(); v = n.cross(u); setModelViewMatrix(); return; } void Camera::roll(float angle) { float c = cos(PI/180.0*angle); float s = sin(PI/180.0*angle); Vector3D t = u; u.set(c*t.x - s*v.x, c*t.y - s*v.y, c*t.z - s*v.z); v.set(s*t.x + c*v.x, s*t.y + c*v.y, s*t.z + c*v.z); setModelViewMatrix(); return; } void Camera::pitch(float angle) { float c = cos(PI/180.0*angle); float s = sin(PI/180.0*angle); Vector3D t = v; v.set(c*t.x - s*n.x, c*t.y - s*n.y, c*t.z - s*n.z); n.set(s*t.x + c*n.x, s*t.y + c*n.y, s*t.z + c*n.z); setModelViewMatrix(); return; } void Camera::yaw(float angle) { float c = cos(PI/180.0*angle); float s = sin(PI/180.0*angle); Vector3D t = n; n.set(c*t.x - s*u.x, c*t.y - s*u.y, c*t.z - s*u.z); u.set(s*t.x + c*u.x, s*t.y + c*u.y, s*t.z + c*u.z); setModelViewMatrix(); return; } void Camera::forward(float dt) { eye.x -= n.x*dt; eye.y -= n.y*dt; eye.z -= n.z*dt; setModelViewMatrix(); return; } void Camera::setShape(float va, float ar, float nd, float fd) { viewAngle = va; aspect = ar; nearDist = nd; farDist = fd; glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(viewAngle, aspect, nearDist, farDist); return; } void Camera::setModelViewMatrix() { float m[16]; Vector3D eyeV(eye.x, eye.y, eye.z); m[0] = u.x; m[4] = u.y; m[ 8] = u.z; m[12] = -eyeV.dot(u); m[1] = v.x; m[5] = v.y; m[ 9] = v.z; m[13] = -eyeV.dot(v); m[2] = n.x; m[6] = n.y; m[10] = n.z; m[14] = -eyeV.dot(n); m[3] = 0.0; m[7] = 0.0; m[11] = 0.0; m[15] = 1.0; glMatrixMode(GL_MODELVIEW); glLoadMatrixf(m); return; }