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

Revision 2887, 5.8 KB checked in by mattausch, 16 years ago (diff)

made changes to view transformation but still not working

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