def draw_circle( self, circle_center, circle_normal, circle_radius, color=RGBA(1.1, 0.2, 0.8), num_segments=20, ): vertices = [] vertices.append((0, 0, 0)) # circle center # create circle vertices in the xy plane for i in np.linspace(0.0, 2.0 * math.pi, num_segments): x = math.sin(i) y = math.cos(i) z = 0 vertices.append((x, y, z)) glPushMatrix() glMatrixMode(GL_MODELVIEW) glLoadMatrixf( self.get_pupil_transformation_matrix(circle_normal, circle_center, circle_radius)) glutils.draw_polyline((vertices), color=color, line_type=GL_TRIANGLE_FAN) # circle glutils.draw_polyline([(0, 0, 0), (0, 0, 4)], color=RGBA(0, 0, 0), line_type=GL_LINES) # normal glPopMatrix()
def enable(self): # setup projection and model view matrix glMatrixMode (GL_PROJECTION) glLoadMatrixf (self.projectionCropMatrix) glMatrixMode (GL_MODELVIEW) glLoadMatrixf (self.modelViewMatrix) self._projectionUpdatedThisFrame = False
def loadMatrixSlow(self): """ set the texture matrix to: bias * crop * projection * modelView * inverseSceneModelView. """ glLoadMatrixf( ShadowProjectiveTextureMatrix.biasMatrix ) glMultMatrixf( self.camera.modelViewProjectionCropMatrix ) glMultMatrixf( self.sceneCamera.inverseModelViewMatrix ) # compute a normal matrix for the same thing (to transform the normals) # Basically, N = ((L)^-1)^-t self.normalMatrix = transpose( inv( glGetFloatv( GL_TEXTURE_MATRIX ) ) )
def render_to_png(filename, callback, obb, model_matrix=(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1)): from pygame import init, display, quit from pygame.constants import OPENGL, DOUBLEBUF from OpenGL.GL import glLightfv, glCullFace, glEnable, glShadeModel, glMatrixMode, glLoadIdentity, glClear, \ glLoadMatrixf, glPolygonMode, glCallList, glReadPixels, GL_LIGHT0, GL_POSITION, GL_AMBIENT, GL_DIFFUSE, \ GL_BACK, GL_LIGHTING, GL_COLOR_MATERIAL, GL_DEPTH_TEST, GL_SMOOTH, GL_PROJECTION, GL_MODELVIEW, \ GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT, GL_FRONT_AND_BACK, GL_FILL, GL_LINE, GL_BGR, GL_UNSIGNED_BYTE from OpenGL.GLU import gluPerspective from cv2 import imwrite from numpy import frombuffer, uint8 init() viewport = (800, 600) display.set_mode(viewport, OPENGL | DOUBLEBUF) glLightfv(GL_LIGHT0, GL_POSITION, (0, -1, 0, 0)) glLightfv(GL_LIGHT0, GL_AMBIENT, (0.2, 0.2, 0.2, 1)) glLightfv(GL_LIGHT0, GL_DIFFUSE, (0.5, 0.5, 0.5, 1)) glCullFace(GL_BACK) glEnable(GL_LIGHT0) glEnable(GL_LIGHTING) glEnable(GL_COLOR_MATERIAL) glEnable(GL_DEPTH_TEST) glShadeModel(GL_SMOOTH) glMatrixMode(GL_PROJECTION) glLoadIdentity() width, height = viewport gluPerspective(90.0, width / float(height), 0.1, 100.0) glEnable(GL_DEPTH_TEST) glMatrixMode(GL_MODELVIEW) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glLoadIdentity() glLoadMatrixf(model_matrix) glPolygonMode(GL_FRONT_AND_BACK, GL_FILL) glCallList(callback()) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) glCallList(create_obb_gl_list(obb)) img_data = glReadPixels(0, 0, width, height, GL_BGR, GL_UNSIGNED_BYTE) img = frombuffer(img_data, dtype=uint8) img = img.reshape((height, width, 3)) imwrite(filename, img) quit()
def paintGL(self): glMatrixMode(GL_PROJECTION) glLoadIdentity() glOrtho(self.zoom, -self.zoom, -self.zoom, self.zoom, -5000, 5000) arriba = 1 if self.theta == 360: arriba = -1 gluLookAt(self.x, self.y, self.z, self.desviacion_x, self.desviacion_y, self.desviacion_z, 0, arriba, 0) glMatrixMode(GL_MODELVIEW) if self.programa.modo_oscuro: glClearColor(0.3, 0.3, 0.3, 0) else: glClearColor(1, 1, 1, 0) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glLoadMatrixf(self.m) glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) self.ordenar_elementos() self.update()
def update_window(self, g_pool, result): if not result: return if not self.window: return self.begin_update_window() self.image_width, self.image_height = g_pool.capture.frame_size self.clear_gl_screen() self.trackball.push() glLoadMatrixf(self.get_image_space_matrix(15)) g_pool.image_tex.draw( quad=( (0, self.image_height), (self.image_width, self.image_height), (self.image_width, 0), (0, 0), ), alpha=1.0, ) glLoadMatrixf(self.get_adjusted_pixel_space_matrix(15)) self.draw_frustum(self.image_width, self.image_height, self.focal_length) glLoadMatrixf(self.get_anthropomorphic_matrix()) self.eye.pose = self.get_anthropomorphic_matrix() self.draw_coordinate_system(4) self.draw_eye(result) self.draw_Dierkes_lines(result) self.trackball.pop() self.draw_debug_info(result) self.draw_residuals(result) self.end_update_window() return True
def processFrame(self, timePassedSecs): """ draws a scene """ # update the model view matrix self.sceneCamera.updateKeys() self.sceneCamera.update() # process queued GLObject events GLObject.signalsEmit() # enable some default scene states glEnable(GL_DEPTH_TEST) ######## SHADOW MAP RENDERING START ######## # offset the geometry slightly to prevent z-fighting # note that this introduces some light-leakage artifacts glEnable(GL_POLYGON_OFFSET_FILL) glPolygonOffset(1.1, 4096.0) # cull front faces for shadow rendering, # this moves z-fighting to backfaces. glCullFace(GL_FRONT) # enable depth rendering shader. # FIXME: support segment geometry shader! # geometry shader could change the shadow shape! self.depthShader.enable() map(_updateLightShadowMap, self.lights) glBindFramebuffer(GL_FRAMEBUFFER, 0) glDisable(GL_POLYGON_OFFSET_FILL) ######## SHADOW MAP RENDERING STOP ######## #### TODO: FOG: integrate FOG #### glEnable(GL_FOG) glFogi(GL_FOG_MODE, GL_EXP2) # approximate the atmosphere's filtering effect as a linear function sunDir = array([4.0, 4.0, 4.0, 0.0], float32) # TODO: FOG: what is the sun dir ? skyColor = array([0.8, sunDir[1] * 0.1 + 0.7, sunDir[1] * 0.4 + 0.5, 1.0], float32) glClearColor(*skyColor) glFogf(GL_FOG_DENSITY, 0.4) glFogf(GL_FOG_START, 16.0) glFogf(GL_FOG_END, self.farClip) glFogfv(GL_FOG_COLOR, skyColor) # fill projection matrix glMatrixMode(GL_PROJECTION) glLoadMatrixf(self.projectionMatrix) glMatrixMode(GL_MODELVIEW) # draw stuff in 3d projection lastCam = None glPushAttrib(GL_COLOR_BUFFER_BIT) for (fbo, draw, cam) in self.sceneFBOS: # render to fbo fbo.enable() if cam != lastCam: cam.enable() lastCam = cam draw() # disable render to texture FBO.disable() glPopAttrib() glViewport(0, 0, self.winSize[0], self.winSize[1]) #### TODO: FOG: integrate FOG #### glDisable(GL_FOG) # change to orthogonal projection glMatrixMode(GL_PROJECTION) glLoadMatrixf(self.orthoMatrix) glMatrixMode(GL_MODELVIEW) glLoadIdentity() # no depth test needed in orthogonal rendering glDisable(GL_DEPTH_TEST) # draw orthogonal to the screen self.orthogonalPass()
def enable(self): """ setup projection and model view matrix. """ glMatrixMode (GL_PROJECTION) glLoadMatrixf (self.projectionMatrix) glMatrixMode (GL_MODELVIEW) glLoadMatrixf (self.modelViewMatrix)
def enable(self): """ enable float32 matrix. """ glLoadMatrixf( self.modelViewMatrix )
def move_z(self): glLoadMatrixf(self._trans_mat) glTranslatef(0, 0, self.tz) self._trans_mat = glGetFloatv(GL_MODELVIEW_MATRIX)
def rotate(self): glPushMatrix() glLoadMatrixf(self._trans_mat) glRotatef(0.5, 0, 1, 0) self._trans_mat = glGetFloatv(GL_MODELVIEW_MATRIX) glPopMatrix()
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 update_window(self, g_pool, result): if not result: return if not self.window: return self.begin_update_window() self.image_width, self.image_height = g_pool.capture.frame_size latest_circle = result["circle"] predicted_circle = result["predicted_circle"] edges = result["edges"] sphere_models = result["models"] self.clear_gl_screen() self.trackball.push() # 2. in pixel space draw video frame glLoadMatrixf(self.get_image_space_matrix(15)) g_pool.image_tex.draw( quad=( (0, self.image_height), (self.image_width, self.image_height), (self.image_width, 0), (0, 0), ), alpha=0.5, ) glLoadMatrixf(self.get_adjusted_pixel_space_matrix(15)) self.draw_frustum(self.image_width, self.image_height, self.focal_length) glLoadMatrixf(self.get_anthropomorphic_matrix()) model_count = 0 sphere_color = RGBA(0, 147 / 255.0, 147 / 255.0, 0.2) initial_sphere_color = RGBA(0, 147 / 255.0, 147 / 255.0, 0.2) alternative_sphere_color = RGBA(1, 0.5, 0.5, 0.05) alternative_initial_sphere_color = RGBA(1, 0.5, 0.5, 0.05) for model in sphere_models: bin_positions = model["bin_positions"] sphere = model["sphere"] initial_sphere = model["initial_sphere"] if model_count == 0: # self.draw_sphere(initial_sphere[0],initial_sphere[1], color = sphere_color ) self.draw_sphere(sphere[0], sphere[1], color=initial_sphere_color) glutils.draw_points(bin_positions, 3, RGBA(0.6, 0.0, 0.6, 0.5)) else: # self.draw_sphere(initial_sphere[0],initial_sphere[1], color = alternative_sphere_color ) self.draw_sphere(sphere[0], sphere[1], color=alternative_initial_sphere_color) model_count += 1 self.draw_circle( latest_circle[0], latest_circle[1], latest_circle[2], RGBA(0.0, 1.0, 1.0, 0.4), ) # self.draw_circle( predicted_circle[0], predicted_circle[1], predicted_circle[2], RGBA(1.0,0.0,0.0,0.4)) glutils.draw_points(edges, 2, RGBA(1.0, 0.0, 0.6, 0.5)) glLoadMatrixf(self.get_anthropomorphic_matrix()) self.draw_coordinate_system(4) self.trackball.pop() self.draw_debug_info(result) self.end_update_window() return True
def rotate(self): glPushMatrix() glLoadMatrixf(self._identity_mat) glRotatef(1, 0, 1, 0) self._identity_mat = glGetFloatv(GL_MODELVIEW_MATRIX) glPopMatrix()