-
Notifications
You must be signed in to change notification settings - Fork 0
/
fbMod2.f90
2530 lines (2423 loc) · 80.3 KB
/
fbMod2.f90
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
! vim:fdm=marker
! Fortran module for interacting with the framebuffer /dev/fb0
! and pixel map objects.
! August 2019
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!80
Module fbMod
implicit none
! pixel buffer type
type PixBuffType
integer :: w, h, len, lline
character(len=:), allocatable :: pb !(BGRA)
logical :: Lzbuff
real(4), allocatable, dimension(:,:) :: zbuff
contains
procedure :: init => pb_initPixBuff
procedure :: save => pb_dump
procedure :: getrec
procedure :: blendpx
procedure :: loadPPM => pb_PPM2PixBuff
procedure :: putPixel => pb_pixel
procedure :: getPixel => pb_getPixel
procedure :: clear => pb_clear
procedure :: fillTriangle => pb_fillTriangle
procedure :: fillTriangle3c => pb_fillTriangle3c
procedure :: line => pb_line
procedure :: line2c => pb_line2c
procedure :: linePolar => pb_linepolar
procedure :: linePolar2 => pb_linepolar2
procedure :: rec => pb_rec
procedure :: fillRec => pb_fillRec
procedure :: putPixBuff => pb_putpic
procedure :: getPixBuff => pb_getpic
procedure :: blurRec => pb_blurRec
procedure :: HSVScaleRec => pb_HSVScaleRec
procedure :: circle => pb_circle
procedure :: fillCircle => pb_fillcircle
procedure :: sphere => pb_sphere
procedure :: cloud => pb_cloud
procedure :: plot => pb_plot
procedure :: mplot => pb_mplot
procedure :: heatPlot => pb_heatPlot
procedure :: matrixPlot => pb_matrixPlot
procedure :: matrixPlot4 => pb_matrixPlot4
procedure :: putNumber => pb_printNumber
procedure :: putString => pb_printString
end type
! frame buffer is a pixel buffer type but with some extra stuff
type, extends(PixBuffType) :: FrameBufferType
integer :: FID !, w, h, line
character(80) :: devicePath !, mode
! character(len=:), allocatable :: pb !pixel buffer: full frame buffer
! the fb_line2c and fb_filltriangle3c routines can use the Z-Buffer
contains
procedure :: fbinit => fb_init !run this first
procedure :: close => fb_close !run this last
procedure :: display => fb_write
procedure :: loadScreen => fb_read
end type
type(FrameBufferType) :: fb
! procedural texture type for texturing triangles
type ProcTexType
integer :: typ !texture pattern type
! parameters are unique to the texture pattern type
integer, dimension(4) :: ip !integer parameters
real(4), dimension(4) :: rp !real parameters
end type
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!80
contains
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!80
! fb_init(i,dev) set variables and open framebuffer device
! i file descriptor
! dev device path of framebuffer, usually /dev/fb0
subroutine fb_init(fbo, i,dev, wd,ht,ln, zbu) !{{{
implicit none
class(FrameBufferType) :: fbo
integer :: i
character(*) :: dev
integer, optional, intent(in) :: wd,ht,ln
logical, optional, intent(in) :: zbu
fbo%FID = i
fbo%devicePath = trim(dev)
!fb%Lbuff = .true.
!fb%mode = trim(mode)
! this can be found with fbset
fbo%w = 1440 !width
fbo%h = 900 !height
fbo%lline = 1472 !for some reason line length is not the same as width, WTF?
if (present(wd)) fbo%w = wd
if (present(ht)) fbo%h = ht
if (present(ln)) fbo%lline = ln !overwrite the initialization
fbo%Lzbuff = .false. !use Z-buffer?
!if (present(zbu)) fbo%Lzbuff = zbu
! mode options:
! direct each function write is directly to frame buffer device
! buffer each function write is to the buffer: "fb%pxbuff"
! this mode must be explicitly written to the device.
!if (trim(mode).eq."direct") then
! fb%Lbuff = .false.
!! 32 bits per pixel means 4 bytes per pixel
!! each record in "file" is a pixel
! open(fb%FID,file=fb%devicePath,ACCESS='DIRECT',RECL=4,FORM='UNFORMATTED')
!elseif (trim(mode).eq."buffer") then
if (present(zbu)) then; call fbo%init( fbo%w, fbo%h, fbo%lline, zbu )
else; call fbo%init( fbo%w, fbo%h, fbo%lline )
endif
! open as stream to be written to in one go. fastest rendering option
open(fbo%FID,file=fbo%devicePath,ACCESS='STREAM',FORM='UNFORMATTED')
!else
! write(0,*) "ERROR: fb_init: no mode option: "//trim(mode)
! STOP
!endif
end subroutine fb_init !}}}
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!80
subroutine fb_write( fb ) !{{{
! write buffer to framebuffer device file to render to screen
class(FrameBufferType) :: fb
rewind(fb%FID)
write(fb%FID) fb%pb
end subroutine fb_write !}}}
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!80
subroutine fb_read( fb ) !{{{
! read framebuffer device file to to buffer
class(FrameBufferType) :: fb
rewind(fb%FID)
read(fb%FID) fb%pb
end subroutine fb_read !}}}
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!80
subroutine pb_dump(pb,FL,mode) !{{{
class(PixBuffType) :: pb
character(*), intent(in) :: FL
integer, intent(in) :: mode
! mode : description
! 1 full buffer dump, as BGRA and excess columns
! 2 full screen binary P6-PPM
! 3 only non NULL pixels printed in ASCII: X Y R G B
integer :: i, k, j, b, g, r, x, y, itmp, FD
character(80) :: frmtstr
character :: rgb(3,pb%lline,pb%h)
FD = 21
open(FD,file=trim(FL))
select case(mode)
case(1); write(FD) pb%pb
case(2);
!write(11,'(''P6'', 2(1x,i4),'' 255 '',$)') fb%lline, fb%height
write(FD,'(A2)') "P6"
write(FD,'(2(1x,i4))') pb%lline, pb%h
write(FD,'(i3)') 255
itmp = pb%lline*pb%h*3
!write(frmtstr,'(''('',i8.8,''A,$)'')') itmp
write(frmtstr,'(''('',i8.8,''A)'')') itmp
do i = 1, pb%lline*pb%h
k = i*4-3
y = int(real(i-1)/real(pb%lline)) +1
x = i -(y-1)*pb%lline
if (x.le.0 .or. y.le.0) then
write(0,*) "ERROR: pixel point of bounds (x,y)=",x, y, i, k
STOP
endif
rgb(3,x,y) = pb%pb(k:k)
rgb(2,x,y) = pb%pb(k+1:k+1)
rgb(1,x,y) = pb%pb(k+2:k+2)
enddo
write(FD,fmt=frmtstr,advance="no") (((rgb(k,i,j),k=1,3),i=1,pb%lline),j=1,pb%h)
case(3);
do i = 1, pb%lline*pb%h
k = i*4-3
b = ichar(pb%pb(k:k))
g = ichar(pb%pb(k+1:k+1))
r = ichar(pb%pb(k+2:k+2))
if (b.eq.0 .and. g.eq.0 .and. r.eq.0) cycle
y = int(real(i)/real(pb%lline)) +1
x = i -(y-1)*pb%lline
write(FD,'(5i5)') x, y, r, g, b
enddo
end select
close(FD)
end subroutine pb_dump !}}}
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!80
subroutine pb_PPM2PixBuff( pb, fil ) !{{{
implicit none
class(PixBuffType) :: pb
character(*) :: fil
character(1) :: c
character(2) :: ty
character(20) :: c20 !frmtstr
integer :: w, h, i, k !, itmp
character, allocatable :: rgb(:,:)
open(11,file=trim(fil),ACCESS='STREAM',FORM='UNFORMATTED')
read(11) ty, c !the type(2) and 0a
!write(0,'(3(z2,x))') ty(1:1), ty(2:2), c; call flush(0)
c20 = ""; k=1
do i = 1, 20
read(11) c !read one byte at a time
if (c.eq.char(10)) then !new line
k = i; exit; endif ! done with the width height line
c20(i:i) = c
enddo
read(c20(1:k),*) w, h
!write(0,'(20(z2,x),x,2i6)') (c20(i:i),i=1,20), w, h; call flush(0)
c20 = ""
do i = 1, 20
read(11) c !read one byte at a time
if (c.eq.char(10)) then !new line
k = i; exit; endif ! done with the bit depth
c20(i:i) = c
enddo
read(c20,*) k
!write(0,'(20(z2,x),x,i6)') (c20(i:i),i=1,20), k; call flush(0)
!read(11,*) w, h
!read(11,*) ! 255
call pb%init( w, h, w )
! itmp = w*h*3
allocate( rgb(3,w*h) ) !assume 24 bit
!rgb = char(0)
! write(frmtstr,'(''('',i8.8,''A)'')') itmp !this should be 3x w*h
!write(0,*) frmtstr," ",ty, w, h, i, " ", itmp
!call flush(0)
!read(11,fmt=frmtstr,advance="no") (rgb(1:3,i),i=1,w*h)
!read(11,fmt=frmtstr,advance="no") (rgb(1:3,i),i=1,w*h)
! do i = 1, w*h
! read(11,fmt='(3A)',advance="no",iostat=er) rgb(1:3,i)
! if (er.ne.0) then
! write(0,'(a,i3,a,i6,x,3(z2,x),x,3(z2,x))') "ERROR: ",er," Pixel ", i, &
! & iachar(rgb(1,i-1)), iachar(rgb(2,i-1)), iachar(rgb(3,i-1)), &
! & iachar(rgb(1,i)), iachar(rgb(2,i)), iachar(rgb(3,i))
! call flush(0)
! endif
! enddo
read(11) ((rgb(k,i),k=1,3),i=1,w*h)
do i = 1, w*h !read each pixel
k = i*4-3
pb%pb(k:k) = rgb(3,i)
pb%pb(k+1:k+1) = rgb(2,i)
pb%pb(k+2:k+2) = rgb(1,i)
enddo
close(11)
end subroutine pb_PPM2PixBuff !}}}
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!80
subroutine fb_close( fb ) !{{{
class(FrameBufferType) :: fb
close(fb%FID)
end subroutine fb_close !}}}
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!80
subroutine pb_initPixBuff( pb, w, h, l, zb ) !{{{
class(PixBuffType) :: pb
integer :: w, h, l
logical, optional, intent(in) :: zb
pb%w = w
pb%h = h
pb%lline = l !for compatibility with framebuffer object, should be 'w'
pb%len = h*l
if (allocated( pb%pb )) deallocate( pb%pb )
allocate( character(len=4*pb%len) :: pb%pb )
pb%Lzbuff = .false. !use Z-buffer?
if (present(zb)) pb%Lzbuff = zb !use Z-buffer?
if (allocated( pb%zbuff )) deallocate( pb%zbuff )
if (pb%Lzbuff) allocate( pb%zbuff(pb%w, pb%h) )
!if (.not.pb%Lzbuff) write(0,*) "pb_init:Zbuffer not activated"
call pb%clear
end subroutine pb_initPixBuff !}}}
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!80
! returns the 4Byte record number for an XY screen position.
! or the byte offset in "buffer" mode.
integer function getrec(pb,x,y) !{{{
class(PixBuffType) :: pb
integer :: x, y
getrec = (y-1)*pb%lline + x !pixel location
getrec = getrec*4-3 !byte location
end function getrec !}}}
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!80
subroutine pb_pixel(pb, x,y,px) !{{{
implicit none
class(PixBuffType) :: pb
integer, intent(in) :: x, y
integer :: k
character(4), intent(in) :: px
k = pb%getrec(x,y)
! check for transparency byte
if (ichar(px(4:4)).gt.0) then
pb%pb(k:k+3) = pb%blendpx(k,px)
else
pb%pb(k:k+3) = px
endif
end subroutine !}}}
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!80
subroutine pb_getPixel(pb, x,y,px) !{{{
implicit none
class(PixBuffType) :: pb
integer, intent(in) :: x, y
integer :: k
character(4), intent(out) :: px
k = pb%getrec(x,y)
!if (pb%Lbuff) then
px = pb%pb(k:k+3)
!else
! read(fb%FID,REC=k) px
!endif
end subroutine pb_getPixel !}}}
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!80
! blend current pixel at pb record k with pixel p2 alpha channel.
function blendpx( pb, k, p2 ) !{{{
implicit none
class(PixBuffType) :: pb
integer, intent(in) :: k
character(4), intent(in) :: p2
character(4) :: blendpx, p1
integer :: t
real(4) :: v
t = ichar(p2(4:4)) ! transparency magnitude 0=opaque, 255=novalue
v = real(t)/255.0
p1 = pb%pb(k:k+3)
blendpx(1:1) = char(nint( real(ichar(p1(1:1)))*v +real(ichar(p2(1:1)))*(1.0-v) ))
blendpx(2:2) = char(nint( real(ichar(p1(2:2)))*v +real(ichar(p2(2:2)))*(1.0-v) ))
blendpx(3:3) = char(nint( real(ichar(p1(3:3)))*v +real(ichar(p2(3:3)))*(1.0-v) ))
blendpx(4:4) = char(0)
end function blendpx !}}}
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!80
subroutine pb_clear( pb, px ) !{{{
class(PixBuffType) :: pb
character(4), optional, intent(in) :: px
character(4) :: bb
bb = char(0)//char(0)//char(0)//char(0) !default byte to write is NULL
if (present(px)) bb=px
pb%pb = repeat(bb,pb%lline*pb%h)
if (pb%Lzbuff) pb%zbuff = -1.0 !clear Z-Buffer as well (-1 is INF)
end subroutine pb_clear !}}}
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!80
subroutine RGB2HSV( r,g,b, h,s,v ) !{{{
! from: https://www.cs.rit.edu/~ncs/color/t_convert.html
implicit none
integer, intent(in) :: r, g, b !range 0-255
integer, intent(out) :: h, s, v !
real :: mn, mx, dl, rh
mn = real(min(r, g, b))
mx = real(max(r, g, b))
v = max(r,g,b) !nint(mx) !value
dl = mx-mn
if (mx.gt.1.e-3) then
s = nint(dl/mx*255.0)
else
s = 0
h = -1
return
endif
if (r==v) then !nint(mx)) then ! if red is max
rh = real(g-b)/dl
elseif (g==v) then !nint(mx)) then ! if green is max
rh = (2.0+real(b-r)/dl)
else !if blue is max
rh = (4.0+real(r-g)/dl)
endif
rh = rh * 60.0 !degrees
if (rh.lt.0.0) rh = rh + 360.0
h = nint(rh *255./360.)
end subroutine RGB2HSV !}}}
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!80
subroutine HSV2RGB( h,s,v, r,g,b ) !{{{
! from: https://www.cs.rit.edu/~ncs/color/t_convert.html
! and : https://dystopiancode.blogspot.com/2012/06/hsv-rgb-conversion-algorithms-in-c.html
implicit none
integer, intent(out) :: r, g, b !range 0-255
integer, intent(in) :: h, s, v !
integer :: i
real :: f,p,t,hh,ss,vv, c
if (s==0) then !grey
r = v; g = v; b = v
return
endif
hh = real(h)/42.51 ! range [0:5]
i = int(hh)
!f = hh - i !fractional part of h
ss = real(s)/255.
vv = real(v)/255.
c = vv*ss
p = vv * (1.0 -ss) ! m = vv - c
!q = vv * (1.0 -ss*f)
!t = vv * (1.0 -ss*(1.0 -f))
f = c*(1.0-abs(mod(hh,2.0)-1.0)) ! = x
! f = c -c*abs(mod(hh,2.0)-1.0)
t = f + p ! x + m
! t = c -c*abs(mod(hh,2.0)-1.0) + vv -vv*ss
select case (i)
case(0); r = nint(vv*255.); g = nint(t*255.); b = nint(p*255.)
case(1); r = nint(t*255.); g = nint(vv*255.); b = nint(p*255.)
case(2); r = nint(p*255.); g = nint(vv*255.); b = nint(t*255.)
case(3); r = nint(p*255.); g = nint(t*255.); b = nint(vv*255.)
case(4); r = nint(t*255.); g = nint(p*255.); b = nint(vv*255.)
case(5); r = nint(vv*255.); g = nint(p*255.); b = nint(t*255.)
! case(0); r = nint(vv*255.); g = nint(t*255.); b = nint(p*255.)
! case(1); r = nint(q*255.); g = nint(vv*255.); b = nint(p*255.)
! case(2); r = nint(p*255.); g = nint(vv*255.); b = nint(t*255.)
! case(3); r = nint(p*255.); g = nint(q*255.); b = nint(vv*255.)
! case(4); r = nint(t*255.); g = nint(p*255.); b = nint(vv*255.)
! case(5); r = nint(vv*255.); g = nint(p*255.); b = nint(q*255.)
end select
end subroutine HSV2RGB !}}}
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!80
subroutine getProcTexture( px1,px2,px3, w, tx, px ) !{{{
! returns the pixel value (px) of a procedural texture pattern (tx)
! for the triangular fractional position (w) with corresponding colours (px123)
implicit none
character(4), intent(in) :: px1, px2, px3
real(4), dimension(3), intent(in) :: w
type(ProcTexType) :: tx
character(4), intent(out) :: px
integer, dimension(3) :: p, r, g, b, a, h, s, v, hsv !for integer colours, each corner
real(4), dimension(3) :: f
real(4) :: ss
integer :: val
r = (/ ichar(px1(3:3)), ichar(px2(3:3)), ichar(px3(3:3)) /)
g = (/ ichar(px1(2:2)), ichar(px2(2:2)), ichar(px3(2:2)) /)
b = (/ ichar(px1(1:1)), ichar(px2(1:1)), ichar(px3(1:1)) /)
a = (/ ichar(px1(4:4)), ichar(px2(4:4)), ichar(px3(4:4)) /)
f = w
select case (tx%typ)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
case(1) ! stripes from node 1 to 2 ( nstripes = tx%ip(1) )
! modify the w(1) parameter to produce stripes between point 1 and the others.
f(1) = real(tx%ip(1))*w(1)
f(1) = f(1) - int(f(1))
! one component is changed, but scale the other two for sum(f) = 1.0
! f1+s*f2+s*f3=1 : solve for s
! f1+s*(f2+f3)=1 : s = (1-f1)/(f2+f3)
ss = (1.0-f(1))/sum(f(2:3))
f(2:3) = ss*f(2:3)
px(1:1) = char(nint( b(1)*f(1) +b(2)*f(2) +b(3)*f(3) ))
px(2:2) = char(nint( g(1)*f(1) +g(2)*f(2) +g(3)*f(3) ))
px(3:3) = char(nint( r(1)*f(1) +r(2)*f(2) +r(3)*f(3) ))
px(4:4) = char(nint( a(1)*f(1) +a(2)*f(2) +a(3)*f(3) ))
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
case(2) ! checkerboard half gradient from node 1 to 2 and 3 to 2 (nstripes = tx%ip(1:2))
! modify the w(1:2) parameters to produce checkerboard pattern.
f(1:2) = real(tx%ip(1:2))*w(1:2)
f(1:2) = f(1:2) - int(f(1:2))
! two components are changed, but scale the other two for sum(f) = 1.0
! s*(f1+f2)+f3=1 : solve for: s = (1-f3)/(f2+f1)
ss = (1.0-f(3))/sum(f(1:2))
f(1:2) = ss*f(1:2)
px(1:1) = char(nint( b(1)*f(1) +b(2)*f(2) +b(3)*f(3) ))
px(2:2) = char(nint( g(1)*f(1) +g(2)*f(2) +g(3)*f(3) ))
px(3:3) = char(nint( r(1)*f(1) +r(2)*f(2) +r(3)*f(3) ))
px(4:4) = char(nint( a(1)*f(1) +a(2)*f(2) +a(3)*f(3) ))
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
case(3) ! checkerboard triangle half gradient (nstripes = tx%ip(1:3))
! modify the w(1:3) parameters to produce checkerboard pattern.
f = real(tx%ip(1:3))*w
f = f - int(f)
! all components are changed, so scale them for sum(f) = 1.0
! s*(f1+f2+f3)=1 : solve for: s = 1.0/sum(f)
ss = 1.0/sum(f)
f = ss*f
px(1:1) = char(nint( b(1)*f(1) +b(2)*f(2) +b(3)*f(3) ))
px(2:2) = char(nint( g(1)*f(1) +g(2)*f(2) +g(3)*f(3) ))
px(3:3) = char(nint( r(1)*f(1) +r(2)*f(2) +r(3)*f(3) ))
px(4:4) = char(nint( a(1)*f(1) +a(2)*f(2) +a(3)*f(3) ))
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
case(4) ! inner boarder along 1-2-3 not 1-3 edges.
! boarder fraction outer tx%rp(1), hue&val of px1
! boarder fraction inner tx%rp(2), hue&val of px2
! inner block is hue&val of px3
call RGB2HSV(r(1),g(1),b(1), h(1),s(1),v(1))
call RGB2HSV(r(2),g(2),b(2), h(2),s(2),v(2))
call RGB2HSV(r(3),g(3),b(3), h(3),s(3),v(3))
val = nint(real(s(1))*w(1) +real(s(2))*w(2) +real(s(3))*w(3))
if (w(1).lt.tx%rp(1) .or. w(3).lt.tx%rp(1)) then
call HSV2RGB(h(1),val,v(1), p(1),p(2),p(3))
px = char(p(1))//char(p(2))//char(p(3))//px1(4:4)
elseif (w(1).lt.tx%rp(2) .or. w(3).lt.tx%rp(2)) then
call HSV2RGB(h(2),val,v(2), p(1),p(2),p(3))
px = char(p(1))//char(p(2))//char(p(3))//px2(4:4)
else
call HSV2RGB(h(3),val,v(3), p(1),p(2),p(3))
px = char(p(1))//char(p(2))//char(p(3))//px3(4:4)
endif
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
case(5) !inner scribed ellipse?
! center of circle at w=(0.5,0.0,0.5)
! circle tangents at w=(0.5,0.0,1.0), w=(1.0,0.5,0.0)
!1\
! \
! \
! A
!C \
! \
!2__U___\3
f = w-(/ 0.5, 0.0, 0.5 /) ! current - center
!ss = sum(f*f) ! sum of squares
ss = f(1)*f(1)+f(3)*f(3)
if (ss .lt. tx%rp(1)*tx%rp(1)) then ! in circle?
! blend node 2 and 3
ss = ss/(tx%rp(1)*tx%rp(1)) !normalize to 1.0
px(1:1) = char(nint(b(2)*ss +b(3)*(1.0-ss) ))
px(2:2) = char(nint(g(2)*ss +g(3)*(1.0-ss) ))
px(3:3) = char(nint(r(2)*ss +r(3)*(1.0-ss) ))
px(4:4) = char(nint(a(2)*ss +a(3)*(1.0-ss) ))
else
px = px1
endif
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
case(6) !Perlin Noise for saturation and value, not Hue
! requires a 2D vector w(1,3)
! frequency: tx%rp(1)
! amplitude: tx%rp(2)
! weight the vertext colors together, like default
p(1) = nint( b(1)*w(1) +b(2)*w(2) +b(3)*w(3) )
p(2) = nint( g(1)*w(1) +g(2)*w(2) +g(3)*w(3) )
p(3) = nint( r(1)*w(1) +r(2)*w(2) +r(3)*w(3) )
! convert to HSV
call RGB2HSV(p(1),p(2),p(3), hsv(1),hsv(2),hsv(3))
! modify the saturation and value by noise
!hsv(2:3) = hsv(2:3) + nint(tx%rp(2)*(noise((/ w(1), w(3) /), tx%rp(1))-0.5))
hsv(2:3) = hsv(2:3) + nint(tx%rp(2)*(random((/ w(1), w(3) /))-0.5))
hsv(2) = min(max(hsv(2),0),255); hsv(3) = min(max(hsv(3),0),255)
! convert HSV back to RGB
call HSV2RGB(hsv(1),hsv(2),hsv(3), p(1),p(2),p(3))
px = char(p(1))//char(p(2))//char(p(3))
px(4:4) = char(nint( a(1)*w(1) +a(2)*w(2) +a(3)*w(3) ))
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
case(7) !fractal brownian motion Perlin Noise for saturation and value, not Hue
! requires a 2D vector w(1,3)
! frequency: tx%rp(1)
! amplitude: tx%rp(2)
! weight the vertext colors together, like default
p(1) = nint( b(1)*w(1) +b(2)*w(2) +b(3)*w(3) )
p(2) = nint( g(1)*w(1) +g(2)*w(2) +g(3)*w(3) )
p(3) = nint( r(1)*w(1) +r(2)*w(2) +r(3)*w(3) )
! convert to HSV
call RGB2HSV(p(1),p(2),p(3), hsv(1),hsv(2),hsv(3))
! modify the saturation and value by noise
!hsv(2:3) = hsv(2:3) + nint(tx%rp(2)*(noise((/ w(1), w(3) /), tx%rp(1))-0.5))
!hsv(2:3) = hsv(2:3) + nint(tx%rp(2)*(random((/ w(1), w(3) /))-0.5))
hsv(2:3) = hsv(2:3) + nint(tx%rp(4)*(fbm((/ w(1), w(3) /), tx%rp(1), tx%rp(2), tx%ip(1), tx%rp(3), tx%ip(2))-0.5))
hsv(2) = min(max(hsv(2),0),255); hsv(3) = min(max(hsv(3),0),255)
! convert HSV back to RGB
call HSV2RGB(hsv(1),hsv(2),hsv(3), p(1),p(2),p(3))
px = char(p(1))//char(p(2))//char(p(3))
px(4:4) = char(nint( a(1)*w(1) +a(2)*w(2) +a(3)*w(3) ))
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
case default !simple interpolation
px(1:1) = char(nint( b(1)*w(1) +b(2)*w(2) +b(3)*w(3) ))
px(2:2) = char(nint( g(1)*w(1) +g(2)*w(2) +g(3)*w(3) ))
px(3:3) = char(nint( r(1)*w(1) +r(2)*w(2) +r(3)*w(3) ))
px(4:4) = char(nint( a(1)*w(1) +a(2)*w(2) +a(3)*w(3) ))
end select
end subroutine getProcTexture !}}}
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!80
! SHAPES
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!80
! point slope form of line !{{{
! returns y given an x that falls on a line from point 1 to 2.
integer function PointSlope(i,x1,y1,x2,y2)
implicit none
integer(kind=4) :: i,x1,x2,y1,y2
real(8) :: tmp
!I forgot point slope form, so I programmed it.
tmp=dble((x2-i)*(y2-y1))/dble(x2-x1)
PointSlope=y2-nint(tmp)
end function PointSlope !}}}
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!80
! pb_filltriangle( ps(2,3), px )
! Draw a filled trianlge with verticies p1, p2, p3 with color, px
subroutine pb_filltriangle( pb, x1, y1, x2, y2, x3, y3, px ) !{{{
implicit none
class(PixBuffType) :: pb
integer(4), dimension(2,3) :: ps
integer(4) :: x1, x2, x3, y1, y2, y3, xs, xe, y, r, x
character(4) :: px
! sort point from top to bottom
if (y1.le.y2 .and.y1.le.y3) then
ps(:,1) = (/ x1, y1 /)
if (y2 < y3) then; ps(:,2) = (/ x2, y2 /); ps(:,3) = (/ x3, y3 /)
else; ps(:,2) = (/ x3, y3 /); ps(:,3) = (/ x2, y2 /)
endif
elseif (y2.le.y1 .and.y2.le.y3) then
ps(:,1) = (/ x2, y2 /)
if (y1 < y3) then; ps(:,2) = (/ x1, y1 /); ps(:,3) = (/ x3, y3 /)
else; ps(:,2) = (/ x3, y3 /); ps(:,3) = (/ x1, y1 /)
endif
else
ps(:,1) = (/ x3, y3 /)
if (y1 < y2) then; ps(:,2) = (/ x1, y1 /); ps(:,3) = (/ x2, y2 /)
else; ps(:,2) = (/ x2, y2 /); ps(:,3) = (/ x1, y1 /)
endif
endif
! lower triangle
if (ps(2,2) > ps(2,1)) then !only if not horizontal top
do y=ps(2,1), ps(2,2) !only raster down to medium y point
xs = PointSlope(y,ps(2,1),ps(1,1),ps(2,2),ps(1,2)) !point 1 to 2
xe = PointSlope(y,ps(2,1),ps(1,1),ps(2,3),ps(1,3)) !point 1 to 3
do x = min(xs,xe), max(xs,xe)
r = pb%getrec(x,y)
if (r.le.0) write(0,*) "fb Byte < 0. (x,y)=",x, y, xs, xe
!write(6,'(a2,i3.3,a1,i3.3,2a1)') char(27)//'[',i,';',PS(i,y1,x1,y2,x2),'H', "*"
!if (fb%Lbuff) then
pb%pb(r:r+3) = px
!else
! write(fb%FID,REC=r) px
!endif
enddo
end do
endif
! upper triangle
if (ps(2,3) > ps(2,2)) then !only if not horizontal top
do y=ps(2,2)+1, ps(2,3) !only raster down from medium y point to bottom
xs = PointSlope(y,ps(2,2),ps(1,2),ps(2,3),ps(1,3)) !point 2 to 3
xe = PointSlope(y,ps(2,1),ps(1,1),ps(2,3),ps(1,3)) !point 1 to 3
do x = min(xs,xe), max(xs,xe)
r = pb%getrec(x,y)
if (r.le.0) write(0,*) "fb Byte < 0. (x,y)=",x, y, xs, xe
!write(6,'(a2,i3.3,a1,i3.3,2a1)') char(27)//'[',i,';',PS(i,y1,x1,y2,x2),'H', "*"
!if (fb%Lbuff) then
pb%pb(r:r+3) = px
!else
! write(fb%FID,REC=r) px
!endif
enddo
end do
endif
end subroutine pb_filltriangle !}}}
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!80
! pb_filltriangle3c( ps(2,3), px(3), [sb(2,2)] )
! Draw a filled trianlge with verticies p1, p2, p3
! each vertex has color px1, px2, px3, fill colour interpolates
! within bounds, sb
! Can use with zbuffer.
subroutine pb_filltriangle3c( pb, x1,y1, x2,y2, x3,y3, px1,px2,px3, sb, z1,z2,z3, tx)!{{{
implicit none
class(PixBuffType) :: pb
integer(4), intent(in) :: x1, x2, x3, y1, y2, y3
character(4), intent(in) :: px1, px2, px3
integer, dimension(2,2), optional, intent(in) :: sb
real(4), optional, intent(in) :: z1,z2,z3
type(ProcTexType), optional, intent(in) :: tx
integer(4), dimension(2,3) :: ps
character(4) :: pxl1, pxl2, pxl3
logical :: texture
integer(4) :: xs, xe, y, r, x, idenom
character(4) :: px
real(4), dimension(3) :: w
integer(4) :: dy23, dy31, dx32, dx13, dy13, ord(3)
real(4) :: denom, z, zp(3)
texture = .false.
if (present(tx)) texture = .true.
! sort point from top to bottom
if (present(z1)) then !{{{
if (y1.le.y2 .and.y1.le.y3) then
ps(:,1) = (/ x1, y1 /); zp(1) = z1; pxl1 = px1; ord(1) = 1
if (y2<y3) then; ps(:,2)=(/x2,y2/); ps(:,3)=(/x3,y3/); zp(2:3)=(/z2,z3/); pxl2=px2; pxl3=px3; ord(2:3) = (/2,3/)
else; ps(:,2)=(/x3,y3/); ps(:,3)=(/x2,y2/); zp(2:3)=(/z3,z2/); pxl2=px3; pxl3=px2; ord(2:3) = (/3,2/)
endif
elseif (y2.le.y1 .and.y2.le.y3) then
ps(:,1) = (/ x2, y2 /); zp(1) = z2; pxl1 = px2; ord(1) = 2
if (y1<y3) then; ps(:,2)=(/x1,y1/); ps(:,3)=(/x3,y3/); zp(2:3)=(/z1,z3/); pxl2=px1;pxl3=px3; ord(2:3) = (/1,3/)
else; ps(:,2)=(/x3,y3/); ps(:,3)=(/x1,y1/); zp(2:3)=(/z3,z1/); pxl2=px3;pxl3=px1; ord(2:3) = (/3,1/)
endif
else
ps(:,1) = (/ x3, y3 /); zp(1) = z3; pxl1 = px3; ord(1) = 3
if (y1<y2) then; ps(:,2)=(/x1,y1/); ps(:,3)=(/x2,y2/); zp(2:3)=(/z1,z2/); pxl2=px1;pxl3=px2; ord(2:3) = (/1,2/)
else; ps(:,2)=(/x2,y2/); ps(:,3)=(/x1,y1/); zp(2:3)=(/z2,z1/); pxl2=px2;pxl3=px1; ord(2:3) = (/2,1/)
endif
endif
else
if (y1.le.y2 .and.y1.le.y3) then
ps(:,1) = (/ x1, y1 /); pxl1 = px1; ord(1) = 1
if (y2 < y3) then; ps(:,2) = (/x2,y2/); ps(:,3) = (/x3,y3/); pxl2=px2; pxl3=px3; ord(2:3) = (/2,3/)
else; ps(:,2) = (/x3,y3/); ps(:,3) = (/x2,y2/); pxl2=px3; pxl3=px2; ord(2:3) = (/3,2/)
endif
elseif (y2.le.y1 .and.y2.le.y3) then
ps(:,1) = (/ x2, y2 /); pxl1 = px2; ord(1) = 2
if (y1 < y3) then; ps(:,2) = (/x1,y1/); ps(:,3) = (/x3,y3/); pxl2=px1;pxl3=px3; ord(2:3) = (/1,3/)
else; ps(:,2) = (/x3,y3/); ps(:,3) = (/x1,y1/); pxl2=px3;pxl3=px1; ord(2:3) = (/3,1/)
endif
else
ps(:,1) = (/ x3, y3 /); pxl1 = px3 ; ord(1) = 3
if (y1 < y2) then; ps(:,2) = (/x1,y1/); ps(:,3) = (/x2,y2/); pxl2=px1;pxl3=px2; ord(2:3) = (/1,2/)
else; ps(:,2) = (/x2,y2/); ps(:,3) = (/x1,y1/); pxl2=px2;pxl3=px1; ord(2:3) = (/2,1/)
endif
endif
endif !}}}
! save these convinient vlues for weight calculation
dy23 = ps(2,2)-ps(2,3) !y2-y3
dy31 = ps(2,3)-ps(2,1) !y3-y1
dy13 = ps(2,1)-ps(2,3) !y1-y3
dx32 = ps(1,3)-ps(1,2) !x3-x2
dx13 = ps(1,1)-ps(1,3) !x1-x3
idenom = dy23*dx13+dx32*dy13
if (idenom.eq.0) then
!write(0,*) "ERROR triangle:", ps(:,1), ":", ps(:,2),":",ps(:,3)
!write(0,*) "denom=0",dy23,dx13,dx32,dy13
Return
endif
denom=real(idenom)
! top triangle
if (ps(2,2) > ps(2,1)) then !only if not horizontal top
do y=ps(2,1), ps(2,2) !only raster down to medium y point
xs = PointSlope(y,ps(2,1),ps(1,1),ps(2,2),ps(1,2)) !point 1 to 2
xe = PointSlope(y,ps(2,1),ps(1,1),ps(2,3),ps(1,3)) !point 1 to 3
do x = min(xs,xe), max(xs,xe)
if (present(sb)) then
if (x.lt.sb(1,1)) then; cycle !x too low
elseif (x.gt.sb(1,2)) then; cycle; endif !x too large
if (y.lt.sb(2,1)) then; cycle !y too low
elseif (y.gt.sb(2,2)) then; cycle; endif !y too large
endif
r = pb%getrec(x,y)
if (r.le.0) write(0,*) "fb Byte < 0. (x,y)=",x, y, xs, xe
! calculate each vertex's position weight on (x,y)
!w(1) = (dy23*(x-x3) + dx32*(y-y3))/denom
!w(2) = (dy31*(x-x3) + dx13*(y-y3))/denom
w(1) = (dy23*(x-ps(1,3)) + dx32*(y-ps(2,3)))/denom
w(2) = (dy31*(x-ps(1,3)) + dx13*(y-ps(2,3)))/denom
w(3) = 1.0 -w(1) -w(2)
if (minval(w).lt.0.0) cycle !outside of triangle
if (pb%Lzbuff) then
!z = z1*w(1) +z2*w(2) +z3*w(3)
z = zp(1)*w(1) +zp(2)*w(2) +zp(3)*w(3)
if (pb%zbuff(x,y).lt.0.0) then
pb%zbuff(x,y) = z !save this to the Z-buffer and render it
else
if (z.lt.pb%zbuff(x,y)) then !then it's behind what is already rendered
pb%zbuff(x,y) = z !save this to the Z-buffer and render it, since it's the closest
else
cycle
endif
endif
endif
if (.not.texture) then
! use the weights to interpolate each RGB value
!px(1:1) = char(nint( ichar(px1(1:1))*w(1) +ichar(px2(1:1))*w(2) +ichar(px3(1:1))*w(3)))
!px(2:2) = char(nint( ichar(px1(2:2))*w(1) +ichar(px2(2:2))*w(2) +ichar(px3(2:2))*w(3)))
!px(3:3) = char(nint( ichar(px1(3:3))*w(1) +ichar(px2(3:3))*w(2) +ichar(px3(3:3))*w(3)))
px(1:1) = char(nint( ichar(pxl1(1:1))*w(1) +ichar(pxl2(1:1))*w(2) +ichar(pxl3(1:1))*w(3)))
px(2:2) = char(nint( ichar(pxl1(2:2))*w(1) +ichar(pxl2(2:2))*w(2) +ichar(pxl3(2:2))*w(3)))
px(3:3) = char(nint( ichar(pxl1(3:3))*w(1) +ichar(pxl2(3:3))*w(2) +ichar(pxl3(3:3))*w(3)))
else
! reorder the weights according to input order not height order
call getProcTexture( px1,px2,px3, (/w(ord(1)),w(ord(2)),w(ord(3))/), tx, px )
endif
!if (fb%Lbuff) then
pb%pb(r:r+3) = px
!else
! write(fb%FID,REC=r) px
!endif
enddo
end do
endif
! Lower triangle
if (ps(2,3) > ps(2,2)) then !only if not horizontal bottom
do y=ps(2,2), ps(2,3) !only raster down from medium y point to bottom
xs = PointSlope(y,ps(2,2),ps(1,2),ps(2,3),ps(1,3)) !point 2 to 3
xe = PointSlope(y,ps(2,1),ps(1,1),ps(2,3),ps(1,3)) !point 1 to 3
do x = min(xs,xe), max(xs,xe)
if (present(sb)) then
if (x.lt.sb(1,1)) then; cycle !x too low
elseif (x.gt.sb(1,2)) then; cycle; endif !x too large
if (y.lt.sb(2,1)) then; cycle !y too low
elseif (y.gt.sb(2,2)) then; cycle; endif !y too large
endif
r = pb%getrec(x,y)
if (r.le.0) write(0,*) "fb Byte < 0. (x,y)=",x, y, xs, xe
! calculate each vertex's position weight on (x,y)
!w(1) = (dy23*(x-x3) + dx32*(y-y3))/denom
!w(2) = (dy31*(x-x3) + dx13*(y-y3))/denom
w(1) = (dy23*(x-ps(1,3)) + dx32*(y-ps(2,3)))/denom
w(2) = (dy31*(x-ps(1,3)) + dx13*(y-ps(2,3)))/denom
w(3) = 1.0 -w(1) -w(2)
if (minval(w).lt.0.0) cycle !outside of triangle
if (pb%Lzbuff) then
!z = z1*w(1) +z2*w(2) +z3*w(3)
z = zp(1)*w(1) +zp(2)*w(2) +zp(3)*w(3)
if (pb%zbuff(x,y).lt.0.0) then
pb%zbuff(x,y) = z !save this to the Z-buffer and render it
else
if (z.lt.pb%zbuff(x,y)) then !then it's behind what is already rendered
pb%zbuff(x,y) = z !save this to the Z-buffer and render it, since it's the closest
else
cycle
endif
endif
endif
if (.not.texture) then
! use the weights to interpolate each RGB value
!px(1:1) = char(nint( ichar(px1(1:1))*w(1) +ichar(px2(1:1))*w(2) +ichar(px3(1:1))*w(3) ))
!px(2:2) = char(nint( ichar(px1(2:2))*w(1) +ichar(px2(2:2))*w(2) +ichar(px3(2:2))*w(3) ))
!px(3:3) = char(nint( ichar(px1(3:3))*w(1) +ichar(px2(3:3))*w(2) +ichar(px3(3:3))*w(3) ))
px(1:1) = char(nint( ichar(pxl1(1:1))*w(1) +ichar(pxl2(1:1))*w(2) +ichar(pxl3(1:1))*w(3) ))
px(2:2) = char(nint( ichar(pxl1(2:2))*w(1) +ichar(pxl2(2:2))*w(2) +ichar(pxl3(2:2))*w(3) ))
px(3:3) = char(nint( ichar(pxl1(3:3))*w(1) +ichar(pxl2(3:3))*w(2) +ichar(pxl3(3:3))*w(3) ))
else
! reorder the weights according to input order not height order
call getProcTexture( px1,px2,px3, (/w(ord(1)),w(ord(2)),w(ord(3))/), tx, px )
endif
!if (fb%Lbuff) then
pb%pb(r:r+3) = px
!else
! write(fb%FID,REC=r) px
!endif
enddo
end do
endif
end subroutine pb_filltriangle3c !}}}
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!80
! pb_line( x1, y1, x2, y2, px )
! Draw a line from point to point to the terminal directly
! Bresenham algorithm
subroutine pb_line(pb, x1,y1,x2,y2,px) !{{{
implicit none
class(PixBuffType) :: pb
integer(kind=4) :: x1,x2,y1,y2, x, y, r, k
character(4) :: px
logical :: LX, LY, trans
LX=.false.
LY=.false.
trans=.false.
if ((x2-x1).ne.0) LX=.true.
if ((y2-y1).ne.0) LY=.true.
if (ichar(px(4:4)).gt.0.or.ichar(px(4:4)).gt.0) trans=.true.
! check for same starting and end point
if (.not.LX .and. .not.LY) then
k = pb%getrec(x1,y1)
if (trans) then
pb%pb(k:k+3) = pb%blendpx( k, px )
else
pb%pb(k:k+3) = px
endif
RETURN
endif
if (LX .and. abs(real(y2-y1)).lt.abs(real(x2-x1))) then
if (trans) then
do x=min(x1,x2),max(x1,x2)
y = PointSlope(x,x1,y1,x2,y2)
r = pb%getrec(x,y)
if (r.le.0) write(0,*) "fb Byte < 0. (x,y)=",x, y, x1, y1, x2, y2
!write(6,'(a2,i3.3,a1,i3.3,2a1)') char(27)//'[',PS(i,x1,y1,x2,y2),';',i,'H', "*"
pb%pb(r:r+3) = pb%blendpx( r, px )
end do
else
do x=min(x1,x2),max(x1,x2)
y = PointSlope(x,x1,y1,x2,y2)
r = pb%getrec(x,y)
if (r.le.0) write(0,*) "fb Byte < 0. (x,y)=",x, y, x1, y1, x2, y2
!write(6,'(a2,i3.3,a1,i3.3,2a1)') char(27)//'[',PS(i,x1,y1,x2,y2),';',i,'H', "*"
pb%pb(r:r+3) = px
end do
endif
else
if (trans) then
do y=min(y1,y2),max(y1,y2)
x = PointSlope(y,y1,x1,y2,x2)
r = pb%getrec(x,y)
if (r.le.0) write(0,*) "fb Byte < 0. (x,y)=",x, y, x1, y1, x2, y2
!write(6,'(a2,i3.3,a1,i3.3,2a1)') char(27)//'[',i,';',PS(i,y1,x1,y2,x2),'H', "*"
pb%pb(r:r+3) = pb%blendpx( r, px )
end do
else
do y=min(y1,y2),max(y1,y2)
x = PointSlope(y,y1,x1,y2,x2)
r = pb%getrec(x,y)
if (r.le.0) write(0,*) "fb Byte < 0. (x,y)=",x, y, x1, y1, x2, y2
!write(6,'(a2,i3.3,a1,i3.3,2a1)') char(27)//'[',i,';',PS(i,y1,x1,y2,x2),'H', "*"
pb%pb(r:r+3) = px
end do
endif
endif
end subroutine pb_line !}}}
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!80
! pb_line2c( x1, y1, x2, y2, px(2), [sb(2,2)], [z1, z2] )
! Draw a line from point to point to the terminal directly
! Bresenham algorithm
! use 'sb' to check bounds
! Can use with zbuffer.
subroutine pb_line2c(pb, x1,y1,x2,y2,px1,px2,sb,z1,z2) !{{{
implicit none
class(PixBuffType) :: pb
integer(kind=4) :: x1,x2,y1,y2, x, y, r, k, m1, m2
character(4) :: px1,px2, px
real(4), optional, intent(in) :: z1,z2
real(4) :: z, w(2), zp(2)
integer, dimension(2,2), optional, intent(in) :: sb
logical :: LX, LY, trans
LX=.false.
LY=.false.
trans=.false. ! transparency check
if ((x2-x1).ne.0) LX=.true.
if ((y2-y1).ne.0) LY=.true.
if (ichar(px1(4:4)).gt.0.or.ichar(px2(4:4)).gt.0) trans=.true.
! check for same starting and end point
if (.not.LX .and. .not.LY) then
if (pb%Lzbuff) then
if (pb%zbuff(x1,y1).gt.0.0.and.min(z1,z2).gt.pb%zbuff(x1,y1)) then !then it's behind what is already rendered
Return
else
pb%zbuff(x1,y1) = min(z1,z2) !save this to the Z-buffer and render it, since it's the closest
endif
endif
k = pb%getrec(x1,y1)
if (trans) then
pb%pb(k:k+3) = pb%blendpx( k, px1 )
else
pb%pb(k:k+3) = px1
endif
RETURN
endif
if (LX .and. abs(real(y2-y1)).lt.abs(real(x2-x1))) then
m1=min(x1,x2)
m2=max(x1,x2)
if (present(z1)) then
if (m1.eq.x1) then; zp(1) = z1; zp(2) = z2
else; zp(1) = z2; zp(2) = z1; endif
endif
do x=m1,m2
y = PointSlope(x,x1,y1,x2,y2)
if (present(sb)) then
if (x.lt.sb(1,1)) then; cycle !x too low
elseif (x.gt.sb(1,2)) then; cycle; endif !x too large
if (y.lt.sb(2,1)) then; cycle !y too low
elseif (y.gt.sb(2,2)) then; cycle; endif !y too large
endif
r = pb%getrec(x,y)
if (r.le.0) write(0,*) "fb Byte < 0. (x,y)=",x, y, x1, y1, x2, y2
w(1) = real(m2-x)/real(m2-m1) !1-0, full on the minimum, m1
if (m1.eq.x1) then; w(2) = 1.0-w(1)
else; w(2) = w(1); w(1) = 1.0-w(2)
endif
if (pb%Lzbuff) then
z = z1*w(1) +z2*w(2)
if (pb%zbuff(x,y).gt.0.0.and.z.gt.pb%zbuff(x,y)) then !then it's behind what is already rendered
cycle
else
pb%zbuff(x,y) = z !save this to the Z-buffer and render it, since it's the closest
endif
endif
! use the weights to interpolate each RGB value
px(1:1) = char(nint( ichar(px1(1:1))*w(1) +ichar(px2(1:1))*w(2) ))
px(2:2) = char(nint( ichar(px1(2:2))*w(1) +ichar(px2(2:2))*w(2) ))
px(3:3) = char(nint( ichar(px1(3:3))*w(1) +ichar(px2(3:3))*w(2) ))
px(4:4) = char(nint( ichar(px1(4:4))*w(1) +ichar(px2(4:4))*w(2) ))
!write(6,'(a2,i3.3,a1,i3.3,2a1)') char(27)//'[',PS(i,x1,y1,x2,y2),';',i,'H', "*"
if (trans) then
pb%pb(r:r+3) = pb%blendpx( r, px )
else
pb%pb(r:r+3) = px
endif
end do
else
m1=min(y1,y2)
m2=max(y1,y2)
if (present(z1)) then
if (m1.eq.x1) then; zp(1) = z1; zp(2) = z2
else; zp(1) = z2; zp(2) = z1; endif
endif
do y=m1,m2
x = PointSlope(y,y1,x1,y2,x2)
if (present(sb)) then