[2753] | 1 | #include "common.h"
|
---|
| 2 | #include "Camera.h"
|
---|
[2755] | 3 | #include "glInterface.h"
|
---|
[2753] | 4 |
|
---|
| 5 |
|
---|
| 6 | namespace CHCDemo
|
---|
| 7 | {
|
---|
| 8 |
|
---|
| 9 |
|
---|
| 10 | Camera::Camera()
|
---|
| 11 | {
|
---|
| 12 | mWidth = 100;
|
---|
| 13 | mHeight = 100;
|
---|
[2760] | 14 | mFovy = 90.0f * M_PI / 180.0f;
|
---|
[2753] | 15 | }
|
---|
| 16 |
|
---|
| 17 |
|
---|
| 18 | Camera::Camera(int width, int height, float fieldOfView)
|
---|
| 19 | {
|
---|
| 20 | mWidth = width;
|
---|
| 21 | mHeight = height;
|
---|
| 22 |
|
---|
[2760] | 23 | mFovy = fieldOfView * M_PI / 180.0f;
|
---|
[2753] | 24 | }
|
---|
| 25 |
|
---|
| 26 |
|
---|
| 27 | void Camera::Precompute()
|
---|
| 28 | {
|
---|
| 29 | mDirection.Normalize();
|
---|
| 30 |
|
---|
[2764] | 31 | //Vector3 side = CrossProd(Vector3(0, 1, 0), mDirection);
|
---|
| 32 | Vector3 side = CrossProd(Vector3(0, 0, 1), mDirection);
|
---|
| 33 |
|
---|
[2756] | 34 | mUp = -Normalize(CrossProd(side, mDirection));
|
---|
| 35 | mRight = -Normalize(CrossProd(mDirection, mUp));
|
---|
[2753] | 36 |
|
---|
[2764] | 37 |
|
---|
[2753] | 38 | float k = tan(mFovy/2);
|
---|
| 39 | mUp *= k;
|
---|
| 40 | mRight *= k*mWidth/mHeight;
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 |
|
---|
| 44 | void Camera::SetPosition(const Vector3 &pos)
|
---|
| 45 | {
|
---|
| 46 | mPosition = pos;
|
---|
| 47 | Precompute();
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 |
|
---|
| 51 | void Camera::SetDirection(const Vector3 &dir)
|
---|
| 52 | {
|
---|
| 53 | mDirection = dir;
|
---|
| 54 | Precompute();
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 |
|
---|
[2764] | 58 | void Camera::SetNear(float nearDist)
|
---|
| 59 | {
|
---|
| 60 | mNear = nearDist;
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 |
|
---|
[2753] | 64 | void Camera::LookInBox(const AxisAlignedBox3 &box)
|
---|
| 65 | {
|
---|
[2764] | 66 | //mDirection = Vector3(0, 0, 1);
|
---|
| 67 | mDirection = Vector3(0, 1, 0);
|
---|
[2753] | 68 | mPosition = box.Center();
|
---|
[2764] | 69 | mPosition.z = box.Min(2) + 0.9f * box.Size(2);
|
---|
[2753] | 70 |
|
---|
| 71 | Precompute();
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 |
|
---|
| 75 | void Camera::LookAtBox(const AxisAlignedBox3 &box)
|
---|
| 76 | {
|
---|
[2764] | 77 | mDirection = Vector3(0, box.Min().y - box.Max().y, 0);
|
---|
[2762] | 78 | mPosition = Vector3(0);//box.Max() - mDirection;
|
---|
[2753] | 79 |
|
---|
| 80 | Precompute();
|
---|
| 81 | }
|
---|
| 82 |
|
---|
[2755] | 83 |
|
---|
| 84 | void Camera::GetProjectionMatrix(Matrix4x4 &mat)
|
---|
| 85 | {
|
---|
| 86 | glGetFloatv(GL_PROJECTION_MATRIX, (float *)mat.x);
|
---|
[2753] | 87 | }
|
---|
| 88 |
|
---|
[2755] | 89 |
|
---|
| 90 | void Camera::GetModelViewMatrix(Matrix4x4 &mat)
|
---|
| 91 | {
|
---|
| 92 | glGetFloatv(GL_MODELVIEW_MATRIX, (float *)mat.x);
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 |
|
---|
| 96 | void Camera::CalcFrustum(Frustum &frustum)
|
---|
| 97 | {
|
---|
| 98 | // we grab the plane equations of the six clipplanes of the viewfrustum
|
---|
| 99 | Matrix4x4 matViewing, matProjectionView;
|
---|
| 100 |
|
---|
| 101 | GetModelViewMatrix(matViewing);
|
---|
| 102 | GetProjectionMatrix(matProjectionView);
|
---|
| 103 |
|
---|
[2762] | 104 | matProjectionView = matViewing * matProjectionView;
|
---|
| 105 | //matProjectionView = matProjectionView * matViewing;
|
---|
| 106 |
|
---|
[2755] | 107 | float planes[6][4];
|
---|
| 108 |
|
---|
[2762] | 109 |
|
---|
[2755] | 110 | //////////
|
---|
| 111 | //-- extract the plane equations
|
---|
| 112 |
|
---|
| 113 | for (int i = 0; i < 4; ++ i)
|
---|
| 114 | {
|
---|
| 115 | planes[0][i] = matProjectionView.x[i][3] - matProjectionView.x[i][0]; // right plane
|
---|
| 116 | planes[1][i] = matProjectionView.x[i][3] + matProjectionView.x[i][0]; // left plane
|
---|
| 117 | planes[2][i] = matProjectionView.x[i][3] + matProjectionView.x[i][1]; // bottom plane
|
---|
| 118 | planes[3][i] = matProjectionView.x[i][3] - matProjectionView.x[i][1]; // top plane
|
---|
| 119 | planes[4][i] = matProjectionView.x[i][3] - matProjectionView.x[i][2]; // far plane
|
---|
| 120 | planes[5][i] = matProjectionView.x[i][3] + matProjectionView.x[i][2]; // near plane
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 |
|
---|
| 124 | ////////////
|
---|
| 125 | //-- normalize the coefficients
|
---|
| 126 |
|
---|
| 127 | for (int i = 0; i < 6; ++ i)
|
---|
| 128 | {
|
---|
| 129 | // the clipping planes look outward the frustum,
|
---|
| 130 | // so distances > 0 mean that a point is outside
|
---|
[2762] | 131 | float fInvLength = -1.0f /
|
---|
| 132 | sqrt(planes[i][0] * planes[i][0] +
|
---|
| 133 | planes[i][1] * planes[i][1] +
|
---|
| 134 | planes[i][2] * planes[i][2]);
|
---|
[2755] | 135 |
|
---|
| 136 | planes[i][0] *= fInvLength;
|
---|
| 137 | planes[i][1] *= fInvLength;
|
---|
| 138 | planes[i][2] *= fInvLength;
|
---|
| 139 | planes[i][3] *= fInvLength;
|
---|
| 140 |
|
---|
[2762] | 141 | frustum.mClipPlanes[i].mNormal = Vector3(planes[i][0], planes[i][1], planes[i][2]);
|
---|
| 142 | frustum.mClipPlanes[i].mD = planes[i][3];
|
---|
[2755] | 143 | }
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 |
|
---|
| 147 | void Camera::Frustum::CalcNPVertexIndices(int *indices)
|
---|
| 148 | {
|
---|
| 149 | for (int i = 0; i < 6; ++ i)
|
---|
| 150 | {
|
---|
| 151 | // n-vertex
|
---|
| 152 | indices[i * 2 + 0] = AxisAlignedBox3::GetIndexNearestVertex(mClipPlanes[i].mNormal);
|
---|
| 153 | // p-vertex
|
---|
| 154 | indices[i * 2 + 1] = AxisAlignedBox3::GetIndexFarthestVertex(mClipPlanes[i].mNormal);
|
---|
| 155 | }
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 |
|
---|
[2756] | 159 | void Camera::SetupCameraView()
|
---|
| 160 | {
|
---|
| 161 | glLoadIdentity();
|
---|
| 162 | gluLookAt(mPosition.x, mPosition.y, mPosition.z,
|
---|
| 163 | mPosition.x + mDirection.x, mPosition.y + mDirection.y, mPosition.z + mDirection.z,
|
---|
| 164 | mUp.x, mUp.y, mUp.z);
|
---|
| 165 |
|
---|
| 166 | //std::cout << "dir: " << mDirection << " pos: " << mPosition << " up: " << mUp << std::endl;
|
---|
[2755] | 167 | }
|
---|
| 168 |
|
---|
[2756] | 169 |
|
---|
[2762] | 170 |
|
---|
| 171 | void Camera::ComputePoints(Vector3 &ftl, Vector3 &ftr, Vector3 &fbl, Vector3 &fbr,
|
---|
| 172 | Vector3 &ntl, Vector3 &ntr, Vector3 &nbl, Vector3 &nbr)
|
---|
| 173 | {
|
---|
| 174 | float z_near = 0.1f;
|
---|
| 175 | float z_far = 300;
|
---|
| 176 |
|
---|
| 177 | const float h_near = 2.0f * tan(mFovy / 2) * z_near;
|
---|
| 178 | const float w_near = h_near * GetAspect();
|
---|
| 179 | const float h_far = 2.0f * tan(mFovy / 2) * z_far;
|
---|
| 180 | const float w_far = h_far * GetAspect();
|
---|
| 181 |
|
---|
| 182 | const Vector3 view = mDirection;
|
---|
| 183 | const Vector3 fc = mPosition + view * z_far;
|
---|
| 184 |
|
---|
| 185 | const Vector3 up = mUp;
|
---|
| 186 |
|
---|
| 187 | const Vector3 right = mRight;
|
---|
| 188 |
|
---|
| 189 | Vector3 t1, t2;
|
---|
| 190 |
|
---|
| 191 | t1 = h_far * 0.5f * up;
|
---|
| 192 | t2 = w_far * 0.5f * right;
|
---|
| 193 |
|
---|
| 194 | ftl = fc + t1 - t2;
|
---|
| 195 | ftr = fc + t1 + t2;
|
---|
| 196 | fbl = fc - t1 - t2;
|
---|
| 197 | fbr = fc - t1 + t2;
|
---|
| 198 |
|
---|
| 199 | const Vector3 nc = mPosition + mDirection * z_near;
|
---|
| 200 |
|
---|
| 201 | t1 = h_near * 0.5f * up;
|
---|
| 202 | t2 = w_near * 0.5f * right;
|
---|
| 203 |
|
---|
| 204 | ntl = nc + t1 - t2;
|
---|
| 205 | ntr = nc + t1 + t2;
|
---|
| 206 | nbl = nc - t1 - t2;
|
---|
| 207 | nbr = nc - t1 + t2;
|
---|
[2756] | 208 | }
|
---|
| 209 |
|
---|
[2762] | 210 |
|
---|
| 211 | }
|
---|
| 212 |
|
---|