-
Notifications
You must be signed in to change notification settings - Fork 2
/
gradient.h
77 lines (58 loc) · 1.45 KB
/
gradient.h
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
#ifndef GRADIENT_H
#define GRADIENT_H
#if 1 // C placeholder
#include <GL/glfw.h>
typedef struct {} Gradient;
GLubyte* gradient_interp(Gradient *g, float x1, float y1);
#else // C++ ... porting TODO
#include <string>
#include <map>
#include <GL/glfw.h>
#include <Box2D/Box2D.h>
#include "matrix.h"
class Gradient;
typedef std::map< float32, GLubyte * > StopMap;
typedef std::map< std::string, Gradient * > GradientMap;
class Gradient {
public:
const char *id;
GLubyte color[4];
StopMap stops;
// Inverse matrix transform
Matrix *inv_transform;
Gradient() {
inv_transform = NULL;
}
Gradient(const char *id) : id(id) {
inv_transform = NULL;
}
GLubyte *interp(b2Vec2 pt);
~Gradient() {
delete inv_transform;
}
virtual float32 grad_value(b2Vec2 pt) {return 0;}
};
class LinearGradient : public Gradient {
public:
// Gradient vector
float32 x1, y1, x2, y2;
LinearGradient(const char *id) : Gradient(id) {
x1 = y1 = x2 = y2 = 0.0;
}
float32 grad_value(b2Vec2 pt) {
return ((pt.x - x1)*(x2 - x1) + (pt.y - y1)*(y2 - y1)) /
(pow(x1 - x2, 2) + pow(y1 - y2, 2));
}
};
class RadialGradient : public Gradient {
public:
float32 cx, cy, r;
RadialGradient(const char *id) : Gradient(id) {
cx = cy = r = 0.0;
}
float32 grad_value(b2Vec2 pt) {
return sqrtf(pow(pt.x - cx, 2) + pow(pt.y - cy, 2))/r;
}
};
#endif
#endif