1 | #include "common.h"
|
---|
2 | #include "Camera.h"
|
---|
3 | #include "glInterface.h"
|
---|
4 |
|
---|
5 |
|
---|
6 | namespace CHCDemo
|
---|
7 | {
|
---|
8 |
|
---|
9 |
|
---|
10 | Camera::Camera()
|
---|
11 | {
|
---|
12 | mWidth = 100;
|
---|
13 | mHeight = 100;
|
---|
14 | mFovy = 90.0f * M_PI/180.0f;
|
---|
15 | }
|
---|
16 |
|
---|
17 |
|
---|
18 | Camera::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 |
|
---|
27 | void 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 |
|
---|
41 | void Camera::SetPosition(const Vector3 &pos)
|
---|
42 | {
|
---|
43 | mPosition = pos;
|
---|
44 | Precompute();
|
---|
45 | }
|
---|
46 |
|
---|
47 |
|
---|
48 | void Camera::SetDirection(const Vector3 &dir)
|
---|
49 | {
|
---|
50 | mDirection = dir;
|
---|
51 | Precompute();
|
---|
52 | }
|
---|
53 |
|
---|
54 |
|
---|
55 | void 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 |
|
---|
65 | void Camera::LookAtBox(const AxisAlignedBox3 &box)
|
---|
66 | {
|
---|
67 | mDirection = box.Min() - box.Max();
|
---|
68 | mPosition = box.Max() - mDirection;
|
---|
69 |
|
---|
70 | Precompute();
|
---|
71 | }
|
---|
72 |
|
---|
73 |
|
---|
74 | void Camera::GetProjectionMatrix(Matrix4x4 &mat)
|
---|
75 | {
|
---|
76 | //float m[16];
|
---|
77 |
|
---|
78 | glGetFloatv(GL_PROJECTION_MATRIX, (float *)mat.x);
|
---|
79 | //mat = Matrix4x4((const float *)m);
|
---|
80 | }
|
---|
81 |
|
---|
82 |
|
---|
83 | void Camera::GetModelViewMatrix(Matrix4x4 &mat)
|
---|
84 | {
|
---|
85 | //float m[16];
|
---|
86 |
|
---|
87 | glGetFloatv(GL_MODELVIEW_MATRIX, (float *)mat.x);
|
---|
88 | //mat = Matrix4f((const float *)m);
|
---|
89 | }
|
---|
90 |
|
---|
91 |
|
---|
92 | void Camera::CalcFrustum(Frustum &frustum)
|
---|
93 | {
|
---|
94 | // we grab the plane equations of the six clipplanes of the viewfrustum
|
---|
95 | Matrix4x4 matViewing, matProjectionView;
|
---|
96 |
|
---|
97 | GetModelViewMatrix(matViewing);
|
---|
98 | GetProjectionMatrix(matProjectionView);
|
---|
99 |
|
---|
100 | matProjectionView *= matViewing;
|
---|
101 |
|
---|
102 |
|
---|
103 | float fInvLength;
|
---|
104 |
|
---|
105 | float planes[6][4];
|
---|
106 |
|
---|
107 | //////////
|
---|
108 | //-- extract the plane equations
|
---|
109 |
|
---|
110 | for (int i = 0; i < 4; ++ i)
|
---|
111 | {
|
---|
112 | planes[0][i] = matProjectionView.x[i][3] - matProjectionView.x[i][0]; // right plane
|
---|
113 | planes[1][i] = matProjectionView.x[i][3] + matProjectionView.x[i][0]; // left plane
|
---|
114 | planes[2][i] = matProjectionView.x[i][3] + matProjectionView.x[i][1]; // bottom plane
|
---|
115 | planes[3][i] = matProjectionView.x[i][3] - matProjectionView.x[i][1]; // top plane
|
---|
116 | planes[4][i] = matProjectionView.x[i][3] - matProjectionView.x[i][2]; // far plane
|
---|
117 | planes[5][i] = matProjectionView.x[i][3] + matProjectionView.x[i][2]; // near plane
|
---|
118 | }
|
---|
119 |
|
---|
120 |
|
---|
121 | ////////////
|
---|
122 | //-- normalize the coefficients
|
---|
123 |
|
---|
124 | for (int i = 0; i < 6; ++ i)
|
---|
125 | {
|
---|
126 | // the clipping planes look outward the frustum,
|
---|
127 | // so distances > 0 mean that a point is outside
|
---|
128 | fInvLength = -1.0f / sqrt(planes[i][0] * planes[i][0] +
|
---|
129 | planes[i][1] * planes[i][1] +
|
---|
130 | planes[i][2] * planes[i][2]);
|
---|
131 |
|
---|
132 | planes[i][0] *= fInvLength;
|
---|
133 | planes[i][1] *= fInvLength;
|
---|
134 | planes[i][2] *= fInvLength;
|
---|
135 | planes[i][3] *= fInvLength;
|
---|
136 |
|
---|
137 | Vector3 normal(planes[i][0], planes[i][1], planes[i][2]);
|
---|
138 | frustum.mClipPlanes[i] = Plane3(normal, planes[i][3]);
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 |
|
---|
143 | void Camera::Frustum::CalcNPVertexIndices(int *indices)
|
---|
144 | {
|
---|
145 | for (int i = 0; i < 6; ++ i)
|
---|
146 | {
|
---|
147 | // n-vertex
|
---|
148 | indices[i * 2 + 0] = AxisAlignedBox3::GetIndexNearestVertex(mClipPlanes[i].mNormal);
|
---|
149 | // p-vertex
|
---|
150 | indices[i * 2 + 1] = AxisAlignedBox3::GetIndexFarthestVertex(mClipPlanes[i].mNormal);
|
---|
151 | }
|
---|
152 | }
|
---|
153 |
|
---|
154 |
|
---|
155 | void Camera::SetupCameraView()
|
---|
156 | {
|
---|
157 | glLoadIdentity();
|
---|
158 | gluLookAt(mPosition.x, mPosition.y, mPosition.z,
|
---|
159 | mPosition.x + mDirection.x, mPosition.y + mDirection.y, mPosition.z + mDirection.z,
|
---|
160 | mUp.x, mUp.y, mUp.z);
|
---|
161 |
|
---|
162 | //std::cout << "dir: " << mDirection << " pos: " << mPosition << " up: " << mUp << std::endl;
|
---|
163 | }
|
---|
164 |
|
---|
165 |
|
---|
166 | }
|
---|
167 |
|
---|