1 | #include <QtOpenGL> |
---|
2 | |
---|
3 | #include "Frustum.h" |
---|
4 | |
---|
5 | using namespace projection; |
---|
6 | using namespace gmtl; |
---|
7 | |
---|
8 | /** |
---|
9 | * Constructor. |
---|
10 | */ |
---|
11 | Frustum::Frustum() : QObject() |
---|
12 | { |
---|
13 | m_bShow = true; |
---|
14 | m_bShowArea = true; |
---|
15 | m_visibleFar = 1.2f; |
---|
16 | m_color.setRgb(0, 0, 255); |
---|
17 | |
---|
18 | m_transformMatrix.setUseScaling(false); |
---|
19 | } |
---|
20 | |
---|
21 | /** |
---|
22 | * Destructor. |
---|
23 | */ |
---|
24 | Frustum::~Frustum() |
---|
25 | { |
---|
26 | } |
---|
27 | |
---|
28 | /** |
---|
29 | * Set projection matrix of the frustum. |
---|
30 | * |
---|
31 | * @param matrix Projection matrix of the frustum. |
---|
32 | */ |
---|
33 | void Frustum::setProjectionMatrix(const ProjectionMatrix& matrix) |
---|
34 | { |
---|
35 | m_projectionMatrix = matrix; |
---|
36 | emit dataChanged(); |
---|
37 | } |
---|
38 | |
---|
39 | /** |
---|
40 | * Retrieve projection matrix of the frustum. |
---|
41 | * |
---|
42 | * @return Projection matrix of the frustum. |
---|
43 | */ |
---|
44 | ProjectionMatrix Frustum::getProjectionMatrix() const |
---|
45 | { |
---|
46 | return m_projectionMatrix; |
---|
47 | } |
---|
48 | |
---|
49 | /** |
---|
50 | * Set transform matrix of the frustum. |
---|
51 | * |
---|
52 | * @param matrix Transform matrix of the frustum. |
---|
53 | */ |
---|
54 | void Frustum::setTransformMatrix(const TransformMatrix& matrix) |
---|
55 | { |
---|
56 | m_transformMatrix = matrix; |
---|
57 | emit dataChanged(); |
---|
58 | } |
---|
59 | |
---|
60 | /** |
---|
61 | * Retrieve transform matrix of the frustum. |
---|
62 | * |
---|
63 | * @return Transform matrix of the frustum. |
---|
64 | */ |
---|
65 | TransformMatrix Frustum::getTransformMatrix() const |
---|
66 | { |
---|
67 | return m_transformMatrix; |
---|
68 | } |
---|
69 | |
---|
70 | /** |
---|
71 | * Set color of the frustum shape object. |
---|
72 | * |
---|
73 | * @param color Color of the frustum shape object. |
---|
74 | */ |
---|
75 | void Frustum::setColor(const QColor& color) |
---|
76 | { |
---|
77 | m_color = color; |
---|
78 | emit dataChanged(); |
---|
79 | } |
---|
80 | |
---|
81 | /** |
---|
82 | * Retrieve color of the frustum shape object. |
---|
83 | * |
---|
84 | * @return Color of the frustum shape object. |
---|
85 | */ |
---|
86 | QColor Frustum::getColor() const |
---|
87 | { |
---|
88 | return m_color; |
---|
89 | } |
---|
90 | |
---|
91 | /** |
---|
92 | * Show or hide the frustum shape object. |
---|
93 | * |
---|
94 | * @param bShow True to show the frustum shape object, false to hide. |
---|
95 | */ |
---|
96 | void Frustum::setShow(bool bShow) |
---|
97 | { |
---|
98 | m_bShow = bShow; |
---|
99 | emit dataChanged(); |
---|
100 | } |
---|
101 | |
---|
102 | /** |
---|
103 | * Check whether the frustum shape object is shown or not. |
---|
104 | * |
---|
105 | * @return True if the frustum shape object is shown. |
---|
106 | */ |
---|
107 | bool Frustum::getShow() const |
---|
108 | { |
---|
109 | return m_bShow; |
---|
110 | } |
---|
111 | |
---|
112 | /** |
---|
113 | * Show or hide the frustum projected area. |
---|
114 | * |
---|
115 | * @param bShow True to show the frustum projected area, false to hide. |
---|
116 | */ |
---|
117 | void Frustum::setShowArea(bool bShow) |
---|
118 | { |
---|
119 | m_bShowArea = bShow; |
---|
120 | emit dataChanged(); |
---|
121 | } |
---|
122 | |
---|
123 | /** |
---|
124 | * Check whether the frustum projected area is shown or not. |
---|
125 | * |
---|
126 | * @return True if the frustum projected area is shown. |
---|
127 | */ |
---|
128 | bool Frustum::getShowArea() const |
---|
129 | { |
---|
130 | return m_bShowArea; |
---|
131 | } |
---|
132 | |
---|
133 | /** |
---|
134 | * Set far clip plane distance of the frame shape object. It doesn't modify the data. |
---|
135 | * |
---|
136 | * @param farDist Far clip plane distance of the frame shape object. |
---|
137 | */ |
---|
138 | void Frustum::setVisibleFar(float farDist) |
---|
139 | { |
---|
140 | m_visibleFar = farDist; |
---|
141 | } |
---|
142 | |
---|
143 | /** |
---|
144 | * Retrieve far clip plane distance of the frame shape object. |
---|
145 | * |
---|
146 | * @return Far clip plane distance of the frame shape object. |
---|
147 | */ |
---|
148 | float Frustum::getVisibleFar() const |
---|
149 | { |
---|
150 | return m_visibleFar; |
---|
151 | } |
---|
152 | |
---|
153 | /** |
---|
154 | * Draw frustum shape object. |
---|
155 | * |
---|
156 | * @param bSelected True to draw with selection highlight. |
---|
157 | */ |
---|
158 | void Frustum::draw(bool bSelected) |
---|
159 | { |
---|
160 | if (m_bShow) |
---|
161 | { |
---|
162 | glPushMatrix(); |
---|
163 | glMultMatrixf(m_transformMatrix.getMatrix().getData()); |
---|
164 | glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT | GL_LINE_BIT); |
---|
165 | |
---|
166 | if (bSelected) |
---|
167 | glLineWidth(3.0f); |
---|
168 | |
---|
169 | float left = -tanf(Math::deg2Rad(m_projectionMatrix.getFOV()/2.0f)) * m_projectionMatrix.getAspectRatio() + m_projectionMatrix.getOffaxisX(); |
---|
170 | float right = tanf(Math::deg2Rad(m_projectionMatrix.getFOV()/2.0f)) * m_projectionMatrix.getAspectRatio() + m_projectionMatrix.getOffaxisX(); |
---|
171 | float top = -tanf(Math::deg2Rad(m_projectionMatrix.getFOV()/2.0f)) + m_projectionMatrix.getOffaxisY(); |
---|
172 | float bottom = tanf(Math::deg2Rad(m_projectionMatrix.getFOV()/2.0f)) + m_projectionMatrix.getOffaxisY(); |
---|
173 | float nearDist = m_projectionMatrix.getNear(); |
---|
174 | float farDist = m_projectionMatrix.getFar(); |
---|
175 | if (m_visibleFar < farDist) |
---|
176 | farDist = m_visibleFar; |
---|
177 | |
---|
178 | glColor4f(1.0f, 1.0f, 1.0f, 1.0f); |
---|
179 | glDisable(GL_LIGHTING); |
---|
180 | |
---|
181 | glBegin(GL_LINE_LOOP); |
---|
182 | glVertex3f(nearDist*left, nearDist*bottom, -nearDist); |
---|
183 | glVertex3f(nearDist*right, nearDist*bottom, -nearDist); |
---|
184 | glVertex3f(nearDist*right, nearDist*top, -nearDist); |
---|
185 | glVertex3f(nearDist*left, nearDist*top, -nearDist); |
---|
186 | glEnd(); |
---|
187 | glBegin(GL_LINE_LOOP); |
---|
188 | glVertex3f(farDist*left, farDist*bottom, -farDist); |
---|
189 | glVertex3f(farDist*right, farDist*bottom, -farDist); |
---|
190 | glVertex3f(farDist*right, farDist*top, -farDist); |
---|
191 | glVertex3f(farDist*left, farDist*top, -farDist); |
---|
192 | glEnd(); |
---|
193 | glBegin(GL_LINES); |
---|
194 | glVertex3f(0.0f, 0.0f, 0.0f); |
---|
195 | glVertex3f(farDist*left, farDist*bottom, -farDist); |
---|
196 | glVertex3f(0.0f, 0.0f, 0.0f); |
---|
197 | glVertex3f(farDist*right, farDist*bottom, -farDist); |
---|
198 | glVertex3f(0.0f, 0.0f, 0.0f); |
---|
199 | glVertex3f(farDist*right, farDist*top, -farDist); |
---|
200 | glVertex3f(0.0f, 0.0f, 0.0f); |
---|
201 | glVertex3f(farDist*left, farDist*top, -farDist); |
---|
202 | glEnd(); |
---|
203 | |
---|
204 | glColor4f((float)m_color.red()/255.0f, (float)m_color.green()/255.0f, (float)m_color.blue()/255.0f, 0.25f); |
---|
205 | glEnable(GL_LIGHTING); |
---|
206 | glEnable(GL_BLEND); |
---|
207 | glDepthMask(false); |
---|
208 | |
---|
209 | glBegin(GL_TRIANGLES); |
---|
210 | glNormal3f(0.0f, cosf(Math::deg2Rad(m_projectionMatrix.getFOV())/2.0f), -sinf(Math::deg2Rad(m_projectionMatrix.getFOV())/2.0f)); |
---|
211 | glVertex3f(0.0f, 0.0f, 0.0f); |
---|
212 | glVertex3f(farDist*left, farDist*bottom, -farDist); |
---|
213 | glVertex3f(farDist*right, farDist*bottom, -farDist); |
---|
214 | glNormal3f( cosf(Math::deg2Rad(m_projectionMatrix.getFOV())*m_projectionMatrix.getAspectRatio()/2.0f), |
---|
215 | 0.0f, |
---|
216 | -sinf(Math::deg2Rad(m_projectionMatrix.getFOV())*m_projectionMatrix.getAspectRatio()/2.0f)); |
---|
217 | glVertex3f(0.0f, 0.0f, 0.0f); |
---|
218 | glVertex3f(farDist*right, farDist*bottom, -farDist); |
---|
219 | glVertex3f(farDist*right, farDist*top, -farDist); |
---|
220 | glNormal3f(0.0f, -cosf(Math::deg2Rad(m_projectionMatrix.getFOV())/2.0f), -sinf(Math::deg2Rad(m_projectionMatrix.getFOV())/2.0f)); |
---|
221 | glVertex3f(0.0f, 0.0f, 0.0f); |
---|
222 | glVertex3f(farDist*right, farDist*top, -farDist); |
---|
223 | glVertex3f(farDist*left, farDist*top, -farDist); |
---|
224 | glNormal3f(-cosf(Math::deg2Rad(m_projectionMatrix.getFOV())*m_projectionMatrix.getAspectRatio()/2.0f), |
---|
225 | 0.0f, |
---|
226 | -sinf(Math::deg2Rad(m_projectionMatrix.getFOV())*m_projectionMatrix.getAspectRatio()/2.0f)); |
---|
227 | glVertex3f(0.0f, 0.0f, 0.0f); |
---|
228 | glVertex3f(farDist*left, farDist*top, -farDist); |
---|
229 | glVertex3f(farDist*left, farDist*bottom, -farDist); |
---|
230 | glEnd(); |
---|
231 | |
---|
232 | glDepthMask(true); |
---|
233 | glPopAttrib(); |
---|
234 | glPopMatrix(); |
---|
235 | } |
---|
236 | } |
---|
237 | |
---|
238 | /** |
---|
239 | * Assignment operator. |
---|
240 | * |
---|
241 | * @param frustum Assignment source frustum. |
---|
242 | */ |
---|
243 | Frustum& Frustum::operator=(const Frustum& frustum) |
---|
244 | { |
---|
245 | if (this != &frustum) |
---|
246 | { |
---|
247 | m_projectionMatrix = frustum.m_projectionMatrix; |
---|
248 | m_transformMatrix = frustum.m_transformMatrix; |
---|
249 | m_bShow = frustum.m_bShow; |
---|
250 | m_visibleFar = frustum.m_visibleFar; |
---|
251 | m_color = frustum.m_color; |
---|
252 | } |
---|
253 | return *this; |
---|
254 | } |
---|
255 | |
---|
256 | /** |
---|
257 | * Restore the frustum data from XML data. |
---|
258 | * |
---|
259 | * @param element Parent XML element of the frustum data. |
---|
260 | */ |
---|
261 | void Frustum::initFromDOMElement(const QDomElement& element) |
---|
262 | { |
---|
263 | if (!element.isNull()) |
---|
264 | { |
---|
265 | m_projectionMatrix.initFromDOMElement(element.firstChildElement("ProjectionMatrix")); |
---|
266 | m_transformMatrix.initFromDOMElement(element.firstChildElement("TransformMatrix")); |
---|
267 | m_bShow = element.attribute("show").toLower()=="true"; |
---|
268 | m_bShowArea = element.attribute("showArea").toLower()=="true"; |
---|
269 | } |
---|
270 | } |
---|
271 | |
---|
272 | /** |
---|
273 | * Store the current frustum data as XML data. |
---|
274 | * |
---|
275 | * @param name XML node name of the data. |
---|
276 | * @param doc XML document to store the data. |
---|
277 | * @return Current frustum data as XML data. |
---|
278 | */ |
---|
279 | QDomElement Frustum::domElement(const QString& name, QDomDocument& doc) const |
---|
280 | { |
---|
281 | QDomElement de = doc.createElement(name); |
---|
282 | |
---|
283 | de.appendChild(m_projectionMatrix.domElement("ProjectionMatrix", doc)); |
---|
284 | de.appendChild(m_transformMatrix.domElement("TransformMatrix", doc)); |
---|
285 | de.setAttribute("show", (m_bShow?"true":"false")); |
---|
286 | de.setAttribute("showArea", (m_bShowArea?"true":"false")); |
---|
287 | |
---|
288 | return de; |
---|
289 | } |
---|