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

Revision 2806, 5.9 KB checked in by mattausch, 17 years ago (diff)

improved visualization

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(direction, up));
51        up = -Normalize(CrossProd(right, direction));
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::SetFar(float farDist)
72{
73        mFar = farDist;
74}
75
76
77void Camera::GetProjectionMatrix(Matrix4x4 &mat)
78{
79        glGetFloatv(GL_PROJECTION_MATRIX, (float *)mat.x);
80}
81
82
83void Camera::GetModelViewMatrix(Matrix4x4 &mat)
84{
85        glGetFloatv(GL_MODELVIEW_MATRIX, (float *)mat.x);
86}
87
88
89void Camera::CalcFrustum(Frustum &frustum)
90{
91        // we grab the plane equations of the six clipplanes of the viewfrustum
92        Matrix4x4 matViewing, matProjectionView;
93
94        GetModelViewMatrix(matViewing);
95        GetProjectionMatrix(matProjectionView);
96
97        matProjectionView = matViewing * matProjectionView;
98        //matProjectionView = matProjectionView * matViewing;
99       
100        float planes[6][4];
101
102
103        //////////
104        //-- extract the plane equations
105
106        for (int i = 0; i < 4; ++ i)
107        {
108                planes[0][i] = matProjectionView.x[i][3] - matProjectionView.x[i][0]; // right plane
109                planes[1][i] = matProjectionView.x[i][3] + matProjectionView.x[i][0]; // left plane
110                planes[2][i] = matProjectionView.x[i][3] + matProjectionView.x[i][1]; // bottom plane
111                planes[3][i] = matProjectionView.x[i][3] - matProjectionView.x[i][1]; // top plane
112                planes[4][i] = matProjectionView.x[i][3] - matProjectionView.x[i][2]; // far plane
113                planes[5][i] = matProjectionView.x[i][3] + matProjectionView.x[i][2]; // near plane
114        }
115
116
117        ////////////
118        //-- normalize the coefficients
119
120        for (int i = 0; i < 6; ++ i)
121        {
122                // the clipping planes look outward the frustum,
123                // so distances > 0 mean that a point is outside
124                float fInvLength = -1.0f /
125                        sqrt(planes[i][0] * planes[i][0] +     
126                             planes[i][1] * planes[i][1] +     
127                                 planes[i][2] * planes[i][2]);
128
129                planes[i][0] *= fInvLength;
130                planes[i][1] *= fInvLength;
131                planes[i][2] *= fInvLength;
132                planes[i][3] *= fInvLength;
133
134                frustum.mClipPlanes[i].mNormal = Vector3(planes[i][0], planes[i][1], planes[i][2]);
135                frustum.mClipPlanes[i].mD = planes[i][3];
136        }
137}
138
139
140void Camera::Frustum::CalcNPVertexIndices(int *indices)
141{
142        for (int i = 0; i < 6; ++ i)
143        {
144                // n-vertex
145                indices[i * 2 + 0] = AxisAlignedBox3::GetIndexNearestVertex(mClipPlanes[i].mNormal);
146                // p-vertex
147                indices[i * 2 + 1] = AxisAlignedBox3::GetIndexFarthestVertex(mClipPlanes[i].mNormal);   
148        }
149}
150
151
152void Camera::SetupCameraView()
153{
154        Matrix4x4 tview = mViewOrientation;
155
156        Vector3 pos = -mPosition;
157        pos = tview * pos;
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        glLoadMatrixf((float *)viewOrientation.x);
166}
167
168
169
170void Camera::ComputePoints(Vector3 &ftl, Vector3 &ftr, Vector3 &fbl, Vector3 &fbr,
171                                                   Vector3 &ntl, Vector3 &ntr, Vector3 &nbl, Vector3 &nbr)
172{
173        float z_near = mNear;
174        float z_far = mFar;
175
176        const float h_near = 2.0f * tan(mFovy / 2) * z_near;
177        const float w_near = h_near * GetAspect();
178        const float h_far = 2.0f * tan(mFovy / 2) * z_far;
179        const float w_far = h_far * GetAspect();
180
181        const Vector3 view = GetDirection();
182        const Vector3 fc = mPosition + view * z_far;
183       
184        const Vector3 up = GetUpVector();
185
186        const Vector3 right = GetRightVector();
187
188        Vector3 t1, t2;
189
190        t1 = h_far * 0.5f * up;
191        t2 = w_far * 0.5f * right;
192
193        ftl = fc + t1 - t2;
194        ftr = fc + t1 + t2;
195        fbl = fc - t1 - t2;
196        fbr = fc - t1 + t2;
197
198        const Vector3 nc = mPosition + view * z_near;
199       
200        t1 = h_near * 0.5f * up;
201        t2 = w_near * 0.5f * right;
202
203        ntl = nc + t1 - t2;
204        ntr = nc + t1 + t2;
205        nbl = nc - t1 - t2;
206        nbr = nc - t1 + t2;
207}
208
209
210void Camera::SetOrtho(bool ortho)
211{
212        mIsOrtho = true;
213}
214
215
216void Camera::Yaw(float angle)
217{
218        mYaw += angle;
219        CalculateFromPitchAndYaw();
220        /*
221        Matrix4x4 viewOrientation(mRight, mUp, mDirection);
222        Matrix4x4 rot = RotationYMatrix(angle);
223
224        viewOrientation = viewOrientation * rot;
225
226        mDirection = Vector3(viewOrientation.x[2][0], viewOrientation.x[2][1], viewOrientation.x[2][2]);
227        mRight =     Vector3(viewOrientation.x[0][0], viewOrientation.x[0][1], viewOrientation.x[0][2]);
228        mUp =        Vector3(viewOrientation.x[1][0], viewOrientation.x[1][1], viewOrientation.x[1][2]);*/
229}
230
231
232void Camera::Pitch(float angle)
233{
234        mPitch += angle;
235        CalculateFromPitchAndYaw();
236}
237
238
239void Camera::CalculateFromPitchAndYaw()
240{
241        mViewOrientation = mBaseOrientation;
242
243        Matrix4x4 roty = RotationYMatrix(mPitch);
244        Matrix4x4 rotx = RotationXMatrix(mYaw);
245
246        mViewOrientation *= roty;
247        mViewOrientation *= rotx;
248}
249
250
251Vector3 Camera::GetDirection() const
252{
253        return -Vector3(mViewOrientation.x[0][2], mViewOrientation.x[1][2], mViewOrientation.x[2][2]);
254}
255
256
257Vector3 Camera::GetUpVector() const
258{
259        return Vector3(mViewOrientation.x[0][1], mViewOrientation.x[1][1], mViewOrientation.x[2][1]);
260}
261
262
263Vector3 Camera::GetRightVector() const
264{
265        return Vector3(mViewOrientation.x[0][0], mViewOrientation.x[1][0], mViewOrientation.x[2][0]);
266               
267}
268
269
270}
271
Note: See TracBrowser for help on using the repository browser.