-
-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathpgl_ext.c
More file actions
1304 lines (1095 loc) · 37.8 KB
/
pgl_ext.c
File metadata and controls
1304 lines (1095 loc) · 37.8 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
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
//Raw draw functions that bypass the OpenGL pipeline and draw
//points/lines/triangles directly to the framebuffer, modify as needed.
//
//Example modifications:
//add the blending part of OpenGL to put_pixel
//change them to take vec4's instead of Color's
//change put_triangle to draw all one color or have a separate path/function
//that draws a single color triangle faster (no need to blend)
//
//pass the framebuffer in instead of drawing to c->back_buffer so
//you can use it elsewhere, independently of a glContext
//etc.
//
PGLDEF void pglClearScreen(void)
{
memset(c->back_buffer.buf, 255, c->back_buffer.w * c->back_buffer.h * sizeof(pix_t));
}
PGLDEF void pglSetInterp(GLsizei n, GLenum* interpolation)
{
c->programs.a[c->cur_program].vs_output_size = n;
c->vs_output.size = n;
memcpy(c->programs.a[c->cur_program].interpolation, interpolation, n*sizeof(GLenum));
// c->vs_output.output_buf was pre-allocated to max size needed in init_glContext
// otherwise would need to assure it's at least
// c->vs_output_size * PGL_MAX_VERTS * sizeof(float) right here
//vs_output.interpolation would be already pointing at current program's array
//unless the programs array was realloced since the last glUseProgram because
//they've created a bunch of programs. Unlikely they'd be changing a shader
//before creating all their shaders but whatever.
c->vs_output.interpolation = c->programs.a[c->cur_program].interpolation;
}
// Uses default_vs for vertex shader (passes vertex unchanged, no other attributes or outputs)
// This function is designed to be used with pglDrawFrame(), you don't need it for pglDrawFrame2()
PGLDEF GLuint pglCreateFragProgram(frag_func fragment_shader, GLboolean fragdepth_or_discard)
{
// Using glAttachShader error if shader is not a shader object which
// is the closest analog
PGL_ERR_RET_VAL((!fragment_shader), GL_INVALID_OPERATION, 0);
glProgram tmp = {default_vs, fragment_shader, NULL, 0, {0}, fragdepth_or_discard, GL_FALSE };
for (int i=1; i<c->programs.size; ++i) {
if (c->programs.a[i].deleted && (GLuint)i != c->cur_program) {
c->programs.a[i] = tmp;
return i;
}
}
cvec_push_glProgram(&c->programs, tmp);
return c->programs.size-1;
}
//TODO
//pglDrawRect(x, y, w, h)
//pglDrawPoint(x, y)
//
// TODO worth another draw_pixel() that never does fragment_processing?
// worth duplicating the loops for initial discard check?
PGLDEF void pglDrawFrame(void)
{
frag_func frag_shader = c->programs.a[c->cur_program].fragment_shader;
void* uniforms = c->programs.a[c->cur_program].uniform;
Shader_Builtins builtins;
//#pragma omp parallel for private(builtins)
for (int y=0; y<c->back_buffer.h; ++y) {
for (int x=0; x<c->back_buffer.w; ++x) {
//ignore z and w components
builtins.gl_FragCoord.x = x + 0.5f;
builtins.gl_FragCoord.y = y + 0.5f;
builtins.discard = GL_FALSE;
frag_shader(NULL, &builtins, uniforms);
if (!builtins.discard)
draw_pixel(builtins.gl_FragColor, x, y, 0.0f, GL_FALSE); //scissor/stencil/depth aren't used for pglDrawFrame
}
}
}
PGLDEF void pglDrawFrame2(frag_func frag_shader, void* uniforms)
{
Shader_Builtins builtins;
//#pragma omp parallel for private(builtins)
for (int y=0; y<c->back_buffer.h; ++y) {
for (int x=0; x<c->back_buffer.w; ++x) {
//ignore z and w components
builtins.gl_FragCoord.x = x + 0.5f;
builtins.gl_FragCoord.y = y + 0.5f;
builtins.discard = GL_FALSE;
frag_shader(NULL, &builtins, uniforms);
if (!builtins.discard)
draw_pixel(builtins.gl_FragColor, x, y, 0.0f, GL_FALSE); //scissor/stencil/depth aren't used for pglDrawFrame
}
}
}
PGLDEF void pglBufferData(GLenum target, GLsizei size, const GLvoid* data, GLenum usage)
{
//TODO check for usage later
PGL_UNUSED(usage);
PGL_ERR((target != GL_ARRAY_BUFFER && target != GL_ELEMENT_ARRAY_BUFFER), GL_INVALID_ENUM);
target -= GL_ARRAY_BUFFER;
PGL_ERR(!c->bound_buffers[target], GL_INVALID_OPERATION);
// data can't be null for user_owned data
PGL_ERR(!data, GL_INVALID_VALUE);
// TODO Should I change this in spec functions too? Or just say don't mix them
// otherwise bad things/undefined behavior??
if (!c->buffers.a[c->bound_buffers[target]].user_owned) {
free(c->buffers.a[c->bound_buffers[target]].data);
}
// user_owned buffer, just assign the pointer, will not free
c->buffers.a[c->bound_buffers[target]].data = (u8*)data;
c->buffers.a[c->bound_buffers[target]].user_owned = GL_TRUE;
c->buffers.a[c->bound_buffers[target]].size = size;
if (target == GL_ELEMENT_ARRAY_BUFFER) {
c->vertex_arrays.a[c->cur_vertex_array].element_buffer = c->bound_buffers[target];
}
}
// TODO/NOTE
// All pglTexImage* functions expect the user to pass in packed GL_RGBA
// data. Unlike glTexImage*, no conversion is done, and format != GL_RGBA
// is an INVALID_ENUM error
//
// At least the latter part will change if I ever expand internal format
// support
PGLDEF void pglTexImage1D(GLenum target, GLint level, GLint internalformat, GLsizei width, GLint border, GLenum format, GLenum type, const GLvoid* data)
{
PGL_ERR(target != GL_TEXTURE_1D, GL_INVALID_ENUM);
GLuint cur_tex = c->bound_textures[target-GL_TEXTURE_UNBOUND-1];
pglTextureImage1D(cur_tex, level, internalformat, width, border, format, type, data);
}
PGLDEF void pglTexImage2D(GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid* data)
{
// NOTE, since this is mapping data, the entire cubemap has to already be arranged in memory in the correct order and we only
// accept GL_TEXTURE_CUBE_MAP, not any of the individual planes as that wouldn't make sense
PGL_ERR((target != GL_TEXTURE_2D &&
target != GL_TEXTURE_RECTANGLE &&
target != GL_TEXTURE_CUBE_MAP), GL_INVALID_ENUM);
GLuint cur_tex = c->bound_textures[target-GL_TEXTURE_UNBOUND-1];
pglTextureImage2D(cur_tex, level, internalformat, width, height, border, format, type, data);
}
PGLDEF void pglTexImage3D(GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLenum format, GLenum type, const GLvoid* data)
{
PGL_ERR((target != GL_TEXTURE_3D && target != GL_TEXTURE_2D_ARRAY), GL_INVALID_ENUM);
GLuint cur_tex = c->bound_textures[target-GL_TEXTURE_UNBOUND-1];
pglTextureImage3D(cur_tex, level, internalformat, width, height, depth, border, format, type, data);
}
PGLDEF void pglTextureImage1D(GLuint texture, GLint level, GLint internalformat, GLsizei width, GLint border, GLenum format, GLenum type, const GLvoid* data)
{
// ignore level and internalformat for now
// (the latter is always converted to RGBA32 anyway)
PGL_UNUSED(level);
PGL_UNUSED(internalformat);
PGL_ERR(border, GL_INVALID_VALUE);
PGL_ERR(type != GL_UNSIGNED_BYTE, GL_INVALID_ENUM);
PGL_ERR(format != GL_RGBA, GL_INVALID_ENUM);
// data can't be null for user_owned data
PGL_ERR(!data, GL_INVALID_VALUE);
// I do not support DSA mapping of default texture 0, for convenience, no way to know which target it was
// and I don't want to duplicate code or add extra functions
PGL_ERR((!texture || texture >= c->textures.size || c->textures.a[texture].deleted), GL_INVALID_OPERATION);
c->textures.a[texture].w = width;
c->textures.a[texture].h = 1;
c->textures.a[texture].d = 1;
// TODO see pglBufferData
if (!c->textures.a[texture].user_owned)
free(c->textures.a[texture].data);
//TODO support other internal formats? components should be of internalformat not format
c->textures.a[texture].data = (u8*)data;
c->textures.a[texture].user_owned = GL_TRUE;
}
PGLDEF void pglTextureImage2D(GLuint texture, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid* data)
{
PGL_UNUSED(level);
PGL_UNUSED(internalformat);
PGL_ERR(border, GL_INVALID_VALUE);
PGL_ERR(type != GL_UNSIGNED_BYTE, GL_INVALID_ENUM);
PGL_ERR(format != GL_RGBA, GL_INVALID_ENUM);
// data can't be null for user_owned data
PGL_ERR(!data, GL_INVALID_VALUE);
PGL_ERR((!texture || texture >= c->textures.size || c->textures.a[texture].deleted), GL_INVALID_OPERATION);
// have to convert type back from offset to actual enum value
GLenum target = c->textures.a[texture].type + GL_TEXTURE_UNBOUND + 1;
if (target == GL_TEXTURE_2D || target == GL_TEXTURE_RECTANGLE) {
c->textures.a[texture].w = width;
c->textures.a[texture].h = height;
c->textures.a[texture].d = 1;
// TODO see pglBufferData
if (!c->textures.a[texture].user_owned)
free(c->textures.a[texture].data);
// If you're using these pgl mapped functions, it assumes you are respecting
// your own current unpack alignment settings already
c->textures.a[texture].data = (u8*)data;
c->textures.a[texture].user_owned = GL_TRUE;
} else { //CUBE_MAP
// We only accept all the data already arranged, since we're mapping,
// no individual planes/copying
// TODO see pglBufferData
if (!c->textures.a[texture].user_owned)
free(c->textures.a[texture].data);
//TODO spec says INVALID_VALUE, man pages say INVALID_ENUM ?
PGL_ERR(width != height, GL_INVALID_VALUE);
c->textures.a[texture].w = width;
c->textures.a[texture].h = height;
c->textures.a[texture].d = 1;
c->textures.a[texture].data = (u8*)data;
c->textures.a[texture].user_owned = GL_TRUE;
} //end CUBE_MAP
}
PGLDEF void pglTextureImage3D(GLuint texture, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLenum format, GLenum type, const GLvoid* data)
{
PGL_UNUSED(level);
PGL_UNUSED(internalformat);
PGL_ERR(border, GL_INVALID_VALUE);
PGL_ERR(type != GL_UNSIGNED_BYTE, GL_INVALID_ENUM);
PGL_ERR(format != GL_RGBA, GL_INVALID_ENUM);
// data can't be null for user_owned data
PGL_ERR(!data, GL_INVALID_VALUE);
PGL_ERR((!texture || texture >= c->textures.size || c->textures.a[texture].deleted), GL_INVALID_OPERATION);
c->textures.a[texture].w = width;
c->textures.a[texture].h = height;
c->textures.a[texture].d = depth;
// TODO see pglBufferData
if (!c->textures.a[texture].user_owned)
free(c->textures.a[texture].data);
c->textures.a[texture].data = (u8*)data;
c->textures.a[texture].user_owned = GL_TRUE;
}
PGLDEF void pglGetBufferData(GLuint buffer, GLvoid** data)
{
// why'd you even call it?
PGL_ERR(!data, GL_INVALID_VALUE);
// matching error code of binding invalid buffecr
PGL_ERR((!buffer || buffer >= c->buffers.size || c->buffers.a[buffer].deleted),
GL_INVALID_OPERATION);
*data = c->buffers.a[buffer].data;
}
PGLDEF void pglGetTextureData(GLuint texture, GLvoid** data)
{
// why'd you even call it?
PGL_ERR(!data, GL_INVALID_VALUE);
// TODO texture 0?
PGL_ERR((texture >= c->textures.size || c->textures.a[texture].deleted), GL_INVALID_OPERATION);
*data = c->textures.a[texture].data;
}
PGLDEF const glTexture* pglGetTexture(GLuint texture)
{
// TODO texture 0?
PGL_ERR_RET_VAL((texture >= c->textures.size || c->textures.a[texture].deleted), GL_INVALID_OPERATION, NULL);
return &c->textures.a[texture];
}
// TODO hmm, void*, or u8*, or GLvoid*?
GLvoid* pglGetBackBuffer(void)
{
return c->back_buffer.buf;
}
PGLDEF void pglSetBackBuffer(GLvoid* backbuf, GLsizei w, GLsizei h, GLboolean user_owned)
{
c->back_buffer.w = w;
c->back_buffer.h = h;
c->back_buffer.buf = (u8*)backbuf;
c->back_buffer.lastrow = c->back_buffer.buf + (h-1)*w*sizeof(pix_t);
c->user_alloced_backbuf = user_owned;
}
PGLDEF void pglSetTexBackBuffer(GLuint texture)
{
// NOTE, I do not support texture 0
PGL_ERR((!texture || texture >= c->textures.size || c->textures.a[texture].deleted ||
c->textures.a[texture].type+GL_TEXTURE_UNBOUND+1 != GL_TEXTURE_2D), GL_INVALID_OPERATION);
glTexture* t = &c->textures.a[texture];
pglSetBackBuffer((GLvoid*)t->data, t->w, t->h, t->user_owned);
}
// Not sure where else to put these two functions, they're helper/stopgap
// measures to deal with PGL only supporting RGBA but they're
// also useful functions on their own and not really "extensions"
// so I don't feel right putting them here or giving them a pgl prefix.
//
// Takes an image with GL_UNSIGNED_BYTE channels in
// a format other than packed GL_RGBA and returns it in (tightly packed) GL_RGBA
// (with the same rules as GLSL texture access for filling the other channels).
// See section 3.6.2 page 65 of the OpenGL ES 2.0.25 spec pdf
//
// IOW this creates an image that will give you the same values in the
// shader that you would have gotten had you used the unsupported
// format. Passing in a GL_RGBA where pitch == w*4 reduces to a single memcpy
//
// If output is NULL, it will allocate the output image for you
// pitch is the length of a row in bytes.
//
// Returns the resulting packed RGBA image
PGLDEF u8* convert_format_to_packed_rgba(u8* output, u8* input, int w, int h, int pitch, GLenum format)
{
int i, j, size = w*h;
int rb = pitch;
u8* out = output;
if (!out) {
out = (u8*)PGL_MALLOC(size*4);
}
memset(out, 0, size*4);
u8* p = out;
if (format == PGL_ONE_ALPHA) {
for (i=0; i<h; ++i) {
for (j=0; j<w; ++j, p+=4) {
p[0] = UINT8_MAX;
p[1] = UINT8_MAX;
p[2] = UINT8_MAX;
p[3] = input[i*rb+j];
}
}
} else if (format == GL_ALPHA) {
for (i=0; i<h; ++i) {
for (j=0; j<w; ++j, p+=4) {
p[3] = input[i*rb+j];
}
}
} else if (format == GL_LUMINANCE) {
for (i=0; i<h; ++i) {
for (j=0; j<w; ++j, p+=4) {
p[0] = input[i*rb+j];
p[1] = input[i*rb+j];
p[2] = input[i*rb+j];
p[3] = UINT8_MAX;
}
}
} else if (format == GL_RED) {
for (i=0; i<h; ++i) {
for (j=0; j<w; ++j, p+=4) {
p[0] = input[i*rb+j];
p[3] = UINT8_MAX;
}
}
} else if (format == GL_LUMINANCE_ALPHA) {
for (i=0; i<h; ++i) {
for (j=0; j<w; ++j, p+=4) {
p[0] = input[i*rb+j*2];
p[1] = input[i*rb+j*2];
p[2] = input[i*rb+j*2];
p[3] = input[i*rb+j*2+1];
}
}
} else if (format == GL_RG) {
for (i=0; i<h; ++i) {
for (j=0; j<w; ++j, p+=4) {
p[0] = input[i*rb+j*2];
p[1] = input[i*rb+j*2+1];
p[3] = UINT8_MAX;
}
}
} else if (format == GL_RGB) {
for (i=0; i<h; ++i) {
for (j=0; j<w; ++j, p+=4) {
p[0] = input[i*rb+j*3];
p[1] = input[i*rb+j*3+1];
p[2] = input[i*rb+j*3+2];
p[3] = UINT8_MAX;
}
}
} else if (format == GL_BGR) {
for (i=0; i<h; ++i) {
for (j=0; j<w; ++j, p+=4) {
p[0] = input[i*rb+j*3+2];
p[1] = input[i*rb+j*3+1];
p[2] = input[i*rb+j*3];
p[3] = UINT8_MAX;
}
}
} else if (format == GL_BGRA) {
for (i=0; i<h; ++i) {
for (j=0; j<w; ++j, p+=4) {
p[0] = input[i*rb+j*4+2];
p[1] = input[i*rb+j*4+1];
p[2] = input[i*rb+j*4];
p[3] = input[i*rb+j*4+3];
}
}
} else if (format == GL_RGBA) {
if (pitch == w*4) {
// Just a plain copy
memcpy(out, input, w*h*4);
} else {
// get rid of row padding
int bw = w*4;
for (i=0; i<h; ++i) {
memcpy(&out[i*bw], &input[i*rb], bw);
}
}
} else {
puts("Unrecognized or unsupported input format!");
free(out);
out = NULL;
}
return out;
}
// pass in packed single channel 8 bit image where background=0, foreground=255
// and get a packed 4-channel rgba image using the colors provided
PGLDEF u8* convert_grayscale_to_rgba(u8* input, int size, u32 bg_rgba, u32 text_rgba)
{
float rb, gb, bb, ab, rt, gt, bt, at;
u8* tmp = (u8*)&bg_rgba;
rb = tmp[0];
gb = tmp[1];
bb = tmp[2];
ab = tmp[3];
tmp = (u8*)&text_rgba;
rt = tmp[0];
gt = tmp[1];
bt = tmp[2];
at = tmp[3];
//printf("background = (%f, %f, %f, %f)\ntext = (%f, %f, %f, %f)\n", rb, gb, bb, ab, rt, gt, bt, at);
u8* color_image = (u8*)PGL_MALLOC(size * 4);
float t;
for (int i=0; i<size; ++i) {
t = (input[i] - 0) / 255.0;
color_image[i*4] = rt * t + rb * (1 - t);
color_image[i*4+1] = gt * t + gb * (1 - t);
color_image[i*4+2] = bt * t + bb * (1 - t);
color_image[i*4+3] = at * t + ab * (1 - t);
}
return color_image;
}
// Just a convenience to have default textures return a specific color, like white here,
// so a textured shading algorithm will look untextured if you bind texture 0
PGLDEF int setup_default_textures(void)
{
// just 1 white pixel
// Could make it static and map it so we don't have a 12 tiny allocations
GLuint image[1] = {
0xFFFFFFFF
};
int w = 1;
int h = 1;
int d = 1;
int frames = 1;
// If this was called in init_glContext() or immediately after we would know
// 0 was already bound
glBindTexture(GL_TEXTURE_1D, 0);
glTexImage1D(GL_TEXTURE_1D, 0, GL_COMPRESSED_RGBA, w, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
glBindTexture(GL_TEXTURE_2D, 0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_COMPRESSED_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
glBindTexture(GL_TEXTURE_3D, 0);
glTexImage3D(GL_TEXTURE_3D, 0, GL_COMPRESSED_RGBA, w, h, d, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
glBindTexture(GL_TEXTURE_1D_ARRAY, 0);
glTexImage2D(GL_TEXTURE_1D_ARRAY, 0, GL_COMPRESSED_RGBA, w, frames, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
glBindTexture(GL_TEXTURE_2D_ARRAY, 0);
glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_COMPRESSED_RGBA, w, h, frames, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
glBindTexture(GL_TEXTURE_RECTANGLE, 0);
glTexImage2D(GL_TEXTURE_RECTANGLE, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
GLenum cube[6] =
{
GL_TEXTURE_CUBE_MAP_POSITIVE_X,
GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
};
for (int i=0; i<6; i++) {
glTexImage2D(cube[i], 0, GL_COMPRESSED_RGBA, w, h, 0,
GL_RGBA, GL_UNSIGNED_BYTE, image);
}
return GL_TRUE;
}
PGLDEF void put_pixel(Color color, int x, int y)
{
//u32* dest = &((u32*)c->back_buffer.lastrow)[-y*c->back_buffer.w + x];
pix_t* dest = &((pix_t*)c->back_buffer.buf)[y*c->back_buffer.w + x];
//*dest = (u32)color.a << PGL_ASHIFT | (u32)color.r << PGL_RSHIFT | (u32)color.g << PGL_GSHIFT | (u32)color.b << PGL_BSHIFT;
*dest = RGBA_TO_PIXEL(color.r, color.g, color.b, color.a);
}
PGLDEF void put_pixel_blend(vec4 src, int x, int y)
{
//u32* dest = &((u32*)c->back_buffer.lastrow)[-y*c->back_buffer.w + x];
u32* dest = &((u32*)c->back_buffer.buf)[y*c->back_buffer.w + x];
//Color dest_color = make_Color((*dest & PGL_RMASK) >> PGL_RSHIFT, (*dest & PGL_GMASK) >> PGL_GSHIFT, (*dest & PGL_BMASK) >> PGL_BSHIFT, (*dest & PGL_AMASK) >> PGL_ASHIFT);
Color dest_color = PIXEL_TO_COLOR(*dest);
vec4 dst = Color_to_v4(dest_color);
// standard alpha blending xyzw = rgba
vec4 final;
final.x = src.x * src.w + dst.x * (1.0f - src.w);
final.y = src.y * src.w + dst.y * (1.0f - src.w);
final.z = src.z * src.w + dst.z * (1.0f - src.w);
final.w = src.w + dst.w * (1.0f - src.w);
Color color = v4_to_Color(final);
//*dest = (u32)color.a << PGL_ASHIFT | (u32)color.r << PGL_RSHIFT | (u32)color.g << PGL_GSHIFT | (u32)color.b << PGL_BSHIFT;
*dest = RGBA_TO_PIXEL(color.r, color.g, color.b, color.a);
}
PGLDEF void put_wide_line_simple(Color the_color, float width, float x1, float y1, float x2, float y2)
{
float tmp;
//always draw from left to right
if (x2 < x1) {
tmp = x1;
x1 = x2;
x2 = tmp;
tmp = y1;
y1 = y2;
y2 = tmp;
}
//calculate slope and implicit line parameters once
float m = (y2-y1)/(x2-x1);
Line line = make_Line(x1, y1, x2, y2);
vec2 ab = make_v2(line.A, line.B);
normalize_v2(&ab);
int x, y;
float x_min = MAX(0, MIN(x1, x2));
float x_max = MIN(c->back_buffer.w-1, MAX(x1, x2));
float y_min = MAX(0, MIN(y1, y2));
float y_max = MIN(c->back_buffer.h-1, MAX(y1, y2));
//4 cases based on slope
if (m <= -1) { //(-infinite, -1]
x = x1;
for (y=y_max; y>=y_min; --y) {
for (float j=x-width/2; j<x+width/2; j++) {
put_pixel(the_color, j, y);
}
if (line_func(&line, x+0.5f, y-1) < 0)
x++;
}
} else if (m <= 0) { //(-1, 0]
y = y1;
for (x=x_min; x<=x_max; ++x) {
for (float j=y-width/2; j<y+width/2; j++) {
put_pixel(the_color, x, j);
}
if (line_func(&line, x+1, y-0.5f) > 0)
y--;
}
} else if (m <= 1) { //(0, 1]
y = y1;
for (x=x_min; x<=x_max; ++x) {
for (float j=y-width/2; j<y+width/2; j++) {
put_pixel(the_color, x, j);
}
//put_pixel(the_color, x, y);
if (line_func(&line, x+1, y+0.5f) < 0)
y++;
}
} else { //(1, +infinite)
x = x1;
for (y=y_min; y<=y_max; ++y) {
for (float j=x-width/2; j<x+width/2; j++) {
put_pixel(the_color, j, y);
}
if (line_func(&line, x+0.5f, y+1) > 0)
x++;
}
}
}
PGLDEF void put_wide_line(Color color1, Color color2, float width, float x1, float y1, float x2, float y2)
{
vec2 a = { x1, y1 };
vec2 b = { x2, y2 };
vec2 tmp;
Color tmpc;
if (x2 < x1) {
tmp = a;
a = b;
b = tmp;
tmpc = color1;
color1 = color2;
color2 = tmpc;
}
vec4 c1 = Color_to_v4(color1);
vec4 c2 = Color_to_v4(color2);
// need half the width to calculate
width /= 2.0f;
float m = (y2-y1)/(x2-x1);
Line line = make_Line(x1, y1, x2, y2);
normalize_line(&line);
vec2 c;
vec2 ab = sub_v2s(b, a);
vec2 ac;
float dot_abab = dot_v2s(ab, ab);
float x_min = floor(a.x - width) + 0.5f;
float x_max = floor(b.x + width) + 0.5f;
float y_min, y_max;
if (m <= 0) {
y_min = floor(b.y - width) + 0.5f;
y_max = floor(a.y + width) + 0.5f;
} else {
y_min = floor(a.y - width) + 0.5f;
y_max = floor(b.y + width) + 0.5f;
}
float x, y, e, dist, t;
float w2 = width*width;
//int last = 1;
Color out_c;
for (y = y_min; y <= y_max; ++y) {
c.y = y;
for (x = x_min; x <= x_max; x++) {
// TODO optimize
c.x = x;
ac = sub_v2s(c, a);
e = dot_v2s(ac, ab);
// c lies past the ends of the segment ab
if (e <= 0.0f || e >= dot_abab) {
continue;
}
// can do this because we normalized the line equation
// TODO square or fabsf?
dist = line_func(&line, c.x, c.y);
if (dist*dist < w2) {
t = e / dot_abab;
out_c = v4_to_Color(mixf_v4(c1, c2, t));
put_pixel(out_c, x, y);
}
}
}
}
//Should I have it take a glFramebuffer as paramater?
PGLDEF void put_line(Color the_color, float x1, float y1, float x2, float y2)
{
float tmp;
//always draw from left to right
if (x2 < x1) {
tmp = x1;
x1 = x2;
x2 = tmp;
tmp = y1;
y1 = y2;
y2 = tmp;
}
//calculate slope and implicit line parameters once
float m = (y2-y1)/(x2-x1);
Line line = make_Line(x1, y1, x2, y2);
int x, y;
float x_min = MAX(0, MIN(x1, x2));
float x_max = MIN(c->back_buffer.w-1, MAX(x1, x2));
float y_min = MAX(0, MIN(y1, y2));
float y_max = MIN(c->back_buffer.h-1, MAX(y1, y2));
x_min = floorf(x_min) + 0.5f;
x_max = floorf(x_max) + 0.5f;
y_min = floorf(y_min) + 0.5f;
y_max = floorf(y_max) + 0.5f;
//4 cases based on slope
if (m <= -1) { //(-infinite, -1]
x = x_min;
for (y=y_max; y>=y_min; --y) {
put_pixel(the_color, x, y);
if (line_func(&line, x+0.5f, y-1) < 0)
x++;
}
} else if (m <= 0) { //(-1, 0]
y = y_max;
for (x=x_min; x<=x_max; ++x) {
put_pixel(the_color, x, y);
if (line_func(&line, x+1, y-0.5f) > 0)
y--;
}
} else if (m <= 1) { //(0, 1]
y = y_min;
for (x=x_min; x<=x_max; ++x) {
put_pixel(the_color, x, y);
if (line_func(&line, x+1, y+0.5f) < 0)
y++;
}
} else { //(1, +infinite)
x = x_min;
for (y=y_min; y<=y_max; ++y) {
put_pixel(the_color, x, y);
if (line_func(&line, x+0.5f, y+1) > 0)
x++;
}
}
}
// can't think of a better/cleaner way to do this than these lines
#define CLIP_TRIANGLE() \
do { \
x_min = MIN(p1.x, p2.x); \
x_max = MAX(p1.x, p2.x); \
y_min = MIN(p1.y, p2.y); \
y_max = MAX(p1.y, p2.y); \
\
x_min = MIN(p3.x, x_min); \
x_max = MAX(p3.x, x_max); \
y_min = MIN(p3.y, y_min); \
y_max = MAX(p3.y, y_max); \
\
x_min = MAX(c->lx, x_min); \
x_max = MIN(c->ux, x_max); \
y_min = MAX(c->ly, y_min); \
y_max = MIN(c->uy, y_max); \
} while (0)
#define MAKE_IMPLICIT_LINES() \
do { \
l12 = make_Line(p1.x, p1.y, p2.x, p2.y); \
l23 = make_Line(p2.x, p2.y, p3.x, p3.y); \
l31 = make_Line(p3.x, p3.y, p1.x, p1.y); \
} while (0)
#define ANY_COLORS_NOT_WHITE(c) \
(c0.r != 255 || c1.r != 255 || c2.r != 255 || \
c0.g != 255 || c1.g != 255 || c2.g != 255 || \
c0.b != 255 || c1.b != 255 || c2.b != 255)
PGLDEF void put_triangle_uniform(vec4 color, vec2 p1, vec2 p2, vec2 p3)
{
float x_min,x_max,y_min,y_max;
Line l12, l23, l31;
float alpha, beta, gamma;
CLIP_TRIANGLE();
MAKE_IMPLICIT_LINES();
x_min = floorf(x_min) + 0.5f;
y_min = floorf(y_min) + 0.5f;
for (float y=y_min; y<y_max; ++y) {
for (float x=x_min; x<x_max; ++x) {
gamma = line_func(&l12, x, y)/line_func(&l12, p3.x, p3.y);
beta = line_func(&l31, x, y)/line_func(&l31, p2.x, p2.y);
alpha = 1 - beta - gamma;
if (alpha >= 0 && beta >= 0 && gamma >= 0) {
//if it's on the edge (==0), draw if the opposite vertex is on the same side as arbitrary point -1, -1
//this is a deterministic way of choosing which triangle gets a pixel for trinagles that share
//edges
if ((alpha > 0 || line_func(&l23, p1.x, p1.y) * line_func(&l23, -1, -1) > 0) &&
(beta > 0 || line_func(&l31, p2.x, p2.y) * line_func(&l31, -1, -1) > 0) &&
(gamma > 0 || line_func(&l12, p3.x, p3.y) * line_func(&l12, -1, -1) > 0)) {
// blend
put_pixel_blend(color, x, y);
//put_pixel(color, x, y);
}
}
}
}
}
PGLDEF void put_triangle(Color c1, Color c2, Color c3, vec2 p1, vec2 p2, vec2 p3)
{
float x_min,x_max,y_min,y_max;
Line l12, l23, l31;
float alpha, beta, gamma;
Color col;
col.a = 255; // hmm
CLIP_TRIANGLE();
MAKE_IMPLICIT_LINES();
x_min = floorf(x_min) + 0.5f;
y_min = floorf(y_min) + 0.5f;
for (float y=y_min; y<y_max; ++y) {
for (float x=x_min; x<x_max; ++x) {
gamma = line_func(&l12, x, y)/line_func(&l12, p3.x, p3.y);
beta = line_func(&l31, x, y)/line_func(&l31, p2.x, p2.y);
alpha = 1 - beta - gamma;
if (alpha >= 0 && beta >= 0 && gamma >= 0) {
//if it's on the edge (==0), draw if the opposite vertex is on the same side as arbitrary point -1, -1
//this is a deterministic way of choosing which triangle gets a pixel for trinagles that share
//edges
if ((alpha > 0 || line_func(&l23, p1.x, p1.y) * line_func(&l23, -1, -1) > 0) &&
(beta > 0 || line_func(&l31, p2.x, p2.y) * line_func(&l31, -1, -1) > 0) &&
(gamma > 0 || line_func(&l12, p3.x, p3.y) * line_func(&l12, -1, -1) > 0)) {
//calculate interoplation here
col.r = alpha*c1.r + beta*c2.r + gamma*c3.r;
col.g = alpha*c1.g + beta*c2.g + gamma*c3.g;
col.b = alpha*c1.b + beta*c2.b + gamma*c3.b;
//col.a = alpha*c1.a + beta*c2.a + gamma*c3.a;
//put_pixel_blend(c, x, y);
put_pixel(col, x, y);
}
}
}
}
}
PGLDEF void put_triangle_tex(int tex, vec2 uv1, vec2 uv2, vec2 uv3, vec2 p1, vec2 p2, vec2 p3)
{
float x_min,x_max,y_min,y_max;
Line l12, l23, l31;
float alpha, beta, gamma;
CLIP_TRIANGLE();
MAKE_IMPLICIT_LINES();
#if 0
print_v2(p1, " p1\n");
print_v2(p2, " p2\n");
print_v2(p3, " p3\n");
print_v2(uv1, " uv1\n");
print_v2(uv2, " uv2\n");
print_v2(uv3, " uv3\n");
#endif
x_min = floorf(x_min) + 0.5f;
y_min = floorf(y_min) + 0.5f;
vec2 uv;
for (float y=y_min; y<y_max; ++y) {
for (float x=x_min; x<x_max; ++x) {
gamma = line_func(&l12, x, y)/line_func(&l12, p3.x, p3.y);
beta = line_func(&l31, x, y)/line_func(&l31, p2.x, p2.y);
alpha = 1 - beta - gamma;
if (alpha >= 0 && beta >= 0 && gamma >= 0) {
//if it's on the edge (==0), draw if the opposite vertex is on the same side as arbitrary point -1, -1
//this is a deterministic way of choosing which triangle gets a pixel for trinagles that share
//edges
if ((alpha > 0 || line_func(&l23, p1.x, p1.y) * line_func(&l23, -1, -1) > 0) &&
(beta > 0 || line_func(&l31, p2.x, p2.y) * line_func(&l31, -1, -1) > 0) &&
(gamma > 0 || line_func(&l12, p3.x, p3.y) * line_func(&l12, -1, -1) > 0)) {
//calculate interoplation here
uv = add_v2s(scale_v2(uv1, alpha), scale_v2(uv2, beta));
uv = add_v2s(uv, scale_v2(uv3, gamma));
put_pixel_blend(texture2D(tex, uv.x, uv.y), x, y);
}
}
}
}
}
PGLDEF void put_triangle_tex_modulate(int tex, vec2 uv1, vec2 uv2, vec2 uv3, vec2 p1, vec2 p2, vec2 p3, Color c1, Color c2, Color c3)
{
float x_min,x_max,y_min,y_max;
Line l12, l23, l31;
float alpha, beta, gamma;
Color col;
CLIP_TRIANGLE();
MAKE_IMPLICIT_LINES();
#if 0
print_v2(p1, " p1\n");
print_v2(p2, " p2\n");
print_v2(p3, " p3\n");
print_v2(uv1, " uv1\n");
print_v2(uv2, " uv2\n");
print_v2(uv3, " uv3\n");
print_Color(c1, " c1\n");
print_Color(c2, " c2\n");
print_Color(c3, " c3\n");
#endif
x_min = floorf(x_min) + 0.5f;
y_min = floorf(y_min) + 0.5f;
vec2 uv;
for (float y=y_min; y<y_max; ++y) {
for (float x=x_min; x<x_max; ++x) {
gamma = line_func(&l12, x, y)/line_func(&l12, p3.x, p3.y);
beta = line_func(&l31, x, y)/line_func(&l31, p2.x, p2.y);
alpha = 1 - beta - gamma;
if (alpha >= 0 && beta >= 0 && gamma >= 0) {
//if it's on the edge (==0), draw if the opposite vertex is on the same side as arbitrary point -1, -1
//this is a deterministic way of choosing which triangle gets a pixel for trinagles that share
//edges
if ((alpha > 0 || line_func(&l23, p1.x, p1.y) * line_func(&l23, -1, -1) > 0) &&
(beta > 0 || line_func(&l31, p2.x, p2.y) * line_func(&l31, -1, -1) > 0) &&
(gamma > 0 || line_func(&l12, p3.x, p3.y) * line_func(&l12, -1, -1) > 0)) {
//calculate interoplation here
uv = add_v2s(scale_v2(uv1, alpha), scale_v2(uv2, beta));
uv = add_v2s(uv, scale_v2(uv3, gamma));
col.r = alpha*c1.r + beta*c2.r + gamma*c3.r;
col.g = alpha*c1.g + beta*c2.g + gamma*c3.g;
col.b = alpha*c1.b + beta*c2.b + gamma*c3.b;
col.a = alpha*c1.a + beta*c2.a + gamma*c3.a;
vec4 cv = Color_to_v4(col);
vec4 texcolor = texture2D(tex, uv.x, uv.y);
put_pixel_blend(mult_v4s(cv, texcolor), x, y);
}
}
}
}
}
#define COLOR_EQ(c1, c2) ((c1).r == (c2).r && (c1).g == (c2).g && (c1).b == (c2).b && (c1).a == (c2).a)
// TODO Color* or vec4*? float* for xy/uv or vec2*?