def _Screen2Real(self, x, y, expZ=0.0): ''' Return the x and y in real coordination if the input is the x and y in screen coordination http://nehe.gamedev.net/article/using_gluunproject/16013/ Inputs: - x, y: coordinates get from mouse in screen coordination - expZ: expected Z of the real coordinates Outputs: coordinates in the real world ''' if not self.init: return 0, 0, 0 projection = glGetDoublev(GL_PROJECTION_MATRIX) modelview = glGetDoublev(GL_MODELVIEW_MATRIX) viewport = glGetIntegerv(GL_VIEWPORT) winX = float(x) winY = float(viewport[3]) - float(y) # winZ = glReadPixels(x, int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT) try: posXF, posYF, posZF = gluUnProject(winX, winY, 1, modelview, projection, viewport) posXN, posYN, posZN = gluUnProject(winX, winY, -1, modelview, projection, viewport) posZ = expZ posX = (posZ - posZN) / (posZF - posZN) * (posXF - posXN) + posXN posY = (posZ - posZN) / (posZF - posZN) * (posYF - posYN) + posYN # posX, posY, posZ = gluUnProject(winX, winY, winZ, modelview, projection, viewport) except: return 0, 0, 0 return posX, posY, posZ
def rotate_absolute(self,axis,angle): # rotate the object self.makeCurrent() glMatrixMode(GL_MODELVIEW) glLoadIdentity() t = [self._modelview_matrix[3][0], self._modelview_matrix[3][1], self._modelview_matrix[3][2]] glTranslatef(t[0], t[1], t[2]) glRotated(angle, axis[0], axis[1], axis[2]) glTranslatef(-t[0], -t[1], -t[2]) matrix = glGetDoublev(GL_MODELVIEW_MATRIX) matrix[3][0], matrix[3][1],matrix[3][2] = t glLoadMatrixd(matrix) # update _modelview_matrix self._modelview_matrix = glGetDoublev(GL_MODELVIEW_MATRIX)
def unproject_mouse_on_scene(self,pos): start_x, start_y, start_z = gluUnProject(pos.x(), pos.y(), 1, model=self._modelview_matrix, proj=glGetDoublev(GL_PROJECTION_MATRIX)) end_x, end_y, end_z = gluUnProject(pos.x(), pos.y(), 0, model=self._modelview_matrix, proj=glGetDoublev(GL_PROJECTION_MATRIX)) diff_x = end_x - start_x diff_y = end_y - start_y diff_z = end_z - start_z t = (0 - start_z) / diff_z x = start_x + (diff_x * t) y = start_y + (diff_y * t) return x, y,0
def translate(self, trans): # translate the object self.makeCurrent() glMatrixMode(GL_MODELVIEW) glLoadIdentity() glTranslated(trans[0], trans[1], trans[2]) glMultMatrixd(self._modelview_matrix) # update _modelview_matrix self._modelview_matrix = glGetDoublev(GL_MODELVIEW_MATRIX)
def GetWorldCoords(self, x, y, camera_z): """ returns (x,y,z) given x,y pygame screen coords. NOTE: try and get the model view matrix back to the camera position when using. """ y = self.height - y mod = glGetDoublev(GL_MODELVIEW_MATRIX) proj = glGetDoublev(GL_PROJECTION_MATRIX) #view = glGetIntegerv(GL_VIEWPORT) view = (0, 0, self.width, self.height) z = abs(camera_z) z_buffer_value = self.buffer_calc_a + self.buffer_calc_b / z objx, objy, objz = gluUnProject(x, y, z_buffer_value, mod, proj, view) return (objx, objy, objz)
def GetWorldCoords(self, x,y, camera_z): """ returns (x,y,z) given x,y pygame screen coords. NOTE: try and get the model view matrix back to the camera position when using. """ y = self.height - y mod = glGetDoublev(GL_MODELVIEW_MATRIX) proj = glGetDoublev(GL_PROJECTION_MATRIX) #view = glGetIntegerv(GL_VIEWPORT) view = (0, 0, self.width, self.height) z = abs(camera_z) z_buffer_value = self.buffer_calc_a +self.buffer_calc_b / z objx, objy, objz = gluUnProject(x,y,z_buffer_value, mod, proj,view) return (objx, objy, objz)
def _getPickingRay(self, x, y): ''' Process the ray for the current mouse position ''' viewport = glGetIntegerv(GL_VIEWPORT) model_mat = np.array(glGetDoublev(GL_MODELVIEW_MATRIX)) proj_mat = np.array(glGetDoublev(GL_PROJECTION_MATRIX)) # win_coord = (x*2, viewport[3] - y*2) win_coord = (x, viewport[3] - y) near_point = np.array( GLU.gluUnProject(win_coord[0], win_coord[1], 0.0, model_mat, proj_mat, viewport)) far_point = np.array( GLU.gluUnProject(win_coord[0], win_coord[1], 1.0, model_mat, proj_mat, viewport)) return far_point - near_point
def rotate(self, axis, angle): # rotate the object self.makeCurrent() glMatrixMode(GL_MODELVIEW) glLoadIdentity() t = [self._modelview_matrix[3][0], self._modelview_matrix[3][1], self._modelview_matrix[3][2]] glTranslatef(t[0], t[1], t[2]) glRotated(angle, axis[0], axis[1], axis[2]) glTranslatef(-t[0], -t[1], -t[2]) glMultMatrixd(self._modelview_matrix) # update _modelview_matrix self._modelview_matrix = glGetDoublev(GL_MODELVIEW_MATRIX)
def gluLookAtMatrix(eye, center, up): ''' get the lookat matrix using opengl. Note that opengl context needs to be initialized at the point of making this call. ''' from OpenGL.GL import GL_MODELVIEW, GL_MODELVIEW_MATRIX, glMatrixMode, \ glLoadIdentity, glGetDoublev, glPushMatrix, \ glPopMatrix from OpenGL.GLU import gluLookAt glMatrixMode(GL_MODELVIEW) glPushMatrix() glLoadIdentity() gluLookAt(eye[0], eye[1], eye[2], center[0], center[1], center[2], up[0], up[1], up[2]) M = np.transpose(glGetDoublev(GL_MODELVIEW_MATRIX)) glPopMatrix() return np.matrix(M)
for i in info[module_name]: try: if len(i) == 2: key, value = i else: key, id, t = i if t[0] == 'b': value = glGetBooleanv(id) if operator.isSequence(value): value = map(_boolean, value) else: value = _boolean(value) elif t[0] == 'i': value = glGetIntegerv(id) elif t[0] == 'd': value = glGetDoublev(id) elif t[0] == 'e': if len(t) > 1: if t[1] == 'u': x = gluGetString(id) else: x = glGetString(id) else: x = id x = string.split(x) x.sort() y = [] for ext in x: try: __import__('OpenGL.' + string.replace(ext, '_', '.', 2), globals(), locals(), ['*']) y.append('<b>%s</b>' % ext)
def reset_view(self): # scene pos and size glMatrixMode(GL_MODELVIEW) glLoadIdentity() self._modelview_matrix = glGetDoublev(GL_MODELVIEW_MATRIX) self.view_all()
def draw_primitives(self, scalefactor=1.0, center=[0.0, 0.0, 0.0], recenter=False, want_camera=False): # measure the bounding box of all our primitives, so that we can # recenter them in our field of view if recenter: all_meshes = self.static_meshes + self.dynamic_meshes all_lines = self.static_lines + self.dynamic_lines if (len(all_meshes) + len(all_lines)) == 0: if want_camera: return { 'modelview_matrix': glGetDoublev(GL_MODELVIEW_MATRIX), 'projection_matrix': glGetDoublev(GL_PROJECTION_MATRIX), 'viewport': glGetIntegerv(GL_VIEWPORT) } else: return None for m in all_meshes: m.v = m.v.reshape((-1, 3)) all_verts = np.concatenate([ m.v[m.f.flatten() if len(m.f) > 0 else np.arange(len(m.v))] for m in all_meshes ] + [l.v[l.e.flatten()] for l in all_lines], axis=0) maximum = np.max(all_verts, axis=0) minimum = np.min(all_verts, axis=0) center = (maximum + minimum) / 2. scalefactor = (maximum - minimum) / 4. scalefactor = np.max(scalefactor) else: center = np.array(center) # for mesh in self.dynamic_meshes : # if mesh.f : mesh.reset_normals() all_meshes = self.static_meshes + self.dynamic_meshes all_lines = self.static_lines + self.dynamic_lines self.current_center = center self.current_scalefactor = scalefactor glMatrixMode(GL_MODELVIEW) glPushMatrix() # uncomment to add a default rotation (useful when automatically snapshoting kinect data # glRotate(220, 0.0, 1.0, 0.0) tf = np.identity(4, 'f') / scalefactor tf[:3, 3] = -center / scalefactor tf[3, 3] = 1 cur_mtx = glGetFloatv(GL_MODELVIEW_MATRIX).T glLoadMatrixf(cur_mtx.dot(tf).T) if want_camera: result = { 'modelview_matrix': glGetDoublev(GL_MODELVIEW_MATRIX), 'projection_matrix': glGetDoublev(GL_PROJECTION_MATRIX), 'viewport': glGetIntegerv(GL_VIEWPORT) } else: result = None for m in all_meshes: if not hasattr(m, 'vbo'): # Precompute vertex vbo fidxs = m.f.flatten() if len(m.f) > 0 else np.arange(len(m.v)) allpts = m.v[fidxs].astype(np.float32).flatten() vbo = OpenGL.arrays.vbo.VBO(allpts) m.vbo = {'v': vbo} # Precompute normals vbo if hasattr(m, 'vn'): ns = m.vn.astype(np.float32) ns = ns[m.f.flatten(), :] m.vbo['vn'] = OpenGL.arrays.vbo.VBO(ns.flatten()) elif hasattr(m, 'f') and m.f.size > 0: ns = TriNormals(m.v, m.f).reshape(-1, 3) ns = np.tile(ns, (1, 3)).reshape(-1, 3).astype(np.float32) m.vbo['vn'] = OpenGL.arrays.vbo.VBO(ns.flatten()) # Precompute texture vbo if hasattr(m, 'ft') and (m.ft.size > 0): ftidxs = m.ft.flatten() data = m.vt[ftidxs].astype(np.float32)[:, 0:2] data[:, 1] = 1.0 - 1.0 * data[:, 1] m.vbo['vt'] = OpenGL.arrays.vbo.VBO(data) # Precompute color vbo if hasattr(m, 'vc'): data = m.vc[fidxs].astype(np.float32) m.vbo['vc'] = OpenGL.arrays.vbo.VBO(data) elif hasattr(m, 'fc'): data = np.tile(m.fc, (1, 3)).reshape(-1, 3).astype(np.float32) m.vbo['vc'] = OpenGL.arrays.vbo.VBO(data) for e in all_lines: self.draw_lines(e) for m in all_meshes: if hasattr(m, 'texture_image') and not hasattr(m, 'textureID'): self.set_texture(m) self.draw_mesh(m, self.lighting_on) glMatrixMode(GL_MODELVIEW) glPopMatrix() return result
def draw_mesh(m, lighting_on): # Supply vertices glEnableClientState(GL_VERTEX_ARRAY) m.vbo['v'].bind() glVertexPointer(3, GL_FLOAT, 0, m.vbo['v']) m.vbo['v'].unbind() # Supply normals if 'vn' in m.vbo.keys(): glEnableClientState(GL_NORMAL_ARRAY) m.vbo['vn'].bind() glNormalPointer(GL_FLOAT, 0, m.vbo['vn']) m.vbo['vn'].unbind() else: glDisableClientState(GL_NORMAL_ARRAY) # Supply colors if 'vc' in m.vbo.keys(): glEnableClientState(GL_COLOR_ARRAY) m.vbo['vc'].bind() glColorPointer(3, GL_FLOAT, 0, m.vbo['vc']) m.vbo['vc'].unbind() else: glDisableClientState(GL_COLOR_ARRAY) if ('vt' in m.vbo.keys()) and hasattr(m, 'textureID'): glEnable(GL_TEXTURE_2D) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE) glBindTexture(GL_TEXTURE_2D, m.textureID) glEnableClientState(GL_TEXTURE_COORD_ARRAY) m.vbo['vt'].bind() glTexCoordPointer(2, GL_FLOAT, 0, m.vbo['vt']) m.vbo['vt'].unbind() else: glDisable(GL_TEXTURE_2D) glDisableClientState(GL_TEXTURE_COORD_ARRAY) # Draw if len(m.f) > 0: # ie if it is triangulated if lighting_on: glEnable(GL_LIGHTING) else: glDisable(GL_LIGHTING) glDrawElementsui(GL_TRIANGLES, np.arange(m.f.size, dtype=np.uint32)) else: # not triangulated, so disable lighting glDisable(GL_LIGHTING) glPointSize(2) glDrawElementsui(GL_POINTS, np.arange(len(m.v), dtype=np.uint32)) if hasattr(m, 'v_to_text'): glEnable(GL_TEXTURE_2D) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR) glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL) # glEnable(GL_TEXTURE_GEN_S) # glEnable(GL_TEXTURE_GEN_T) # glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR) # glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR) bgcolor = np.array(glGetDoublev(GL_COLOR_CLEAR_VALUE)) fgcolor = 1. - bgcolor from .lines import Lines sc = float(np.max(np.max(m.v, axis=0) - np.min(m.v, axis=0))) / 10. cur_mtx = np.linalg.pinv(glGetFloatv(GL_MODELVIEW_MATRIX).T) xdir = cur_mtx[:3, 0] ydir = cur_mtx[:3, 1] glEnable(GL_LINE_SMOOTH) glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) for vidx, text in m.v_to_text.items(): pos0 = m.v[vidx].copy() pos1 = m.v[vidx].copy() if hasattr(m, 'vn'): pos1 += m.vn[vidx] * sc glLineWidth(5.0) ln = Lines(v=np.vstack((pos0, pos1)), e=np.array([[0, 1]])) glEnable(GL_LIGHTING) glColor3f(1. - 0.8, 1. - 0.8, 1. - 1.00) MeshViewerSingle.draw_lines(ln) glDisable(GL_LIGHTING) texture_id = get_textureid_with_text(text, bgcolor, fgcolor) glBindTexture(GL_TEXTURE_2D, texture_id) glPushMatrix() glTranslatef(pos1[0], pos1[1], pos1[2]) dx = xdir * .10 dy = ydir * .10 if False: glBegin(GL_QUADS) glTexCoord2f(1., 0.) glVertex3f(*(+dx + dy)) glTexCoord2f(1., 1.) glVertex3f(*(+dx - dy)) glTexCoord2f(0., 1.) glVertex3f(*(-dx - dy)) glTexCoord2f(0., 0.) glVertex3f(*(-dx + dy)) # gluSphere(quadratic,0.05,32,32) glEnd() else: glBegin(GL_POLYGON) for r in np.arange(0, np.pi * 2., .01): glTexCoord2f(np.cos(r) / 2. + .5, np.sin(r) / 2. + .5) glVertex3f(*(dx * np.cos(r) + -dy * np.sin(r))) glEnd() glPopMatrix()
for i in info[module_name]: try: if len(i) == 2: key, value = i else: key, id, t = i if t[0] == 'b': value = glGetBooleanv(id) if operator.isSequence(value): value = map(_boolean, value) else: value = _boolean(value) elif t[0] == 'i': value = glGetIntegerv(id) elif t[0] == 'd': value = glGetDoublev(id) elif t[0] == 'e': if len(t) > 1: if t[1] == 'u': x = gluGetString(id) else: x = glGetString(id) else: x = id x = string.split(x) x.sort() y = [] for ext in x: try: __import__( 'OpenGL.' +
def load_view_matrix(self,view_matrix): self.makeCurrent() glMatrixMode(GL_MODELVIEW) glLoadIdentity() glLoadMatrixd(view_matrix) self._modelview_matrix = glGetDoublev(GL_MODELVIEW_MATRIX)
shaders.glUseProgram(shaderProg) points = vbo.VBO(data=imgnp, usage="GL_DYNAMIC_DRAW", target='GL_ARRAY_BUFFER', size=None) points.bind() modelmat = glGetUniformLocation(shaderProg, 'model') viewmat = glGetUniformLocation(shaderProg, 'view') projectionmat = glGetUniformLocation(shaderProg, 'projection') glPushMatrix() glRotatef(-90, 0, 0, 1) scal = 0.01 glTranslatef(-0.5, -0.5, 0) glScalef(1 / imgnp.shape[0], 1 / imgnp.shape[1], 1) model2 = glGetDoublev(GL_MODELVIEW_MATRIX) glPopMatrix() # print(model2) glUniformMatrix4fv(modelmat, 1, GL_FALSE, model2) glutDisplayFunc(draw_points) # glutInitWindowPosition(600, 100) # wind2 = glutCreateWindow("OpenGL Window2") # glutMouseFunc(moveMouse) glutMainLoop()