1 | #include "screen/ScreenBox.h" |
---|
2 | |
---|
3 | using namespace projection; |
---|
4 | |
---|
5 | /** |
---|
6 | * Constructor. |
---|
7 | * |
---|
8 | * @param pScreen Screen data. |
---|
9 | */ |
---|
10 | ScreenBox::ScreenBox(Screen* pScreen) : ScreenShape(pScreen) |
---|
11 | { |
---|
12 | m_width = 2.0; |
---|
13 | m_height = 2.0; |
---|
14 | m_depth = 2.0; |
---|
15 | m_horResolution = 4; |
---|
16 | m_vertResolution = 4; |
---|
17 | m_depthResolution = 4; |
---|
18 | } |
---|
19 | |
---|
20 | /** |
---|
21 | * Destructor. |
---|
22 | */ |
---|
23 | ScreenBox::~ScreenBox() |
---|
24 | { |
---|
25 | } |
---|
26 | |
---|
27 | /** |
---|
28 | * Set width of the box screen. |
---|
29 | * |
---|
30 | * @param width Width of the box screen. |
---|
31 | */ |
---|
32 | void ScreenBox::setWidth(double width) |
---|
33 | { |
---|
34 | if (m_width != width) |
---|
35 | { |
---|
36 | m_width = width; |
---|
37 | notifyRedraw(); |
---|
38 | } |
---|
39 | } |
---|
40 | |
---|
41 | /** |
---|
42 | * Set height of the box screen. |
---|
43 | * |
---|
44 | * @param height Height of the box screen. |
---|
45 | */ |
---|
46 | void ScreenBox::setHeight(double height) |
---|
47 | { |
---|
48 | if (m_height != height) |
---|
49 | { |
---|
50 | m_height = height; |
---|
51 | notifyRedraw(); |
---|
52 | } |
---|
53 | } |
---|
54 | |
---|
55 | /** |
---|
56 | * Set depth of the box screen. |
---|
57 | * |
---|
58 | * @param depth Depth of the box screen. |
---|
59 | */ |
---|
60 | void ScreenBox::setDepth(double depth) |
---|
61 | { |
---|
62 | if (m_depth != depth) |
---|
63 | { |
---|
64 | m_depth = depth; |
---|
65 | notifyRedraw(); |
---|
66 | } |
---|
67 | } |
---|
68 | |
---|
69 | /** |
---|
70 | * Set horizontal resolution of the box screen. |
---|
71 | * |
---|
72 | * @param resolution Horizontal resolution of the box screen. |
---|
73 | */ |
---|
74 | void ScreenBox::setHorResolution(unsigned int resolution) |
---|
75 | { |
---|
76 | m_horResolution = resolution; |
---|
77 | notifyRedraw(); |
---|
78 | } |
---|
79 | |
---|
80 | /** |
---|
81 | * Set vertical resolution of the box screen. |
---|
82 | * |
---|
83 | * @param resolution Vertical resolution of the box screen. |
---|
84 | */ |
---|
85 | void ScreenBox::setVertResolution(unsigned int resolution) |
---|
86 | { |
---|
87 | m_vertResolution = resolution; |
---|
88 | notifyRedraw(); |
---|
89 | } |
---|
90 | |
---|
91 | /** |
---|
92 | * Set depth resolution of the box screen. |
---|
93 | * |
---|
94 | * @param resolution Depth resolution of the box screen. |
---|
95 | */ |
---|
96 | void ScreenBox::setDepthResolution(unsigned int resolution) |
---|
97 | { |
---|
98 | m_depthResolution = resolution; |
---|
99 | notifyRedraw(); |
---|
100 | } |
---|
101 | |
---|
102 | /** |
---|
103 | * Retrieve bounding box of the screen shape. |
---|
104 | * |
---|
105 | * @param min One corner of the screen shape. |
---|
106 | * @param max Another corner of the screen shape. |
---|
107 | */ |
---|
108 | void ScreenBox::getBoundingBox(gmtl::Vec3f& min, gmtl::Vec3f& max) |
---|
109 | { |
---|
110 | min.set((float)-m_width/2.0f, (float)-m_depth/2.0f, (float)-m_height/2.0f); |
---|
111 | max.set((float)m_width/2.0f, (float)m_depth/2.0f, (float)m_height/2.0f); |
---|
112 | } |
---|
113 | |
---|
114 | /** |
---|
115 | * Restore the screen shape from XML data. |
---|
116 | * |
---|
117 | * @param element Parent XML element of the screen shape data. |
---|
118 | */ |
---|
119 | bool ScreenBox::initFromDOMElement(const QDomElement& element) |
---|
120 | { |
---|
121 | // don't notify redrawing by each restoring step |
---|
122 | if (!element.isNull()) |
---|
123 | { |
---|
124 | m_width = element.attribute("width").toFloat(); |
---|
125 | m_height = element.attribute("height").toFloat(); |
---|
126 | m_depth = element.attribute("depth").toFloat(); |
---|
127 | m_horResolution = element.attribute("horResolution").toInt(); |
---|
128 | m_vertResolution = element.attribute("vertResolution").toInt(); |
---|
129 | m_depthResolution = element.attribute("depthResolution").toInt(); |
---|
130 | // if change |
---|
131 | notifyRedraw(); |
---|
132 | } |
---|
133 | |
---|
134 | return true; // todo: secure this function and return false on any critical error |
---|
135 | } |
---|
136 | |
---|
137 | /** |
---|
138 | * Store the current screen shape as XML data. |
---|
139 | * |
---|
140 | * @param name XML node name of the data. |
---|
141 | * @param doc XML document to store the data. |
---|
142 | * @return Current screen shape data as XML data. |
---|
143 | */ |
---|
144 | QDomElement ScreenBox::domElement(const QString& name, QDomDocument& doc) const |
---|
145 | { |
---|
146 | QDomElement de = doc.createElement(name); |
---|
147 | de.setAttribute("name", getName()); |
---|
148 | de.setAttribute("width", m_width); |
---|
149 | de.setAttribute("height", m_height); |
---|
150 | de.setAttribute("depth", m_depth); |
---|
151 | de.setAttribute("horResolution", m_horResolution); |
---|
152 | de.setAttribute("vertResolution", m_vertResolution); |
---|
153 | de.setAttribute("depthResolution", m_depthResolution); |
---|
154 | |
---|
155 | return de; |
---|
156 | } |
---|
157 | |
---|
158 | /** |
---|
159 | * Draw the shape model in inherited class. |
---|
160 | * |
---|
161 | * @param bFrame True to draw a wire frame mesh. False to draw as a polygon model. |
---|
162 | */ |
---|
163 | void ScreenBox::drawShape(bool) |
---|
164 | { |
---|
165 | glPushAttrib(GL_ENABLE_BIT); |
---|
166 | glEnable(GL_CULL_FACE); |
---|
167 | |
---|
168 | double horDelta = m_width / m_horResolution; |
---|
169 | double vertDelta = m_height / m_vertResolution; |
---|
170 | double depthDelta = m_depth / m_depthResolution; |
---|
171 | unsigned int horCount; |
---|
172 | unsigned int vertCount; |
---|
173 | unsigned int depthCount; |
---|
174 | |
---|
175 | for (vertCount=0; vertCount<m_vertResolution; ++vertCount) |
---|
176 | { |
---|
177 | glBegin(GL_QUAD_STRIP); |
---|
178 | for (horCount=0; horCount<=m_horResolution; ++horCount) |
---|
179 | { |
---|
180 | glNormal3d(0, 1, 0); |
---|
181 | glVertex3d(-m_width/2.0 + horDelta*horCount, |
---|
182 | -m_depth/2.0, |
---|
183 | -m_height/2.0 + vertDelta*vertCount); |
---|
184 | glNormal3d(0, 1, 0); |
---|
185 | glVertex3d(-m_width/2.0 + horDelta*(horCount), |
---|
186 | -m_depth/2.0, |
---|
187 | -m_height/2.0 + vertDelta*(vertCount+1)); |
---|
188 | } |
---|
189 | glEnd(); |
---|
190 | } |
---|
191 | for (vertCount=0; vertCount<m_vertResolution; ++vertCount) |
---|
192 | { |
---|
193 | glBegin(GL_QUAD_STRIP); |
---|
194 | for (horCount=0; horCount<=m_horResolution; ++horCount) |
---|
195 | { |
---|
196 | glNormal3d(0, -1, 0); |
---|
197 | glVertex3d(m_width/2.0 - horDelta*horCount, |
---|
198 | m_depth/2.0, |
---|
199 | -m_height/2.0 + vertDelta*vertCount); |
---|
200 | glNormal3d(0, -1, 0); |
---|
201 | glVertex3d(m_width/2.0 - horDelta*(horCount), |
---|
202 | m_depth/2.0, |
---|
203 | -m_height/2.0 + vertDelta*(vertCount+1)); |
---|
204 | } |
---|
205 | glEnd(); |
---|
206 | } |
---|
207 | for (vertCount=0; vertCount<m_vertResolution; ++vertCount) |
---|
208 | { |
---|
209 | glBegin(GL_QUAD_STRIP); |
---|
210 | for (depthCount=0; depthCount<=m_depthResolution; ++depthCount) |
---|
211 | { |
---|
212 | glNormal3d(1, 0, 0); |
---|
213 | glVertex3d(-m_width/2.0, |
---|
214 | m_depth/2.0 - depthDelta*depthCount, |
---|
215 | -m_height/2.0 + vertDelta*vertCount); |
---|
216 | glNormal3d(1, 0, 0); |
---|
217 | glVertex3d(-m_width/2.0, |
---|
218 | m_depth/2.0 - depthDelta*(depthCount), |
---|
219 | -m_height/2.0 + vertDelta*(vertCount+1)); |
---|
220 | } |
---|
221 | glEnd(); |
---|
222 | } |
---|
223 | for (vertCount=0; vertCount<m_vertResolution; ++vertCount) |
---|
224 | { |
---|
225 | glBegin(GL_QUAD_STRIP); |
---|
226 | for (depthCount=0; depthCount<=m_depthResolution; ++depthCount) |
---|
227 | { |
---|
228 | glNormal3d(-1, 0, 0); |
---|
229 | glVertex3d(m_width/2.0, |
---|
230 | -m_depth/2.0 + depthDelta*depthCount, |
---|
231 | -m_height/2.0 + vertDelta*vertCount); |
---|
232 | glNormal3d(-1, 0, 0); |
---|
233 | glVertex3d(m_width/2.0, |
---|
234 | -m_depth/2.0 + depthDelta*(depthCount), |
---|
235 | -m_height/2.0 + vertDelta*(vertCount+1)); |
---|
236 | } |
---|
237 | glEnd(); |
---|
238 | } |
---|
239 | for (depthCount=0; depthCount<m_depthResolution; ++depthCount) |
---|
240 | { |
---|
241 | glBegin(GL_QUAD_STRIP); |
---|
242 | for (horCount=0; horCount<=m_horResolution; ++horCount) |
---|
243 | { |
---|
244 | glNormal3d(0, 0, 1); |
---|
245 | glVertex3d(m_width/2.0 - horDelta*horCount, |
---|
246 | -m_depth/2.0 + depthDelta*depthCount, |
---|
247 | -m_height/2.0); |
---|
248 | glNormal3d(0, 0, 1); |
---|
249 | glVertex3d(m_width/2.0 - horDelta*horCount, |
---|
250 | -m_depth/2.0 + depthDelta*(depthCount+1), |
---|
251 | -m_height/2.0); |
---|
252 | } |
---|
253 | glEnd(); |
---|
254 | } |
---|
255 | for (depthCount=0; depthCount<m_depthResolution; ++depthCount) |
---|
256 | { |
---|
257 | glBegin(GL_QUAD_STRIP); |
---|
258 | for (horCount=0; horCount<=m_horResolution; ++horCount) |
---|
259 | { |
---|
260 | glNormal3d(0, 0, -1); |
---|
261 | glVertex3d(-m_width/2.0 + horDelta*horCount, |
---|
262 | -m_depth/2.0 + depthDelta*depthCount, |
---|
263 | m_height/2.0); |
---|
264 | glNormal3d(0, 0, -1); |
---|
265 | glVertex3d(-m_width/2.0 + horDelta*horCount, |
---|
266 | -m_depth/2.0 + depthDelta*(depthCount+1), |
---|
267 | m_height/2.0); |
---|
268 | } |
---|
269 | glEnd(); |
---|
270 | } |
---|
271 | |
---|
272 | glPopAttrib(); |
---|
273 | } |
---|