-
Notifications
You must be signed in to change notification settings - Fork 1
/
myGLWidget.py
242 lines (187 loc) · 8.07 KB
/
myGLWidget.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
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
from PyQt5 import QtGui
from PyQt5.QtCore import Qt
from PyQt5 import QtOpenGL # provides QGLWidget, a special OpenGL QWidget
from OpenGL.GL import * # python wrapping of OpenGL
from OpenGL import GLU # OpenGL Utility Library, extends OpenGL functionality
from OpenGL.arrays import vbo
import numpy as np
from myGLutils import drawCube,drawVector,drawVBOs
class GLWidget(QtOpenGL.QGLWidget):
def __init__(self, parent=None):
self.parent = parent
self.pos_vbo=None
self.col_vbo=None
self.numpoints=0
self.spacesize = 64.0
self.campos=np.array([0, 0, 0],np.float32)
self.camYangle=0.0
self.labelinfo=None
self.arrows=[]
self.Btext=""
self.bTextInfo=True
# camera speed for rotating and moving
self.rotangle=3.0
self.speed=2.0
QtOpenGL.QGLWidget.__init__(self, self.parent)
def setTextInfo(self,bShow):
self.bTextInfo=bShow
def drawOGLText(self,pos,text,fontsize=12, color=[1.,1.,1.]):
glPushMatrix ()
glTranslate(-pos[0], -pos[1], -pos[2])
glColor3f(color[0], color[1],color[2])
font = QtGui.QFont("Arial");
font.setPointSize(fontsize)
self.renderText(0,0,0,text,font)
glPopMatrix ()
def scaleArrow(self,na,s):
self.arrows[na*4+3]*=s
def clearArrows(self):
self.arrows=[]
def setspacesize(self,spacesize):
self.spacesize = spacesize
self.update()
def setVBOs(self,pos_vbo,col_vbo,numpoints):
self.pos_vbo=pos_vbo
self.col_vbo=col_vbo
self.numpoints=numpoints
#self.update()
def addArrow(self,apos, avector, color,length=22.):
self.arrows.append(apos)
self.arrows.append(avector)
self.arrows.append(color)
self.arrows.append(length)
def keyPressEvent(self, event):
if event.key() == Qt.Key_Up:
if event.modifiers() == Qt.ShiftModifier:
self.movecam(Yinc=-self.speed)
else:
self.movecam(-self.speed)
elif event.key() == Qt.Key_Down:
if event.modifiers() == Qt.ShiftModifier:
self.movecam(Yinc=self.speed)
else:
self.movecam(self.speed)
elif event.key() == Qt.Key_Right:
if event.modifiers() == Qt.ShiftModifier:
self.movecam(Ry=self.rotangle)
else:
self.movecam(0,self.speed)
elif event.key() == Qt.Key_Left:
if event.modifiers() == Qt.ShiftModifier:
self.movecam(Ry=-self.rotangle)
else:
self.movecam(0,-self.speed)
def setlabelinfo(self, labelinfo):
self.labelinfo=labelinfo
def __del__(self):
print("GLWidget destructor")
def initializeGL(self):
self.qglClearColor(QtGui.QColor(0., 0, 0)) # initialize the screen to blue
glEnable(GL_DEPTH_TEST) # enable depth testing
def resizeGL(self, width, height):
glViewport(0, 0, width, height)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
aspect = width / float(height)
GLU.gluPerspective(45.0, aspect, 1.0, 300.0)
glMatrixMode(GL_MODELVIEW)
def movecam(self,speedfrontal=0,speedlateral=0,Yinc=0, Ry=0):
if Yinc != 0.: self.campos[1] +=Yinc
if Ry != 0.:
self.camYangle+=Ry
if self.camYangle>=360: self.camYangle-=360.
if self.camYangle<0: self.camYangle+=360.
# forward move
dirx= np.sin( self.camYangle * np.pi/180.0)
dirz= -np.cos(self.camYangle*np.pi/180.0)
self.campos[0] += dirx * speedfrontal
self.campos[2] += dirz * speedfrontal
# lateral move
angulolateral =self.camYangle-90.0
if angulolateral<0:
angulolateral+=360
dirx= np.sin(angulolateral*np.pi/180.0)
dirz= -np.cos(angulolateral*np.pi/180.0)
self.campos[0] +=dirx * speedlateral
self.campos[2] +=dirz * speedlateral
#self.paintGL()
self.update()
if self.labelinfo is not None:
self.labelinfo.setText(str(np.round(self.campos,1)))
def drawArrows(self):
for i in range(len( self.arrows)//4):
self.drawArrow(i)
def drawArrow(self,i):
if len( self.arrows)//4 > i:
pos=self.arrows[i*4]
vec=self.arrows[i*4+1]
color=self.arrows[i*4+2]
length=self.arrows[i*4+3]
head= length*0.025
drawVector(pos,vec,length,head,color)
def drawAxes(self):
color=[1.,1.,1.]
# X
pos = [self.spacesize/2, self.spacesize/2+6, -self.spacesize/2]
self.drawOGLText(pos,"X ",fontsize=16)
pos =np.array(pos)*-1.
pos[0]+=3.5
pos[1]+=1.3
vec=[1,0.,0]
drawVector(pos,vec,7,0.3,color)
# Y
pos = [-self.spacesize/2-4,self.spacesize/2,-self.spacesize/2]
self.drawOGLText(pos,"Y",fontsize=16)
pos =np.array(pos)*-1.
pos[1]+=4.
pos[0]+=1.
vec=[0,1.0,0]
drawVector(pos,vec,7,0.3,color)
# Y
def paintGL(self):
#self.makeCurrent()
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
# convert ogl axis(Y up x right) to matplotlib axis (Z up y right)
# rotating
glRotate(self.camYangle, 0.0, 1.0, 0.0)
glTranslate(self.campos[0], self.campos[1], self.campos[2])
glRotate(-90, 1.0, 0.0, 0.0)
drawCube(0,0, 0,self.spacesize/2,self.spacesize/2,self.spacesize/2)
self.drawAxes()
if self.Btext!="" and self.bTextInfo:
pos = [ -self.spacesize*0.7,0,0]
self.drawOGLText(pos,self.Btext,fontsize=14,color=[0.9, 0, 0])
if self.pos_vbo is not None:
# B arrow
if len( self.arrows)//4==2:
self.drawArrow(0)
# translate to psi coordinates
glTranslate(-self.spacesize/2.,
-self.spacesize/2., -self.spacesize/2)
# spin arrow
if len( self.arrows)//4==2:
self.drawArrow(1)
else: self.drawArrow(0)
drawVBOs( self.pos_vbo, self.col_vbo , self.numpoints)
########################
else:
if self.bTextInfo:
self.drawOGLText([self.spacesize/2,0,0],"OPENGL DIRAC MAGNETIC FIELD")
self.drawOGLText([self.spacesize/2,0,10],"Click and move with arrow keys + shift")
self.drawArrows()
#glPopMatrix()
#painter.endNativePainting()
## painter = QtGui.QPainter(self)
## painter.begin(self)
## pen = QtGui.QPen(QtGui.QColor(255,0,0), 10)
## painter.setPen(pen)
## painter.setRenderHint(QtGui.QPainter.Antialiasing, True)
## painter.setRenderHint(QtGui.QPainter.TextAntialiasing,True)
## painter.drawText(10,10, "texto QPainter en QGLWidget.PaintGL")
## painter.end()
def camgoto(self,campos):
self.campos=np.array(campos,np.float32)
self.update()
if self.labelinfo is not None:
self.labelinfo.setText(str(np.round(self.campos,1)))