[845] | 1 | #include ".\camera.h"
|
---|
| 2 |
|
---|
| 3 | Camera::Camera(void):
|
---|
| 4 | m_EyePosition(0,0,0),
|
---|
| 5 | m_LookAt(0,0,-1),
|
---|
| 6 | m_UpVector(0,1,0),
|
---|
| 7 | m_RightVector(1,0,0),
|
---|
| 8 | m_ViewDirection(0,0,-1),
|
---|
| 9 | m_Orientation(0,0,0),
|
---|
| 10 | m_AspectRatio(1),
|
---|
| 11 | m_NearClipDistance(0.1),
|
---|
| 12 | m_FarClipDistance(100),
|
---|
| 13 | m_FovX(45),
|
---|
| 14 | m_FovY(45),
|
---|
| 15 | m_LeftCorner(-1),
|
---|
| 16 | m_RightCorner(1),
|
---|
| 17 | m_DownCorner(-1),
|
---|
| 18 | m_TopCorner(1)
|
---|
| 19 | {
|
---|
| 20 |
|
---|
| 21 | }
|
---|
| 22 |
|
---|
| 23 | Camera::~Camera(void)
|
---|
| 24 | {
|
---|
| 25 |
|
---|
| 26 | }
|
---|
| 27 |
|
---|
| 28 | void Camera::Rotate(Vector rotvec)
|
---|
| 29 | {
|
---|
| 30 | m_Orientation+=rotvec;
|
---|
| 31 |
|
---|
| 32 | m_UpVector=Vector(0,1,0);
|
---|
| 33 | m_UpVector=m_UpVector.Rotate(Vector(0,0,1),m_Orientation.z);
|
---|
| 34 | m_UpVector=m_UpVector.Rotate(Vector(0,1,0),m_Orientation.y);
|
---|
| 35 | m_UpVector=m_UpVector.Rotate(Vector(1,0,0),m_Orientation.x);
|
---|
| 36 |
|
---|
| 37 | m_RightVector=Vector(1,0,0);
|
---|
| 38 | m_RightVector=m_RightVector.Rotate(Vector(0,0,1),m_Orientation.z);
|
---|
| 39 | m_RightVector=m_RightVector.Rotate(Vector(0,1,0),m_Orientation.y);
|
---|
| 40 | m_RightVector=m_RightVector.Rotate(Vector(1,0,0),m_Orientation.x);
|
---|
| 41 |
|
---|
| 42 | m_ViewDirection=Vector(0,0,-1);
|
---|
| 43 | m_ViewDirection=m_ViewDirection.Rotate(Vector(0,0,1),m_Orientation.z);
|
---|
| 44 | m_ViewDirection=m_ViewDirection.Rotate(Vector(0,1,0),m_Orientation.y);
|
---|
| 45 | m_ViewDirection=m_ViewDirection.Rotate(Vector(1,0,0),m_Orientation.x);
|
---|
| 46 |
|
---|
| 47 | m_LookAt=m_EyePosition+m_ViewDirection;
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | void Camera::Rotate(Axis axis, double angle)
|
---|
| 51 | {
|
---|
| 52 | switch (axis)
|
---|
| 53 | {
|
---|
| 54 | case AXIS_X:
|
---|
| 55 | m_UpVector=m_UpVector.Rotate(m_RightVector,angle);
|
---|
| 56 | m_ViewDirection=m_ViewDirection.Rotate(m_RightVector,angle);
|
---|
| 57 | UpdateCamera();
|
---|
| 58 | break;
|
---|
| 59 | case AXIS_Y:
|
---|
| 60 | m_ViewDirection=m_ViewDirection.Rotate(m_UpVector,angle);
|
---|
| 61 | m_RightVector=m_RightVector.Rotate(m_UpVector,angle);
|
---|
| 62 | UpdateCamera();
|
---|
| 63 | break;
|
---|
| 64 | case AXIS_Z:
|
---|
| 65 | m_RightVector=m_RightVector.Rotate(m_ViewDirection,angle);
|
---|
| 66 | m_UpVector=m_UpVector.Rotate(m_ViewDirection,angle);
|
---|
| 67 | break;
|
---|
| 68 | }
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | void Camera::UpdateCamera(void)
|
---|
| 72 | {
|
---|
| 73 | m_LookAt=m_EyePosition+m_ViewDirection;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 |
|
---|
| 77 |
|
---|
| 78 | void Camera::SetProjection(void)
|
---|
| 79 | {
|
---|
| 80 |
|
---|
| 81 | glMatrixMode(GL_PROJECTION);
|
---|
| 82 | glLoadIdentity();
|
---|
| 83 | gluPerspective(m_FovY,m_AspectRatio,m_NearClipDistance,m_FarClipDistance);
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | void Camera::SetViewandProjection(void)
|
---|
| 87 | {
|
---|
| 88 |
|
---|
| 89 | glMatrixMode(GL_PROJECTION);
|
---|
| 90 | glLoadIdentity();
|
---|
| 91 | gluPerspective(m_FovY,m_AspectRatio,m_NearClipDistance,m_FarClipDistance);
|
---|
| 92 |
|
---|
| 93 | glMatrixMode(GL_MODELVIEW);
|
---|
| 94 | glLoadIdentity();
|
---|
| 95 |
|
---|
| 96 | gluLookAt(m_EyePosition.x,
|
---|
| 97 | m_EyePosition.y,
|
---|
| 98 | m_EyePosition.z,
|
---|
| 99 | m_LookAt.x,
|
---|
| 100 | m_LookAt.y,
|
---|
| 101 | m_LookAt.z,
|
---|
| 102 | m_UpVector.x,
|
---|
| 103 | m_UpVector.y,
|
---|
| 104 | m_UpVector.z);
|
---|
| 105 | /*
|
---|
| 106 | glRotatef(-m_Orientation.y,0,1,0);
|
---|
| 107 | glRotatef(-m_Orientation.x,1,0,0);
|
---|
| 108 | //glRotatef(m_Orientation.z,0,0,1);
|
---|
| 109 | glTranslatef(-m_EyePosition.x,-m_EyePosition.y,m_EyePosition.z);*/
|
---|
| 110 |
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | void Camera::SetViewandProjectionOrto(void)
|
---|
| 114 | {
|
---|
| 115 | glMatrixMode(GL_PROJECTION);
|
---|
| 116 | glLoadIdentity();
|
---|
| 117 | glOrtho(m_LeftCorner,m_RightCorner,m_DownCorner,m_TopCorner,m_NearClipDistance,m_FarClipDistance);
|
---|
| 118 |
|
---|
| 119 | glMatrixMode(GL_MODELVIEW);
|
---|
| 120 | glLoadIdentity();
|
---|
| 121 | gluLookAt(m_EyePosition.x,
|
---|
| 122 | m_EyePosition.y,
|
---|
| 123 | m_EyePosition.z,
|
---|
| 124 | m_LookAt.x,
|
---|
| 125 | m_LookAt.y,
|
---|
| 126 | m_LookAt.z,
|
---|
| 127 | m_UpVector.x,
|
---|
| 128 | m_UpVector.y,
|
---|
| 129 | m_UpVector.z);
|
---|
| 130 |
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | void Camera::Move(Axis axis, double distance)
|
---|
| 134 | {
|
---|
| 135 | switch (axis)
|
---|
| 136 | {
|
---|
| 137 | case AXIS_X:
|
---|
| 138 | m_EyePosition+=m_RightVector.UnitVector()*distance;
|
---|
| 139 | m_LookAt+=m_RightVector.UnitVector()*distance;
|
---|
| 140 | break;
|
---|
| 141 | case AXIS_Y:
|
---|
| 142 | m_EyePosition+=m_UpVector.UnitVector()*distance;
|
---|
| 143 | m_LookAt+=m_UpVector.UnitVector()*distance;
|
---|
| 144 | break;
|
---|
| 145 | case AXIS_Z:
|
---|
| 146 | m_EyePosition+=m_ViewDirection.UnitVector()*distance;
|
---|
| 147 | m_LookAt+=m_ViewDirection.UnitVector()*distance;
|
---|
| 148 | break;
|
---|
| 149 | }
|
---|
| 150 | }
|
---|