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

Revision 2988, 8.3 KB checked in by mattausch, 16 years ago (diff)

eye linear depth not working

RevLine 
[2753]1#include "common.h"
2#include "Camera.h"
[2755]3#include "glInterface.h"
[2911]4#include "Polygon3.h"
5#include "Matrix4x4.h"
6#include "Polyhedron.h"
[2753]7
[2776]8namespace CHCDemoEngine
[2753]9{
10
[2796]11using namespace std;
[2753]12
[2932]13// our coordinate system is left-handed
14// we look into positive y and have the positive z axis pointing up
[2928]15static const Vector3 baseDir = -Vector3::UNIT_Y();
[2796]16
[2887]17
[2753]18Camera::Camera()
19{
20        mWidth = 100;
21        mHeight = 100;
[2796]22        mFovy = 60.0f * M_PI / 180.0f;
23        mIsOrtho = false;
24       
25        SetPosition(Vector3(0, 0, 0));
26
27        mPitch = mYaw = 0;
[2917]28        Precompute(baseDir);
[2887]29
30        CalculateFromPitchAndYaw();
[2753]31}
32
33
34Camera::Camera(int width, int height, float fieldOfView)
35{
36        mWidth = width;
37        mHeight = height;
38       
[2760]39        mFovy = fieldOfView * M_PI / 180.0f;
[2796]40
41        mIsOrtho = false;
42
[2942]43        SetPosition(Vector3::ZERO());
[2796]44
45        mPitch = mYaw = 0;
[2918]46        Precompute(baseDir);
[2887]47
48        CalculateFromPitchAndYaw();
[2753]49}
50
51
[2928]52void Camera::Precompute(const Vector3 &dir)
[2753]53{
[2928]54        Vector3 up = Vector3::UNIT_Z();
[2941]55        //Vector3 right = Normalize(CrossProd(dir, up));
56        //up = Normalize(CrossProd(right, dir));
[2753]57
[2941]58        //mBaseOrientation = Matrix4x4(right, up, -dir);
[2944]59        mBaseOrientation = LookAt(Vector3::ZERO(), dir, up);//Matrix4x4(right, up, -dir);
[2796]60        mViewOrientation = mBaseOrientation;
[2753]61}
62
63
64void Camera::SetPosition(const Vector3 &pos)
65{
66        mPosition = pos;
67}
68
69
[2764]70void Camera::SetNear(float nearDist)
71{
72        mNear = nearDist;
73}
74
75
[2806]76void Camera::SetFar(float farDist)
77{
78        mFar = farDist;
79}
80
81
[2986]82void Camera::GetProjectionMatrix(Matrix4x4 &mat) const
[2755]83{
84        glGetFloatv(GL_PROJECTION_MATRIX, (float *)mat.x);
[2753]85}
86
[2755]87
[2986]88void Camera::GetModelViewMatrix(Matrix4x4 &mat) const
[2755]89{
[2893]90        mat = mViewOrientation;
91
[2932]92        // note: left handed system => we go into positive z
93        mat.x[3][0] = -DotProd(GetRightVector(), mPosition);
94        mat.x[3][1] = -DotProd(GetUpVector(), mPosition);
95        mat.x[3][2] = DotProd(GetDirection(), mPosition);
[2755]96}
97
98
[2986]99void Camera::GetViewOrientationMatrix(Matrix4x4 &mat) const
100{
101        mat = mViewOrientation;
102}
103
104
[2755]105void Camera::CalcFrustum(Frustum &frustum)
106{
107        // we grab the plane equations of the six clipplanes of the viewfrustum
108        Matrix4x4 matViewing, matProjectionView;
109
110        GetModelViewMatrix(matViewing);
111        GetProjectionMatrix(matProjectionView);
112
[2762]113        matProjectionView = matViewing * matProjectionView;
[2755]114
[2911]115        frustum = Frustum(matProjectionView);
[2762]116
[2944]117
[2755]118        ////////////
119        //-- normalize the coefficients
120
121        for (int i = 0; i < 6; ++ i)
122        {
123                // the clipping planes look outward the frustum,
124                // so distances > 0 mean that a point is outside
[2911]125                const float invLength = -1.0f / Magnitude(frustum.mClipPlanes[i].mNormal);
126       
127                frustum.mClipPlanes[i].mD *= invLength;
128                frustum.mClipPlanes[i].mNormal *= invLength;
[2755]129        }
130}
131
132
[2756]133void Camera::SetupCameraView()
134{
[2932]135        Matrix4x4 m;
136        GetModelViewMatrix(m);
[2780]137
[2932]138        glLoadMatrixf((float *)m.x);
[2755]139}
140
[2756]141
[2986]142
[2911]143void Camera::ComputePointsInternal(Vector3 &ftl, Vector3 &ftr, Vector3 &fbl, Vector3 &fbr,
144                                                                   Vector3 &ntl, Vector3 &ntr, Vector3 &nbl, Vector3 &nbr,
145                                                                   const Vector3 &view, const Vector3 &right, const Vector3 &up,
[2947]146                                                                   const Vector3 &pos,
147                                                                   float farthestVisibleDistance) const
[2762]148{
[2806]149        float z_near = mNear;
[2947]150        float z_far = min(mFar, farthestVisibleDistance);
[2762]151
[2891]152        float fov = mFovy;
[2889]153
[2913]154        const float aspectRatio = GetAspect();
155
[2914]156        const float h_near = tan(fov * 0.5f) * z_near;
157        const float w_near = h_near * aspectRatio;
[2889]158
[2914]159        const float h_far = tan(fov * 0.5f) * z_far;
160        const float w_far = h_far * aspectRatio;
[2762]161
[2911]162        const Vector3 fc = pos + view * z_far;
[2762]163       
164        Vector3 t1, t2;
165
[2914]166        t1 = h_far * up;
167        t2 = w_far * right;
[2762]168
169        ftl = fc + t1 - t2;
170        ftr = fc + t1 + t2;
171        fbl = fc - t1 - t2;
172        fbr = fc - t1 + t2;
173
[2911]174        const Vector3 nc = pos + view * z_near;
[2762]175       
[2914]176        t1 = h_near * up;
177        t2 = w_near * right;
[2762]178
179        ntl = nc + t1 - t2;
180        ntr = nc + t1 + t2;
181        nbl = nc - t1 - t2;
182        nbr = nc - t1 + t2;
[2756]183}
184
[2762]185
[2911]186void Camera::ComputePoints(Vector3 &ftl, Vector3 &ftr, Vector3 &fbl, Vector3 &fbr,
[2947]187                                                   Vector3 &ntl, Vector3 &ntr, Vector3 &nbl, Vector3 &nbr,
188                                                   float farthestVisibleDistance) const
[2911]189{
190        ComputePointsInternal(ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr,
[2947]191                                                  GetDirection(), GetRightVector(), GetUpVector(), GetPosition(),
192                                                  farthestVisibleDistance);
[2911]193}
194
195
[2796]196void Camera::SetOrtho(bool ortho)
197{
198        mIsOrtho = true;
[2762]199}
200
[2796]201
202void Camera::Yaw(float angle)
203{
204        mYaw += angle;
205        CalculateFromPitchAndYaw();
206}
207
208
209void Camera::Pitch(float angle)
210{
211        mPitch += angle;
212        CalculateFromPitchAndYaw();
213}
214
215
[2864]216void Camera::SetDirection(const Vector3 &dir)
[2829]217{
[2927]218        Vector3 ndir = -Normalize(dir);
[2829]219
[2888]220        mPitch = -atan2(ndir.x, ndir.y);
221        mYaw = atan2(ndir.z, sqrt((ndir.x * ndir.x) + (ndir.y * ndir.y)));
[2887]222
[2829]223        CalculateFromPitchAndYaw();
224}
225
226
[2796]227void Camera::CalculateFromPitchAndYaw()
228{
229        mViewOrientation = mBaseOrientation;
230
231        Matrix4x4 roty = RotationYMatrix(mPitch);
232        Matrix4x4 rotx = RotationXMatrix(mYaw);
233
234        mViewOrientation *= roty;
235        mViewOrientation *= rotx;
236}
237
238
239Vector3 Camera::GetDirection() const
240{
[2927]241        return -Vector3(mViewOrientation.x[0][2], mViewOrientation.x[1][2], mViewOrientation.x[2][2]);
[2796]242}
243
244
245Vector3 Camera::GetUpVector() const
246{
247        return Vector3(mViewOrientation.x[0][1], mViewOrientation.x[1][1], mViewOrientation.x[2][1]);
248}
249
250
251Vector3 Camera::GetRightVector() const
252{
[2911]253        return Vector3(mViewOrientation.x[0][0], mViewOrientation.x[1][0], mViewOrientation.x[2][0]);           
[2796]254}
255
256
[2911]257Vector3 Camera::GetBaseDirection() const
258{
[2927]259        return -Vector3(mBaseOrientation.x[0][2], mBaseOrientation.x[1][2], mBaseOrientation.x[2][2]);
[2796]260}
261
[2911]262
263Vector3 Camera::GetBaseUpVector() const
264{
265        return Vector3(mBaseOrientation.x[0][1], mBaseOrientation.x[1][1], mBaseOrientation.x[2][1]);
266}
267
268
269Vector3 Camera::GetBaseRightVector() const
270{
271        return Vector3(mBaseOrientation.x[0][0], mBaseOrientation.x[1][0], mBaseOrientation.x[2][0]);
272}
273
274
275Frustum::Frustum(const Matrix4x4 &trafo)
276{
277        //////////
278        //-- extract the plane equations
279
280        for (int i = 0; i < 4; ++ i)
281        {
282                mClipPlanes[Frustum::RIGHT_PLANE][i] = trafo.x[i][3] - trafo.x[i][0];
283                mClipPlanes[Frustum::LEFT_PLANE][i] = trafo.x[i][3] + trafo.x[i][0];
[2936]284               
[2911]285                mClipPlanes[Frustum::BOTTOM_PLANE][i] = trafo.x[i][3] + trafo.x[i][1];
286                mClipPlanes[Frustum::TOP_PLANE][i] = trafo.x[i][3] - trafo.x[i][1];
[2936]287
[2911]288                mClipPlanes[Frustum::FAR_PLANE][i] = trafo.x[i][3] - trafo.x[i][2];
289                mClipPlanes[Frustum::NEAR_PLANE][i] = trafo.x[i][3] + trafo.x[i][2];
290        }
291}
292
293
294void Frustum::EnclosePolyhedron(const Polyhedron &polyhedron)
295{
296        VertexArray vertices;
297        polyhedron.CollectVertices(vertices);
298
299        for (int i = 0; i < 6; ++ i)
300        {
301                Plane3 &plane = mClipPlanes[i];
302
303                //cout << "p" << i << " " << plane.mD  << endl;
304
305                VertexArray::const_iterator it, it_end = vertices.end();
306
307                float minDist = 1e20;
308
309                for (it = vertices.begin(); it != it_end; ++ it)
310                {
311                        float dist = plane.Distance(*it);
312
313                        if (dist < minDist)
314                        {
315                                cout << "minDist: " << dist << endl;
316                                minDist = dist;
317                        }
318                }
319
320                plane.mD += minDist;
321        }
322}
323
324
325void Frustum::ExtractTransformation(Matrix4x4 &m) const
326{
327        for (int i = 0; i < 4; ++ i)
328        {
329                m.x[i][0] = (mClipPlanes[LEFT_PLANE][i] - mClipPlanes[RIGHT_PLANE][i]) * 0.5f;
330                m.x[i][1] = (mClipPlanes[BOTTOM_PLANE][i] - mClipPlanes[TOP_PLANE][i]) * 0.5f;
331                m.x[i][2] = (mClipPlanes[NEAR_PLANE][i] - mClipPlanes[FAR_PLANE][i]) * 0.5f;
332                m.x[i][3] = (mClipPlanes[LEFT_PLANE][i] + mClipPlanes[RIGHT_PLANE][i]) * 0.5f;
333        }
334}
335
336
[2947]337Polyhedron *Camera::ComputeFrustum(float farthestVisibleDistance) const
[2931]338{
339        Vector3 ftl, ftr, fbl, fbr;
340        Vector3 ntl, ntr, nbl, nbr;
[2911]341
[2931]342        VertexArray sides[6];
343
[2947]344        ComputePoints(ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr, farthestVisibleDistance);
[2931]345
346        for (int i = 0; i < 6; ++ i)
347                sides[i].resize(4);
348       
349        // left, right
350        sides[0][0] = ftl; sides[0][1] = fbl; sides[0][2] = nbl; sides[0][3] = ntl;
351        sides[1][0] = fbr; sides[1][1] = ftr; sides[1][2] = ntr; sides[1][3] = nbr;
352        // bottom, top
353        sides[2][0] = fbl; sides[2][1] = fbr; sides[2][2] = nbr; sides[2][3] = nbl;
354        sides[3][0] = ftr; sides[3][1] = ftl; sides[3][2] = ntl; sides[3][3] = ntr;
355        // near, far
356        sides[4][0] = ntr; sides[4][1] = ntl; sides[4][2] = nbl; sides[4][3] = nbr;
357        sides[5][0] = ftl; sides[5][1] = ftr; sides[5][2] = fbr; sides[5][3] = fbl;
358
359
360        //////////
361        //-- compute polyhedron
362
363        PolygonContainer polygons;
364
365        for (int i = 0; i < 6; ++ i)
366        {
367                Polygon3 *poly = new Polygon3(sides[i]);
368                polygons.push_back(poly);
369        }
370
371        return new Polyhedron(polygons);
[2911]372}
373
[2931]374
375}
Note: See TracBrowser for help on using the repository browser.