-
Notifications
You must be signed in to change notification settings - Fork 12
/
utils.pyx
884 lines (699 loc) · 26.7 KB
/
utils.pyx
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
from cython cimport view
from libcpp.vector cimport vector
cimport shader
import math
cdef class RGBA:
#cdef public float r,g,b,a
def __cinit__(self,r=1,g=1,b=1,a=1):
self.r = r
self.g = g
self.b = b
self.a = a
def __init__(self,r=1,g=1,b=1,a=1):
pass
def __getitem__(self,idx):
if isinstance(idx,slice):
return (self.r,self.g,self.b,self.a)[idx]
else:
if idx==0:
return self.r
elif idx==1:
return self.g
elif idx==2:
return self.b
elif idx==3:
return self.a
raise IndexError()
def __setitem__(self,idx,obj):
if isinstance(idx,slice):
t = [self.r,self.g,self.b,self.a]
t[idx] = obj
self.r,self.g,self.b,self.a = t
else:
raise IndexError()
basic_shader = None
simple_pt_shader = None
simple_yuv422_shader = None
simple_concentric_circle_shader = None
simple_circle_shader = None
cpdef init():
global basic_shader
global simple_pt_shader
global simple_yuv422_shader
global simple_concentric_circle_shader
global simple_circle_shader
basic_shader = None
simple_pt_shader = None
simple_yuv422_shader = None
simple_concentric_circle_shader = None
simple_circle_shader = None
if glewInit() != GLEW_OK:
raise Exception("GLEW could not be initialized!")
if not glewIsSupported("GL_VERSION_2_1"):
raise Exception("This OpenGL context is below 2.1.")
init_basic_shader()
cdef init_basic_shader():
global basic_shader # we cache the shader because we only create it the first time we call this fn.
if not basic_shader:
VERT_SHADER = """
#version 120
attribute vec3 vertex;
void main () {
gl_Position = gl_ModelViewProjectionMatrix * vec4(vertex,1.);
}
"""
FRAG_SHADER = """
#version 120
uniform vec4 color;
void main()
{
gl_FragColor = color;
}
"""
GEOM_SHADER = """"""
#shader link and compile
basic_shader = shader.Shader(VERT_SHADER,FRAG_SHADER,GEOM_SHADER)
cpdef draw_points(points,float size=20,RGBA color=RGBA(1.,0.5,0.5,.5),float sharpness=0.8):
global simple_pt_shader # we cache the shader because we only create it the first time we call this fn.
if not simple_pt_shader:
VERT_SHADER = """
#version 120
varying vec4 f_color;
uniform float size = 20;
uniform float sharpness = 0.8;
void main () {
gl_Position = gl_ModelViewProjectionMatrix*vec4(gl_Vertex.xyz,1.);
gl_PointSize = size;
f_color = gl_Color;
}
"""
FRAG_SHADER = """
#version 120
varying vec4 f_color;
uniform float size = 20;
uniform float sharpness = 0.8;
void main()
{
float dist = distance(gl_PointCoord.xy, vec2(0.5, 0.5))*size;
gl_FragColor = mix(f_color, vec4(f_color.rgb,0.0), smoothstep(sharpness*size*0.5, 0.5*size, dist));
}
"""
GEOM_SHADER = """"""
#shader link and compile
simple_pt_shader = shader.Shader(VERT_SHADER,FRAG_SHADER,GEOM_SHADER)
simple_pt_shader.bind()
simple_pt_shader.uniform1f('size',size)
simple_pt_shader.uniform1f('sharpness',sharpness)
glColor4f(color.r,color.g,color.b,color.a)
glBegin(GL_POINTS)
for pt in points:
if len(pt) == 2:
for pt in points:
glVertex2f(pt[0],pt[1])
else:
for pt in points:
glVertex3f(pt[0],pt[1],pt[2])
break
glEnd()
simple_pt_shader.unbind()
cpdef draw_circle( center_position = (0,0) ,float radius=20,float stroke_width= 2, RGBA color=RGBA(1.,0.5,0.5,0.5),float sharpness=0.8):
global simple_circle_shader
if not simple_circle_shader:
VERT_SHADER = """
#version 120
uniform vec2 center_position; // position in screen coordinates
uniform float radius;
uniform vec4 color;
uniform float stroke_width;
varying float quadSize;
void main () {
quadSize = radius * 2.0;
gl_Position = gl_ModelViewProjectionMatrix * vec4( center_position + quadSize * 0.5 * gl_Vertex.xy, 0.0, 1.0);
gl_TexCoord[0] = gl_MultiTexCoord0;
}
"""
FRAG_SHADER = """
#version 120
uniform vec4 color;
uniform float radius;
uniform float sharpness;
uniform float stroke_width;
varying float quadSize;
void main()
{
vec2 texCoord = gl_TexCoord[0].st ;
float center_distance = distance(texCoord.xy, vec2(0.5, 0.5)) * quadSize; // in pixels
if( center_distance <= radius ){
float radius_distance = abs(center_distance - radius); // in pixels
float factor = smoothstep(stroke_width, stroke_width*sharpness ,radius_distance ) * smoothstep(0.0 , stroke_width * (1.0-sharpness), radius_distance ) ;
gl_FragColor = mix(color, vec4(color.rgb,0.0), 1.0 - factor );
}else{
gl_FragColor = vec4(0,0,0,0);
}
}
"""
GEOM_SHADER = """"""
#shader link and compile
simple_circle_shader = shader.Shader(VERT_SHADER,FRAG_SHADER,GEOM_SHADER)
simple_circle_shader.bind()
simple_circle_shader.uniform1f('radius', radius)
simple_circle_shader.uniform1f('stroke_width', stroke_width)
simple_circle_shader.uniformf('center_position', center_position )
simple_circle_shader.uniform1f('sharpness',sharpness)
simple_circle_shader.uniformf('color', color[:] )
glBegin(GL_QUADS)
glTexCoord2f(0.0, 1.0)
glVertex2f(-0.5,-0.5)
glTexCoord2f(1.0, 1.0)
glVertex2f(0.5,-0.5)
glTexCoord2f(1.0, 0.0)
glVertex2f(0.5,0.5)
glTexCoord2f(0.0, 0.0)
glVertex2f(-0.5,0.5)
glEnd()
simple_circle_shader.unbind()
cpdef draw_concentric_circles( center_position = (0,0), radius = 10 , circle_count = 5, alpha = 1.0):
global simple_concentric_circle_shader
if not simple_concentric_circle_shader:
VERT_SHADER = """
#version 120
uniform vec2 center_position; // position in screen coordinates
uniform float radius = 10;
void main () {
float quadSize = radius * 2 * 1.1;
gl_Position = gl_ModelViewProjectionMatrix * vec4( center_position + quadSize * 0.5 * gl_Vertex.xy, 0.0, 1.0);
gl_TexCoord[0] = gl_MultiTexCoord0;
}
"""
FRAG_SHADER = """
#version 120
uniform float alpha = 1;
uniform float radius = 10;
uniform int circle_count = 4;
void main()
{
vec2 texCoord = gl_TexCoord[0].st ;
float quadSize = radius * 2 * 1.1;
float normalizedradius = radius/quadSize/circle_count;
float circleSize = normalizedradius * circle_count;
float dist = distance(texCoord , vec2(0.5,0.5));
float circleIndex = mod(dist/normalizedradius, 2);
float segmentDelta = fract(circleIndex) ;
int colorIndex = int(floor(circleIndex));
const float colors[2] = float[2](0.0f, 1.0f);
const float colors2[2] = float[2](1.0f, 0.0f);
vec3 color = vec3(colors[colorIndex]);
vec3 colorOpposit = vec3(colors2[colorIndex]);
const float sharpness = 0.3;
color = mix(color, colorOpposit, smoothstep(0.0, sharpness, segmentDelta) );
if( dist > circleSize ){
float delta = dist - circleSize;
gl_FragColor = vec4(color, alpha * smoothstep(1.0-sharpness, 1.0, (0.207 - delta)/0.207 ) ); // 0.207 = sqrt(0.5^2 + 0.5^2) - 0.5 , length of texture corner to outer circle radius
}
else
gl_FragColor = vec4(color,alpha);
}
"""
GEOM_SHADER = """"""
#shader link and compile
simple_concentric_circle_shader = shader.Shader(VERT_SHADER,FRAG_SHADER,GEOM_SHADER)
simple_concentric_circle_shader.bind()
simple_concentric_circle_shader.uniform1f('alpha', alpha)
simple_concentric_circle_shader.uniform1f('radius', radius)
simple_concentric_circle_shader.uniform1i('circle_count', circle_count)
simple_concentric_circle_shader.uniformf('center_position', center_position )
glBegin(GL_QUADS)
glTexCoord2f(0.0, 1.0)
glVertex2f(-0.5,-0.5)
glTexCoord2f(1.0, 1.0)
glVertex2f(0.5,-0.5)
glTexCoord2f(1.0, 0.0)
glVertex2f(0.5,0.5)
glTexCoord2f(0.0, 0.0)
glVertex2f(-0.5,0.5)
glEnd()
simple_concentric_circle_shader.unbind()
cpdef draw_points_norm(points,float size=20,RGBA color=RGBA(1.,0.5,0.5,.5),float sharpness=0.8):
glMatrixMode(GL_PROJECTION)
glPushMatrix()
glLoadIdentity()
glOrtho(0, 1, 0, 1, -1, 1) # gl coord convention
glMatrixMode(GL_MODELVIEW)
glPushMatrix()
glLoadIdentity()
draw_points(points,size,color,sharpness)
glMatrixMode(GL_PROJECTION)
glPopMatrix()
glMatrixMode(GL_MODELVIEW)
glPopMatrix()
cpdef draw_polyline(verts,float thickness=1,RGBA color=RGBA(1.,0.5,0.5,.5),line_type = GL_LINE_STRIP):
glColor4f(color.r,color.g,color.b,color.a)
glLineWidth(thickness)
glBegin(line_type)
for pt in verts:
if len(pt) == 2:
for pt in verts:
glVertex2f(pt[0],pt[1])
else:
for pt in verts:
glVertex3f(pt[0],pt[1],pt[2])
break
glEnd()
cpdef draw_polyline_norm(verts,float thickness=1,RGBA color=RGBA(1.,0.5,0.5,.5),line_type = GL_LINE_STRIP):
glMatrixMode(GL_PROJECTION)
glPushMatrix()
glLoadIdentity()
glOrtho(0, 1, 0, 1, -1, 1) # gl coord convention
glMatrixMode(GL_MODELVIEW)
glPushMatrix()
glLoadIdentity()
draw_polyline(verts,thickness,color,line_type)
glMatrixMode(GL_PROJECTION)
glPopMatrix()
glMatrixMode(GL_MODELVIEW)
glPopMatrix()
cdef class Sphere:
### OpenGL funtions for creating and drawing a sphere.
def __cinit__(self):
pass
def __init__(self,resolution=10):
cdef int doubleRes = resolution*2
cdef float polarInc = math.pi/resolution
cdef float azimInc = math.pi*2.0/doubleRes
cdef vector[GLfloat] vertices
cdef vector[GLuint] indices
cdef float nx,ny,nz
cdef float tr
for i in range(0, resolution+1):
tr = math.sin( math.pi-i * polarInc )
ny = math.cos( math.pi-i * polarInc )
for j in range(0, doubleRes+1):
nx = tr * math.sin(j * azimInc)
nz = tr * math.cos(j * azimInc)
vertices.push_back(nx)
vertices.push_back(ny)
vertices.push_back(nz)
nr = doubleRes+1
for y in range(0,resolution ):
for x in range(0,doubleRes+1 ):
indices.push_back( y*nr + x )
indices.push_back( (y+1)*nr + x )
self.vertex_buffer_size = vertices.size() * sizeof(GLfloat)
self.indices_amount = indices.size()
self.index_buffer_size = indices.size() * sizeof(GLuint)
glGenBuffers(1, &self.index_buffer_id)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, self.index_buffer_id)
glBufferData(GL_ELEMENT_ARRAY_BUFFER, self.index_buffer_size, &indices[0], GL_STATIC_DRAW)
glGenBuffers(1, &self.vertex_buffer_id)
glBindBuffer(GL_ARRAY_BUFFER, self.vertex_buffer_id)
glBufferData(GL_ARRAY_BUFFER, self.vertex_buffer_size, &vertices[0] , GL_STATIC_DRAW)
glBindBuffer(GL_ARRAY_BUFFER,0)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0)
def draw(self, color = RGBA(0.5,0.5,0,0.5), primitive_type = GL_LINE_STRIP):
basic_shader.bind()
basic_shader.uniformf('color', color[:] )
glBindAttribLocation(basic_shader.handle , 0 , 'vertex')
glBindBuffer(GL_ARRAY_BUFFER, self.vertex_buffer_id)
glEnableVertexAttribArray(0)
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL )
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, self.index_buffer_id)
glDrawElements(primitive_type, self.indices_amount , GL_UNSIGNED_INT, NULL )
glBindBuffer(GL_ARRAY_BUFFER, 0)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0)
glDisableVertexAttribArray(0)
basic_shader.unbind()
def __dealloc__(self):
glDeleteBuffers(1,&self.index_buffer_id)
glDeleteBuffers(1,&self.vertex_buffer_id)
cdef class Named_Texture:
### OpenGL funtions for creating, updating and drawing a texture.
### Using a Frame object to update.
#cdef GLuint texture_id
#cdef bint use_yuv_shader
def __cinit__(self):
pass
def __init__(self):
self.texture_id = create_named_texture()
def update_from_ndarray(self,img):
update_named_texture(self.texture_id,img)
self.use_yuv_shader = False
def update_from_yuv_buffer(self,yuv_buffer,width,height):
update_named_texture_yuv422(self.texture_id,yuv_buffer,width,height)
self.use_yuv_shader = True
def draw(self,interpolation=True, quad=((0.,0.),(1.,0.),(1.,1.),(0.,1.)),alpha=1.0):
if self.use_yuv_shader:
draw_named_texture_yuv422(self.texture_id,interpolation,quad,alpha)
else:
draw_named_texture(self.texture_id,interpolation,quad,alpha)
def __dealloc__(self):
destroy_named_texture(self.texture_id)
cpdef GLuint create_named_texture():
cdef GLuint texture_id = 0
glGenTextures(1, &texture_id)
return texture_id
cpdef destroy_named_texture(int texture_id):
cdef GLuint texture_cid = texture_id
glDeleteTextures(1,&texture_cid)
cpdef update_named_texture_yuv422(texture_id, unsigned char[::1] imageData, width, height):
glBindTexture(GL_TEXTURE_2D, texture_id)
glPixelStorei(GL_UNPACK_ALIGNMENT,1)
# Create Texture and upload data
glTexImage2D(GL_TEXTURE_2D,
0,
GL_LUMINANCE,
width ,
height * 2, # take the sampling in to account
0,
GL_LUMINANCE,
GL_UNSIGNED_BYTE,
<void*>&imageData[0])
glBindTexture(GL_TEXTURE_2D, 0)
cpdef draw_named_texture_yuv422(texture_id , interpolation=True, quad=((0.,0.),(1.,0.),(1.,1.),(0.,1.)),alpha=1.0 ):
"""
We draw the image as a texture on a quad from 0,0 to img.width,img.height.
We set the coord system to pixel dimensions.
to save cpu power, update can be false and we will reuse the old img instead of uploading the new.
"""
global simple_yuv422_shader
if not simple_yuv422_shader:
VERT_SHADER = """
#version 120
void main () {
gl_Position = gl_ModelViewProjectionMatrix*vec4(gl_Vertex.xyz,1.);
gl_TexCoord[0] = gl_MultiTexCoord0;
}
"""
FRAG_SHADER = """
#version 120
// texture sampler of the YUV422 image
uniform sampler2D yuv422_image;
float getYPixel(vec2 texCoord) {
texCoord.y = texCoord.y / 2.0;
return texture2D(yuv422_image, texCoord).x;
}
float getUPixel(vec2 position) {
float x = position.x / 2.0;
float y = position.y / 4.0 + 0.5;
return texture2D(yuv422_image, vec2(x,y)).x;
}
float getVPixel(vec2 position) {
float x = position.x / 2.0 ;
float y = position.y / 4.0 + 0.75;
return texture2D(yuv422_image, vec2(x,y)).x;
}
void main()
{
vec2 texCoord = gl_TexCoord[0].st;
float yChannel = getYPixel(texCoord);
float uChannel = getUPixel(texCoord);
float vChannel = getVPixel(texCoord);
// This does the colorspace conversion from Y'UV to RGB as a matrix
// multiply. It also does the offset of the U and V channels from
// [0,1] to [-.5,.5] as part of the transform.
vec4 channels = vec4(yChannel, uChannel, vChannel, 1.0);
mat4 conversion = mat4(1.0, 0.0, 1.402, -0.701,
1.0, -0.344, -0.714, 0.529,
1.0, 1.772, 0.0, -0.886,
0, 0, 0, 0);
vec3 rgb = (channels * conversion).xyz;
gl_FragColor = vec4(rgb, 1.0);
}
"""
simple_yuv422_shader = shader.Shader(VERT_SHADER, FRAG_SHADER, "")
glActiveTexture(GL_TEXTURE0)
glBindTexture(GL_TEXTURE_2D, texture_id)
glEnable(GL_TEXTURE_2D)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) # interpolation here
if not interpolation:
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
simple_yuv422_shader.bind()
simple_yuv422_shader.uniform1i("yuv422_image", 0)
# someday replace with this:
# glEnableClientState(GL_VERTEX_ARRAY)
# glEnableClientState(GL_TEXTURE_COORD_ARRAY)
# Varray = numpy.array([[0,0],[0,1],[1,1],[1,0]],numpy.float)
# glVertexPointer(2,GL_FLOAT,0,Varray)
# glTexCoordPointer(2,GL_FLOAT,0,Varray)
# indices = [0,1,2,3]
# glDrawElements(GL_QUADS,1,GL_UNSIGNED_SHORT,indices)
glColor4f(1.0,1.0,1.0,alpha)
# Draw textured Quad.
glBegin(GL_QUADS)
# glTexCoord2f(0.0, 0.0)
glTexCoord2f(0.0, 1.0)
glVertex2f(quad[0][0],quad[0][1])
glTexCoord2f(1.0, 1.0)
glVertex2f(quad[1][0],quad[1][1])
glTexCoord2f(1.0, 0.0)
glVertex2f(quad[2][0],quad[2][1])
glTexCoord2f(0.0, 0.0)
glVertex2f(quad[3][0],quad[3][1])
glEnd()
simple_yuv422_shader.unbind()
glBindTexture(GL_TEXTURE_2D, 0)
glDisable(GL_TEXTURE_2D)
cpdef update_named_texture(texture_id, image):
cdef unsigned char[:,:,:] data_3
cdef unsigned char[:,:] data_1
glBindTexture(GL_TEXTURE_2D, texture_id)
if len(image.shape) == 2:
height, width = image.shape
channels = 1
data_1 = image
else:
height, width, channels = image.shape
data_3 = image
gl_blend = (None,GL_LUMINANCE,None,GL_BGR,GL_BGRA)[channels]
gl_blend_init = (None,GL_LUMINANCE,None,GL_RGB,GL_RGBA)[channels]
glPixelStorei(GL_UNPACK_ALIGNMENT,1)
# Create Texture and upload data
if channels ==1:
glTexImage2D(GL_TEXTURE_2D,
0,
gl_blend_init,
width,
height,
0,
gl_blend,
GL_UNSIGNED_BYTE,
<void*>&data_1[0,0])
else:
glTexImage2D(GL_TEXTURE_2D,
0,
gl_blend_init,
width,
height,
0,
gl_blend,
GL_UNSIGNED_BYTE,
<void*>&data_3[0,0,0])
glBindTexture(GL_TEXTURE_2D, 0)
cpdef draw_named_texture(texture_id, interpolation=True, quad=((0.,0.),(1.,0.),(1.,1.),(0.,1.)),alpha=1.0):
"""
We draw the image as a texture on a quad from 0,0 to img.width,img.height.
We set the coord system to pixel dimensions.
to save cpu power, update can be false and we will reuse the old img instead of uploading the new.
"""
glBindTexture(GL_TEXTURE_2D, texture_id)
glEnable(GL_TEXTURE_2D)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) # interpolation here
if not interpolation:
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
# someday replace with this:
# glEnableClientState(GL_VERTEX_ARRAY)
# glEnableClientState(GL_TEXTURE_COORD_ARRAY)
# Varray = numpy.array([[0,0],[0,1],[1,1],[1,0]],numpy.float)
# glVertexPointer(2,GL_FLOAT,0,Varray)
# glTexCoordPointer(2,GL_FLOAT,0,Varray)
# indices = [0,1,2,3]
# glDrawElements(GL_QUADS,1,GL_UNSIGNED_SHORT,indices)
glColor4f(1.0,1.0,1.0,alpha)
# Draw textured Quad.
glBegin(GL_QUADS)
# glTexCoord2f(0.0, 0.0)
glTexCoord2f(0.0, 1.0)
glVertex2f(quad[0][0],quad[0][1])
glTexCoord2f(1.0, 1.0)
glVertex2f(quad[1][0],quad[1][1])
glTexCoord2f(1.0, 0.0)
glVertex2f(quad[2][0],quad[2][1])
glTexCoord2f(0.0, 0.0)
glVertex2f(quad[3][0],quad[3][1])
glEnd()
glBindTexture(GL_TEXTURE_2D, 0)
glDisable(GL_TEXTURE_2D)
def draw_gl_texture(image,interpolation=True,alpha=1.0):
"""
We draw the image as a texture on a quad from 0,0 to img.width,img.height.
Simple anaymos texture one time use. Look at named texture fn's for better perfomance
"""
cdef unsigned char[:,:,:] data_3
cdef unsigned char[:,:] data_1
if len(image.shape) == 2:
height, width = image.shape
channels = 1
data_1 = image
else:
height, width, channels = image.shape
if channels == 1:
data_1 = image
else:
data_3 = image
gl_blend = (None,GL_LUMINANCE,None,GL_BGR,GL_BGRA)[channels]
gl_blend_init = (None,GL_LUMINANCE,None,GL_RGB,GL_RGBA)[channels]
glPixelStorei(GL_UNPACK_ALIGNMENT,1)
glEnable(GL_TEXTURE_2D)
# Create Texture and upload data
if channels == 1:
glTexImage2D(GL_TEXTURE_2D,
0,
gl_blend_init,
width,
height,
0,
gl_blend,
GL_UNSIGNED_BYTE,
<void*>&data_1[0,0])
else:
glTexImage2D(GL_TEXTURE_2D,
0,
gl_blend_init,
width,
height,
0,
gl_blend,
GL_UNSIGNED_BYTE,
<void*>&data_3[0,0,0])
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) # interpolation here
if not interpolation:
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glColor4f(1.0,1.0,1.0,alpha)
# Draw textured Quad.
glBegin(GL_QUADS)
glTexCoord2f(0.0, 1.0)
glVertex2f(0,0)
glTexCoord2f(1.0, 1.0)
glVertex2f(1,0)
glTexCoord2f(1.0, 0.0)
glVertex2f(1,1)
glTexCoord2f(0.0, 0.0)
glVertex2f(0,1)
glEnd()
glDisable(GL_TEXTURE_2D)
cdef class Render_Target:
### OpenGL funtions for rendering to texture.
### Using this saves us considerable cpu/gpu time when the UI remains static.
#cdef fbo_tex_id fbo_tex defined in .pxd
def __cinit__(self,w,h):
pass
def __init__(self,w,h):
self.fbo_tex = create_ui_texture(w,h)
def push(self):
render_to_ui_texture(self.fbo_tex)
def pop(self):
render_to_screen()
def draw(self,float alpha=1.0):
draw_ui_texture(self.fbo_tex,alpha)
def resize(self,int w, int h):
resize_ui_texture(self.fbo_tex,w,h)
def __dealloc__(self):
destroy_ui_texture(self.fbo_tex)
### OpenGL funtions for rendering to texture.
### Using this saves us considerable cpu/gpu time when the UI remains static.
cdef fbo_tex_id create_ui_texture(int w,int h):
cdef fbo_tex_id ui_layer
ui_layer.fbo_id = 0
ui_layer.tex_id = 0
# create Framebufer Object
#requires gl ext or opengl > 3.0
glGenFramebuffers(1, &ui_layer.fbo_id)
glBindFramebuffer(GL_FRAMEBUFFER, ui_layer.fbo_id)
#create texture object
glGenTextures(1, &ui_layer.tex_id)
glBindTexture(GL_TEXTURE_2D, ui_layer.tex_id)
# configure Texture
glTexImage2D(GL_TEXTURE_2D, 0,GL_RGBA, w,
h, 0,GL_RGBA, GL_UNSIGNED_BYTE,
NULL)
#set filtering
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
#attach texture to fbo
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
GL_TEXTURE_2D, ui_layer.tex_id, 0)
if glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE:
raise Exception("UI Framebuffer could not be created.")
#unbind fbo and texture
glBindTexture(GL_TEXTURE_2D, 0)
glBindFramebuffer(GL_FRAMEBUFFER, 0)
return ui_layer
cdef destroy_ui_texture(fbo_tex_id ui_layer):
glDeleteTextures(1,&ui_layer.tex_id)
glDeleteFramebuffers(1,&ui_layer.fbo_id)
cdef resize_ui_texture(fbo_tex_id ui_layer, int w,int h):
glBindTexture(GL_TEXTURE_2D, ui_layer.tex_id)
glTexImage2D(GL_TEXTURE_2D, 0,GL_RGBA, w,
h, 0,GL_RGBA, GL_UNSIGNED_BYTE,
NULL)
glBindTexture(GL_TEXTURE_2D, 0)
cdef render_to_ui_texture(fbo_tex_id ui_layer):
# set fbo as render target
# blending method after:
# http://stackoverflow.com/questions/24346585/opengl-render-to-texture-with-partial-transparancy-translucency-and-then-rende/24380226#24380226
glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA,
GL_ONE_MINUS_DST_ALPHA, GL_ONE)
glBindFramebuffer(GL_FRAMEBUFFER, ui_layer.fbo_id)
glClearColor(0.,0.,0.,0.)
glClear(GL_COLOR_BUFFER_BIT)
cdef render_to_screen():
# set rendertarget 0
glBindFramebuffer(GL_FRAMEBUFFER, 0)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
cdef draw_ui_texture(fbo_tex_id ui_layer,float alpha = 1.0):
# render texture
# set blending
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA)
# bind texture and use.
glBindTexture(GL_TEXTURE_2D, ui_layer.tex_id)
glEnable(GL_TEXTURE_2D)
#set up coord system
glMatrixMode(GL_PROJECTION)
glPushMatrix()
glLoadIdentity()
glOrtho(0, 1, 1, 0, -1, 1)
glMatrixMode(GL_MODELVIEW)
glPushMatrix()
glLoadIdentity()
glColor4f(alpha,alpha,alpha,alpha)
# Draw textured Quad.
glBegin(GL_QUADS)
glTexCoord2f(0.0, 1.0)
glVertex2f(0,0)
glTexCoord2f(1.0, 1.0)
glVertex2f(1,0)
glTexCoord2f(1.0, 0.0)
glVertex2f(1,1)
glTexCoord2f(0.0, 0.0)
glVertex2f(0,1)
glEnd()
#pop coord systems
glMatrixMode(GL_PROJECTION)
glPopMatrix()
glMatrixMode(GL_MODELVIEW)
glPopMatrix()
glBindTexture(GL_TEXTURE_2D, 0)
glDisable(GL_TEXTURE_2D)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
cpdef push_ortho(int w,int h):
glMatrixMode(GL_PROJECTION)
glPushMatrix()
glLoadIdentity()
glOrtho(0, w,h, 0, -1, 1)
glMatrixMode(GL_MODELVIEW)
glPushMatrix()
glLoadIdentity()
cpdef pop_ortho():
glMatrixMode(GL_PROJECTION)
glPopMatrix()
glMatrixMode(GL_MODELVIEW)
glPopMatrix()