CUGL 2.0
Cornell University Game Library
|
#include <CUGradient.h>
Public Member Functions | |
Gradient () | |
Gradient (const Gradient &grad) | |
Gradient (Gradient &&grad) | |
~Gradient () | |
void | dispose () |
bool | init (const Color4f color) |
bool | init (const Color4f inner, const Color4f outer, const Vec2 start, const Vec2 end) |
bool | init (const Color4f inner, const Color4f outer, const Vec2 center, float radius) |
bool | init (const Color4f inner, const Color4f outer, const Vec2 center, float iradius, float oradius) |
bool | init (const Color4f inner, const Color4f outer, const Rect box, float radius, float feather) |
bool | init (const std::shared_ptr< Gradient > &grad) |
Gradient & | operator= (const Gradient &other) |
Gradient & | operator= (Gradient &&other) |
Gradient & | operator= (const Color4f color) |
Gradient & | set (const Gradient &grad) |
Gradient & | set (const std::shared_ptr< Gradient > &grad) |
Gradient & | set (const Color4f color) |
Gradient & | set (const Color4f inner, const Color4f outer, const Vec2 start, const Vec2 end) |
Gradient & | set (const Color4f inner, const Color4f outer, const Vec2 center, float radius) |
Gradient & | set (const Color4f inner, const Color4f outer, const Vec2 center, float iradius, float oradius) |
Gradient & | set (const Color4f inner, const Color4f outer, const Rect box, float radius, float feather) |
Affine2 | getTransform () const |
void | setTransform (const Affine2 &transform) |
void | setTransform (const Mat4 &transform) |
Color4f | getInnerColor () const |
void | setInnerColor (const Color4f color) |
Color4f | getOuterColor () const |
void | setOuterColor (const Color4f color) |
Vec2 | getExtent () const |
void | setExtent (const Vec2 extent) |
float | getRadius () const |
void | setRadius (float radius) |
float | getFeather () const |
void | setFeather (float feather) |
const Color4f | getTintColor () const |
void | setTintColor (const Color4f tint) |
bool | getTintStatus () const |
void | setTintStatus (bool status) |
Gradient & | rotate (float angle) |
Gradient & | scale (float value) |
Gradient & | scale (const Vec2 s) |
Gradient & | scale (float sx, float sy) |
Gradient & | translate (const Vec2 t) |
Gradient & | translate (float tx, float ty) |
Gradient & | multiply (const Mat4 &mat) |
Gradient & | multiply (const Affine2 &aff) |
Gradient & | operator*= (const Mat4 &mat) |
Gradient & | operator*= (const Affine2 &aff) |
Gradient | operator* (const Mat4 &mat) const |
Gradient | operator* (const Affine2 &aff) const |
float * | getData (float *array) const |
float * | getComponents (float *array) const |
std::string | toString (bool verbose=false) const |
operator std::string () const | |
Static Public Member Functions | |
static std::shared_ptr< Gradient > | alloc (const Color4f color) |
static std::shared_ptr< Gradient > | alloc (const Color4f inner, const Color4f outer, const Vec2 start, const Vec2 end) |
static std::shared_ptr< Gradient > | alloc (const Color4f inner, const Color4f outer, const Vec2 center, float radius) |
static std::shared_ptr< Gradient > | alloc (const Color4f inner, const Color4f outer, const Vec2 center, float iradius, float oradius) |
static std::shared_ptr< Gradient > | alloc (const Color4f inner, const Color4f outer, const Rect box, float radius, float feather) |
static std::shared_ptr< Gradient > | alloc (const std::shared_ptr< Gradient > &grad) |
This class defines a two color gradient.
All gradients, including linear and radial gradients, are variations of (rounded) box gradients. A box gradient is defined by (in terms of largest to smallest data):
Assuming this data is in std140 format, this is a 24 element array of floats. And this is the format that this data is represented in the getData method so that it can be passed to a UniformBuffer for improved performance. It is also possible to get access to the individual components of the paint gradient, to pass them to a shader directly (though the transform must be inverted first if it is passed directly).
Paint gradients are applied to surfaces in the same way textures are. The gradient is defined on a unit square from (0,0) to (1,1). To be consistent with textures, the origin is at the top right corner. To apply the gradient, the shader should use the texture coordinates of each vertex (or an attribute similar to texture coordinates) combined with the uniforms for this gradient. For a tutorial on how to do this, see the GradientTexture shaders in the the CUGL render package.
For SpriteBatch and scene graph compatibility, gradients may be tinted by another color. This tint is managed separately from the gradient definition.
For simplicity we only permit two colors in a gradient. For multicolored gradients, the shape should be tesellated with multiple gradient values.
cugl::Gradient::Gradient | ( | ) |
Creates a degenerate, white-colored gradient.
|
inline |
Creates a copy of the given gradient.
grad | The gradient to copy |
cugl::Gradient::Gradient | ( | Gradient && | grad | ) |
Creates a copy with the resources of the given gradient.
The original gradient is no longer safe to use after calling this constructor.
grad | The gradient to take from |
|
inline |
Deletes this gradient, releasing all resources.
Returns a new degenerate gradient of the given color.
color | The gradient color |
|
inlinestatic |
Returns a new box gradient of the two colors.
Box gradients paint the inner color in a rounded rectangle, and then use a feather setting to transition to the outer color. The box position and corner radius are given in texture coordinates. That is, (0,0) is the top left corner of the gradient bounding box and (1,1) is the bottom right corner. It is permissible for these coordinates to be out of range (so negative values or greater than 1). Such values will be interpretted accordingly.
To be well-defined, the corner radius should be no larger than half the width and height (at which point it defines an ellipse). Shapes with abnormally large radii are undefined.
The feather value acts like the inner and outer radius of a radial gradient. If a line is drawn from the center of the round rectangle to a corner, consider two segments. The first starts at the corner and moves towards the center of the rectangle half-feather in distance. The end of this segment is the end of the inner color The second second starts at the corner and moves in the opposite direction the same amount. The end of this segement is the other color. In between, the colors are smoothly interpolated.
So, if feather is 0, there is no gradient and the shift from inner color to outer color is immediate. On the other hand, if feather is larger than the width and hight of the rectangle, the inner color immediately transitions to the outer color.
inner | The inner gradient color |
outer | The outer gradient color |
box | The bounds of the rounded rectangle. |
radius | The corner radius of the rounded rectangle. |
feather | The feather value for color interpolation |
|
inlinestatic |
Returns a new general radial gradient of the two colors.
In a general radial gradient, the inner color starts at the center and continues to the inner radius. It then transitions smoothly to the outer color at the outer radius.
The center and radii are all specified in texture coordinates. That is, (0,0) is the top left corner of the gradient bounding box and (1,1) is the bottom right corner. It is permissible to a center value out of range (so coordinates negative or greater than 1). Such value will be interpretted accordingly.
inner | The inner gradient color |
outer | The outer gradient color |
center | The center of the radial gradient |
iradius | The radius for the inner color |
oradius | The radius for the outer color |
|
inlinestatic |
Returns a new simple radial gradient of the two colors.
In a simple radial gradient, the inner color starts at the center and transitions smoothly to the outer color at the given radius.
The center and radius are specified in texture coordinates. That is, (0,0) is the top left corner of the gradient bounding box and (1,1) is the bottom right corner. It is permissible to a center value out of range (so coordinates negative or greater than 1). Such values will be interpretted accordingly.
inner | The inner gradient color |
outer | The outer gradient color |
center | The center of the radial gradient |
radius | The radius for the outer color |
|
inlinestatic |
Returns a new linear gradient of the two colors.
In a linear color, the inner starts at position start, and transitions to the outer color at position end. The transition is along the vector end-start.
The values start and end are specified in texture coordinates. That is, (0,0) is the top left corner of the gradient bounding box and (1,1) is the bottom right corner. It is permissible to have coordinates out of range (so negative or greater than 1). Such values will be interpretted accordingly.
inner | The inner gradient color |
outer | The outer gradient color |
start | The start position of the inner color |
end | The start position of the outer color |
|
inlinestatic |
Returns a new copy of the given gradient.
grad | The gradient to copy |
void cugl::Gradient::dispose | ( | ) |
Deletes the gradient and resets all attributes.
You must reinitialize the gradient to use it.
float* cugl::Gradient::getComponents | ( | float * | array | ) | const |
Reads the gradient into the provided array
The gradient is written to the array so that it can be passed the the shader one component at a time (e.g. NOT in std140 format). It differs from getData in that it only uses 9 floats for the affine transform (as a 3x3 homogenous matrix).
array | The array to store the values |
float* cugl::Gradient::getData | ( | float * | array | ) | const |
Reads the gradient into the provided array
The gradient is written to the given array in std140 format. That is (1) 12 floats for the affine transform (as a 3x3 homogenous matrix), (2) 4 floats for the inner color, (3) 4 floats for the outer color, (4) 2 floats for the extent, (5) 1 float for the corner radius, and (6) 1 float for the feather value. Values are written in this order.
array | The array to store the values |
|
inline |
Returns the extent of this gradient
The extent is the vector from the center of the rounded rectangle to one of its corners. It defines the size of the rounded rectangle.
|
inline |
Returns the feather value for this gradient.
The feature value is perhaps the trickiest value to understand. This value acts like the inner and outer radius of a radial gradient. If a line is drawn from the center of the round rectangle to a corner, consider two segments. The first starts at the corner and moves towards the center of the rectangle half-feather in distance. The end of this segment is the end of the inner color The second second starts at the corner and moves in the opposite direction the same amount. The end of this segement is the other color. In between, the colors are smoothly interpolated.
So, if feather is 0, there is no gradient and the shift from inner color to outer color is immediate. On the other hand, if feather is larger than the width and hight of the rectangle, the inner color immediately transitions to the outer color.
|
inline |
Returns the inner color of this gradient
The inner color is the color inside of the rounded rectangle defining the gradient.
|
inline |
Returns the outer color of this gradient
The outer color is the color outside of the rounded rectangle defining the gradient.
|
inline |
Returns the corner radius of the gradient rectangle
The corner radius is the radius of the circle inscribed in (each) corner of the rounded rectangle.
To be well-defined, it should be no more than half the width and height. When it is equal to both half the width and half the height, the rectangle becomes a circle. For large values this inner rectangle will collapse and disappear completely.
|
inline |
Returns the tint color for this gradient.
An active tint color is applied to the inner and outer colors when the methods getData and getComponents are called.
By default the color is white. Tinting may be turned on or off with the setTintStatus method.
|
inline |
Returns the tint status for this gradient.
An active tint color is applied to the inner and outer colors when the methods getData and getComponents are called. However, tinting is only active if the status is true.
Affine2 cugl::Gradient::getTransform | ( | ) | const |
Returns the transform component of this gradient
The transform maps the origin of the current coordinate system to the center and rotation of the rounded rectangular box with the inner color. Applying further transforms will adjust the gradient in texture space.
The transform is primarily for representing rotation. It typically only has a scale component when the gradient is linear.
If this transform is passed directly to a gradient shader, it should be inverted first. If you really need to pass individual components to a shader, you should use getComponents instead.
bool cugl::Gradient::init | ( | const Color4f | color | ) |
Initializes a degenerate gradient of the given color.
color | The gradient color |
bool cugl::Gradient::init | ( | const Color4f | inner, |
const Color4f | outer, | ||
const Rect | box, | ||
float | radius, | ||
float | feather | ||
) |
Initializes a box gradient of the two colors.
Box gradients paint the inner color in a rounded rectangle, and then use a feather setting to transition to the outer color. The box position and corner radius are given in texture coordinates. That is, (0,0) is the top left corner of the gradient bounding box and (1,1) is the bottom right corner. It is permissible for these coordinates to be out of range (so negative values or greater than 1). Such values will be interpretted accordingly.
To be well-defined, the corner radius should be no larger than half the width and height (at which point it defines an ellipse). Shapes with abnormally large radii are undefined.
The feather value acts like the inner and outer radius of a radial gradient. If a line is drawn from the center of the round rectangle to a corner, consider two segments. The first starts at the corner and moves towards the center of the rectangle half-feather in distance. The end of this segment is the end of the inner color The second second starts at the corner and moves in the opposite direction the same amount. The end of this segement is the other color. In between, the colors are smoothly interpolated.
So, if feather is 0, there is no gradient and the shift from inner color to outer color is immediate. On the other hand, if feather is larger than the width and hight of the rectangle, the inner color immediately transitions to the outer color.
inner | The inner gradient color |
outer | The outer gradient color |
box | The bounds of the rounded rectangle. |
radius | The corner radius of the rounded rectangle. |
feather | The feather value for color interpolation |
bool cugl::Gradient::init | ( | const Color4f | inner, |
const Color4f | outer, | ||
const Vec2 | center, | ||
float | iradius, | ||
float | oradius | ||
) |
Initializes a general radial gradient of the two colors.
In a general radial gradient, the inner color starts at the center and continues to the inner radius. It then transitions smoothly to the outer color at the outer radius.
The center and radii are all specified in texture coordinates. That is, (0,0) is the top left corner of the gradient bounding box and (1,1) is the bottom right corner. It is permissible to a center value out of range (so coordinates negative or greater than 1). Such value will be interpretted accordingly.
inner | The inner gradient color |
outer | The outer gradient color |
center | The center of the radial gradient |
iradius | The radius for the inner color |
oradius | The radius for the outer color |
bool cugl::Gradient::init | ( | const Color4f | inner, |
const Color4f | outer, | ||
const Vec2 | center, | ||
float | radius | ||
) |
Initializes a simple radial gradient of the two colors.
In a simple radial gradient, the inner color starts at the center and transitions smoothly to the outer color at the given radius.
The center and radius are specified in texture coordinates. That is, (0,0) is the top left corner of the gradient bounding box and (1,1) is the bottom right corner. It is permissible to a center value out of range (so coordinates negative or greater than 1). Such values will be interpretted accordingly.
inner | The inner gradient color |
outer | The outer gradient color |
center | The center of the radial gradient |
radius | The radius for the outer color |
bool cugl::Gradient::init | ( | const Color4f | inner, |
const Color4f | outer, | ||
const Vec2 | start, | ||
const Vec2 | end | ||
) |
Initializes a linear gradient of the two colors.
In a linear color, the inner starts at position start, and transitions to the outer color at position end. The transition is along the vector end-start.
The values start and end are specified in texture coordinates. That is, (0,0) is the top left corner of the gradient bounding box and (1,1) is the bottom right corner. It is permissible to have coordinates out of range (so negative or greater than 1). Such values will be interpretted accordingly.
inner | The inner gradient color |
outer | The outer gradient color |
start | The start position of the inner color |
end | The start position of the outer color |
bool cugl::Gradient::init | ( | const std::shared_ptr< Gradient > & | grad | ) |
Initializes this gradient to be a copy of the given gradient.
grad | The gradient to copy |
Applies the given transform to this gradient.
This transform is applied after the existing gradient transform (which is natural, since the transform defines the gradient shape). To pre-multiply a transform, set the transform directly.
aff | The matrix to multiply by. |
Applies the given transform to this gradient.
This transform is applied after the existing gradient transform (which is natural, since the transform defines the gradient shape). To pre-multiply a transform, set the transform directly.
mat | The matrix to multiply by. |
|
inline |
Cast from Gradient to a string.
Returns a copy of the gradient transformed by the given matrix.
The matrix transform is applied after the existing gradient transform (which is natural, since the transform defines the gradient shape). To pre-multiply a transform, set the transform directly.
Note: This does not modify the gradient.
aff | The transform to multiply by. |
Returns a copy of the gradient transformed by the given matrix.
The matrix transform is applied after the existing gradient transform (which is natural, since the transform defines the gradient shape). To pre-multiply a transform, set the transform directly.
Note: This does not modify the gradient.
mat | The transform to multiply by. |
Applies the given transform to this gradient.
This transform is applied after the existing gradient transform (which is natural, since the transform defines the gradient shape). To pre-multiply a transform, set the transform directly.
aff | The matrix to multiply by. |
Applies the given transform to this gradient.
This transform is applied after the existing gradient transform (which is natural, since the transform defines the gradient shape). To pre-multiply a transform, set the transform directly.
mat | The matrix to multiply by. |
Sets this to be a degenerate gradient with the given color.
The inner color and outer color will be the same, so there will be no transition.
color | The gradient color |
Sets this gradient to be a copy of the given one.
other | The gradient to copy |
Sets this gradient to be have the resources of the given one.
The original gradient is no longer safe to use after calling this operator.
other | The gradient to take from |
Gradient& cugl::Gradient::rotate | ( | float | angle | ) |
Applies a rotation to this gradient.
The rotation is in radians, counter-clockwise about the given axis.
angle | The angle (in radians). |
Applies a non-uniform scale to this gradient.
s | The vector storing the individual scaling factors |
|
inline |
Applies a non-uniform scale to this gradient.
sx | The amount to scale along the x-axis. |
sy | The amount to scale along the y-axis. |
Gradient& cugl::Gradient::scale | ( | float | value | ) |
Applies a uniform scale to this gradient.
value | The scalar to multiply by. |
Sets this to be a degenerate gradient with the given color.
The inner color and outer color will be the same, so there will be no transition.
color | The gradient color |
Gradient& cugl::Gradient::set | ( | const Color4f | inner, |
const Color4f | outer, | ||
const Rect | box, | ||
float | radius, | ||
float | feather | ||
) |
Sets this to be a box gradient of the two colors.
Box gradients paint the inner color in a rounded rectangle, and then use a feather setting to transition to the outer color. The box position and corner radius are given in texture coordinates. That is, (0,0) is the top left corner of the gradient bounding box and (1,1) is the bottom right corner. It is permissible for these coordinates to be out of range (so negative values or greater than 1). Such values will be interpretted accordingly.
To be well-defined, the corner radius should be no larger than half the width and height (at which point it defines an ellipse). Shapes with abnormally large radii are undefined.
The feather value acts like the inner and outer radius of a radial gradient. If a line is drawn from the center of the round rectangle to a corner, consider two segments. The first starts at the corner and moves towards the center of the rectangle half-feather in distance. The end of this segment is the end of the inner color The second second starts at the corner and moves in the opposite direction the same amount. The end of this segement is the other color. In between, the colors are smoothly interpolated.
So, if feather is 0, there is no gradient and the shift from inner color to outer color is immediate. On the other hand, if feather is larger than the width and hight of the rectangle, the inner color immediately transitions to the outer color.
inner | The inner gradient color |
outer | The outer gradient color |
box | The bounds of the rounded rectangle. |
radius | The corner radius of the rounded rectangle. |
feather | The feather value for color interpolation |
Gradient& cugl::Gradient::set | ( | const Color4f | inner, |
const Color4f | outer, | ||
const Vec2 | center, | ||
float | iradius, | ||
float | oradius | ||
) |
Sets this to be a general radial gradient of the two colors.
In a general radial gradient, the inner color starts at the center and continues to the inner radius. It then transitions smoothly to the outer color at the outer radius.
The center and radii are all specified in texture coordinates. That is, (0,0) is the top left corner of the gradient bounding box and (1,1) is the bottom right corner. It is permissible to a center value out of range (so coordinates negative or greater than 1). Such value will be interpretted accordingly.
inner | The inner gradient color |
outer | The outer gradient color |
center | The center of the radial gradient |
iradius | The radius for the inner color |
oradius | The radius for the outer color |
Gradient& cugl::Gradient::set | ( | const Color4f | inner, |
const Color4f | outer, | ||
const Vec2 | center, | ||
float | radius | ||
) |
Sets this to be a simple radial gradient of the two colors.
In a simple radial gradient, the inner color starts at the center and transitions smoothly to the outer color at the given radius.
The center and radius are specified in texture coordinates. That is, (0,0) is the top left corner of the gradient bounding box and (1,1) is the bottom right corner. It is permissible to a center value out of range (so coordinates negative or greater than 1). Such values will be interpretted accordingly.
inner | The inner gradient color |
outer | The outer gradient color |
center | The center of the radial gradient |
radius | The radius for the outer color |
Gradient& cugl::Gradient::set | ( | const Color4f | inner, |
const Color4f | outer, | ||
const Vec2 | start, | ||
const Vec2 | end | ||
) |
Sets this to be a linear gradient of the two colors.
In a linear color, the inner starts at position start, and transitions to the outer color at position end. The transition is along the vector end-start.
The values start and end are specified in texture coordinates. That is, (0,0) is the top left corner of the gradient bounding box and (1,1) is the bottom right corner. It is permissible to have coordinates out of range (so negative or greater than 1). Such values will be interpretted accordingly.
inner | The inner gradient color |
outer | The outer gradient color |
start | The start position of the inner color |
end | The start position of the outer color |
Sets this gradient to be a copy of the given one.
grad | The gradient to copy |
Sets this gradient to be a copy of the given one.
grad | The gradient to copy |
|
inline |
Sets the extent of this gradient
The extent is the vector from the center of the rounded rectangle to one of its corners. It defines the size of the rounded rectangle.
extent | The extent of this gradient |
|
inline |
Sets the feather value for this gradient.
The feature value is perhaps the trickiest value to understand. This value acts like the inner and outer radius of a radial gradient. If a line is drawn from the center of the round rectangle to a corner, consider two segments. The first starts at the corner and moves towards the center of the rectangle half-feather in distance. The end of this segment is the end of the inner color The second second starts at the corner and moves in the opposite direction the same amount. The end of this segement is the other color. In between, the colors are smoothly interpolated.
So, if feather is 0, there is no gradient and the shift from inner color to outer color is immediate. On the other hand, if feather is larger than the width and hight of the rectangle, the inner color immediately transitions to the outer color.
feather | The feather value for this gradient. |
|
inline |
Sets the inner color of this gradient
The inner color is the color inside of the rounded rectangle defining the gradient.
color | The inner color of this gradient |
|
inline |
Sets the outer color of this gradient
The outer color is the color outside of the rounded rectangle defining the gradient.
color | The outer color of this gradientt |
|
inline |
Sets the corner radius of the gradient rectangle
The corner radius is the radius of the circle inscribed in (each) corner of the rounded rectangle.
To be well-defined, it should be no more than half the width and height. When it is equal to both half the width and half the height, the rectangle becomes a circle. For large values this inner rectangle will collapse and disappear completely.
radius | The corner radius of the gradient rectangle |
|
inline |
Sets the tint color for this gradient.
An active tint color is applied to the inner and outer colors when the methods getData and getComponents are called.
By default the color is white. Tinting may be turned on or off with the setTintStatus method.
tint | The tint color for this gradient. |
|
inline |
Sets the tint status for this gradient.
An active tint color is applied to the inner and outer colors when the methods getData and getComponents are called. However, tinting is only active if the status is true.
status | The tint status for this gradient. |
void cugl::Gradient::setTransform | ( | const Affine2 & | transform | ) |
Sets the transform component of this gradient
The transform maps the origin of the current coordinate system to the center and rotation of the rounded rectangular box with the inner color. Applying further transforms will adjust the gradient in texture space.
The transform is primarily for representing rotation. It typically only has a scale component when the gradient is linear.
If this transform is passed directly to a gradient shader, it should be inverted first. If you really need to pass individual components to a shader, you should use getComponents instead.
transform | The transform component of this gradient |
void cugl::Gradient::setTransform | ( | const Mat4 & | transform | ) |
Sets the transform component of this gradient
The transform maps the origin of the current coordinate system to the center and rotation of the rounded rectangular box with the inner color. Applying further transforms will adjust the gradient in texture space.
The transform is primarily for representing rotation. It typically only has a scale component when the gradient is linear.
If this transform is passed directly to a gradient shader, it should be inverted first. If you really need to pass individual components to a shader, you should use getComponents instead.
transform | The transform component of this gradient |
std::string cugl::Gradient::toString | ( | bool | verbose = false | ) | const |
Returns a string representation of this gradient for debuggging purposes.
If verbose is true, the string will include class information. This allows us to unambiguously identify the class.
verbose | Whether to include class information |
Applies a translation to this gradient.
The translation should be in texture coordinates, which (generally) have values 0 to 1.
t | The vector storing the individual translation offsets |
|
inline |
Applies a translation to this gradient.
The translation should be in texture coordinates, which (generally) have values 0 to 1.
tx | The translation offset for the x-axis. |
ty | The translation offset for the y-axis. |