-
Notifications
You must be signed in to change notification settings - Fork 2
/
dpg_demo.py
146 lines (105 loc) · 4.18 KB
/
dpg_demo.py
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
import OpenGL.GL as gl
import OpenGL.arrays.vbo as glvbo
from OpenGL import platform as gl_platform
from OpenGL.raw.GL.VERSION.GL_1_1 import glBindTexture
from OpenGL.raw.GL.VERSION.GL_1_5 import glBindBuffer
from OpenGL.raw.GL.VERSION.GL_3_0 import GL_COLOR_ATTACHMENT0, glBindFramebuffer, glGenFramebuffers
from OpenGL.raw.GL.VERSION.GL_3_2 import glFramebufferTexture
from vispy import gloo
import numpy
import dearpygui.dearpygui as dpg
from dearpygui.demo import show_demo
dpg.enable_docking(dock_space=True)
with dpg.font_registry():
# add font (set as default for entire app)
dpg.add_font(
"/usr/share/fonts/adobe-source-han-sans/SourceHanSansCN-Regular.otf",
40,
default_font=True)
import numpy as np
import threading
width = 800
height = 800
grid_arr = np.ones((width, height, 4), dtype=np.float32)
rng = np.random.default_rng()
with dpg.texture_registry():
image_id = dpg.add_raw_texture(width,
height,
grid_arr,
format=dpg.mvFormat_Float_rgba)
image_id_1 = dpg.add_raw_texture(width,
height,
grid_arr,
format=dpg.mvFormat_Float_rgba)
with dpg.window(label="Tutorial"):
# with dpg.drawlist(width=width, height=height):
dpg.add_image(image_id) # , (0, 0), (width, height))
should_continue = True
def setter():
import time
while dpg.get_raw_texture(image_id) == 0:
time.sleep(0.01)
texture_id = dpg.get_raw_texture(image_id)
dpg.set_update_enable(image_id, 0)
# fbo = gl.glGenFramebuffers(1)
# gl.glBindFramebuffer(gl.GL_FRAMEBUFFER, fbo)
# gl.glBindTexture(gl.GL_TEXTURE_2D, texture_id)
# glTexImage2D(gl.GL_TEXTURE_2D, 0, gl.GL_RGB, 400, 400, 0, gl.GL_RGBA, gl.GL_FLOAT, 0)
# glTextureParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
# glTextureParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
# glFramebufferTexture2D(
# GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, texture_id, 0
# )
while should_continue:
rng.standard_normal(out=grid_arr, dtype=np.float32)
# dpg.set_value(image_id, grid_arr)
time.sleep(0.01)
t = threading.Thread(target=setter)
t.start()
def exit_handler():
global should_continue
print("EXIT!")
should_continue = False
dpg.set_exit_callback(exit_handler)
class GLPlotter:
width, height = 800, 800
def __init__(self, texture_id) -> None:
self.data = np.array(.2*numpy.random.randn(100000,2),dtype=np.float32)
self.vbo = glvbo.VBO(self.data)
self.texture_id = texture_id
self.fbo = gl.GLuint()
glGenFramebuffers(1, self.fbo)
def paintGL(self):
self.data = np.array(.2*numpy.random.randn(100000,2),dtype=np.float32)
glBindFramebuffer(gl.GL_FRAMEBUFFER, self.fbo)
glBindTexture(gl.GL_TEXTURE_2D, self.texture_id)
self.vbo[:] = self.data
self.vbo.bind()
self.vbo.copy_data()
# gl.glTexSubImage2D(gl.GL_TEXTURE_2D, 0, 0, 0, width, height, gl.GL_RGBA, gl.GL_FLOAT, 0)
gl.glFramebufferTexture2D(gl.GL_FRAMEBUFFER, gl.GL_COLOR_ATTACHMENT0, gl.GL_TEXTURE_2D, self.texture_id, 0)
gl.glEnableClientState(gl.GL_VERTEX_ARRAY)
gl.glVertexPointer(2, gl.GL_FLOAT, 0, self.vbo)
gl.glClear(gl.GL_COLOR_BUFFER_BIT)
gl.glViewport(0, 0, width, height)
gl.glColor(1, 1, 0, 1)
gl.glDrawArrays(gl.GL_POINTS, 0, self.data.shape[0])
self.vbo.unbind()
glBindFramebuffer(gl.GL_FRAMEBUFFER, 0)
if __name__ == "__main__":
dpg.setup_viewport()
dpg_context = gl_platform.GetCurrentContext()
print(f"DPG context: {dpg_context}")
show_demo()
glp = None
while dpg.is_dearpygui_running():
if glp is None:
texture_id = dpg.get_raw_texture(image_id)
if texture_id != 0:
dpg.set_update_enable(image_id, 0)
glp = GLPlotter(texture_id)
print("Got Painter")
else:
glp.paintGL()
dpg.render_dearpygui_frame()
dpg.cleanup_dearpygui()