def screen_to_model(c): """ Returns the 3D coordinate of given screen coordinate :param c: The screen coordinates for example :type c: 3-tuple of float (x,y,0) :rtype: 3-tuple float (x,y,z) """ m = (c_double*16)() glGetDoublev(GL_MODELVIEW_MATRIX, m) p = (c_double*16)() glGetDoublev(GL_PROJECTION_MATRIX, p) v = (c_int*4)() glGetIntegerv(GL_VIEWPORT, v) x, y, z = c_double(), c_double(), c_double() y.value = v[3] - y.value gluUnProject(c[0], c[1], c[2], m, p, v, x, y, z) return x, y, z
def screen_to_model(x, y, z): m = get_model_matrix(c_double, pgl.glGetDoublev) p = get_projection_matrix(c_double, pgl.glGetDoublev) w = get_viewport() mx, my, mz = c_double(), c_double(), c_double() pgl.gluUnProject(x, y, z, m, p, w, mx, my, mz) return float(mx.value), float(my.value), float(mz.value)
def mouse_to_ray(self, x, y, local_transform=False): x = float(x) y = self.height - float(y) pmat = (GLdouble * 16)() mvmat = (GLdouble * 16)() viewport = (GLint * 4)() px = (GLdouble)() py = (GLdouble)() pz = (GLdouble)() glGetIntegerv(GL_VIEWPORT, viewport) glGetDoublev(GL_PROJECTION_MATRIX, pmat) mvmat = self.get_modelview_mat(local_transform) gluUnProject(x, y, 1, mvmat, pmat, viewport, px, py, pz) ray_far = (px.value, py.value, pz.value) gluUnProject(x, y, 0., mvmat, pmat, viewport, px, py, pz) ray_near = (px.value, py.value, pz.value) return ray_near, ray_far
def convert_to_obj_coordinates(self, x, y): x = int(x) * self.rect._m_scale y = int(y) * self.rect._m_scale pmat = (gl.GLdouble * 16)() mvmat = (gl.GLdouble * 16)() viewport = (gl.GLint * 4)() # Model, projection and viewport matrices gl.glGetDoublev(gl.GL_MODELVIEW_MATRIX, mvmat) gl.glGetDoublev(gl.GL_PROJECTION_MATRIX, pmat) gl.glGetIntegerv(gl.GL_VIEWPORT, viewport) # Unproject point = [gl.GLdouble() for _ in range(3)] gl.gluUnProject(x, y, 1, mvmat, pmat, viewport, *point) obj_coord = [v.value for v in point] return obj_coord[0:2]
def mouse_to_ray(self, x, y, local_transform = False): x = float(x) y = self.height - float(y) pmat = (GLdouble * 16)() mvmat = (GLdouble * 16)() viewport = (GLint * 4)() px = (GLdouble)() py = (GLdouble)() pz = (GLdouble)() glGetIntegerv(GL_VIEWPORT, viewport) glGetDoublev(GL_PROJECTION_MATRIX, pmat) mvmat = self.get_modelview_mat(local_transform) gluUnProject(x, y, 1, mvmat, pmat, viewport, px, py, pz) ray_far = (px.value, py.value, pz.value) gluUnProject(x, y, 0., mvmat, pmat, viewport, px, py, pz) ray_near = (px.value, py.value, pz.value) return ray_near, ray_far
def mouse_to_3d(self, x, y, z=1.0, local_transform=False): x = float(x) y = self.height - float(y) # The following could work if we were not initially scaling to zoom on # the bed # if self.orthographic: # return (x - self.width / 2, y - self.height / 2, 0) pmat = (GLdouble * 16)() mvmat = self.get_modelview_mat(local_transform) viewport = (GLint * 4)() px = (GLdouble)() py = (GLdouble)() pz = (GLdouble)() glGetIntegerv(GL_VIEWPORT, viewport) glGetDoublev(GL_PROJECTION_MATRIX, pmat) glGetDoublev(GL_MODELVIEW_MATRIX, mvmat) gluUnProject(x, y, z, mvmat, pmat, viewport, px, py, pz) return (px.value, py.value, pz.value)
def mouse_to_3d(self, x, y, z = 1.0, local_transform = False): x = float(x) y = self.height - float(y) # The following could work if we were not initially scaling to zoom on # the bed # if self.orthographic: # return (x - self.width / 2, y - self.height / 2, 0) pmat = (GLdouble * 16)() mvmat = self.get_modelview_mat(local_transform) viewport = (GLint * 4)() px = (GLdouble)() py = (GLdouble)() pz = (GLdouble)() glGetIntegerv(GL_VIEWPORT, viewport) glGetDoublev(GL_PROJECTION_MATRIX, pmat) glGetDoublev(GL_MODELVIEW_MATRIX, mvmat) gluUnProject(x, y, z, mvmat, pmat, viewport, px, py, pz) return (px.value, py.value, pz.value)
def _worldobj_on_translate(control, target, x, y, dx, dy): from miru.context import context from miru import camera self = control if isinstance(context.camera, camera.MetaCamera): context.camera.focussed.render() viewport = (gl.GLint * 4)() mvmatrix = (gl.GLdouble * 16)() projmatrix = (gl.GLdouble * 16)() gl.glGetIntegerv(gl.GL_VIEWPORT, viewport) gl.glGetDoublev(gl.GL_MODELVIEW_MATRIX, mvmatrix) gl.glGetDoublev(gl.GL_PROJECTION_MATRIX, projmatrix) wx = gl.GLdouble() wy = gl.GLdouble() wz = gl.GLdouble() sz = gl.GLdouble() ex = target.pos.x ey = target.pos.y ez = target.pos.z gl.gluProject(ex, ey, ez, mvmatrix, projmatrix, viewport, wx, wy, sz) gl.gluUnProject(x, y, sz, mvmatrix, projmatrix, viewport, wx, wy, wz) if self.axis == self.AXIS_X: target.pos = (wx.value, wz.value, target.pos.z) elif self.axis == self.AXIS_Y: target.pos = (target.pos.x, wy.value, wz.value) elif self.axis == self.AXIS_Z: tz = dy * 0.01 tz += (dx * 0.01) target.pos += (0, 0, tz) else: target.pos = (wx.value, wy.value, wz.value)