31 from __future__
import division
35 from python_qt_binding.QtCore
import QPoint, Qt
36 from python_qt_binding.QtOpenGL
import QGLFormat, QGLWidget
39 OpenGL.ERROR_CHECKING =
True 40 from OpenGL.GL
import glClear, glClearColor, glEnable, glGetDoublev, glLoadIdentity, glLoadMatrixd, glMatrixMode, glMultMatrixd, glRotated, glTranslated, glTranslatef, glViewport, GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT, GL_DEPTH_TEST, GL_MODELVIEW, GL_MODELVIEW_MATRIX, GL_PROJECTION
41 from OpenGL.GLU
import gluPerspective
47 glformat = QGLFormat()
48 glformat.setSampleBuffers(
True)
49 super(GLWidget, self).
__init__(glformat, parent)
51 self.setCursor(Qt.OpenHandCursor)
52 self.setMouseTracking(
True)
64 glClearColor(0.0, 0.0, 0.0, 0.0)
65 glEnable(GL_DEPTH_TEST)
68 glViewport(0, 0, width, height)
73 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
74 glMatrixMode(GL_MODELVIEW)
78 return self._modelview_matrix.tolist()
88 glMatrixMode(GL_PROJECTION)
90 height = max(self.height(), 1)
91 gluPerspective(self.
_fovy, float(self.width()) / float(height), self.
_near, self.
_far)
96 glMatrixMode(GL_MODELVIEW)
105 glMatrixMode(GL_MODELVIEW)
111 glMatrixMode(GL_MODELVIEW)
113 glTranslated(trans[0], trans[1], trans[2])
121 glMatrixMode(GL_MODELVIEW)
124 glTranslatef(t[0], t[1], t[2])
125 glRotated(angle, axis[0], axis[1], axis[2])
126 glTranslatef(-t[0], -t[1], -t[2])
136 if event.buttons() == Qt.NoButton:
138 delta = event.angleDelta().y()
139 except AttributeError:
140 delta = event.delta()
141 d = float(delta) / 200.0 * self.
_radius 151 new_point_2d = event.pos()
153 if not self.rect().contains(new_point_2d):
158 dy = float(new_point_2d.y() - self._last_point_2d.y())
159 h = float(self.height())
162 if event.buttons() == Qt.LeftButton
and event.modifiers() == Qt.NoModifier:
165 if abs(cos_angle) < 1.0:
167 angle = 2.0 * math.acos(cos_angle) * 180.0 / math.pi
171 elif event.buttons() == Qt.MidButton
or (event.buttons() == Qt.LeftButton
and event.modifiers() == Qt.ShiftModifier):
172 dx = float(new_point_2d.x() - self._last_point_2d.x())
173 w = float(self.width())
176 up = math.tan(self.
_fovy / 2.0 * math.pi / 180.0) * n
178 self.
translate([2.0 * dx / w * right / n * z, -2.0 * dy / h * up / n * z, 0.0])
181 elif event.buttons() == (Qt.LeftButton | Qt.MidButton)
or (event.buttons() == Qt.LeftButton
and event.modifiers() == Qt.ControlModifier):
182 delta_z = self.
_radius * dy * 2.0 / h
199 if self.rect().contains(pos):
201 v[0] = float(pos.x() - 0.5 * self.width()) / self.width()
202 v[1] = float(0.5 * self.height() - pos.y()) / self.height()
204 v[2] = math.sqrt(max(0.5 - v[0] * v[0] - v[1] * v[1], 0.0))
206 v = numpy.array(v) / numpy.linalg.norm(v)