FastUIDraw
painter_gradient_brush_shader_data.hpp
Go to the documentation of this file.
1 /*!
2  * \file painter_gradient_brush_shader_data.hpp
3  * \brief file painter_gradient_brush_shader_data.hpp
4  *
5  * Copyright 2019 by Intel.
6  *
7  * Contact: kevin.rogovin@gmail.com
8  *
9  * This Source Code Form is subject to the
10  * terms of the Mozilla Public License, v. 2.0.
11  * If a copy of the MPL was not distributed with
12  * this file, You can obtain one at
13  * http://mozilla.org/MPL/2.0/.
14  *
15  * \author Kevin Rogovin <kevin.rogovin@gmail.com>
16  *
17  */
18 
19 
20 #ifndef FASTUIDRAW_PAINTER_GRADIENT_BRUSH_SHADER_DATA_HPP
21 #define FASTUIDRAW_PAINTER_GRADIENT_BRUSH_SHADER_DATA_HPP
22 
26 
27 namespace fastuidraw
28 {
29 
30 /*!\addtogroup PainterShaderData
31  * @{
32  */
33 
34  /*!
35  * \brief
36  * A \ref PainterGradientBrushShaderData defines the \ref
37  * PainterBrushShaderData that the shaders of a \ref
38  * PainterGradientBrushShader consume. It specifies what
39  * \ref ColorStopSequence to use together with the
40  * geometric properties of the gradient.
41  */
44  public PainterBrushEnums
45  {
46  public:
47  /*!
48  * \brief
49  * Bit encoding for packing ColorStopSequence::texel_location()
50  */
52  {
53  color_stop_x_num_bits = 16, /*!< number bits to encode ColorStopSequence::texel_location().x() */
54  color_stop_y_num_bits = 16, /*!< number bits to encode ColorStopSequence::texel_location().y() */
55 
56  color_stop_x_bit0 = 0, /*!< where ColorStopSequence::texel_location().x() is encoded */
57  color_stop_y_bit0 = color_stop_x_num_bits /*!< where ColorStopSequence::texel_location().y() is encoded */
58  };
59 
60  /*!
61  * \brief
62  * Enumeration that provides offset, in units of
63  * uint32_t, of the packing of the gradient data
64  */
66  {
67  /*!
68  * Offset to x-coordinate of starting point of both
69  * linear and radial gradients (packed at float)
70  */
72 
73  /*!
74  * Offset to y-coordinate of starting point of both
75  * linear and radial gradients (packed at float)
76  */
78 
79  /*!
80  * Offset to x-coordinate of ending point of both
81  * linear and radial gradients (packed at float)
82  */
84 
85  /*!
86  * Offset to y-coordinate of ending point of both
87  * linear and radial gradients (packed at float)
88  */
90 
91  /*!
92  * Offset to the x and y-location of the color stops.
93  * The offset is stored as a uint32 packed as according
94  * in the enumeration \ref color_stop_xy_encoding
95  */
97 
98  /*!
99  * Offset to the length of the color stop in -texels-, i.e.
100  * ColorStopSequence::width(), packed as a uint32
101  */
103 
104  /*!
105  * Size of the data for linear gradients.
106  */
108 
109  /*!
110  * Offset to starting radius of gradient
111  * (packed at float) (radial gradient only)
112  */
114 
115  /*!
116  * Offset to ending radius of gradient
117  * (packed at float) (radial gradient only)
118  */
120 
121  /*!
122  * Size of the data for radial gradients.
123  */
125 
126  /*!
127  * Offset to the x-coordinate of the point
128  * of a sweep gradient.
129  */
131 
132  /*!
133  * Offset to the y-coordinate of the point
134  * of a sweep gradient.
135  */
137 
138  /*!
139  * Offset to the angle of a sweep gradient.
140  */
142 
143  /*!
144  * Offset to the sign-factor of the sweep gradient.
145  * The sign of the value is the sign of the sweep
146  * gradient and the magnitude is the repeat factor
147  * of the gradient.
148  */
150 
151  /*!
152  * Size of the data for sweep gradients.
153  */
155  };
156 
157  /*!
158  * Ctor. Initializes the brush to have no gradient.
159  */
161  {}
162 
163  /*!
164  * Copy ctor.
165  */
167  m_data(obj.m_data)
168  {}
169 
170  /*!
171  * Assignment operator.
172  * \param obj value from which to copy
173  */
176  {
177  m_data = obj.m_data;
178  return *this;
179  }
180 
181  /*!
182  * Reset the brush to initial conditions.
183  */
185  reset(void)
186  {
187  m_data.m_cs.clear();
188  m_data.m_type = gradient_non;
189  return *this;
190  }
191 
192  /*!
193  * Returns the type of gradient the data specifies.
194  */
195  enum gradient_type_t
196  type(void) const
197  {
198  return m_data.m_type;
199  }
200 
201  /*!
202  * Returns the value of the handle to the
203  * ColorStopSequence that the
204  * brush is set to use.
205  */
207  color_stops(void) const
208  {
209  return m_data.m_cs;
210  }
211 
212  /*!
213  * Sets the brush to have a linear gradient.
214  * \param cs color stops for gradient. If handle is invalid,
215  * then sets brush to not have a gradient.
216  * \param start_p start position of gradient
217  * \param end_p end position of gradient.
218  */
221  const vec2 &start_p, const vec2 &end_p)
222  {
223  m_data.m_cs = cs;
224  m_data.m_grad_start = start_p;
225  m_data.m_grad_end = end_p;
226  m_data.m_type = cs ? gradient_linear : gradient_non;
227  return *this;
228  }
229 
230  /*!
231  * Sets the brush to have a radial gradient.
232  * \param cs color stops for gradient. If handle is invalid,
233  * then sets brush to not have a gradient.
234  * \param start_p start position of gradient
235  * \param start_r starting radius of radial gradient
236  * \param end_p end position of gradient.
237  * \param end_r ending radius of radial gradient
238  */
241  const vec2 &start_p, float start_r,
242  const vec2 &end_p, float end_r)
243  {
244  m_data.m_cs = cs;
245  m_data.m_grad_start = start_p;
246  m_data.m_grad_start_r = start_r;
247  m_data.m_grad_end = end_p;
248  m_data.m_grad_end_r = end_r;
249  m_data.m_type = cs ? gradient_radial : gradient_non;
250  return *this;
251  }
252 
253  /*!
254  * Sets the brush to have a radial gradient. Provided as
255  * a conveniance, equivalent to
256  * \code
257  * radial_gradient(cs, p, 0.0f, p, r);
258  * \endcode
259  * \param cs color stops for gradient. If handle is invalid,
260  * then sets brush to not have a gradient.
261  * \param p start and end position of gradient
262  * \param r ending radius of radial gradient
263  */
266  const vec2 &p, float r)
267  {
268  return radial_gradient(cs, p, 0.0f, p, r);
269  }
270 
271  /*!
272  * Sets the brush to have a sweep gradient (directly).
273  * \param cs color stops for gradient. If handle is invalid,
274  * then sets brush to not have a gradient.
275  * \param p position of gradient
276  * \param theta start angle of the sweep gradient, this value
277  * should be in the range [-PI, PI]
278  * \param F the repeat factor applied to the interpolate, the
279  * sign of F is used to determine the sign of the
280  * sweep gradient.
281  */
284  const vec2 &p, float theta, float F = 1.0f)
285  {
286  m_data.m_cs = cs;
287  m_data.m_grad_start = p;
288  m_data.m_grad_end = vec2(theta, F);
289  m_data.m_type = cs ? gradient_sweep : gradient_non;
290  return *this;
291  }
292 
293  /*!
294  * Sets the brush to have a sweep gradient where the sign is
295  * determined by a PainterEnums::screen_orientation and a
296  * PainterEnums::rotation_orientation_t.
297  * \param cs color stops for gradient. If handle is invalid,
298  * then sets brush to not have a gradient.
299  * \param p position of gradient
300  * \param theta angle of the sweep gradient, this value
301  * should be in the range [-PI, PI]
302  * \param F the repeat factor applied to the interpolate,
303  * a negative reverses the orientation of the sweep.
304  * \param orientation orientation of the screen
305  * \param rotation_orientation orientation of the sweep
306  */
309  const vec2 &p, float theta,
310  enum PainterEnums::screen_orientation orientation,
311  enum PainterEnums::rotation_orientation_t rotation_orientation,
312  float F)
313  {
314  float S;
315  bool b1(orientation == PainterEnums::y_increases_upwards);
316  bool b2(rotation_orientation == PainterEnums::counter_clockwise);
317 
318  S = (b1 == b2) ? 1.0f : -1.0f;
319  return sweep_gradient(cs, p, theta, S * F);
320  }
321 
322  /*!
323  * Sets the brush to have a sweep gradient with a repeat factor
324  * of 1.0 and where the sign is determined by a
325  * PainterEnums::screen_orientation and a
326  * PainterEnums::rotation_orientation_t. Equivalent to
327  * \code
328  * sweep_gradient(cs, p, theta, orientation, rotation_orientation, 1.0f, repeat);
329  * \endcode
330  * \param cs color stops for gradient. If handle is invalid,
331  * then sets brush to not have a gradient.
332  * \param p position of gradient
333  * \param theta angle of the sweep gradient, this value
334  * should be in the range [-PI, PI]
335  * \param orientation orientation of the screen
336  * \param rotation_orientation orientation of the sweep
337  */
340  const vec2 &p, float theta,
341  enum PainterEnums::screen_orientation orientation,
342  enum PainterEnums::rotation_orientation_t rotation_orientation)
343  {
344  return sweep_gradient(cs, p, theta, orientation, rotation_orientation, 1.0f);
345  }
346 
347  unsigned int
348  data_size(void) const override;
349 
350  void
351  pack_data(c_array<uvec4> dst) const override;
352 
353  void
355 
356  unsigned int
357  number_resources(void) const override;
358 
360  bind_images(void) const override
361  {
363  }
364 
365  private:
366  class data
367  {
368  public:
369  data(void):
370  m_grad_start(0.0f, 0.0f),
371  m_grad_end(1.0f, 1.0f),
372  m_grad_start_r(0.0f),
373  m_grad_end_r(1.0f),
374  m_type(gradient_non)
375  {}
376 
378  vec2 m_grad_start, m_grad_end;
379  float m_grad_start_r, m_grad_end_r;
380  enum gradient_type_t m_type;
381  };
382 
383  data m_data;
384  };
385 
386 /*! @} */
387 }
388 
389 #endif
gradient_offset_t
Enumeration that provides offset, in units of uint32_t, of the packing of the gradient data...
color_stop_xy_encoding
Bit encoding for packing ColorStopSequence::texel_location()
A PainterGradientBrushShaderData defines the PainterBrushShaderData that the shaders of a PainterGrad...
screen_orientation
Enumeration to indicate in what direction the y-coordinate increases.
all classes and functions of FastUIDraw are in the namespace fastuidraw.
Definition: colorstop.hpp:28
A wrapper over a pointer to implement reference counting.
PainterGradientBrushShaderData & sweep_gradient(const reference_counted_ptr< const ColorStopSequence > &cs, const vec2 &p, float theta, float F=1.0f)
const reference_counted_ptr< const ColorStopSequence > & color_stops(void) const
c_array< const reference_counted_ptr< const Image > > bind_images(void) const override
file painter_brush_shader_data.hpp
void save_resources(c_array< reference_counted_ptr< const resource_base > > dst) const override
vecN< float, 2 > vec2
Definition: vecN.hpp:1231
PainterGradientBrushShaderData & radial_gradient(const reference_counted_ptr< const ColorStopSequence > &cs, const vec2 &start_p, float start_r, const vec2 &end_p, float end_r)
Base class to hold custom data for custom brush shaders.
unsigned int number_resources(void) const override
A c_array is a wrapper over a C pointer with a size parameter to facilitate bounds checking and provi...
Definition: c_array.hpp:43
file painter_enums.hpp
rotation_orientation_t
Enumeration to specify orientation of a rotation.
PainterGradientBrushShaderData & sweep_gradient(const reference_counted_ptr< const ColorStopSequence > &cs, const vec2 &p, float theta, enum PainterEnums::screen_orientation orientation, enum PainterEnums::rotation_orientation_t rotation_orientation)
file colorstop_atlas.hpp
PainterGradientBrushShaderData & sweep_gradient(const reference_counted_ptr< const ColorStopSequence > &cs, const vec2 &p, float theta, enum PainterEnums::screen_orientation orientation, enum PainterEnums::rotation_orientation_t rotation_orientation, float F)
unsigned int data_size(void) const override
void pack_data(c_array< uvec4 > dst) const override
PainterGradientBrushShaderData(const PainterGradientBrushShaderData &obj)
Class to contain various enumerations needed for describing a brush.
PainterGradientBrushShaderData & linear_gradient(const reference_counted_ptr< const ColorStopSequence > &cs, const vec2 &start_p, const vec2 &end_p)
PainterGradientBrushShaderData & radial_gradient(const reference_counted_ptr< const ColorStopSequence > &cs, const vec2 &p, float r)
PainterGradientBrushShaderData & operator=(const PainterGradientBrushShaderData &obj)