-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathr_gui.h
175 lines (137 loc) · 3.91 KB
/
r_gui.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
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
#ifndef R_GUI_HDR
#define R_GUI_HDR
#include "r_method.h"
#include "r_texture.h"
#include "u_map.h"
#include "u_optional.h"
namespace m {
struct perspective;
}
namespace r {
struct model;
struct pipeline;
struct guiMethod : method {
guiMethod();
bool init(const u::vector<const char *> &defines = u::vector<const char *>());
void setPerspective(const m::perspective &p);
void setColorTextureUnit(int unit);
private:
uniform *m_screenSize;
uniform *m_colorMap;
};
struct guiModelMethod : method {
guiModelMethod();
bool init(const u::vector<const char *> &defines = u::vector<const char *>());
void setWVP(const m::mat4 &wvp);
void setWorld(const m::mat4 &world);
void setColorTextureUnit(int unit);
void setEyeWorldPos(const m::vec3 &pos);
void setScroll(int u, int v, float millis);
private:
uniform *m_WVP;
uniform *m_world;
uniform *m_colorTextureUnit;
uniform *m_eyeWorldPosition;
uniform *m_scrollRate;
uniform *m_scrollMillis;
};
struct gui {
gui();
~gui();
bool load(const u::string &font);
bool upload();
void render(const pipeline &pl);
protected:
template <size_t E>
void drawPolygon(const float (&coords)[E], float r, uint32_t color);
void drawRectangle(float x, float y, float w, float h, float fth, uint32_t color);
void drawRectangle(float x, float y, float w, float h, float r, float fth, uint32_t color);
void drawLine(float x0, float y0, float x1, float y1, float r, float fth, uint32_t color);
void drawText(float x, float y, const char *contents, int align, uint32_t color);
void drawImage(float x, float y, float w, float h, const char *path);
void drawTexture(float x, float y, float w, float h, const unsigned char *data);
private:
struct glyphQuad {
float x0, y0;
float x1, y1;
float s0, s1;
float t0, t1;
};
public:
u::optional<glyphQuad> getGlyphQuad(int pw, int ph, size_t index, float &xpos, float &ypos);
private:
enum {
kMethodNormal,
kMethodFont,
kMethodImage
};
struct glyph {
int x0, y0;
int x1, y1;
float xoff;
float yoff;
float xadvance;
};
struct atlas {
atlas();
struct node {
node();
~node();
node *l, *r;
int x, y;
int w, h;
};
node *insert(int w, int h);
float occupancy() const;
size_t width() const;
size_t height() const;
private:
friend struct gui;
node m_root;
size_t m_width;
size_t m_height;
protected:
size_t usedSurfaceArea(const node &n) const;
node *insert(node *n, int w, int h);
};
struct vertex {
m::vec2 position;
m::vec2 coordinate;
m::vec4 color;
};
u::vector<glyph> m_glyphs;
// Batch by texture/shader
struct batch {
size_t start;
size_t count;
int method;
};
void addBatch(batch &b);
u::vector<vertex> m_vertices;
u::vector<batch> m_batches;
static constexpr size_t kCoordCount = 100;
static constexpr size_t kCircleVertices = 8 * 4;
float m_coords[kCoordCount * 2];
float m_normals[kCoordCount * 2];
float m_circleVertices[kCircleVertices * 2];
GLuint m_vbos[2];
GLuint m_vao;
unsigned char m_bufferIndex;
u::map<u::string, texture2D*> m_modelTextures;
u::map<u::string, atlas::node*> m_textures;
u::map<u::string, model*> m_models;
texture2D m_font;
atlas::node *m_notex;
guiMethod m_methods[3];
guiModelMethod m_modelMethod;
guiModelMethod m_modelScrollMethod;
atlas::node *atlasPack(const u::string &file);
atlas m_atlas;
unsigned char *m_atlasData;
GLuint m_atlasTexture;
GLuint m_miscTexture; // miscellaneous texture for drawTexture
m::vec2 m_resolution;
size_t m_coalesced;
};
}
#endif