source: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Camera.cpp @ 2844

Revision 2844, 6.0 KB checked in by mattausch, 16 years ago (diff)

lod starting to work

Line 
1#include "common.h"
2#include "Camera.h"
3#include "glInterface.h"
4
5
6namespace CHCDemoEngine
7{
8
9using namespace std;
10
11
12Camera::Camera()
13{
14        mWidth = 100;
15        mHeight = 100;
16        mFovy = 60.0f * M_PI / 180.0f;
17        mIsOrtho = false;
18       
19        SetPosition(Vector3(0, 0, 0));
20
21        mPitch = mYaw = 0;
22        Precompute(Vector3(0, 1, 0));
23}
24
25
26Camera::Camera(int width, int height, float fieldOfView)
27{
28        mWidth = width;
29        mHeight = height;
30       
31        mFovy = fieldOfView * M_PI / 180.0f;
32
33        mIsOrtho = false;
34
35        SetPosition(Vector3(0, 0, 0));
36
37        mPitch = mYaw = 0;
38        Precompute(Vector3(0, 1, 0));
39}
40
41
42void Camera::Precompute(const Vector3 &direction)
43{
44        /*
45        Vector3 side = CrossProd(Vector3(1, 0, 0), direction);
46        Vector3 up = -Normalize(CrossProd(side, direction));
47        Vector3 right = -Normalize(CrossProd(direction, up));
48        */
49        Vector3 up = Vector3(0, 0, 1);
50        Vector3 right = Normalize(CrossProd(up, direction));
51        up = Normalize(CrossProd(direction, right));
52
53        mBaseOrientation = Matrix4x4(right, up, direction);
54        mViewOrientation = mBaseOrientation;
55
56        /*cout << "right: " << right << endl;
57        cout << "up:    " << up << endl;
58        cout << "dir:   " << direction << endl;
59        */
60}
61
62
63
64void Camera::SetPosition(const Vector3 &pos)
65{
66        mPosition = pos;
67}
68
69
70void Camera::SetNear(float nearDist)
71{
72        mNear = nearDist;
73}
74
75
76void Camera::SetFar(float farDist)
77{
78        mFar = farDist;
79}
80
81
82void Camera::GetProjectionMatrix(Matrix4x4 &mat)
83{
84        glGetFloatv(GL_PROJECTION_MATRIX, (float *)mat.x);
85}
86
87
88void Camera::GetModelViewMatrix(Matrix4x4 &mat)
89{
90        glGetFloatv(GL_MODELVIEW_MATRIX, (float *)mat.x);
91}
92
93
94void Camera::CalcFrustum(Frustum &frustum)
95{
96        // we grab the plane equations of the six clipplanes of the viewfrustum
97        Matrix4x4 matViewing, matProjectionView;
98
99        GetModelViewMatrix(matViewing);
100        GetProjectionMatrix(matProjectionView);
101
102        matProjectionView = matViewing * matProjectionView;
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
144void 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
156void Camera::SetupCameraView()
157{
158        Matrix4x4 tview = mViewOrientation;
159
160        Vector3 pos = -mPosition;
161        pos = tview * pos;
162
163        Matrix4x4 viewOrientation = mViewOrientation;
164
165        viewOrientation.x[3][0] = pos.x;
166        viewOrientation.x[3][1] = pos.y;
167        viewOrientation.x[3][2] = pos.z;
168
169        glLoadMatrixf((float *)viewOrientation.x);
170}
171
172
173
174void Camera::ComputePoints(Vector3 &ftl, Vector3 &ftr, Vector3 &fbl, Vector3 &fbr,
175                                                   Vector3 &ntl, Vector3 &ntr, Vector3 &nbl, Vector3 &nbr)
176{
177        float z_near = mNear;
178        float z_far = mFar;
179
180        const float w_near = 2.0f * tan(mFovy / 2) * z_near;
181        const float h_near = w_near / GetAspect();
182        const float w_far = 2.0f * tan(mFovy / 2) * z_far;
183        const float h_far = w_far / GetAspect();
184
185        const Vector3 view = GetDirection();
186        const Vector3 fc = mPosition + view * z_far;
187       
188        const Vector3 up = GetUpVector();
189        const Vector3 right = GetRightVector();
190
191        Vector3 t1, t2;
192
193        t1 = h_far * 0.5f * up;
194        t2 = w_far * 0.5f * right;
195
196        ftl = fc + t1 - t2;
197        ftr = fc + t1 + t2;
198        fbl = fc - t1 - t2;
199        fbr = fc - t1 + t2;
200
201        const Vector3 nc = mPosition + view * z_near;
202       
203        t1 = h_near * 0.5f * up;
204        t2 = w_near * 0.5f * right;
205
206        ntl = nc + t1 - t2;
207        ntr = nc + t1 + t2;
208        nbl = nc - t1 - t2;
209        nbr = nc - t1 + t2;
210}
211
212
213void Camera::SetOrtho(bool ortho)
214{
215        mIsOrtho = true;
216}
217
218
219void Camera::Yaw(float angle)
220{
221        mYaw += angle;
222        CalculateFromPitchAndYaw();
223}
224
225
226void Camera::Pitch(float angle)
227{
228        mPitch += angle;
229        CalculateFromPitchAndYaw();
230}
231
232
233void Camera::SetDirection(const Vector3 &direction)
234{
235        Vector3 h1 = direction; h1.z = 0;
236       
237        if (SqrMagnitude(h1) > 0)
238        {
239                h1.Normalize();
240                mPitch = acos(DotProd(h1, Vector3(0, 1, 0)));
241        }
242
243        Vector3 h2 = direction;// h2.x = 0;
244       
245        if (SqrMagnitude(h2) > 0)
246        {
247                h2.Normalize();
248                mYaw = -acos(DotProd(h2, h1));//Vector3(0, 1, 0)));
249        }
250
251        CalculateFromPitchAndYaw();
252}
253
254
255void Camera::CalculateFromPitchAndYaw()
256{
257        mViewOrientation = mBaseOrientation;
258
259        Matrix4x4 roty = RotationYMatrix(mPitch);
260        Matrix4x4 rotx = RotationXMatrix(mYaw);
261
262        mViewOrientation *= roty;
263        mViewOrientation *= rotx;
264}
265
266
267Vector3 Camera::GetDirection() const
268{
269        return -Vector3(mViewOrientation.x[0][2], mViewOrientation.x[1][2], mViewOrientation.x[2][2]);
270}
271
272
273Vector3 Camera::GetUpVector() const
274{
275        return Vector3(mViewOrientation.x[0][1], mViewOrientation.x[1][1], mViewOrientation.x[2][1]);
276}
277
278
279Vector3 Camera::GetRightVector() const
280{
281        return Vector3(mViewOrientation.x[0][0], mViewOrientation.x[1][0], mViewOrientation.x[2][0]);
282               
283}
284
285
286}
287
Note: See TracBrowser for help on using the repository browser.