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

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