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

Revision 2762, 4.5 KB checked in by mattausch, 16 years ago (diff)

debug version

Line 
1#include "common.h"
2#include "Camera.h"
3#include "glInterface.h"
4
5
6namespace CHCDemo
7{
8
9
10Camera::Camera()
11{
12        mWidth = 100;
13        mHeight = 100;
14        mFovy = 90.0f * M_PI / 180.0f;
15}
16
17
18Camera::Camera(int width, int height, float fieldOfView)
19{
20        mWidth = width;
21        mHeight = height;
22       
23        mFovy = fieldOfView * M_PI / 180.0f;
24}
25
26
27void Camera::Precompute()
28{
29        mDirection.Normalize();
30
31        Vector3 side = CrossProd(Vector3(0, 1, 0), mDirection);
32        mUp = -Normalize(CrossProd(side, mDirection));
33        mRight = -Normalize(CrossProd(mDirection, mUp));
34
35        float k = tan(mFovy/2);
36        mUp *= k;
37        mRight *= k*mWidth/mHeight;
38}
39
40
41void Camera::SetPosition(const Vector3 &pos)
42{
43        mPosition = pos;
44        Precompute();
45}
46
47
48void Camera::SetDirection(const Vector3 &dir)
49{
50        mDirection = dir;
51        Precompute();
52}
53
54
55void Camera::LookInBox(const AxisAlignedBox3 &box)
56{
57        mDirection = Vector3(0, 0, 1);
58        mPosition = box.Center();
59        mPosition.y += 50;
60
61        Precompute();
62}
63
64
65void Camera::LookAtBox(const AxisAlignedBox3 &box)
66{
67        mDirection = Vector3(0, 0, box.Min().z - box.Max().z);
68        mPosition = Vector3(0);//box.Max() - mDirection;
69
70        Precompute();
71}
72
73
74void Camera::GetProjectionMatrix(Matrix4x4 &mat)
75{
76        glGetFloatv(GL_PROJECTION_MATRIX, (float *)mat.x);
77}
78
79
80void Camera::GetModelViewMatrix(Matrix4x4 &mat)
81{
82        glGetFloatv(GL_MODELVIEW_MATRIX, (float *)mat.x);
83}
84
85
86void Camera::CalcFrustum(Frustum &frustum)
87{
88        // we grab the plane equations of the six clipplanes of the viewfrustum
89        Matrix4x4 matViewing, matProjectionView;
90
91        GetModelViewMatrix(matViewing);
92        GetProjectionMatrix(matProjectionView);
93
94        matProjectionView = matViewing * matProjectionView;
95        //matProjectionView = matProjectionView * matViewing;
96       
97        float planes[6][4];
98
99
100        //////////
101        //-- extract the plane equations
102
103        for (int i = 0; i < 4; ++ i)
104        {
105                planes[0][i] = matProjectionView.x[i][3] - matProjectionView.x[i][0]; // right plane
106                planes[1][i] = matProjectionView.x[i][3] + matProjectionView.x[i][0]; // left plane
107                planes[2][i] = matProjectionView.x[i][3] + matProjectionView.x[i][1]; // bottom plane
108                planes[3][i] = matProjectionView.x[i][3] - matProjectionView.x[i][1]; // top plane
109                planes[4][i] = matProjectionView.x[i][3] - matProjectionView.x[i][2]; // far plane
110                planes[5][i] = matProjectionView.x[i][3] + matProjectionView.x[i][2]; // near plane
111        }
112
113
114        ////////////
115        //-- normalize the coefficients
116
117        for (int i = 0; i < 6; ++ i)
118        {
119                // the clipping planes look outward the frustum,
120                // so distances > 0 mean that a point is outside
121                float fInvLength = -1.0f /
122                        sqrt(planes[i][0] * planes[i][0] +     
123                             planes[i][1] * planes[i][1] +     
124                                 planes[i][2] * planes[i][2]);
125
126                planes[i][0] *= fInvLength;
127                planes[i][1] *= fInvLength;
128                planes[i][2] *= fInvLength;
129                planes[i][3] *= fInvLength;
130
131                frustum.mClipPlanes[i].mNormal = Vector3(planes[i][0], planes[i][1], planes[i][2]);
132                frustum.mClipPlanes[i].mD = planes[i][3];
133        }
134}
135
136
137void Camera::Frustum::CalcNPVertexIndices(int *indices)
138{
139        for (int i = 0; i < 6; ++ i)
140        {
141                // n-vertex
142                indices[i * 2 + 0] = AxisAlignedBox3::GetIndexNearestVertex(mClipPlanes[i].mNormal);
143                // p-vertex
144                indices[i * 2 + 1] = AxisAlignedBox3::GetIndexFarthestVertex(mClipPlanes[i].mNormal);   
145        }
146}
147
148
149void Camera::SetupCameraView()
150{
151        glLoadIdentity();
152        gluLookAt(mPosition.x, mPosition.y, mPosition.z,
153                  mPosition.x + mDirection.x, mPosition.y + mDirection.y, mPosition.z + mDirection.z,
154                          mUp.x, mUp.y, mUp.z);
155
156        //std::cout << "dir: " << mDirection << " pos: " << mPosition << " up: " << mUp << std::endl;
157}
158
159
160
161void Camera::ComputePoints(Vector3 &ftl, Vector3 &ftr, Vector3 &fbl, Vector3 &fbr,
162                                                   Vector3 &ntl, Vector3 &ntr, Vector3 &nbl, Vector3 &nbr)
163{
164        float z_near = 0.1f;
165        float z_far = 300;
166
167        const float h_near = 2.0f * tan(mFovy / 2) * z_near;
168        const float w_near = h_near * GetAspect();
169        const float h_far = 2.0f * tan(mFovy / 2) * z_far;
170        const float w_far = h_far * GetAspect();
171
172        const Vector3 view = mDirection;
173        const Vector3 fc = mPosition + view * z_far;
174       
175        const Vector3 up = mUp;
176
177        const Vector3 right = mRight;
178
179        Vector3 t1, t2;
180
181        t1 = h_far * 0.5f * up;
182        t2 = w_far * 0.5f * right;
183
184        ftl = fc + t1 - t2;
185        ftr = fc + t1 + t2;
186        fbl = fc - t1 - t2;
187        fbr = fc - t1 + t2;
188
189        const Vector3 nc = mPosition + mDirection * z_near;
190       
191        t1 = h_near * 0.5f * up;
192        t2 = w_near * 0.5f * right;
193
194        ntl = nc + t1 - t2;
195        ntr = nc + t1 + t2;
196        nbl = nc - t1 - t2;
197        nbr = nc - t1 + t2;
198}
199
200
201}
202
Note: See TracBrowser for help on using the repository browser.