-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinearGradientShader.cpp
More file actions
226 lines (197 loc) · 7.6 KB
/
Copy pathLinearGradientShader.cpp
File metadata and controls
226 lines (197 loc) · 7.6 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include "./include/GShader.h"
#include "./include/GMatrix.h"
#include "./include/GColor.h"
#include "./GBlenders.h"
class LinearGradientShader : public GShader {
public:
LinearGradientShader(GPoint p0, GPoint p1, const GColor c[], int count, GShader::TileMode md) {
mode = md;
float x11 = p1.fX - p0.fX;
float x21 = p1.fY - p0.fY;
float x12 = - x21;
float x22 = x11;
bool success = GMatrix({x11, x12, p0.fX, x21, x22, p0.fY}).invert(&T_gradient);
if (!success) {
assert(success);
}
assert(count < 10); // I pre-alloc color, so must ensure size is correct
std::copy(c, c + count, colors);
colors[count] = GColor({1, 1, 1, 1});
numOfColors = count;
}
bool isOpaque() {
for (int i = 0; i < numOfColors - 1; i ++) {
if (colors[i].a != 1) return false;
}
return true;
}
bool setContext(const GMatrix& ctm) override {
GMatrix inv_ctm;
if (!ctm.invert(&inv_ctm)) return false;
m = GMatrix::Concat(T_gradient, inv_ctm);
return true;
}
void shadeRow(int x, int y, int count, GPixel row[]) {
GPoint tmp = m * GPoint{x + 0.5f, y + 0.5f};
float localX = tmp.fX;
float dx = m[0];
for (int j = 0; j < count; j ++) {
float ix = localX + dx * j;
float cix;
switch (mode) {
case GShader::kClamp:
cix = clamp(ix); // clamp
break;
case GShader::kRepeat:
cix = ix - floor(ix);
break;
case GShader::kMirror:
// Just like how we use division + floor as module for kRepeat,
// we use the same trick here.
float tmp = ix / 2;
// proportion is between 0~1, where 0~0.5 represent vector (0,0) -> (1,0)
// in shader space, and 0.5~1.0 represent vector (1,0) -> (2,0)
float proportion = tmp - floor(tmp);
if (proportion > 0.5) {
// mirror = symmetry by 0.5
proportion = 1 - proportion;
}
// cix is now the position in the (0,0) -> (1,0) shader space
// since 0~0.5 represent half of the space
cix = proportion * 2;
}
float fColorIndx = cix * (numOfColors - 1);
int startColorIdx = std::floor(fColorIndx); //!! Do we need to calculate this every time?
int endColorIdx = startColorIdx + 1;
float w = endColorIdx - fColorIndx;
GColor ic = interpolate(colors[startColorIdx], colors[endColorIdx], w); // calculate color
GPixel premult_ic = Blenders::prepSrcPixel(ic);
row[j] = premult_ic;
}
}
private:
GColor colors[10]; // pre-allocate to 10
int numOfColors;
GMatrix m; // Matrix to use
GMatrix T_gradient; // Matrix that transform from world space to gradient-line space
GShader::TileMode mode;
GColor interpolate(GColor start, GColor end, float w) {
float ew = 1 - w;
float nr = start.r * w + end.r * ew;
float ng = start.g * w + end.g * ew;
float nb = start.b * w + end.b * ew;
float na = start.a * w + end.a * ew;
return GColor({nr, ng, nb, na});
}
float clamp(float ix) {
if (ix < 0) return 0;
else if (ix > 1) return 1;
else return ix;
}
};
class SingleColorShader : public GShader {
public:
SingleColorShader(GPoint p0, GPoint p1, const GColor c[], int count, GShader::TileMode mode) {
p = Blenders::prepSrcPixel(c[0]);
}
bool isOpaque() {
return false; // we don't care, always return the prepared pixel
}
bool setContext(const GMatrix& ctm) override {
return true; // we don't care, always return the prepared pixel
}
void shadeRow(int x, int y, int count, GPixel row[]) {
for (int j = 0; j < count; j ++) {
row[j] = p;
}
}
private:
GPixel p;
};
class TwoColorLinearGradientShader : public GShader {
public:
TwoColorLinearGradientShader(GPoint p0, GPoint p1, const GColor c[], int count, GShader::TileMode md) {
mode = md;
float x11 = p1.fX - p0.fX;
float x21 = p1.fY - p0.fY;
float x12 = - x21;
float x22 = x11;
bool success = GMatrix({x11, x12, p0.fX, x21, x22, p0.fY}).invert(&T_gradient);
if (!success) {
assert(success);
}
colors[0] = c[0];
colors[1] = c[1];
left = Blenders::prepSrcPixel(colors[0]);
right = Blenders::prepSrcPixel(colors[1]);
numOfColors = 2;
}
bool isOpaque() {
return colors[0].a == 1 && colors[1].a == 1;
}
bool setContext(const GMatrix& ctm) override {
GMatrix inv_ctm;
if (!ctm.invert(&inv_ctm)) return false;
m = GMatrix::Concat(T_gradient, inv_ctm);
return true;
}
void shadeRow(int x, int y, int count, GPixel row[]) {
GPoint tmp = m * GPoint{x + 0.5f, y + 0.5f};
float localX = tmp.fX;
float dx = m[0];
for (int j = 0; j < count; j ++) {
float ix = localX + dx * j;
float cix;
switch(mode) {
case GShader::kClamp: {
cix = ix;
if (ix >= 1.0f) {
row[j] = right;
return;
}
if (ix <= 0.0f) {
row[j] = left;
return;
}
break;
}
case GShader::kRepeat: {
cix = ix - floor(ix);
break;
}
case GShader::kMirror: {
float tmp = ix / 2;
float proportion = tmp - floor(tmp);
if (proportion > 0.5) {
proportion = 1 - proportion;
}
cix = proportion * 2;
}
}
GColor ic = interpolate(cix); // calculate color
GPixel premult_ic = Blenders::prepSrcPixel(ic);
row[j] = premult_ic;
}
}
private:
GColor colors[10]; // pre-allocate to 10
int numOfColors;
GMatrix m; // Matrix to use
GMatrix T_gradient; // Matrix that transform from world space to gradient-line space
GPixel left;
GPixel right;
GShader::TileMode mode;
GColor interpolate(float ix) {
float c = 1.0f - ix;
float nr = colors[0].r * c + colors[1].r * ix;
float ng = colors[0].g * c + colors[1].g * ix;
float nb = colors[0].b * c + colors[1].b * ix;
float na = colors[0].a * c + colors[1].a * ix;
return GColor({nr, ng, nb, na});
}
};
std::unique_ptr<GShader> GCreateLinearGradient(GPoint p0, GPoint p1, const GColor colors[], int count, GShader::TileMode mode){
if (count == 1) return std::unique_ptr<GShader>(new SingleColorShader(p0, p1, colors, count, mode));
if (count == 2) return std::unique_ptr<GShader>(new TwoColorLinearGradientShader(p0, p1, colors, count, mode));
return std::unique_ptr<GShader>(new LinearGradientShader(p0, p1, colors, count, mode));
}