-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdrawable2d.cpp
More file actions
161 lines (130 loc) · 4.35 KB
/
drawable2d.cpp
File metadata and controls
161 lines (130 loc) · 4.35 KB
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
#include <fea/rendering/drawable2d.hpp>
#include <fea/assert.hpp>
namespace fea
{
Drawable2D::Drawable2D() : mDrawMode(0), mRotation(0.0f), mScaling(glm::vec2(1.0f, 1.0f)), mParallax(glm::vec2(1.0f, 1.0f)), mColor(1.0f, 1.0f, 1.0f, 1.0f)
{
}
const std::vector<float>& Drawable2D::getVertices() const
{
return mVertices;
}
void Drawable2D::setPosition(const glm::vec2& position)
{
mPosition = position;
}
const glm::vec2& Drawable2D::getPosition() const
{
return mPosition;
}
void Drawable2D::translate(const glm::vec2& position)
{
mPosition += position;
}
void Drawable2D::setOrigin(const glm::vec2& origin)
{
mOrigin = origin;
}
const glm::vec2& Drawable2D::getOrigin() const
{
return mOrigin;
}
void Drawable2D::setRotation(const float radians)
{
mRotation = radians;
}
float Drawable2D::getRotation() const
{
return mRotation;
}
void Drawable2D::rotate(const float radians)
{
mRotation += radians;
}
void Drawable2D::setScale(const glm::vec2& scaling)
{
mScaling = scaling;
}
const glm::vec2& Drawable2D::getScale() const
{
return mScaling;
}
void Drawable2D::scale(const glm::vec2& scaling)
{
mScaling *= scaling;
}
void Drawable2D::setParallax(const glm::vec2& parallax)
{
mParallax = parallax;
}
const glm::vec2& Drawable2D::getParallax() const
{
return mParallax;
}
void Drawable2D::setColor(const Color& color)
{
mColor = color;
}
Color Drawable2D::getColor() const
{
return mColor;
}
void Drawable2D::setOpacity(float opacity)
{
FEA_ASSERT(opacity >= 0.0f && opacity <= 1.0f, "Opacity must be within the range of [0.0f, 1.0f]! " + std::to_string(opacity) + " provided.");
mColor.setAAsFloat(opacity);
}
float Drawable2D::getOpacity() const
{
return mColor.aAsFloat();
}
std::vector<RenderEntity> Drawable2D::getRenderInfo() const
{
std::vector<RenderEntity> entities(1);
RenderEntity& temp = entities[0];
glm::vec3 colorInfo = glm::vec3(mColor.rAsFloat(), mColor.gAsFloat(), mColor.bAsFloat());
float opacity = mColor.aAsFloat();
temp.mDrawMode = mDrawMode;
temp.mElementAmount = mVertices.size() / 2;
if(mVertices.size() > 0)
temp.mVertexAttributes.emplace_back("vertex", 2, mVertices);
if(mTexCoords.size() > 0)
temp.mVertexAttributes.emplace_back("texCoords", 2, mTexCoords);
if(mVertexColors.size() > 0)
temp.mVertexAttributes.emplace_back("colors", 4, mVertexColors);
temp.mUniforms.reserve(16);
temp.mUniforms.emplace_back("position", VEC2, mPosition);
temp.mUniforms.emplace_back("origin", VEC2, mOrigin);
temp.mUniforms.emplace_back("rotation", FLOAT, mRotation);
temp.mUniforms.emplace_back("scaling", VEC2, mScaling);
temp.mUniforms.emplace_back("parallax", VEC2, mParallax);
temp.mUniforms.emplace_back("constraints", VEC4, glm::vec4(0.0f, 1.0f, 0.0f, 1.0f));
temp.mUniforms.emplace_back("color", VEC3, colorInfo);
temp.mUniforms.emplace_back("opacity", FLOAT, opacity);
for(const auto& uniform : mExtraUniforms)
temp.mUniforms.push_back(uniform.second);
return entities;
}
void Drawable2D::setExtraUniform(const Uniform& uniform)
{
mExtraUniforms[uniform.mName] = uniform;
}
bool Drawable2D::hasExtraUniform(const std::string& name) const
{
return mExtraUniforms.find(name) != mExtraUniforms.end();
}
const Uniform& Drawable2D::getExtraUniform(const std::string& name) const
{
FEA_ASSERT(mExtraUniforms.find(name) != mExtraUniforms.end(), "Trying to access a uniform called " << name << " which doesn't exist");
return mExtraUniforms.at(name);
}
void Drawable2D::removeExtraUniform(const std::string& name)
{
FEA_ASSERT(mExtraUniforms.find(name) != mExtraUniforms.end(), "Trying to erase a uniform called " << name << " which doesn't exist");
mExtraUniforms.erase(name);
}
void Drawable2D::clearExtraUniforms()
{
mExtraUniforms.clear();
}
}