Free Electron
DrawCommon.h
Go to the documentation of this file.
1 /* Copyright (C) 2003-2021 Free Electron Organization
2  Any use of this software requires a license. If a valid license
3  was not distributed with this file, visit freeelectron.org. */
4 
5 /** @file */
6 
7 #ifndef __draw_DrawCommon_h__
8 #define __draw_DrawCommon_h__
9 
10 namespace fe
11 {
12 namespace ext
13 {
14 
15 #define FE_DC_CIRCLE_SIDES 64 //* tweak
16 #define FE_DC_CYLINDER_MAXSLICES 16 //* tweak
17 
18 #define FE_DC_BUFFER_VERTICES 100 //* reusable space vs new()
19 #define FE_DC_ICOSAHEDRON_VERTICES 26 //* don't tweak, includes repeats
20 
21 // TODO SAFEGUARD all drawing operations
22 
23 /**************************************************************************//**
24  @brief Drawing functions not specific to the graphics language
25 
26  @ingroup draw
27 
28  Using setDrawMode(NULL) activates the default mode,
29  which you can get with drawMode() and change.
30  The default mode is also the initial mode.
31 *//***************************************************************************/
32 class FE_DL_EXPORT DrawCommon: public Initialize<DrawCommon>,
33  public ObjectSafe<DrawCommon>, virtual public DrawI
34  {
35  public:
36  DrawCommon(void);
37  void initialize(void);
38 virtual ~DrawCommon(void);
39 
40 virtual void setDrawMode(sp<DrawMode> spMode);
41 virtual sp<DrawMode> drawMode(void) const
42  { SAFEGUARD;
43  return m_spDrawMode; }
44 virtual void pushDrawMode(sp<DrawMode> mode);
45 virtual sp<DrawMode> popDrawMode(void);
46 virtual void pushMatrixState(void) {}
47 virtual void popMatrixState(void) {}
48 
49 virtual void pushMatrix(MatrixMode a_mode,Real a_values[16]) {}
50 virtual void popMatrix(MatrixMode a_mode) {}
51 
52 virtual void unbindVertexArray(void) {}
53 
54 virtual void setBrightness(Real a_brightness)
55  { m_brightness=a_brightness; }
56 virtual Real brightness(void) { return m_brightness; }
57 
58 virtual void setContrast(Real a_contrast)
59  { m_contrast=a_contrast; }
60 virtual Real contrast(void) { return m_contrast; }
61 
62 virtual void setView(sp<ViewI> spViewI)
63  { SAFEGUARD;
64  m_spViewI=spViewI.isValid()?
65  spViewI: m_spDefaultView; }
66 virtual sp<ViewI> view(void) const
67  {
68  SAFEGUARD;
69  if(!m_spViewI.isValid())
70  {
71  if(!m_spDefaultView.isValid())
72  {
73  const_cast<DrawCommon*>(this)
74  ->createDefaultView();
75  }
76  const_cast<DrawCommon*>(this)
77  ->m_spViewI=m_spDefaultView;
78  }
79  return m_spViewI;
80  }
81 
82 virtual void drawCurve(const SpatialVector *vertex,
83  const SpatialVector *normal,
84  const Real *radius,U32 vertices,
85  BWORD multicolor,const Color *color);
86 virtual void draw(cp<DrawableI> cpDrawableI,const Color* color);
87 virtual void draw(cp<DrawableI> cpDrawableI,const Color* color,
88  sp<DrawBufferI> spDrawBuffer);
89 
90 virtual void drawTransformed(const SpatialTransform& transform,
91  cp<DrawableI> cpDrawableI,const Color* color);
92 virtual void drawTransformed(const SpatialTransform& transform,
93  cp<DrawableI> cpDrawableI,const Color* color,
94  sp<DrawBufferI> spDrawBuffer);
95 
96 virtual void drawBox(const Box2& box,const Color& color);
97 virtual void drawBox(const Box2i& box,const Color& color);
98 virtual void drawBox(const Box3& box,const Color& color);
99 
100 virtual void drawCircle(const SpatialTransform &transform,
101  const SpatialVector *scale,const Color &color);
102 
103 virtual void drawSphere(const SpatialTransform &transform,
104  const SpatialVector *scale,const Color &color);
105 
106 virtual void drawCylinder(const SpatialTransform& transform,
107  const SpatialVector *scale,Real baseScale,
108  const Color& color,U32 slices);
109 
110 virtual void drawCylinder(const SpatialVector& location1,
111  const SpatialVector& location2,
112  Real radius1,Real radius2,
113  const Color& color,U32 slices);
114 
115 virtual void drawCylinders(const SpatialTransform* transform,
116  const SpatialVector* scale,
117  const Real *baseScale,
118  const U32* slices,U32 cylinders,
119  BWORD multicolor,const Color* color);
120 
121 virtual void drawTransformedCylinders(
122  const SpatialTransform& pretransform,
123  const SpatialTransform* transform,
124  const SpatialVector* scale,
125  const Real *baseScale,
126  const U32* slices,U32 cylinders,
127  BWORD multicolor,const Color* color);
128 
129 virtual void drawArc(const SpatialTransform &transform,
130  const SpatialVector *scale,
131  Real startangle,Real endangle,
132  const Color &color);
133 
134 virtual void drawTransformedPoints(
135  const SpatialTransform &transform,
136  const SpatialVector *scale,
137  const SpatialVector *vertex,
138  const SpatialVector *normal,U32 vertices,
139  BWORD multicolor,const Color *color);
140 virtual void drawTransformedPoints(
141  const SpatialTransform &transform,
142  const SpatialVector *scale,
143  const SpatialVector *vertex,
144  const SpatialVector *normal,U32 vertices,
145  BWORD multicolor,const Color *color,
146  sp<DrawBufferI> spDrawBuffer);
147 
148 virtual void drawTransformedLines(
149  const SpatialTransform &transform,
150  const SpatialVector *scale,
151  const SpatialVector *vertex,
152  const SpatialVector *normal,
153  U32 vertices,StripMode strip,BWORD multicolor,
154  const Color *color);
155 virtual void drawTransformedLines(
156  const SpatialTransform &transform,
157  const SpatialVector *scale,
158  const SpatialVector *vertex,
159  const SpatialVector *normal,
160  U32 vertices,StripMode strip,
161  BWORD multicolor,const Color *color,
162  BWORD multiradius,const Real *radius,
163  const Vector3i *element,U32 elementCount,
164  sp<DrawBufferI> spDrawBuffer);
165 
166 virtual void drawTransformedCurve(
167  const SpatialTransform &transform,
168  const SpatialVector *scale,
169  const SpatialVector *vertex,
170  const SpatialVector *normal,
171  const Real *radius,
172  U32 vertices,BWORD multicolor,
173  const Color *color);
174 
175 virtual void drawTransformedTriangles(
176  const SpatialTransform &transform,
177  const SpatialVector *scale,
178  const SpatialVector *vertex,
179  const SpatialVector *normal,
180  const Vector2 *texture,U32 vertices,
181  StripMode strip,BWORD multicolor,
182  const Color *color);
183 virtual void drawTransformedTriangles(
184  const SpatialTransform &transform,
185  const SpatialVector *scale,
186  const SpatialVector *vertex,
187  const SpatialVector *normal,
188  const Vector2 *texture,U32 vertices,
189  StripMode strip,BWORD multicolor,
190  const Color *color,
191  const Array<I32>* vertexMap,
192  const Array<I32>* hullPointMap,
193  const Array<Vector4i>* hullFacePoint,
194  sp<DrawBufferI> spDrawBuffer);
195 
196 virtual void drawTransformedBox(const SpatialTransform& transform,
197  const Box3& box, const Color& color);
198 
199 virtual void drawTransformedBoxes(
200  const SpatialTransform* transform,
201  const SpatialVector* scale,
202  U32 boxes,
203  BWORD multicolor,
204  const Color* color);
205 
206 virtual void drawAxes(Real scale);
207 virtual void drawTransformedAxes(const SpatialTransform &transform,
208  Real scale);
209 
210 virtual void drawMarker(Real radius,const Color& color);
211 virtual void drawTransformedMarker(
212  const SpatialTransform &transform,
213  Real radius,const Color& color);
214 
215 virtual BWORD isDirect(void) const { return TRUE; }
216 virtual void setDrawChain(sp<DrawI> a_spDrawI) {}
217 virtual sp<DrawI> drawChain(void)
218  { return sp<DrawI>(NULL); }
219 virtual void setTransform(const SpatialTransform& a_rTransform) {}
220 virtual void flush(void) {}
221 virtual void flushLive(void) {}
222 virtual void clearInput(void) {}
223 virtual BWORD empty(void) const { return TRUE; }
224 
225 virtual sp<FontI> font(void) { return sp<FontI>(NULL); }
226 //~virtual void setFontBase(U32 index) {}
227 //~virtual I32 fontHeight(I32* pAscent,I32* pDescent) { return 0; }
228 //~virtual I32 pixelWidth(String string) { return 0; }
229 
230 virtual Real multiplication(void) { return Real(1); }
231 
232 virtual void drawPoints(const SpatialVector *vertex,
233  const SpatialVector *normal,U32 vertices,
234  BWORD multicolor,const Color *color);
235 virtual void drawPoints(const SpatialVector *vertex,
236  const SpatialVector *normal,U32 vertices,
237  BWORD multicolor,const Color *color,
238  sp<DrawBufferI> spDrawBuffer);
239 virtual void drawLines(const SpatialVector *vertex,
240  const SpatialVector *normal,U32 vertices,
241  StripMode strip,BWORD multicolor,
242  const Color *color);
243 virtual void drawLines(const SpatialVector *vertex,
244  const SpatialVector *normal,U32 vertices,
245  StripMode strip,
246  BWORD multicolor,const Color *color,
247  BWORD multiradius,const Real *radius,
248  const Vector3i *element,U32 elementCount,
249  sp<DrawBufferI> spDrawBuffer);
250 virtual void drawTriangles(const SpatialVector *vertex,
251  const SpatialVector *normal,
252  const Vector2 *texture,U32 vertices,
253  StripMode strip,BWORD multicolor,
254  const Color *color);
255 virtual void drawTriangles(const SpatialVector *vertex,
256  const SpatialVector *normal,
257  const Vector2 *texture,U32 vertices,
258  StripMode strip,BWORD multicolor,
259  const Color *color,
260  const Array<I32>* vertexMap,
261  const Array<I32>* hullPointMap,
262  const Array<Vector4i>* hullFacePoint,
263  sp<DrawBufferI> spDrawBuffer);
264 virtual void drawRectangles(const SpatialVector *vertex,
265  U32 vertices,
266  BWORD multicolor,const Color *color) {}
267 virtual void drawAlignedText(const SpatialVector& location,
268  const String text,const Color& color) {}
269 virtual void drawRaster(sp<ImageI> spImageI,
270  const SpatialVector& location) {}
271 
272 virtual sp<DrawBufferI> createBuffer(void)
273  { return sp<DrawBufferI>(NULL); }
274 
275  protected:
276 
277  DrawMode::DrawStyle drawStyle(void) const
278  { DrawMode::DrawStyle style=m_spDrawMode->drawStyle();
279  return style==DrawMode::e_defaultStyle?
280  (isDirect()? DrawMode::e_edgedSolid:
281  DrawMode::e_outline): style; }
282 
283  sp<ImageI> textureImage(void) const
284  { return m_spDrawMode->textureImage(); }
285  I32 textureImageID(void) const
286  { return m_spDrawMode->textureImageID(); }
287  Real pointSize(void) const
288  { return m_spDrawMode->pointSize(); }
289  Real lineWidth(void) const
290  { return m_spDrawMode->lineWidth(); }
291  BWORD antialias(void) const
292  { return m_spDrawMode->antialias(); }
293  BWORD frontfaceCulling(void) const
294  { return m_spDrawMode->frontfaceCulling(); }
295  BWORD backfaceCulling(void) const
296  { return m_spDrawMode->backfaceCulling(); }
297  BWORD twoSidedLighting(void) const
298  { return m_spDrawMode->twoSidedLighting(); }
299  BWORD zBuffering(void) const
300  { return m_spDrawMode->zBuffering(); }
301  BWORD isLit(void) const
302  { return m_spDrawMode.isValid() &&
303  m_spDrawMode->lit() &&
304  (drawStyle()==DrawMode::e_solid ||
305  drawStyle()==DrawMode::e_edgedSolid ||
306  drawStyle()==DrawMode::e_ghost); }
307  BWORD isUvSpace(void) const
308  { return m_spDrawMode.isValid() &&
309  m_spDrawMode->uvSpace(); }
310 
311 virtual void createDefaultView(void);
312 
313  sp<ViewI> m_spDefaultView;
314 
315  Real m_brightness;
316  Real m_contrast;
317 
318  private:
319 
320  void drawOrthogonalCircles(
321  const SpatialTransform &transform,
322  const SpatialVector *scale,const Color &color);
323  void drawIcosahedron(const SpatialTransform &transform,
324  const SpatialVector *scale,const Color &color);
325 
326  void createCircle(void);
327  void createIcosahedron(void);
328  void createCylinders(void);
329  void destroyCylinders(void);
330 
331  SpatialVector m_circleVertex[FE_DC_CIRCLE_SIDES+2];
332  SpatialVector m_circleNormal[FE_DC_CIRCLE_SIDES+2];
333 
334  SpatialVector m_transformBuffer[FE_DC_BUFFER_VERTICES];
335  SpatialVector m_normalBuffer[FE_DC_BUFFER_VERTICES];
336 
337  SpatialVector m_icosahedronVertex[FE_DC_ICOSAHEDRON_VERTICES];
338  SpatialVector m_icosahedronNormal[FE_DC_ICOSAHEDRON_VERTICES];
339  Vector2 m_icosahedronTexture[FE_DC_ICOSAHEDRON_VERTICES];
340 
341  SpatialVector* m_pCylinderVertex[FE_DC_CYLINDER_MAXSLICES-2];
342  SpatialVector* m_pCylinderNormal[FE_DC_CYLINDER_MAXSLICES-2];
343  Vector2* m_pCylinderTexture[FE_DC_CYLINDER_MAXSLICES-2];
344 
345  sp<DrawMode> m_spDefaultMode;
346  sp<DrawMode> m_spDrawMode;
347  sp<ViewI> m_spViewI;
348 
349  std::vector< sp<DrawMode> > m_drawStack;
350  };
351 
352 } /* namespace ext */
353 } /* namespace fe */
354 
355 #endif /* __draw_DrawCommon_h__ */
virtual void setView(sp< ViewI > spViewI)
Set current view.
Definition: DrawCommon.h:62
Object level locking for thread safety.
Definition: Safe.h:216
virtual BWORD isDirect(void) const
Returns TRUE is display is local and rendered directly.
Definition: DrawCommon.h:215
Drawing functions not specific to the graphics language.
Definition: DrawCommon.h:32
Generalized drawing of points, lines, text, and basic triangles.
Definition: DrawI.h:20
virtual void flush(void)
Called once after each frame.
Definition: DrawCommon.h:220
kernel
Definition: namespace.dox:3
virtual void unbindVertexArray(void)
currently, sets vertex array to zero in OpenGL
Definition: DrawCommon.h:52
StripMode
Format of vertices for tri-stripping.
Definition: DrawI.h:26
virtual Real brightness(void)
get lightening
Definition: DrawCommon.h:56
Per-class participation in the Initialized <> mechanism.
Definition: Initialized.h:117
virtual void pushMatrixState(void)
Push transform matrices.
Definition: DrawCommon.h:46
virtual void setDrawChain(sp< DrawI > a_spDrawI)
Set next DrawI in a chain.
Definition: DrawCommon.h:216
Special vector for color (RGBA)
Definition: Color.h:21
virtual Real contrast(void)
get light scaling
Definition: DrawCommon.h:60
N-dimensional axis-aligned bounding-box.
Definition: Box.h:18
virtual void pushMatrix(MatrixMode a_mode, Real a_values[16])
push a new 4x4 matrix on a particular stack
Definition: DrawCommon.h:49
Dense vector - size fixed by template.
Definition: Vector.h:19
virtual void setContrast(Real a_contrast)
set multiplicitive light scaling
Definition: DrawCommon.h:58
virtual void flushLive(void)
Pass on current input data as is.
Definition: DrawCommon.h:221
Automatically reference-counted string container.
Definition: String.h:128
virtual void popMatrixState(void)
Pop transform matrices.
Definition: DrawCommon.h:47
virtual BWORD empty(void) const
Returns TRUE if the current input queue is empty.
Definition: DrawCommon.h:223
virtual void popMatrix(MatrixMode a_mode)
pop the top 4x4 matrix from a particular stack
Definition: DrawCommon.h:50
virtual void setBrightness(Real a_brightness)
set additive lightening
Definition: DrawCommon.h:54
virtual void drawRectangles(const SpatialVector *vertex, U32 vertices, BWORD multicolor, const Color *color)
Draw rectangles.
Definition: DrawCommon.h:264
DrawStyle
Rendering schemes that an an implementation may recognize.
Definition: DrawMode.h:42
virtual sp< ViewI > view(void) const
Get current view.
Definition: DrawCommon.h:66
Copy-On-Write shared pointer.
Definition: Protectable.h:93
virtual void setTransform(const SpatialTransform &a_rTransform)
Impose a spatial translation.
Definition: DrawCommon.h:219
virtual sp< FontI > font(void)
Get current font.
Definition: DrawCommon.h:225
virtual void clearInput(void)
Reset the current input queue.
Definition: DrawCommon.h:222
virtual sp< DrawMode > drawMode(void) const
Get rendering mode.
Definition: DrawCommon.h:41
virtual void drawAlignedText(const SpatialVector &location, const String text, const Color &color)
Draw screen-aligned single color text.
Definition: DrawCommon.h:267
virtual Real multiplication(void)
Select the GL-specific index/handle.
Definition: DrawCommon.h:230