1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
#pragma once
#include "SceneNode.h"
#include "../Primitives/Rect.h"
#include "RenderCommand.h"
namespace nCine
{
class RenderCommand;
class RenderQueue;
/// Object that can be drawn through the render queue
class DrawableNode : public SceneNode
{
friend class ShaderState;
friend class Viewport;
public:
/** @{ @name Constants */
// Notable anchor points for a drawable node
static const Vector2f AnchorCenter;
static const Vector2f AnchorBottomLeft;
static const Vector2f AnchorTopLeft;
static const Vector2f AnchorBottomRight;
static const Vector2f AnchorTopRight;
/** @} */
/// Presets for blending factors
enum class BlendingPreset
{
DISABLED, ///< uses `GL_ONE` and `GL_ZERO`
ALPHA, ///< uses `GL_SRC_ALPHA` and `GL_ONE_MINUS_SRC_ALPHA`
PREMULTIPLIED_ALPHA, ///< uses `GL_ONE` and `GL_ONE_MINUS_SRC_ALPHA`
ADDITIVE, ///< uses `GL_SRC_ALPHA` and `GL_ONE`
MULTIPLY ///< uses `GL_DST_COLOR` and `GL_ZERO`
};
/// OpenGL blending factors
enum class BlendingFactor
{
ZERO,
ONE,
SRC_COLOR,
ONE_MINUS_SRC_COLOR,
DST_COLOR,
ONE_MINUS_DST_COLOR,
SRC_ALPHA,
ONE_MINUS_SRC_ALPHA,
DST_ALPHA,
ONE_MINUS_DST_ALPHA,
CONSTANT_COLOR,
ONE_MINUS_CONSTANT_COLOR,
CONSTANT_ALPHA,
ONE_MINUS_CONSTANT_ALPHA,
SRC_ALPHA_SATURATE,
};
/// Constructor for a drawable node with a parent and a specified relative position
DrawableNode(SceneNode* parent, float xx, float yy);
/// Constructor for a drawable node with a parent and a specified relative position as a vector
DrawableNode(SceneNode* parent, Vector2f position);
/// Constructor for a drawable node with a parent and positioned in the relative origin
explicit DrawableNode(SceneNode* parent);
/// Constructor for a drawable node with no parent and positioned in the origin
DrawableNode();
~DrawableNode() override;
DrawableNode& operator=(const DrawableNode&) = delete;
DrawableNode(DrawableNode&&);
DrawableNode& operator=(DrawableNode&&);
/// Updates the draw command and adds it to the queue
bool OnDraw(RenderQueue& renderQueue) override;
/// Returns the width of the node area
inline virtual float width() const {
return width_ * scaleFactor_.X;
}
/// Returns the height of the node area
inline virtual float height() const {
return height_ * scaleFactor_.Y;
}
/// Returns the size of the node area
inline Vector2f size() const {
return Vector2f(width(), height());
}
/// Returns the absolute width of the node area
inline virtual float absWidth() const {
return width_ * absScaleFactor_.X;
}
/// Returns the absolute height of the node area
inline virtual float absHeight() const {
return height_ * absScaleFactor_.Y;
}
/// Returns the absolute size of the node area
inline Vector2f absSize() const {
return Vector2f(absWidth(), absHeight());
}
/// Gets the transformation anchor point
inline Vector2f anchorPoint() const {
return (anchorPoint_ / size()) + 0.5f;
}
/// Sets the transformation anchor point
void setAnchorPoint(float xx, float yy);
/// Sets the transformation anchor point with a `Vector2f`
inline void setAnchorPoint(Vector2f point) {
setAnchorPoint(point.X, point.Y);
}
/// Returns true if the node renders with blending enabled
bool isBlendingEnabled() const;
/// Sets the blending state for node rendering
void setBlendingEnabled(bool blendingEnabled);
/// Returns the source blending factor
BlendingFactor srcBlendingFactor() const;
/// Returns the destination blending factor
BlendingFactor destBlendingFactor() const;
/// Sets a blending preset for source and destination blending factors
void setBlendingPreset(BlendingPreset blendingPreset);
/// Sets a specific source and destination blending factors
void setBlendingFactors(BlendingFactor srcBlendingFactor, BlendingFactor destBlendingFactor);
/// Returns the last frame in which any of the viewports have rendered this node (node was not culled)
inline std::uint32_t lastFrameRendered() const {
return lastFrameRendered_;
}
/// Returns the axis-aligned bounding box of the node area in the last frame
inline Rectf aabb() const {
return aabb_;
}
protected:
/// Node width in pixel
float width_;
/// Node height in pixel
float height_;
/// The render command class associated with this node
RenderCommand renderCommand_;
/// The last frame any viewports rendered this node
std::uint32_t lastFrameRendered_;
/// Axis-aligned bounding box of the node area
Rectf aabb_;
/// Calculates updated values for the AABB
virtual void updateAabb();
/// Called by each viewport update method to update a node culling state
void updateCulling();
/// Protected copy constructor used to clone objects
DrawableNode(const DrawableNode& other);
/// Performs the required tasks upon a change to the shader
virtual void shaderHasChanged() = 0;
/// Updates the render command
virtual void updateRenderCommand() = 0;
};
}
|