[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 |
|
---|
[2756] | 31 | Vector3 side = CrossProd(Vector3(0, 1, 0), mDirection);
|
---|
| 32 | mUp = -Normalize(CrossProd(side, mDirection));
|
---|
| 33 | mRight = -Normalize(CrossProd(mDirection, mUp));
|
---|
[2753] | 34 |
|
---|
| 35 | float k = tan(mFovy/2);
|
---|
| 36 | mUp *= k;
|
---|
| 37 | mRight *= k*mWidth/mHeight;
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 |
|
---|
| 41 | void Camera::SetPosition(const Vector3 &pos)
|
---|
| 42 | {
|
---|
| 43 | mPosition = pos;
|
---|
| 44 | Precompute();
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 |
|
---|
| 48 | void Camera::SetDirection(const Vector3 &dir)
|
---|
| 49 | {
|
---|
| 50 | mDirection = dir;
|
---|
| 51 | Precompute();
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 |
|
---|
| 55 | void Camera::LookInBox(const AxisAlignedBox3 &box)
|
---|
| 56 | {
|
---|
[2756] | 57 | mDirection = Vector3(0, 0, 1);
|
---|
[2753] | 58 | mPosition = box.Center();
|
---|
[2756] | 59 | mPosition.y += 50;
|
---|
[2753] | 60 |
|
---|
| 61 | Precompute();
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 |
|
---|
| 65 | void Camera::LookAtBox(const AxisAlignedBox3 &box)
|
---|
| 66 | {
|
---|
[2756] | 67 | mDirection = box.Min() - box.Max();
|
---|
| 68 | mPosition = box.Max() - mDirection;
|
---|
[2753] | 69 |
|
---|
| 70 | Precompute();
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[2755] | 73 |
|
---|
| 74 | void Camera::GetProjectionMatrix(Matrix4x4 &mat)
|
---|
| 75 | {
|
---|
| 76 | glGetFloatv(GL_PROJECTION_MATRIX, (float *)mat.x);
|
---|
[2753] | 77 | }
|
---|
| 78 |
|
---|
[2755] | 79 |
|
---|
| 80 | void Camera::GetModelViewMatrix(Matrix4x4 &mat)
|
---|
| 81 | {
|
---|
| 82 | glGetFloatv(GL_MODELVIEW_MATRIX, (float *)mat.x);
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 |
|
---|
| 86 | void Camera::CalcFrustum(Frustum &frustum)
|
---|
| 87 | {
|
---|
| 88 | // we grab the plane equations of the six clipplanes of the viewfrustum
|
---|
| 89 | Matrix4x4 matViewing, matProjectionView;
|
---|
| 90 |
|
---|
| 91 | GetModelViewMatrix(matViewing);
|
---|
| 92 | GetProjectionMatrix(matProjectionView);
|
---|
| 93 |
|
---|
| 94 | matProjectionView *= matViewing;
|
---|
| 95 |
|
---|
| 96 | float fInvLength;
|
---|
| 97 | float planes[6][4];
|
---|
| 98 |
|
---|
| 99 | //////////
|
---|
| 100 | //-- extract the plane equations
|
---|
| 101 |
|
---|
| 102 | for (int i = 0; i < 4; ++ i)
|
---|
| 103 | {
|
---|
| 104 | planes[0][i] = matProjectionView.x[i][3] - matProjectionView.x[i][0]; // right plane
|
---|
| 105 | planes[1][i] = matProjectionView.x[i][3] + matProjectionView.x[i][0]; // left plane
|
---|
| 106 | planes[2][i] = matProjectionView.x[i][3] + matProjectionView.x[i][1]; // bottom plane
|
---|
| 107 | planes[3][i] = matProjectionView.x[i][3] - matProjectionView.x[i][1]; // top plane
|
---|
| 108 | planes[4][i] = matProjectionView.x[i][3] - matProjectionView.x[i][2]; // far plane
|
---|
| 109 | planes[5][i] = matProjectionView.x[i][3] + matProjectionView.x[i][2]; // near plane
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 |
|
---|
| 113 | ////////////
|
---|
| 114 | //-- normalize the coefficients
|
---|
| 115 |
|
---|
| 116 | for (int i = 0; i < 6; ++ i)
|
---|
| 117 | {
|
---|
| 118 | // the clipping planes look outward the frustum,
|
---|
| 119 | // so distances > 0 mean that a point is outside
|
---|
| 120 | fInvLength = -1.0f / sqrt(planes[i][0] * planes[i][0] +
|
---|
| 121 | planes[i][1] * planes[i][1] +
|
---|
| 122 | planes[i][2] * planes[i][2]);
|
---|
| 123 |
|
---|
| 124 | planes[i][0] *= fInvLength;
|
---|
| 125 | planes[i][1] *= fInvLength;
|
---|
| 126 | planes[i][2] *= fInvLength;
|
---|
| 127 | planes[i][3] *= fInvLength;
|
---|
| 128 |
|
---|
| 129 | Vector3 normal(planes[i][0], planes[i][1], planes[i][2]);
|
---|
| 130 | frustum.mClipPlanes[i] = Plane3(normal, planes[i][3]);
|
---|
| 131 | }
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 |
|
---|
| 135 | void Camera::Frustum::CalcNPVertexIndices(int *indices)
|
---|
| 136 | {
|
---|
| 137 | for (int i = 0; i < 6; ++ i)
|
---|
| 138 | {
|
---|
| 139 | // n-vertex
|
---|
| 140 | indices[i * 2 + 0] = AxisAlignedBox3::GetIndexNearestVertex(mClipPlanes[i].mNormal);
|
---|
| 141 | // p-vertex
|
---|
| 142 | indices[i * 2 + 1] = AxisAlignedBox3::GetIndexFarthestVertex(mClipPlanes[i].mNormal);
|
---|
| 143 | }
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 |
|
---|
[2756] | 147 | void Camera::SetupCameraView()
|
---|
| 148 | {
|
---|
| 149 | glLoadIdentity();
|
---|
| 150 | gluLookAt(mPosition.x, mPosition.y, mPosition.z,
|
---|
| 151 | mPosition.x + mDirection.x, mPosition.y + mDirection.y, mPosition.z + mDirection.z,
|
---|
| 152 | mUp.x, mUp.y, mUp.z);
|
---|
| 153 |
|
---|
| 154 | //std::cout << "dir: " << mDirection << " pos: " << mPosition << " up: " << mUp << std::endl;
|
---|
[2755] | 155 | }
|
---|
| 156 |
|
---|
[2756] | 157 |
|
---|
| 158 | }
|
---|
| 159 |
|
---|