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

Revision 2796, 5.9 KB checked in by mattausch, 17 years ago (diff)
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::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        Matrix4x4 tview = mViewOrientation;
149
150        Vector3 pos = -mPosition;
151        pos = tview * pos;
152
153        Matrix4x4 viewOrientation = mViewOrientation;
154
155        viewOrientation.x[3][0] = pos.x;
156        viewOrientation.x[3][1] = pos.y;
157        viewOrientation.x[3][2] = pos.z;
158
159        glLoadMatrixf((float *)viewOrientation.x);
160}
161
162
163
164void Camera::ComputePoints(Vector3 &ftl, Vector3 &ftr, Vector3 &fbl, Vector3 &fbr,
165                                                   Vector3 &ntl, Vector3 &ntr, Vector3 &nbl, Vector3 &nbr)
166{
167        float z_near = 0.1f;
168        float z_far = 300;
169
170        const float h_near = 2.0f * tan(mFovy / 2) * z_near;
171        const float w_near = h_near * GetAspect();
172        const float h_far = 2.0f * tan(mFovy / 2) * z_far;
173        const float w_far = h_far * GetAspect();
174
175        const Vector3 view = GetDirection();
176        const Vector3 fc = mPosition + view * z_far;
177       
178        const Vector3 up = GetUpVector();
179
180        const Vector3 right = GetRightVector();
181
182        Vector3 t1, t2;
183
184        t1 = h_far * 0.5f * up;
185        t2 = w_far * 0.5f * right;
186
187        ftl = fc + t1 - t2;
188        ftr = fc + t1 + t2;
189        fbl = fc - t1 - t2;
190        fbr = fc - t1 + t2;
191
192        const Vector3 nc = mPosition + view * z_near;
193       
194        t1 = h_near * 0.5f * up;
195        t2 = w_near * 0.5f * right;
196
197        ntl = nc + t1 - t2;
198        ntr = nc + t1 + t2;
199        nbl = nc - t1 - t2;
200        nbr = nc - t1 + t2;
201}
202
203
204void Camera::SetOrtho(bool ortho)
205{
206        mIsOrtho = true;
207}
208
209
210void Camera::Yaw(float angle)
211{
212        mYaw += angle;
213        CalculateFromPitchAndYaw();
214        /*
215        Matrix4x4 viewOrientation(mRight, mUp, mDirection);
216        Matrix4x4 rot = RotationYMatrix(angle);
217
218        viewOrientation = viewOrientation * rot;
219
220        mDirection = Vector3(viewOrientation.x[2][0], viewOrientation.x[2][1], viewOrientation.x[2][2]);
221        mRight =     Vector3(viewOrientation.x[0][0], viewOrientation.x[0][1], viewOrientation.x[0][2]);
222        mUp =        Vector3(viewOrientation.x[1][0], viewOrientation.x[1][1], viewOrientation.x[1][2]);*/
223}
224
225
226void Camera::Pitch(float angle)
227{
228        mPitch += angle;
229        CalculateFromPitchAndYaw();
230}
231
232
233void Camera::CalculateFromPitchAndYaw()
234{
235        mViewOrientation = mBaseOrientation;
236
237        Matrix4x4 roty = RotationYMatrix(mPitch);
238        Matrix4x4 rotx = RotationXMatrix(mYaw);
239
240        mViewOrientation *= roty;
241        mViewOrientation *= rotx;
242}
243
244
245Vector3 Camera::GetDirection() const
246{
247        return -Vector3(mViewOrientation.x[0][2], mViewOrientation.x[1][2], mViewOrientation.x[2][2]);
248}
249
250
251Vector3 Camera::GetUpVector() const
252{
253        return Vector3(mViewOrientation.x[0][1], mViewOrientation.x[1][1], mViewOrientation.x[2][1]);
254}
255
256
257Vector3 Camera::GetRightVector() const
258{
259        return Vector3(mViewOrientation.x[0][0], mViewOrientation.x[1][0], mViewOrientation.x[2][0]);
260               
261}
262
263
264}
265
Note: See TracBrowser for help on using the repository browser.