source: GTP/trunk/App/Demos/Vis/CHC_revisited/Camera.cpp @ 2778

Revision 2778, 6.4 KB checked in by mattausch, 16 years ago (diff)

updated the camera model

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 = 90.0f * M_PI / 180.0f;
17        mIsOrtho = false;
18       
19        SetPosition(Vector3(0, 0, 0));
20
21        mPitch = mYaw = 0;
22        Precompute();
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        SetPosition(Vector3(0, 0, 0));
38
39        mPitch = mYaw = 0;
40        Precompute();
41}
42
43
44void Camera::Precompute()
45{
46        Vector3 direction = Vector3(0, 1, 0);
47
48        Vector3 side = CrossProd(Vector3(1, 0, 0), direction);
49       
50        Vector3 up = -Normalize(CrossProd(side, direction));
51        Vector3 right = -Normalize(CrossProd(direction, up));
52
53        mBaseOrientation = Matrix4x4(right, up, direction);
54        mViewOrientation = mBaseOrientation;
55}
56
57
58
59void Camera::SetPosition(const Vector3 &pos)
60{
61        mPosition = pos;
62}
63
64
65void Camera::SetNear(float nearDist)
66{
67        mNear = nearDist;
68}
69
70
71void Camera::GetProjectionMatrix(Matrix4x4 &mat)
72{
73        glGetFloatv(GL_PROJECTION_MATRIX, (float *)mat.x);
74}
75
76
77void Camera::GetModelViewMatrix(Matrix4x4 &mat)
78{
79        glGetFloatv(GL_MODELVIEW_MATRIX, (float *)mat.x);
80}
81
82
83void Camera::CalcFrustum(Frustum &frustum)
84{
85        // we grab the plane equations of the six clipplanes of the viewfrustum
86        Matrix4x4 matViewing, matProjectionView;
87
88        GetModelViewMatrix(matViewing);
89        GetProjectionMatrix(matProjectionView);
90
91        matProjectionView = matViewing * matProjectionView;
92        //matProjectionView = matProjectionView * matViewing;
93       
94        float planes[6][4];
95
96
97        //////////
98        //-- extract the plane equations
99
100        for (int i = 0; i < 4; ++ i)
101        {
102                planes[0][i] = matProjectionView.x[i][3] - matProjectionView.x[i][0]; // right plane
103                planes[1][i] = matProjectionView.x[i][3] + matProjectionView.x[i][0]; // left plane
104                planes[2][i] = matProjectionView.x[i][3] + matProjectionView.x[i][1]; // bottom plane
105                planes[3][i] = matProjectionView.x[i][3] - matProjectionView.x[i][1]; // top plane
106                planes[4][i] = matProjectionView.x[i][3] - matProjectionView.x[i][2]; // far plane
107                planes[5][i] = matProjectionView.x[i][3] + matProjectionView.x[i][2]; // near plane
108        }
109
110
111        ////////////
112        //-- normalize the coefficients
113
114        for (int i = 0; i < 6; ++ i)
115        {
116                // the clipping planes look outward the frustum,
117                // so distances > 0 mean that a point is outside
118                float fInvLength = -1.0f /
119                        sqrt(planes[i][0] * planes[i][0] +     
120                             planes[i][1] * planes[i][1] +     
121                                 planes[i][2] * planes[i][2]);
122
123                planes[i][0] *= fInvLength;
124                planes[i][1] *= fInvLength;
125                planes[i][2] *= fInvLength;
126                planes[i][3] *= fInvLength;
127
128                frustum.mClipPlanes[i].mNormal = Vector3(planes[i][0], planes[i][1], planes[i][2]);
129                frustum.mClipPlanes[i].mD = planes[i][3];
130        }
131}
132
133
134void Camera::Frustum::CalcNPVertexIndices(int *indices)
135{
136        for (int i = 0; i < 6; ++ i)
137        {
138                // n-vertex
139                indices[i * 2 + 0] = AxisAlignedBox3::GetIndexNearestVertex(mClipPlanes[i].mNormal);
140                // p-vertex
141                indices[i * 2 + 1] = AxisAlignedBox3::GetIndexFarthestVertex(mClipPlanes[i].mNormal);   
142        }
143}
144
145
146void Camera::SetupCameraView()
147{
148#if 1
149        //Matrix4x4 viewOrientation(mRight, mDirection, mUp);
150        //Matrix4x4 viewOrientation(mRight, mUp, mDirection);
151        Matrix4x4 tview = mViewOrientation;
152
153        //Vector3 pos = tview * mPosition;
154        Vector3 pos = -mPosition;
155        pos = tview * pos;
156
157        Debug << "vieworient:\n" << mViewOrientation << " pos " << pos << " position " << mPosition << endl;
158
159        Matrix4x4 viewOrientation = mViewOrientation;
160
161        viewOrientation.x[3][0] = pos.x;
162        viewOrientation.x[3][1] = pos.y;
163        viewOrientation.x[3][2] = pos.z;
164
165        glLoadIdentity();
166        glMultMatrixf((float *)viewOrientation.x);
167        //glMultMatrixf((float *)tm.x);
168        Debug << "matrix:\n" << viewOrientation << endl;
169#else
170        glLoadIdentity();
171
172        gluLookAt(mPosition.x, mPosition.y, mPosition.z,
173                  mPosition.x + mDirection.x, mPosition.y + mDirection.y, mPosition.z + mDirection.z,
174                          mUp.x, mUp.y, mUp.z);
175#endif
176        //std::cout << "dir: " << mDirection << " pos: " << mPosition << " up: " << mUp << std::endl;
177}
178
179
180
181void Camera::ComputePoints(Vector3 &ftl, Vector3 &ftr, Vector3 &fbl, Vector3 &fbr,
182                                                   Vector3 &ntl, Vector3 &ntr, Vector3 &nbl, Vector3 &nbr)
183{
184        float z_near = 0.1f;
185        float z_far = 300;
186
187        const float h_near = 2.0f * tan(mFovy / 2) * z_near;
188        const float w_near = h_near * GetAspect();
189        const float h_far = 2.0f * tan(mFovy / 2) * z_far;
190        const float w_far = h_far * GetAspect();
191
192        const Vector3 view = GetDirection();
193        const Vector3 fc = mPosition + view * z_far;
194       
195        const Vector3 up = GetUpVector();
196
197        const Vector3 right = GetRightVector();
198
199        Vector3 t1, t2;
200
201        t1 = h_far * 0.5f * up;
202        t2 = w_far * 0.5f * right;
203
204        ftl = fc + t1 - t2;
205        ftr = fc + t1 + t2;
206        fbl = fc - t1 - t2;
207        fbr = fc - t1 + t2;
208
209        const Vector3 nc = mPosition + view * z_near;
210       
211        t1 = h_near * 0.5f * up;
212        t2 = w_near * 0.5f * right;
213
214        ntl = nc + t1 - t2;
215        ntr = nc + t1 + t2;
216        nbl = nc - t1 - t2;
217        nbr = nc - t1 + t2;
218}
219
220
221void Camera::SetOrtho(bool ortho)
222{
223        mIsOrtho = true;
224}
225
226
227void Camera::Yaw(float angle)
228{
229        mYaw += angle;
230        CalculateFromPitchAndYaw();
231        /*
232        Matrix4x4 viewOrientation(mRight, mUp, mDirection);
233        Matrix4x4 rot = RotationYMatrix(angle);
234
235        viewOrientation = viewOrientation * rot;
236
237        mDirection = Vector3(viewOrientation.x[2][0], viewOrientation.x[2][1], viewOrientation.x[2][2]);
238        mRight =     Vector3(viewOrientation.x[0][0], viewOrientation.x[0][1], viewOrientation.x[0][2]);
239        mUp =        Vector3(viewOrientation.x[1][0], viewOrientation.x[1][1], viewOrientation.x[1][2]);*/
240}
241
242
243void Camera::Pitch(float angle)
244{
245        mPitch += angle;
246        CalculateFromPitchAndYaw();
247}
248
249
250void Camera::CalculateFromPitchAndYaw()
251{
252        mViewOrientation = mBaseOrientation;
253
254        Matrix4x4 roty = RotationYMatrix(mPitch);
255        Matrix4x4 rotx = RotationXMatrix(mYaw);
256
257        mViewOrientation *= roty;
258        mViewOrientation *= rotx;
259}
260
261
262Vector3 Camera::GetDirection() const
263{
264        return Vector3(mViewOrientation.x[0][2], mViewOrientation.x[1][2], mViewOrientation.x[2][2]);
265}
266
267
268Vector3 Camera::GetUpVector() const
269{
270        return Vector3(mViewOrientation.x[0][1], mViewOrientation.x[1][1], mViewOrientation.x[2][1]);
271}
272
273
274Vector3 Camera::GetRightVector() const
275{
276        return Vector3(mViewOrientation.x[0][0], mViewOrientation.x[1][0], mViewOrientation.x[2][0]);
277               
278}
279
280
281}
282
Note: See TracBrowser for help on using the repository browser.