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

Revision 2760, 3.5 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        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 = box.Min() - box.Max();
68        mPosition = 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;
95
96        float fInvLength;
97        float planes[6][4];
98
99        //////////
100        //-- extract the plane equations
101
102        for (int i = 0; i < 4; ++ i)
103        {
104                planes[0][i] = matProjectionView.x[i][3] - matProjectionView.x[i][0]; // right plane
105                planes[1][i] = matProjectionView.x[i][3] + matProjectionView.x[i][0]; // left plane
106                planes[2][i] = matProjectionView.x[i][3] + matProjectionView.x[i][1]; // bottom plane
107                planes[3][i] = matProjectionView.x[i][3] - matProjectionView.x[i][1]; // top plane
108                planes[4][i] = matProjectionView.x[i][3] - matProjectionView.x[i][2]; // far plane
109                planes[5][i] = matProjectionView.x[i][3] + matProjectionView.x[i][2]; // near plane
110        }
111
112
113        ////////////
114        //-- normalize the coefficients
115
116        for (int i = 0; i < 6; ++ i)
117        {
118                // the clipping planes look outward the frustum,
119                // so distances > 0 mean that a point is outside
120                fInvLength      = -1.0f / sqrt(planes[i][0] * planes[i][0] +   
121                                                                   planes[i][1] * planes[i][1] +       
122                                                                   planes[i][2] * planes[i][2]);
123
124                planes[i][0] *= fInvLength;
125                planes[i][1] *= fInvLength;
126                planes[i][2] *= fInvLength;
127                planes[i][3] *= fInvLength;
128
129                Vector3 normal(planes[i][0], planes[i][1], planes[i][2]);
130                frustum.mClipPlanes[i] = Plane3(normal, planes[i][3]);
131        }
132}
133
134
135void Camera::Frustum::CalcNPVertexIndices(int *indices)
136{
137        for (int i = 0; i < 6; ++ i)
138        {
139                // n-vertex
140                indices[i * 2 + 0] = AxisAlignedBox3::GetIndexNearestVertex(mClipPlanes[i].mNormal);
141                // p-vertex
142                indices[i * 2 + 1] = AxisAlignedBox3::GetIndexFarthestVertex(mClipPlanes[i].mNormal);   
143        }
144}
145
146
147void Camera::SetupCameraView()
148{
149        glLoadIdentity();
150        gluLookAt(mPosition.x, mPosition.y, mPosition.z,
151                  mPosition.x + mDirection.x, mPosition.y + mDirection.y, mPosition.z + mDirection.z,
152                          mUp.x, mUp.y, mUp.z);
153
154        //std::cout << "dir: " << mDirection << " pos: " << mPosition << " up: " << mUp << std::endl;
155}
156
157
158}
159
Note: See TracBrowser for help on using the repository browser.