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