Free Electron
DrawRayTrace.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 __ray_DrawRayTrace_h__
8 #define __ray_DrawRayTrace_h__
9 namespace fe
10 {
11 namespace ext
12 {
13 
14 /**************************************************************************//**
15  @brief Ray-tracing implementation for DrawI
16 
17  @ingroup ray
18 *//***************************************************************************/
19 class FE_DL_EXPORT DrawRayTrace: public Initialize<DrawRayTrace>,
20  public DrawNull, virtual public HandlerI
21 {
22  public:
23  class Sphere
24  {
25  public:
26  Sphere(void):
27  m_radius(0.0f)
28  {
29  set(m_center);
30  set(m_color);
31  }
32  SpatialVector m_center;
33  Color m_color;
34  Real m_radius;
35  Box2i m_box;
36  };
37  class Cylinder
38  {
39  public:
40  Cylinder(void):
41  m_radius(0.0f)
42  {
43  set(m_base);
44  set(m_axis);
45  set(m_color);
46  set(m_box);
47  }
48  SpatialVector m_base;
49  SpatialVector m_axis;
50  Color m_color;
51  Real m_radius;
52  Box2i m_box;
53  };
54  class Disk
55  {
56  public:
57  Disk(void):
58  m_radius(0.0f)
59  {
60  set(m_center);
61  set(m_facing);
62  set(m_color);
63  set(m_box);
64  }
65  SpatialVector m_center;
66  SpatialVector m_facing;
67  Color m_color;
68  Real m_radius;
69  Box2i m_box;
70  };
71  class Drawable
72  {
73  public:
74  Drawable(void):
75  m_transformed(FALSE)
76  {
77  set(m_color);
78  set(m_box);
79  }
80  cp<DrawableI> m_cpDrawableI;
81  SpatialTransform m_transform;
82  BWORD m_transformed;
83  Color m_color;
84  Box2i m_box;
85  };
86  class Impact:
87  public SurfaceBase::Impact,
88  public CastableAs<Impact>
89  {
90  public:
91  virtual SpatialVector origin(void) const { return m_origin; }
92 
93  virtual SpatialVector direction(void) const { return m_direction; }
94 
95  virtual Real distance(void) const { return m_distance; }
96 
97  virtual SpatialVector intersection(void) { return m_intersection; }
98 
99  virtual SpatialVector normal(void) { return m_normal; }
100 
101  virtual Color diffuse(void) const
102  { return m_diffuse; }
103  virtual void setDiffuse(Color a_diffuse)
104  { m_diffuse=a_diffuse; }
105 
106  SpatialVector m_origin;
107  SpatialVector m_direction;
108  Color m_diffuse;
109  SpatialVector m_intersection;
110  SpatialVector m_normal;
111  Real m_distance;
112  };
113 
114  DrawRayTrace();
115 virtual ~DrawRayTrace();
116  void initialize(void);
117 
118  //* As HandlerI
119 virtual void handle(Record& record);
120 
121  //* As DrawI
122 virtual sp<ViewI> view(void) const
123  {
124  //* HACK ignore const
125  const_cast<DrawRayTrace*>(this)->ensureDrawChain();
126  return m_spDrawChain->view();
127  }
128 
129 virtual void setTransform(const SpatialTransform& a_rTransform)
130  { m_offset=a_rTransform.translation(); }
131 
132 virtual void setDrawChain(sp<DrawI> a_spDrawI)
133  { m_spDrawChain=a_spDrawI; }
134 
135 virtual void flush(void);
136 virtual void flushLive(void);
137 virtual void clearInput(void);
138 
139 virtual void drawSphere(const SpatialTransform &transform,
140  const SpatialVector *scale,const Color &color);
141 
142 virtual void drawCircle(const SpatialTransform& transform,
143  const SpatialVector *scale,
144  const Color& color)
145  { m_spDrawChain->drawCircle(transform,scale,color); }
146 virtual void drawCylinder(const SpatialTransform& transform,
147  const SpatialVector *scale,Real baseScale,
148  const Color& color,U32 slices)
149  { m_spDrawChain->drawCylinder(transform,scale,baseScale,
150  color,slices); }
151 virtual void drawCylinder(const SpatialVector& location1,
152  const SpatialVector& location2,
153  Real radius1,Real radius2,
154  const Color& color,U32 slices);
155 virtual void drawDisk(const SpatialVector& center,
156  const SpatialVector& facing,Real radius,
157  const Color& color,U32 slices);
158 
159  using DrawNull::draw;
160 
161 virtual void draw(cp<DrawableI> cpDrawableI,const Color* color);
162 
163  using DrawNull::drawTransformed;
164 
165 virtual void drawTransformed(const SpatialTransform& transform,
166  cp<DrawableI> cpDrawableI,const Color* color);
167 
168 virtual void drawAxes(Real scale)
169  { m_spDrawChain->drawAxes(scale); }
170 virtual void drawTransformedMarker(const SpatialTransform& transform,
171  Real radius,const Color& color)
172  { m_spDrawChain->drawTransformedMarker(transform,radius,
173  color); }
174 virtual void drawAlignedText(const SpatialVector& location,
175  const String text,const Color& color)
176  { m_spDrawChain->drawAlignedText(location,text,color); }
177 
178  //* single ray testing
179  void clearObjects(void);
180  BWORD rayPick(const SpatialVector& origin,
181  const SpatialVector& direction,
182  Real maxDistance,
183  sp<SurfaceI::ImpactI>& rspImpactI,
184  cp<SurfaceI>& rcpSurfaceI);
185 
186  private:
187  void ensureDrawChain(void);
188  void drawInternal(cp<DrawableI> cpDrawableI,const Color* color,
189  const SpatialTransform* pTransform=NULL);
190  void drawCropBox(const Box2i& box,const Color& color);
191 
192  void runTracers(void);
193  void traceImage(U32 section,U32 sections);
194  BWORD traceRay(U32 depth,const Vector2i& coordinate,
195  U8* element,Real contribution,BWORD from_shadow,
196  const SpatialVector& origin,
197  const SpatialVector& direction,
198  Real maxDistance,BWORD probe,
199  BWORD getImpact,sp<SurfaceI::ImpactI>& rspImpactI,
200  cp<SurfaceI>& rcpSurfaceI);
201  void logElement(U8* element);
202 
203  U8* pixel(U32 a_x,U32 a_y)
204  {
205  const U32 x=(a_x>=m_lenx)? m_lenx-1: a_x;
206  const U32 y=(a_y>=m_leny)? m_leny-1: a_y;
207  return &m_pPixelData[(y*m_lenx+x)*3];
208  }
209 
210  BWORD probeCheck(U32 x,U32 y)
211  { return m_mousePress && fabs(x-m_mouse[0])<0.01f &&
212  fabs(y-m_mouse[1])<0.01f; }
213  sp<ViewI> view(void)
214  { return m_spDrawChain.isValid()? m_spDrawChain->view():
215  sp<ViewI>(NULL); }
216 
217  void stopThreads(void);
218 
219  WindowEvent m_event;
220  sp<DrawMode> m_spWireframe;
221  Array<Sphere> m_spheres;
222  Array<Cylinder> m_cylinders;
223  Array<Disk> m_disks;
224  Array<Drawable> m_drawables;
225  Array<SpatialVector> m_showHitArray;
226  Array<SpatialVector> m_showNormalArray;
227  Array<SpatialVector> m_showReflectionArray;
228  Array<SpatialVector> m_showShadowArray;
229  U8* m_pPixelData;
230  U32 m_sphereCount;
231  U32 m_cylinderCount;
232  U32 m_diskCount;
233  U32 m_drawableCount;
234  sp<DrawI> m_spDrawChain;
235  BWORD m_autoDrawChain;
236  sp<ImageI> m_spImageI[2];
237  U32 m_current;
238  U32 m_lenx;
239  U32 m_leny;
240  SpatialVector m_light;
241  SpatialVector m_mouse;
242  BWORD m_mousePress;
243  SpatialVector m_offset;
244 
245  class TracerWorker: public Thread::Functor
246  {
247  public:
248  TracerWorker(sp< JobQueue<I32> > a_spJobQueue,
249  U32 a_id,String a_stage):
250  m_id(a_id),
251  m_hpJobQueue(a_spJobQueue) {}
252  virtual ~TracerWorker(void) {}
253 
254  virtual void operate(void);
255 
256  void setObject(sp<Component> a_spDrawRayTrace)
257  { m_hpDrawRayTrace=a_spDrawRayTrace; }
258 
259  private:
260  U32 m_id;
261  hp< JobQueue<I32> > m_hpJobQueue;
262  hp<DrawRayTrace> m_hpDrawRayTrace;
263  };
264 
265  sp< JobQueue<I32> > m_spJobQueue;
266  sp< Gang<TracerWorker,I32> > m_spGang;
267 };
268 
269 } /* namespace ext */
270 } /* namespace fe */
271 
272 #endif /* __ray_DrawRayTrace_h__ */
virtual sp< ViewI > view(void) const
Get current view.
Definition: DrawRayTrace.h:122
virtual void setDrawChain(sp< DrawI > a_spDrawI)
Set next DrawI in a chain.
Definition: DrawRayTrace.h:132
virtual void drawCylinder(const SpatialTransform &transform, const SpatialVector *scale, Real baseScale, const Color &color, U32 slices)
Draw a transformed cylinder or cone.
Definition: DrawRayTrace.h:146
Ray-tracing implementation for DrawI.
Definition: DrawRayTrace.h:19
virtual void drawAlignedText(const SpatialVector &location, const String text, const Color &color)
Draw screen-aligned single color text.
Definition: DrawRayTrace.h:174
kernel
Definition: namespace.dox:3
virtual void setTransform(const SpatialTransform &a_rTransform)
Impose a spatial translation.
Definition: DrawRayTrace.h:129
Per-class participation in the Initialized <> mechanism.
Definition: Initialized.h:117
virtual void draw(cp< DrawableI > cpDrawableI, const Color *color)
Draw DrawableI.
Definition: DrawNull.h:95
Special vector for color (RGBA)
Definition: Color.h:21
Dense vector - size fixed by template.
Definition: Vector.h:19
Interface to handle signals from an SignalerI.
Definition: HandlerI.h:22
Generalized windowing event.
Definition: WindowEvent.h:43
Safe handle for shared pointer.
Definition: Handled.h:61
Automatically reference-counted string container.
Definition: String.h:128
Reference to an instance of a Layout.
Definition: RecordSB.h:35
Draw nothing for DrawI, elegantly.
Definition: DrawNull.h:20
Copy-On-Write shared pointer.
Definition: Protectable.h:93
Per-class participation non-RTTI fallback dynamic casting mechanism.
Definition: Castable.h:192