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

Revision 2834, 5.7 KB checked in by mattausch, 16 years ago (diff)

research version: implemented temporal smoothing and color bleeding

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