-
Notifications
You must be signed in to change notification settings - Fork 1
/
rendering.cpp
1262 lines (1078 loc) · 34.9 KB
/
rendering.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <stdint.h>
#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <glm/gtx/transform.hpp>
#include "arg.h"
#define Glyph Glyph_
#define Font Font_
#include "st.h"
#include "rendering.h"
#include <cstddef>
#include <cassert>
#include <glad/glad.h>
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>
#include <functional>
#include <cmath>
#include <glm/glm.hpp>
#include FT_BITMAP_H
extern int font_size;
// Feel factors
#define JOUNCE_MAX_AMOUNT 0.005f
#define JOUNCE_GROW_FACTOR 0.5f
#define JOUNCE_SHIFT 8.f
#define JOUNCE_MULTIPLIER 8.f
#define JOUNCE_DECAY_FACTOR 0.9f
#define ROTATION_MAX 0.00314 // About 0.1% of pi
#define KEYPRESS_TIMEOUT 0.5f
// Technical parameters
#define ATLAS_SIZE 4096
#define PARTICLE_FB_SCALE 3
#define POSITION_LOCATION 0
#define COLOR_LOCATION 1
#define UV_LOCATION 2
#define TEXTURE_BINDING 0
#define TRANSFORM_LOCATION 0
#define TEXTURE_LOCATION 1
static const char * vert_shader =
"#version 450\n"
"layout(location=0) in vec3 position;\n"
"layout(location=1) in vec4 in_color;\n"
"layout(location=2) in vec2 in_uv;\n"
"layout(location=0) uniform mat4 transform;\n"
"out vec2 cross_uv;\n"
"out vec4 cross_color;\n"
"void main() {"
" gl_Position = transform * vec4(position, 1);\n"
" cross_uv = in_uv;\n"
" cross_color = in_color;\n"
"}\n";
static const char * frag_shader =
"#version 450\n"
"out vec4 color;\n"
"in vec4 cross_color;\n"
"in vec2 cross_uv;\n"
"layout(location=1, binding=0) uniform sampler2D font_tex;\n"
"void main() {\n"
// " color = vec4(1.f, 1.f, 1.f, texture(font_tex, cross_uv).r);\n"
// " color = vec4(cross_color.rgb, texture(font_tex, cross_uv).r);\n"
// " color = cross_color * texture(font_tex, cross_uv).r;\n"
" color = cross_color;\n"
" color.a *= texture(font_tex, cross_uv).r;\n"
"}\n";
static const char * framebuffer_frag_shader =
"#version 450\n"
"out vec4 color;\n"
"in vec4 cross_color;\n"
"in vec2 cross_uv;\n"
"layout(location=1, binding=0) uniform sampler2D font_tex;\n"
"void main() {\n"
" color = cross_color * texture(font_tex, cross_uv);\n"
"}\n";
static const char * particle_blit_shader =
"#version 450\n"
"out vec4 color;\n"
"in vec4 cross_color;\n"
"in vec2 cross_uv;\n"
"layout(location=1, binding=0) uniform sampler2D font_tex;\n"
"void main() {\n"
" color = cross_color * texture(font_tex, cross_uv);\n"
" color.a = texture(font_tex, cross_uv).a;\n"
"}\n";
static const char * color_shader =
"#version 450\n"
"out vec4 color;\n"
"in vec4 cross_color;\n"
"in vec2 cross_uv;\n"
"layout(location=1, binding=0) uniform sampler2D font_tex;\n"
"void main() {\n"
" color = cross_color;\n"
" color.a = 1.f;\n"
"}\n";
struct __attribute__((packed)) vec2 {
float x, y;
};
struct rect {
vec2 origin;
vec2 size;
};
struct __attribute__((packed)) vertex {
struct __attribute__((packed)) {
float x, y, z;
} pos;
struct vec2 texcoords;
struct color c;
};
class GlTexture;
template<typename VertexClass> class GlBuffer;
template<typename VertexClass> class GlVAO;
template<typename Updatefunc> class ParticleSystem;
////////////////////////////////////////////////////////////////////////////////
// GlTexture
////////////////////////////////////////////////////////////////////////////////
class GlTexture {
private:
GLuint _id;
public:
GlTexture() {
glGenTextures(1, &_id);
}
GlTexture(GlTexture & other) = delete;
GlTexture(GlTexture && other) {
_id = other._id;
}
GlTexture & operator=(GlTexture & other) = delete;
GlTexture & operator=(GlTexture && other) {
_id = other._id;
return *this;
}
~GlTexture() {
glDeleteTextures(1, &_id);
}
GLuint GetID() {
return _id;
}
};
////////////////////////////////////////////////////////////////////////////////
// GlFrameBuffer
////////////////////////////////////////////////////////////////////////////////
class GlFrameBuffer {
private:
GLuint _id;
GLsizei _width, _height;
GLuint _color_id;
GLuint _depth_id;
public:
GlFrameBuffer(GLsizei width, GLsizei height, bool linear, GLenum depth_format);
GlFrameBuffer(const GlFrameBuffer & other) = delete;
GlFrameBuffer(GlFrameBuffer && other);
GlFrameBuffer & operator=(GlFrameBuffer & other) = delete;
GlFrameBuffer & operator=(GlFrameBuffer && other);
~GlFrameBuffer();
void blit_entirely(GLuint dst) const;
void blit_entirely(const GlFrameBuffer & dst) const;
void bind(GLenum target) const;
GLuint get_main_color() const { return _color_id; }
};
GlFrameBuffer::GlFrameBuffer(GLsizei width, GLsizei height, bool linear, GLenum depth_format)
: _width(width),
_height(height)
{
GLuint texs[2];
glCreateFramebuffers(1, &_id);
glCreateTextures(GL_TEXTURE_2D, 2, texs);
_color_id = texs[0];
_depth_id = texs[1];
printf("Creating frame buffer of size %d %d \n", width, height);
GLenum fmt = (linear) ? GL_RGB8 : GL_SRGB8_ALPHA8;
glTextureStorage2D(_color_id, 1, fmt, width, height);
glTextureParameteri(_color_id, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTextureParameteri(_color_id, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTextureStorage2D(_depth_id, 1, depth_format, width, height);
glNamedFramebufferTexture(_id, GL_COLOR_ATTACHMENT0, _color_id, 0);
glNamedFramebufferTexture(_id, GL_DEPTH_ATTACHMENT, _depth_id, 0);
if (!linear) {
int p;
glGetNamedFramebufferAttachmentParameteriv(_id, GL_COLOR_ATTACHMENT0,
GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING,
&p);
assert(p == GL_SRGB);
}
}
GlFrameBuffer::~GlFrameBuffer() {
glDeleteFramebuffers(1, &_id);
GLuint textures[2] = {_color_id, _depth_id};
glDeleteTextures(2, textures);
}
void GlFrameBuffer::bind(GLenum target) const {
glBindFramebuffer(target, _id);
}
void GlFrameBuffer::blit_entirely(GLuint dst) const {
glBlitNamedFramebuffer(_id, dst,
0, 0, _width, _height,
0, 0, _width, _height,
GL_COLOR_BUFFER_BIT,
GL_LINEAR);
}
////////////////////////////////////////////////////////////////////////////////
// GlBuffer
////////////////////////////////////////////////////////////////////////////////
template<typename T>
class GlBuffer {
const static int NUM_BUFFERS = 1;
private:
/** Name of the buffer under management */
GLuint _ids[NUM_BUFFERS];
/** Current buffer */
int _buffer;
/** The number of bytes allocated in that buffer */
size_t _capacities[NUM_BUFFERS];
/** CPU-side storage for all the information */
std::vector<T> _storage;
friend GlVAO<T>;
template<typename F> friend class ParticleSystem;
public:
GlBuffer() {
glCreateBuffers(NUM_BUFFERS, _ids);
_buffer = 0;
_storage.reserve(1); // Make sure there is room for at least 1 element
for (auto i = 0; i < NUM_BUFFERS; ++i) {
_capacities[i] = _storage.capacity() * sizeof(T);
glNamedBufferData(_ids[i], _capacities[i], NULL, GL_STREAM_DRAW);
}
}
GlBuffer(GlBuffer & other) = delete;
GlBuffer(GlBuffer && other) {
_ids = other._ids;
_capacities = other._capacities;
_storage = std::move(other._storage);
}
GlBuffer & operator= (GlBuffer & other) = delete;
GlBuffer & operator= (GlBuffer && other) {
_ids = other._ids;
_capacities = other._capacities;
_storage = std::move(other._storage);
return *this;
}
void push_elements(T * elems, size_t count) {
if (_storage.size() + count > 65535) {
fprintf(stderr, "Warning: too many elements, not adding to buffer\n");
return;
}
_storage.insert(_storage.end(), elems, elems + count);
}
void sync() {
size_t bsize = _storage.size() * sizeof(T);
if (_capacities[_buffer] < bsize) {
_capacities[_buffer] = bsize;
glNamedBufferData(_ids[_buffer], _capacities[_buffer], NULL, GL_STREAM_DRAW);
}
glNamedBufferSubData(_ids[_buffer], 0, bsize, _storage.data());
_buffer = (_buffer + 1) % NUM_BUFFERS;
}
void clear() {
_storage.clear();
}
typename std::vector<T>::size_type num_elems() const {
return _storage.size();
}
};
//////////////////////////////////////////////////////////////////////////////
// GlShader
//////////////////////////////////////////////////////////////////////////////
class GlShader {
private:
GLuint _prog_id;
GLuint AllocShader(GLenum shader_type, std::string & src);
public:
/** Creates a new shader
@param vert the vertex shader
@param frag the fragment shader
*/
GlShader(std::string vert, std::string frag);
GlShader(GlShader & other) = delete;
GlShader(GlShader && other);
GlShader & operator=(GlShader & other) = delete;
GlShader & operator=(GlShader && other);
~GlShader();
void bind(void);
void uniform(GLint location, GLboolean transpose, const glm::mat4 & m);
};
static void PrintShaderInfoLog(GLuint shader) {
int len;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &len);
if (len > 0) {
char * buf = (char*)malloc(len + 1);
glGetShaderInfoLog(shader, len + 1, &len, buf);
printf("%s\n", buf);
free(buf);
}
}
GLuint GlShader::AllocShader(GLenum shader_type, std::string & src) {
GLuint shdr = glCreateShader(shader_type);
int len = src.length();
const char * const csrc = src.c_str();
glShaderSource(shdr, 1, &csrc, &len);
glCompileShader(shdr);
PrintShaderInfoLog(shdr);
int compiled;
glGetShaderiv(shdr, GL_COMPILE_STATUS, &compiled);
if (compiled != GL_TRUE) {
die("unable to compile shader\n");
}
return shdr;
}
GlShader::GlShader(std::string vert, std::string frag) {
printf("Make vertex shader\n");
GLuint vshdr = AllocShader(GL_VERTEX_SHADER, vert);
printf("Make fragment shader\n");
GLuint fshdr = AllocShader(GL_FRAGMENT_SHADER, frag);
_prog_id = glCreateProgram();
glAttachShader(_prog_id, vshdr);
glAttachShader(_prog_id, fshdr);
glLinkProgram(_prog_id);
glDeleteShader(vshdr);
glDeleteShader(fshdr);
int linked;
glGetProgramiv(_prog_id, GL_LINK_STATUS, &linked);
if (linked != GL_TRUE) {
int size;
glGetProgramiv(_prog_id, GL_INFO_LOG_LENGTH, &size);
void * data = malloc(size + 1);
glGetProgramInfoLog(_prog_id, size, &size, (char*)data);
printf("%s\n", data);
free(data);
die("Unable to link program\n");
}
}
GlShader::~GlShader() {
glDeleteProgram(_prog_id);
}
GlShader::GlShader(GlShader && other) {
_prog_id = other._prog_id;
}
void GlShader::bind() {
glUseProgram(_prog_id);
}
void GlShader::uniform(GLint location, GLboolean transpose, const glm::mat4 & mat) {
glProgramUniformMatrix4fv(_prog_id, location, 1, transpose, glm::value_ptr(mat));
}
////////////////////////////////////////////////////////////////////////////////
// GlVAO
////////////////////////////////////////////////////////////////////////////////
template<typename VertexType>
class GlVAO {
public:
typedef std::shared_ptr<GlBuffer<VertexType>> buffer_ref;
typedef std::shared_ptr<const GlBuffer<VertexType>> buffer_const_ref;
private:
buffer_ref _buffer;
GLuint _id;
public:
GlVAO(buffer_ref buffer);
GlVAO(const GlVAO & other) = delete;
GlVAO(GlVAO && other);
GlVAO & operator= (const GlVAO & other) = delete;
GlVAO & operator= (GlVAO && other);
~GlVAO();
void bind_buffer(buffer_const_ref buffer, GLuint buffer_index, GLintptr offset, GLsizei stride);
void bind_attrib(GLuint attrib_index, GLuint buffer_index);
void enable_attrib(GLuint attrib_index);
void attrib_format(GLuint attrib_index, GLint size, GLenum type, GLboolean normalized, GLuint relativeOffset);
void bind() const;
};
template<typename VertexType>
GlVAO<VertexType>::GlVAO(std::shared_ptr<GlBuffer<VertexType>> buffer):
_buffer(buffer) {
glCreateVertexArrays(1, &_id);
}
template<typename V>
GlVAO<V>::~GlVAO() {
glDeleteVertexArrays(1, &_id);
}
template<typename V>
void GlVAO<V>::bind_buffer(buffer_const_ref buffer, GLuint buffer_index, GLintptr offset, GLsizei stride) {
glVertexArrayVertexBuffer(_id, buffer_index, buffer->_ids[buffer->_buffer], offset, stride);
}
template<typename V>
void GlVAO<V>::bind_attrib(GLuint attrib_index, GLuint buffer_index) {
glVertexArrayAttribBinding(_id, attrib_index, buffer_index);
}
template<typename V>
void GlVAO<V>::attrib_format(GLuint attrib_index, GLint size, GLenum type, GLboolean normalized, GLuint relative_offset) {
glVertexArrayAttribFormat(_id, attrib_index, size, type, normalized, relative_offset);
}
template<typename V>
void GlVAO<V>::enable_attrib(GLuint attrib_index) {
glEnableVertexArrayAttrib(_id, attrib_index);
}
template<typename V>
void GlVAO<V>::bind() const {
glBindVertexArray(_id);
}
////////////////////////////////////////////////////////////////////////////////
// Particle system
////////////////////////////////////////////////////////////////////////////////
const char * particle_vert_shader =
"#version 450\n"
"layout(location=0) in vec4 position_life;\n"
"layout(location=1) in vec4 in_color;\n"
"layout(location=0) uniform mat4 transform;\n"
"out float life;\n"
"out vec4 cross_color;\n"
"void main() {"
" gl_Position = transform * vec4(position_life.xyz, 1);\n"
" gl_PointSize = 3.f;\n"
" cross_color = in_color;\n"
" life = position_life.w;\n"
"}\n";
static const char * particle_frag_shader =
"#version 450\n"
"out vec4 color;\n"
"in vec4 cross_color;\n"
"in float life;\n"
"void main() {\n"
" color = cross_color;\n"
"}\n";
struct __attribute__((packed)) particle {
float x, y, z, t;
float vx, vy, vz;
struct color c;
particle(const glm::vec3 & pos, const glm::vec3 & vel, float t, const color & c) {
x = pos.x;
y = pos.y;
z = pos.z;
this->t = t;
vx = vel.x;
vy = vel.y;
vz = vel.z;
this->c = c;
}
};
template<typename UpdateFunc>
class ParticleSystem {
private:
std::shared_ptr<GlBuffer<particle>> _particles;
std::shared_ptr<GlVAO<particle>> _vao;
GlShader _shader;
UpdateFunc _update;
float _max_age;
public:
ParticleSystem(UpdateFunc f, float max_age);
ParticleSystem(const ParticleSystem & other) = delete;
ParticleSystem(const ParticleSystem && other);
ParticleSystem & operator=(ParticleSystem & other) = delete;
ParticleSystem & operator=(ParticleSystem && other);
~ParticleSystem();
void add_particle(const glm::vec3 & pos, const glm::vec3 & vel, float t, const color & c);
void do_update(float dt);
void render(const glm::mat4& t);
};
template<typename F>
ParticleSystem<F>::ParticleSystem(F f, float max_age)
: _particles(new GlBuffer<particle>()),
_vao(new GlVAO<particle>(_particles)),
_shader(std::string(particle_vert_shader), std::string(particle_frag_shader)),
_update(f),
_max_age(max_age)
{
_vao->enable_attrib(POSITION_LOCATION);
_vao->enable_attrib(COLOR_LOCATION);
_vao->bind_attrib(POSITION_LOCATION, 0);
_vao->bind_attrib(COLOR_LOCATION, 0);
_vao->attrib_format(POSITION_LOCATION, 4, GL_FLOAT, GL_FALSE, offsetof(particle, x));
_vao->attrib_format(COLOR_LOCATION, 4, GL_FLOAT, GL_FALSE, offsetof(particle, c));
}
template<typename F>
ParticleSystem<F>::~ParticleSystem() {}
template<typename F>
void ParticleSystem<F>::add_particle(const glm::vec3 & pos, const glm::vec3 & vel, float t, const color & c) {
particle np(pos, vel, t, c);
if (_particles->_storage.size() > 32192) {
int ridx = rand() % 32192;
_particles->_storage[ridx] = np;
} else {
_particles->_storage.push_back(np);
}
}
template<typename F>
void ParticleSystem<F>::do_update(float dt) {
dt /= _max_age;
auto & store = _particles->_storage;
for (auto i = 0ul; i < store.size(); i++) {
_update(store[i], dt);
if (store[i].t > 1.f) {
store[i] = store[store.size() - 1];
store.pop_back();
}
}
}
template<typename F>
void ParticleSystem<F>::render(const glm::mat4 & transform) {
if (_particles->num_elems() == 0) {
return;
}
_vao->bind_buffer(_particles, 0, 0, sizeof(particle));
_particles->sync();
_shader.bind();
_vao->bind();
_shader.uniform(TRANSFORM_LOCATION, GL_TRUE, transform);
glDrawArrays(GL_POINTS, 0, _particles->num_elems());
}
////////////////////////////////////////////////////////////////////////////////
// atlas
////////////////////////////////////////////////////////////////////////////////
struct glyph_render_params{
struct rect uvs;
struct vec2 offset;
};
struct atlas {
GlTexture tex;
FT_Face face;
std::unordered_map<FT_UInt, struct glyph_render_params> uvs;
unsigned int cx, cy, rowmax;
atlas(FT_Face my_face):
tex(),
face(my_face) {
glBindTexture(GL_TEXTURE_2D, tex.GetID());
glTexStorage2D(GL_TEXTURE_2D, 1, GL_R8, ATLAS_SIZE, ATLAS_SIZE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
cx = 0;
cy = 0;
rowmax = 0;
}
void glyph_render_params(FT_UInt glyph, struct glyph_render_params & out) {
auto kv = uvs.find(glyph);
if (kv != uvs.end()) {
out = kv->second;
return;
}
// render the glyph
int error;
if ((error = FT_Load_Glyph(face, glyph, FT_LOAD_DEFAULT)) != 0) {
die("Failed to load glyph %d %d\n", glyph, error);
}
if ((error = FT_Render_Glyph(face->glyph, FT_RENDER_MODE_NORMAL)) != 0) {
die("Failed to render glyph %d %d\n", glyph, error);
}
FT_Bitmap * bm = &face->glyph->bitmap;
if (cx + bm->width >= ATLAS_SIZE) {
cx = 0;
cy += rowmax + 1;
if (cy >= ATLAS_SIZE) {
die("Ran out of space in atlas!\n");
}
rowmax = 0;
}
if (bm->rows > rowmax) {
rowmax = bm->rows;
}
out.offset.x = face->glyph->bitmap_left;
out.offset.y = face->glyph->bitmap_top;
out.uvs.origin.x = cx / (float)ATLAS_SIZE;
out.uvs.origin.y = cy / (float)ATLAS_SIZE;
out.uvs.size.x = bm->width / (float)ATLAS_SIZE;
out.uvs.size.y = bm->rows / (float)ATLAS_SIZE;
glBindTexture(GL_TEXTURE_2D, tex.GetID());
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexSubImage2D(GL_TEXTURE_2D, 0, cx, cy, bm->width, bm->rows,
GL_RED, GL_UNSIGNED_BYTE, bm->buffer);
cx += bm->width + 1;
uvs.insert({glyph, out});
}
void bind_texture(int binding) {
(void) binding;
glBindTexture(GL_TEXTURE_2D, tex.GetID());
}
};
////////////////////////////////////////////////////////////////////////////////
// FontBatch
////////////////////////////////////////////////////////////////////////////////
class FontBatch {
private:
std::shared_ptr<GlBuffer<vertex>> _verts;
std::shared_ptr<GlVAO<vertex>> _vert_vao;
public:
FontBatch();
FontBatch(const FontBatch & other) = delete;
FontBatch(FontBatch && other);
FontBatch & operator=(const FontBatch & other) = delete;
FontBatch & operator=(FontBatch && other);
~FontBatch();
void enqueue_glyph(const glyph_spec * const spec);
void render();
};
FontBatch::FontBatch() :
_verts(new GlBuffer<vertex>()),
_vert_vao(new GlVAO<vertex>(_verts)) {
_vert_vao->enable_attrib(POSITION_LOCATION);
_vert_vao->enable_attrib(UV_LOCATION);
_vert_vao->enable_attrib(COLOR_LOCATION);
_vert_vao->bind_attrib(POSITION_LOCATION, 0);
_vert_vao->bind_attrib(UV_LOCATION, 0);
_vert_vao->bind_attrib(COLOR_LOCATION, 0);
_vert_vao->attrib_format(POSITION_LOCATION, 3, GL_FLOAT, GL_FALSE, offsetof(vertex, pos));
_vert_vao->attrib_format(UV_LOCATION, 2, GL_FLOAT, GL_FALSE, offsetof(vertex, texcoords));
_vert_vao->attrib_format(COLOR_LOCATION, 4, GL_FLOAT, GL_FALSE, offsetof(vertex, c));
}
FontBatch::~FontBatch() {}
void FontBatch::enqueue_glyph(const glyph_spec * const spec) {
struct glyph_render_params rps;
spec->font->glyph_render_params(spec->glyph, rps);
float pixel_width = ATLAS_SIZE * rps.uvs.size.x;
float pixel_height = ATLAS_SIZE * rps.uvs.size.y;
float base_x = rps.offset.x + static_cast<float>(spec->x);
float base_y = -rps.offset.y + static_cast<float>(spec->y);
if (pixel_width < 1 || pixel_height < 1) {
// Don't enqueue tiny shapes
return;
}
vertex lverts[4] = {
{
.pos = {
.x = base_x,
.y = base_y,
.z = 0,
},
.texcoords = rps.uvs.origin,
.c = *spec->c
},
{
.pos = {
.x = base_x + pixel_width,
.y = base_y + pixel_height,
.z = 0,
},
.texcoords = {
.x = rps.uvs.origin.x + rps.uvs.size.x,
.y = rps.uvs.origin.y + rps.uvs.size.y,
},
.c = *spec->c
},
{
.pos = {
.x = base_x,
.y = base_y + pixel_height,
.z = 0,
},
.texcoords = {
.x = rps.uvs.origin.x,
.y = rps.uvs.origin.y + rps.uvs.size.y,
},
.c = *spec->c
},
{
.pos = {
.x = base_x + pixel_width,
.y = base_y,
.z = 0,
},
.texcoords = {
.x = rps.uvs.origin.x + rps.uvs.size.x,
.y = rps.uvs.origin.y,
},
.c = *spec->c
},
};
_verts->push_elements(lverts, 3);
lverts[2] = lverts[3];
_verts->push_elements(lverts, 3);
}
void FontBatch::render() {
_vert_vao->bind_buffer(_verts, 0, 0, sizeof(vertex));
_verts->sync();
_vert_vao->bind();
glDrawArrays(GL_TRIANGLES, 0, _verts->num_elems());
_verts->clear();
}
////////////////////////////////////////////////////////////////////////////////
// FBBlitJob
////////////////////////////////////////////////////////////////////////////////
class FBBlitJob {
private:
std::shared_ptr<GlShader> _shader;
std::shared_ptr<GlBuffer<vertex>> _verts;
std::shared_ptr<GlVAO<vertex>> _vert_vao;
public:
FBBlitJob(std::shared_ptr<GlShader> shader);
FBBlitJob(const FBBlitJob & other) = delete;
FBBlitJob(const FBBlitJob && other);
FBBlitJob& operator=(const FBBlitJob & other) = delete;
FBBlitJob& operator=(const FBBlitJob && other) = delete;
~FBBlitJob();
void do_blit(GLuint backing_texture);
};
FBBlitJob::FBBlitJob(std::shared_ptr<GlShader> shader)
: _shader(shader),
_verts(new GlBuffer<vertex>()),
_vert_vao(new GlVAO<vertex>(_verts))
{
_vert_vao->enable_attrib(POSITION_LOCATION);
_vert_vao->enable_attrib(UV_LOCATION);
_vert_vao->enable_attrib(COLOR_LOCATION);
_vert_vao->bind_attrib(POSITION_LOCATION, 0);
_vert_vao->bind_attrib(UV_LOCATION, 0);
_vert_vao->bind_attrib(COLOR_LOCATION, 0);
_vert_vao->attrib_format(POSITION_LOCATION, 3, GL_FLOAT, GL_FALSE, offsetof(vertex, pos));
_vert_vao->attrib_format(UV_LOCATION, 2, GL_FLOAT, GL_FALSE, offsetof(vertex, texcoords));
_vert_vao->attrib_format(COLOR_LOCATION, 4, GL_FLOAT, GL_FALSE, offsetof(vertex, c));
vertex lverts[4] = {
{
.pos = {.x = -1.f, .y = -1.f, .z = 0.f },
.texcoords = {.x = 0.f, .y = 0.f },
.c = { .r = 1.f, .g = 1.f, .b = 1.f }
},
{
.pos = {.x = 1.f, .y = 1.f, .z = 0.f },
.texcoords = {.x = 1.f, .y = 1.f },
.c = { .r = 1.f, .g = 1.f, .b = 1.f }
},
{
.pos = {.x = -1.f, .y = 1.f, .z = 0.f },
.texcoords = {.x = 0.f, .y = 1.f },
.c = { .r = 1.f, .g = 1.f, .b = 1.f }
},
{
.pos = {.x = 1.f, .y = -1.f, .z = 0.f },
.texcoords = {.x = 1.f, .y = 0.f },
.c = { .r = 1.f, .g = 1.f, .b = 1.f }
},
};
_verts->push_elements(lverts, 3);
lverts[2] = lverts[3];
_verts->push_elements(lverts, 3);
_verts->sync();
}
FBBlitJob::~FBBlitJob() {
}
void FBBlitJob::do_blit(GLuint backing_texture) {
_vert_vao->bind_buffer(_verts, 0, 0, sizeof(vertex));
glBindTextureUnit(TEXTURE_BINDING, backing_texture);
_shader->bind();
auto transform = glm::mat4(1.f);
_shader->uniform(TRANSFORM_LOCATION, GL_TRUE, transform);
_vert_vao->bind();
glDrawArrays(GL_TRIANGLES, 0, _verts->num_elems());
}
////////////////////////////////////////////////////////////////////////////////
// RectJob
////////////////////////////////////////////////////////////////////////////////
class RectJob {
private:
std::shared_ptr<GlShader> _shader;
std::shared_ptr<GlBuffer<vertex>> _verts;
std::shared_ptr<GlVAO<vertex>> _vert_vao;
public:
RectJob(std::shared_ptr<GlShader> shader);
RectJob(const RectJob & other) = delete;
RectJob(const RectJob && other);
RectJob& operator=(const RectJob & other) = delete;
RectJob& operator=(const RectJob && other) = delete;
~RectJob();
void draw_rect(const struct color * const c, int x, int y, int w, int h);
void render(const glm::mat4 & transform);
};
RectJob::RectJob(std::shared_ptr<GlShader> shader)
: _shader(shader),
_verts(new GlBuffer<vertex>()),
_vert_vao(new GlVAO<vertex>(_verts))
{
_vert_vao->enable_attrib(POSITION_LOCATION);
_vert_vao->enable_attrib(UV_LOCATION);
_vert_vao->enable_attrib(COLOR_LOCATION);
_vert_vao->bind_attrib(POSITION_LOCATION, 0);
_vert_vao->bind_attrib(UV_LOCATION, 0);
_vert_vao->bind_attrib(COLOR_LOCATION, 0);
_vert_vao->attrib_format(POSITION_LOCATION, 3, GL_FLOAT, GL_FALSE, offsetof(vertex, pos));
_vert_vao->attrib_format(UV_LOCATION, 2, GL_FLOAT, GL_FALSE, offsetof(vertex, texcoords));
_vert_vao->attrib_format(COLOR_LOCATION, 4, GL_FLOAT, GL_FALSE, offsetof(vertex, c));
}
RectJob::~RectJob() {
}
void RectJob::render(const glm::mat4 & transform) {
_vert_vao->bind_buffer(_verts, 0, 0, sizeof(vertex));
_verts->sync();
_shader->bind();
_shader->uniform(TRANSFORM_LOCATION, GL_TRUE, transform);
_vert_vao->bind();
glDrawArrays(GL_TRIANGLES, 0, _verts->num_elems());
_verts->clear();
}
void RectJob::draw_rect(const struct color * const c, int xi, int yi, int w, int h) {
if (w == 0 || h == 0) {
// Don't enqueue empty rectangles
return;
}
float x = static_cast<float>(xi);
float y = static_cast<float>(yi);
vertex lverts[4] = {
{
.pos = { .x = x, .y = y, .z = 0 },
.texcoords = { .x = 0, .y = 0 },
.c = *c
},
{
.pos = { .x = x + w, .y = y + h, .z = 0},
.texcoords = { .x = 1, .y = 1 },
.c = *c
},
{
.pos = { .x = x, .y = y + h, .z = 0 },
.texcoords = { .x = 0, .y = 1 },
.c = *c
},
{
.pos = { .x = x + w, .y = y, .z = 0 },
.texcoords = { .x = 1, .y = 0 },
.c = *c
},
};
_verts->push_elements(lverts, 3);
lverts[2] = lverts[3];
_verts->push_elements(lverts, 3);
}
////////////////////////////////////////////////////////////////////////////////
// render_context
////////////////////////////////////////////////////////////////////////////////
void flame_curve(float f, color & c) {
// t0 = 1
// t1 = f
float t2 = 2 * f * f - 1;
float t3 = f * (2 * t2 - 1); // 2 f t2 - t1
float t4 = 2 * f * t3 - t2;
const float rc[] = { -7.42828172, 13.49066809, -9.02608052, 3.68452288, -0.71117265 };
const float gc[] = { -4.56657944, 8.57004895, -6.62752371, 3.5393551, -0.99186063 };
const float bc[] = { 8.60556488, -14.57317637, 8.7124878, -3.42279269, 0.7211981 };
c.r = rc[0] + f * rc[1] + t2 * rc[2] + t3 * rc[3] + t4 * rc[4];
c.g = gc[0] + f * gc[1] + t2 * gc[2] + t3 * gc[3] + t4 * gc[4];
c.b = bc[0] + f * bc[1] + t2 * bc[2] + t3 * bc[3] + t4 * bc[4];
c.a = 1.f - f;
}
void basic_update(particle & p, float dt) {
p.t += dt;
p.x += dt * p.vx;
p.y += dt * p.vy;
p.vy += 100.f * dt;
flame_curve(p.t, p.c);
}
struct render_context {
GlShader _shader;
color _clear_color;
std::unique_ptr<GlFrameBuffer> _fb;
std::unique_ptr<GlFrameBuffer> _particle_fb;
std::unordered_map<struct atlas*, FontBatch> _text_batches;
int _win_w;
int _win_h;
ParticleSystem<std::function<void(particle&, float)>> _parts;