-
Notifications
You must be signed in to change notification settings - Fork 0
/
gl_sdl_2d.cpp
277 lines (230 loc) · 7.06 KB
/
gl_sdl_2d.cpp
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include "gl_sdl_2d.hpp"
#include "gl_sdl_utils.hpp"
static GLuint prog_rect;
static GLuint prog_normal = 0;
const char vs_normal[] =
"#version 300 es\n"
"layout(location = 0) in vec2 pos;\n"
"void main() {\n"
"gl_Position = vec4(pos, 0.0f, 1.0f);\n"
"}\n";
const char vs_rect[] =
"#version 300 es\n"
"layout(location = 0) in vec2 pos;\n"
"uniform vec2 origin;\n"
"uniform float aspect;\n"
"uniform float scale_w;\n"
"uniform float multi_w;\n"
"uniform float multi_h;\n"
"uniform vec2 offset;\n"
"uniform float phi;\n"
"\n"
"void main() {\n"
"vec2 pos_m = mat2(cos(phi), sin(phi), -sin(phi), cos(phi)) * pos + offset;\n"
"vec2 loc = scale_w * vec2(multi_w * pos_m.x, multi_h * pos_m.y) + origin;\n"
"vec2 opengl_coords = 2.0f * vec2(loc.x, loc.y * aspect) - vec2(1.0f, 1.0f);\n"
"gl_Position = vec4(opengl_coords, 0.0f, 1.0f);\n"
"}\n";
const char fs[] =
"#version 300 es\n"
"precision mediump float;\n"
"out vec4 frag_color;\n"
"uniform vec4 draw_color;\n"
"void main() {\n"
"frag_color = draw_color;\n"
"}\n";
int init_2d()
{
GLuint shaders_normal[] = {
read_shader(vs_normal, GL_VERTEX_SHADER),
read_shader(fs, GL_FRAGMENT_SHADER)
};
GLuint shaders_rect[] = {
read_shader(vs_rect, GL_VERTEX_SHADER),
read_shader(fs, GL_FRAGMENT_SHADER)
};
color default_color = { 0, 0, 255 };
prog_normal = create_program(shaders_normal, 2);
prog_rect = create_program(shaders_rect, 2);
set_draw_color(&default_color);
return 0;
}
int use_opengl_coords(space_2d *space)
{
space->use_normal = true;
return 0;
}
int use_rectangle(space_2d *space, rect *drawing_space, float w_int)
{
space->use_normal = false;
space->origin = { drawing_space->x, drawing_space->y };
space->w_loc = drawing_space->w;
space->h_loc = drawing_space->h;
space->w_int = w_int;
return 0;
}
int start_2d(space_2d *space)
{
glClear(GL_DEPTH_BUFFER_BIT);
glDisable(GL_CULL_FACE);
glDisable(GL_DEPTH_TEST);
if (space->use_normal) {
glUseProgram(prog_normal);
return 0;
}
glUseProgram(prog_rect);
GLint vp_rect[4];
glGetIntegerv(GL_VIEWPORT, vp_rect);
float aspect = (float)vp_rect[2] / (float)vp_rect[3];
float scale_w = fabs(space->w_loc) / space->w_int;
float multi_w = space->w_loc < 0.0f ? -1.0f : 1.0f;
float multi_h = space->h_loc < 0.0f ? -1.0f : 1.0f;
GLint aspect_loc = glGetUniformLocation(prog_rect, "aspect");
GLint scale_w_loc = glGetUniformLocation(prog_rect, "scale_w");
GLint multi_w_loc = glGetUniformLocation(prog_rect, "multi_w");
GLint multi_h_loc = glGetUniformLocation(prog_rect, "multi_h");
GLint origin_loc = glGetUniformLocation(prog_rect, "origin");
glUniform1f(aspect_loc, aspect);
glUniform1f(scale_w_loc, scale_w);
glUniform1f(multi_w_loc, multi_w);
glUniform1f(multi_h_loc, multi_h);
glUniform2f(origin_loc, space->origin.x, space->origin.y);
return 0;
}
#define ARRAY_SIZE(x) ((sizeof(x)) / (sizeof(*x)))
int set_draw_color(color *color)
{
GLuint progs[] = { prog_rect /*, prog_normal*/ };
GLfloat f_color[] = { color->r / 255.0f, color->g / 255.0f,
color->b / 255.0f, color->a / 255.0f };
for (uint i = 0; i < ARRAY_SIZE(progs); i++) {
GLint loc = glGetUniformLocation (progs[i], "draw_color");
if (loc >= 0)
glUniform4fv(loc, 1, f_color);
}
return 0;
}
static GLint get_cur_uniform_loc(const char *name)
{
GLint prog = 0;
glGetIntegerv(GL_CURRENT_PROGRAM, &prog);
if (prog <= 0)
return -1;
GLint loc = glGetUniformLocation(prog, name);
if (loc < 0)
return -1;
return loc;
}
int set_rot_angle(float phi) {
GLint loc = glGetUniformLocation(prog_rect, "phi");
if (loc < 0)
std::runtime_error("Current program does not contain a rotation component");
glUniform1f(loc, phi);
return 0;
}
int set_offset(point *offset) {
GLint loc = get_cur_uniform_loc("offset");
if (loc < 0)
std::runtime_error("Current program does not contain an offset component");
glUniform2f(loc, offset->x, offset->y);
return 0;
}
static void draw_tri_generic(tri *tri, bool border)
{
GLfloat verts[] = { tri->points[0].x, tri->points[0].y,
tri->points[1].x, tri->points[1].y,
tri->points[2].x, tri->points[2].y };
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, verts);
glEnableVertexAttribArray(0);
glDrawArrays(border ? GL_LINE_LOOP : GL_TRIANGLES, 0, 3);
}
/* TODO : use actual input */
int draw_tri(tri *tri)
{
draw_tri_generic(tri, false);
return 0;
}
int draw_tri_border(tri *tri)
{
draw_tri_generic(tri, true);
return 0;
}
static void draw_rect_generic(rect *rect, bool border)
{
GLfloat verts[] = { rect->x, rect->y,
rect->x + rect->w, rect->y,
rect->x + rect->w, rect->y + rect->h,
rect->x, rect->y + rect->h,
rect->x + rect->w, rect->y + rect->h,
rect->x, rect->y };
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, verts);
glEnableVertexAttribArray(0);
glDrawArrays(border ? GL_LINE_LOOP : GL_TRIANGLES, 0, border ? 4 : 6);
}
int draw_rect(rect *rect)
{
draw_rect_generic(rect, false);
return 0;
}
int draw_rect_border(rect *rect)
{
draw_rect_generic(rect, true);
return 0;
}
#define PI 3.1415926f
#define CIRCLE_DIVS 16
static void draw_circle_generic(circle *circle, bool border)
{
GLfloat verts[2 * CIRCLE_DIVS + 4];
uint offset = 0;
if (!border) {
verts[0] = circle->center.x;
verts[1] = circle->center.y;
offset = 1;
}
for (uint i = offset; i < CIRCLE_DIVS + offset; i++) {
float phi = 2.0f * PI * (float)i / (float)CIRCLE_DIVS;
verts[i * 2] = circle->center.x + circle->radius * cosf(phi);
verts[i * 2 + 1] = circle->center.y + circle->radius * sinf(phi);
}
if (!border) {
verts[CIRCLE_DIVS * 2 + 2] = verts[2];
verts[CIRCLE_DIVS * 2 + 3] = verts[3];
}
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, verts);
glEnableVertexAttribArray(0);
if (border)
glDrawArrays(GL_LINE_LOOP, 0, CIRCLE_DIVS);
else
glDrawArrays(GL_TRIANGLE_FAN, 0, ARRAY_SIZE(verts) / 2);
}
int draw_circle(circle *circle)
{
draw_circle_generic(circle, false);
return 0;
}
int draw_circle_border(circle *circle)
{
draw_circle_generic(circle, true);
return 0;
}
int draw_line(line *line)
{
GLfloat verts[4] = {line->start.x, line->start.y,
line->end.x, line->end.y};
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, verts);
glEnableVertexAttribArray(0);
glDrawArrays(GL_LINE_STRIP, 0, 2);
return 0;
}
void set_line_width(float w)
{
glLineWidth(w);
}
float get_h_to_w_aspect()
{
GLint vp_rect[4];
glGetIntegerv(GL_VIEWPORT, vp_rect);
float aspect = (float)vp_rect[3] / (float)vp_rect[2];
return aspect;
}