def gl_display_cache_bars(self): """ """ padding = 20. frame_max = len(self.g_pool.timestamps) #last marker is garanteed to be frame max. glMatrixMode(GL_PROJECTION) glPushMatrix() glLoadIdentity() width,height = self.win_size h_pad = padding * (frame_max-2)/float(width) v_pad = padding* 1./(height-2) gluOrtho2D(-h_pad, (frame_max-1)+h_pad, -v_pad, 1+v_pad) # ranging from 0 to cache_len-1 (horizontal) and 0 to 1 (vertical) glMatrixMode(GL_MODELVIEW) glPushMatrix() glLoadIdentity() glTranslatef(0,-.02,0) color = (7.,.1,.2,8.) draw_gl_polyline(self.gl_display_ranges,color=color,type='Lines',thickness=2) glMatrixMode(GL_PROJECTION) glPopMatrix() glMatrixMode(GL_MODELVIEW) glPopMatrix()
def gl_display(self): glMatrixMode(GL_PROJECTION) glPushMatrix() glLoadIdentity() width, height = glfwGetWindowSize(glfwGetCurrentContext()) h_pad = self.padding / width v_pad = self.padding / height gluOrtho2D(-h_pad, 1 + h_pad, -v_pad, 1 + v_pad) # gl coord convention glMatrixMode(GL_MODELVIEW) glPushMatrix() glLoadIdentity() if self.drag_mode: color1 = (0., .8, .5, .5) color2 = (0., .8, .5, 1.) else: color1 = (.25, .8, .8, .5) color2 = (.25, .8, .8, 1.) draw_gl_polyline([(0, 0), (self.norm_seek_pos, 0)], color=color1) draw_gl_polyline([(self.norm_seek_pos, 0), (1, 0)], color=(.5, .5, .5, .5)) draw_gl_point((self.norm_seek_pos, 0), color=color1, size=40) draw_gl_point((self.norm_seek_pos, 0), color=color2, size=10) glMatrixMode(GL_PROJECTION) glPopMatrix() glMatrixMode(GL_MODELVIEW) glPopMatrix()
def gl_display(self): glMatrixMode(GL_PROJECTION) glPushMatrix() glLoadIdentity() width,height = glfwGetWindowSize(glfwGetCurrentContext()) h_pad = self.padding/width v_pad = self.padding/height gluOrtho2D(-h_pad, 1+h_pad, -v_pad, 1+v_pad) # gl coord convention glMatrixMode(GL_MODELVIEW) glPushMatrix() glLoadIdentity() if self.drag_mode: color1 = (0.,.8,.5,.5) color2 = (0.,.8,.5,1.) else: color1 = (.25,.8,.8,.5) color2 = (.25,.8,.8,1.) draw_gl_polyline( [(0,0),(self.norm_seek_pos,0)],color=color1) draw_gl_polyline( [(self.norm_seek_pos,0),(1,0)],color=(.5,.5,.5,.5)) draw_gl_point((self.norm_seek_pos,0),color=color1,size=40) draw_gl_point((self.norm_seek_pos,0),color=color2,size=10) glMatrixMode(GL_PROJECTION) glPopMatrix() glMatrixMode(GL_MODELVIEW) glPopMatrix()
def _orthoSetupPush(self, screenWidth, screenHeight): # set frustum and set glMatrixMode(GL_PROJECTION) glPushMatrix() glLoadIdentity() gluOrtho2D(0, screenWidth, 0, screenHeight) glMatrixMode(GL_MODELVIEW)
def gl_display_in_window(self, world_tex_id): """ here we map a selected surface onto a seperate window. """ if self._window and self.detected: active_window = glfwGetCurrentContext() glfwMakeContextCurrent(self._window) clear_gl_screen() # cv uses 3x3 gl uses 4x4 tranformation matricies m = cvmat_to_glmat(self.m_from_screen) glMatrixMode(GL_PROJECTION) glPushMatrix() glLoadIdentity() gluOrtho2D(0, 1, 0, 1) # gl coord convention glMatrixMode(GL_MODELVIEW) glPushMatrix() # apply m to our quad - this will stretch the quad such that the ref suface will span the window extends glLoadMatrixf(m) draw_named_texture(world_tex_id) glMatrixMode(GL_PROJECTION) glPopMatrix() glMatrixMode(GL_MODELVIEW) glPopMatrix() # now lets get recent pupil positions on this surface: draw_gl_points_norm(self.gaze_on_srf, color=(0.0, 8.0, 0.5, 0.8), size=80) glfwSwapBuffers(self._window) glfwMakeContextCurrent(active_window)
def refresh2d(width, height): glViewport(0, 0, width, height) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluOrtho2D(*bounds) glMatrixMode(GL_MODELVIEW) glLoadIdentity()
def begin(self): """begin() Call when you want to render text. This sets orthogonal projection, font texture, and some other msic attributes. All OpenGL states are restored in end() call. """ glBindTexture(GL_TEXTURE_2D, self.tex) glPushAttrib(GL_TRANSFORM_BIT) viewport = glGetIntegerv(GL_VIEWPORT) glMatrixMode(GL_PROJECTION) glPushMatrix() glLoadIdentity() gluOrtho2D(viewport[0], viewport[2], viewport[1], viewport[3]) glMatrixMode(GL_MODELVIEW) glPushMatrix() glLoadIdentity() glPushAttrib(GL_LIST_BIT | GL_CURRENT_BIT | GL_ENABLE_BIT | GL_TRANSFORM_BIT | GL_TEXTURE_BIT) glDisable(GL_LIGHTING) glEnable(GL_TEXTURE_2D) glDisable(GL_DEPTH_TEST) glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE) glDepthFunc(GL_NEVER)
def gl_display(self): glMatrixMode(GL_PROJECTION) glPushMatrix() glLoadIdentity() gluOrtho2D(-self.h_pad, (self.frame_count)+self.h_pad, -self.v_pad, 1+self.v_pad) # ranging from 0 to cache_len-1 (horizontal) and 0 to 1 (vertical) glMatrixMode(GL_MODELVIEW) glPushMatrix() glLoadIdentity() #Draw............................................................................... for pos_to_draw in self.pos_begin_trial: draw_gl_point((pos_to_draw, 0), size = 5, color = (.5, .5, .5, .5)) draw_gl_polyline( [(pos_to_draw,.05),(pos_to_draw,0)], color = (.5, .5, .5, .5)) for pos_to_draw in self.pos_first_response: draw_gl_point((pos_to_draw, 0), size = 5, color = (.0, .0, .5, 1.)) draw_gl_polyline( [(pos_to_draw,.025),(pos_to_draw,0)], color = (.0, .0, .7, 1.)) for pos_to_draw in self.pos_end_limited_hold: draw_gl_point((pos_to_draw, 0), size = 5, color = (.5, .0, .0, 1.)) draw_gl_polyline( [(pos_to_draw,.025),(pos_to_draw,0)], color = (.7, .0, .0, 1.)) for x, first_response in enumerate(self.pos_first_response): draw_gl_polyline( [(self.pos_end_limited_hold[x],.025),(first_response,.025)], color = (1., .5, .0, 1.)) #Draw............................................................................... glMatrixMode(GL_PROJECTION) glPopMatrix() glMatrixMode(GL_MODELVIEW) glPopMatrix()
def gl_display(self): glMatrixMode(GL_PROJECTION) glPushMatrix() glLoadIdentity() gluOrtho2D(-self.h_pad, (self.frame_count)+self.h_pad, -self.v_pad, 1+self.v_pad) # ranging from 0 to cache_len-1 (horizontal) and 0 to 1 (vertical) glMatrixMode(GL_MODELVIEW) glPushMatrix() glLoadIdentity() # if self.drag_mode: # color1 = (0.,.8,.5,.5) # color2 = (0.,.8,.5,1.) # else: color1 = (1,1,1,0.4)#(.25,.8,.8,.5) color2 = (1,1,1,1.)#(.25,.8,.8,1.) thickness = 10. draw_polyline(verts=[(0,0),(self.current_frame_index,0)], thickness=thickness,color=RGBA(*color1)) draw_polyline(verts=[(self.current_frame_index,0),(self.frame_count,0)], thickness=thickness,color=RGBA(*color1)) if not self.drag_mode: draw_points([(self.current_frame_index,0)],color=RGBA(*color1),size=30) draw_points([(self.current_frame_index,0)],color=RGBA(*color2),size=20) glMatrixMode(GL_PROJECTION) glPopMatrix() glMatrixMode(GL_MODELVIEW) glPopMatrix()
def gl_display(self): glMatrixMode(GL_PROJECTION) glPushMatrix() glLoadIdentity() gluOrtho2D( -self.h_pad, (self.frame_count) + self.h_pad, -self.v_pad, 1 + self.v_pad ) # ranging from 0 to cache_len-1 (horizontal) and 0 to 1 (vertical) glMatrixMode(GL_MODELVIEW) glPushMatrix() glLoadIdentity() if self.drag_mode: color1 = (0., .8, .5, .5) color2 = (0., .8, .5, 1.) else: color1 = (.25, .8, .8, .5) color2 = (.25, .8, .8, 1.) draw_gl_polyline([(0, 0), (self.current_frame_index, 0)], color=color1) draw_gl_polyline([(self.current_frame_index, 0), (self.frame_count, 0)], color=(.5, .5, .5, .5)) draw_gl_point((self.current_frame_index, 0), color=color1, size=40) draw_gl_point((self.current_frame_index, 0), color=color2, size=10) glMatrixMode(GL_PROJECTION) glPopMatrix() glMatrixMode(GL_MODELVIEW) glPopMatrix()
def gl_display(self): glMatrixMode(GL_PROJECTION) glPushMatrix() glLoadIdentity() gluOrtho2D(-self.h_pad, (self.frame_count)+self.h_pad, -self.v_pad, 1+self.v_pad) # ranging from 0 to cache_len-1 (horizontal) and 0 to 1 (vertical) glMatrixMode(GL_MODELVIEW) glPushMatrix() glLoadIdentity() if self.drag_mode: color1 = (0.,.8,.5,.5) color2 = (0.,.8,.5,1.) else: color1 = (.25,.8,.8,.5) color2 = (.25,.8,.8,1.) draw_gl_polyline( [(0,0),(self.current_frame_index,0)],color=color1) draw_gl_polyline( [(self.current_frame_index,0),(self.frame_count,0)],color=(.5,.5,.5,.5)) draw_gl_point((self.current_frame_index,0),color=color1,size=40) draw_gl_point((self.current_frame_index,0),color=color2,size=10) glMatrixMode(GL_PROJECTION) glPopMatrix() glMatrixMode(GL_MODELVIEW) glPopMatrix()
def gl_display(self): glMatrixMode(GL_PROJECTION) glPushMatrix() glLoadIdentity() gluOrtho2D( -self.h_pad, (self.frame_count) + self.h_pad, -self.v_pad, 1 + self.v_pad ) # ranging from 0 to cache_len-1 (horizontal) and 0 to 1 (vertical) glMatrixMode(GL_MODELVIEW) glPushMatrix() glLoadIdentity() color1 = (.1, .9, .2, .5) color2 = (.1, .9, .2, .5) if self.in_mark != 0 or self.out_mark != self.frame_count: draw_gl_polyline([(self.in_mark, 0), (self.out_mark, 0)], color=(.1, .9, .2, .5), thickness=2) draw_gl_point((self.in_mark, 0), color=color2, size=10) draw_gl_point((self.out_mark, 0), color=color2, size=10) glMatrixMode(GL_PROJECTION) glPopMatrix() glMatrixMode(GL_MODELVIEW) glPopMatrix()
def gl_display_in_window(self,world_tex_id): """ here we map a selected surface onto a seperate window. """ if self._window and self.detected: active_window = glfwGetCurrentContext() glfwMakeContextCurrent(self._window) clear_gl_screen() # cv uses 3x3 gl uses 4x4 tranformation matricies m = cvmat_to_glmat(self.m_from_screen) glMatrixMode(GL_PROJECTION) glPushMatrix() glLoadIdentity() gluOrtho2D(0, 1, 0, 1) # gl coord convention glMatrixMode(GL_MODELVIEW) glPushMatrix() #apply m to our quad - this will stretch the quad such that the ref suface will span the window extends glLoadMatrixf(m) draw_named_texture(world_tex_id) glMatrixMode(GL_PROJECTION) glPopMatrix() glMatrixMode(GL_MODELVIEW) glPopMatrix() # now lets get recent pupil positions on this surface: draw_gl_points_norm(self.gaze_on_srf,color=(0.,8.,.5,.8), size=80) glfwSwapBuffers(self._window) glfwMakeContextCurrent(active_window)
def initGL(self): """ initialize GL """ glMatrixMode(GL_PROJECTION) glLoadIdentity() gluOrtho2D(self.clipping_area[0], self.clipping_area[1], self.clipping_area[2], self.clipping_area[3])
def create_window(self): glutInit(sys.argv) glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB) glutInitWindowSize(800, 800) glutInitWindowPosition(320, 100) glutCreateWindow("lab1") glClearColor(self.background_color[0], self.background_color[1], self.background_color[2], 1.0) gluOrtho2D(-20.0, 20.0, -20.0, 20.0)
def resize(self): pygame.display.set_mode((self.W, self.H), pygame.OPENGL | pygame.DOUBLEBUF | pygame.RESIZABLE) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluOrtho2D(0, self.W, self.H, 0) glViewport(0, 0, self.W, self.H) # gluOrtho2D(self.L * (self.W / self.H), self.R * (self.W / self.H), self.B, self.T) # gluOrtho2D(self.L, self.R, self.B, self.T) pygame.display.flip()
def make_coord_system_pixel_based(img_shape): height,width,channels = img_shape # Set Projection Matrix glMatrixMode(GL_PROJECTION) glLoadIdentity() gluOrtho2D(0, width, height, 0) # origin in the top left corner just like the img np-array # Switch back to Model View Matrix glMatrixMode(GL_MODELVIEW) glLoadIdentity()
def tick(self, interval): """Functions called every frame """ Menu.tick(self, interval) posx, posy = pygame.mouse.get_pos() posy = self.height - posy for item in self.menu_items: if self.point_in_sprite(posx, posy, item): self.current_menu = self.menu_items.index(item) self.cursor.y = item.y self.cursor.x = (self.width / 2) - (item.width / 2) - self.cursor.width - 10 if self.transition_in: if self.menu_alpha < 1.0: self.menu_alpha += interval / 500.0 if self.menu_alpha >= 1.0: self.menu_alpha = 1.0 elif self.background_alpha < 1.0: self.background_alpha += interval / 500.0 if self.background_alpha >= 1.0: self.background_alpha = 1.0 #self.intro_music.play() self.transition_in = False if self.transition_out: self.zoom -= interval / 5000.0 if self.zoom < 0.5: self.menu_alpha -= interval / 500.0 if self.menu_alpha < 0.0: self.menu_alpha = 0.0 self.background_alpha -= interval / 500.0 if self.background_alpha < 0.0: self.background_alpha = 0.0 if self.zoom < 0.002: self.stop() glColor4f(1.0, 1.0, 1.0, self.background_alpha) self.g_background.draw() # Zoom for menu zoom_width = (self.width * self.zoom - self.width) zoom_height = (self.height * self.zoom - self.height) left = self.x + zoom_width / 2 right = left + self.width - zoom_width bottom = self.y + zoom_height / 2 top = bottom + self.height - zoom_height glMatrixMode(GL_PROJECTION) glLoadIdentity() gluOrtho2D(left, right, bottom, top) glColor4f(1.0, 1.0, 1.0, self.menu_alpha) self.g_menu.draw() # Reset view glColor4f(1.0, 1.0, 1.0, 1.0) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluOrtho2D(0.0, self.width, 0.0, self.height)
def init(): glutInit(sys.argv) glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB) glutInitWindowSize(*WINDOW_SIZE) glutInitWindowPosition(50, 50) glutCreateWindow("PyGLSnake") glClearColor( *(ELEMENT_TYPES[0] + [255]) ) #All our colors are 3-ints, this one needs alpha channel too so we just tack it on gluOrtho2D(0, WINDOW_SIZE[0], WINDOW_SIZE[1], 0)
def make_coord_system_pixel_based(img_shape): height, width, channels = img_shape # Set Projection Matrix glMatrixMode(GL_PROJECTION) glLoadIdentity() gluOrtho2D(0, width, height, 0) # origin in the top left corner just like the img np-array # Switch back to Model View Matrix glMatrixMode(GL_MODELVIEW) glLoadIdentity()
def draw_gl_texture(image): """ We draw the image as a texture on a quad that is perfectly set into our window. """ height, width, channels = image.shape if channels == 3: gl_blend = GL_BGR gl_blend_init = GL_RGB else: gl_blend = GL_BGRA gl_blend_init = GL_RGBA # Create Texture glTexImage2D(GL_TEXTURE_2D, 0, gl_blend_init, width, height, 0, gl_blend, GL_UNSIGNED_BYTE, image) glEnable(GL_TEXTURE_2D) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) # interpolation here # Set Projection Matrix glMatrixMode(GL_PROJECTION) glLoadIdentity() gluOrtho2D(0, width, height, 0) # origin in the top left corner just like the img np-array # Switch back to Model View Matrix glMatrixMode(GL_MODELVIEW) glLoadIdentity() # someday replace with this: # glEnableClientState(GL_VERTEX_ARRAY) # glEnableClientState(GL_TEXTURE_COORD_ARRAY) # Varray = numpy.array([[0,0],[0,1],[1,1],[1,0]],numpy.float) # glVertexPointer(2,GL_FLOAT,0,Varray) # glTexCoordPointer(2,GL_FLOAT,0,Varray) # indices = [0,1,2,3] # glDrawElements(GL_QUADS,1,GL_UNSIGNED_SHORT,indices) glColor4f(1.0,1.0,1.0,1.0) # Draw textured Quad. glBegin(GL_QUADS) glTexCoord2f(0.0, 0.0) glVertex2f(0.0, 0.0) glTexCoord2f(1.0, 0.0) glVertex2f(width, 0.0) glTexCoord2f(1.0, 1.0) glVertex2f(width, height) glTexCoord2f(0.0, 1.0) glVertex2f(0.0, height) glEnd() glDisable(GL_TEXTURE_2D)
def set_up_matrices(self, gl): # Save the matrix values. gl.glMatrixMode(gl.GL_PROJECTION) gl.glPushMatrix() gl.glLoadIdentity() gl.glMatrixMode(gl.GL_MODELVIEW); gl.glPushMatrix() left = self.width / 2.0 bottom = self.height / 2.0 gl.glLoadIdentity() gluOrtho2D(left, -left, bottom, -bottom)
def draw(self): gl.glClearColor(0.0, 0.0, 0.0, 0.0) gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) gl.glLoadIdentity() mat = np.dot(np.dot(np.dot(translation_matrix(0, 0, -3.0), self.modelview), scale_matrix(2.0, 1.0, 0.3)), translation_matrix(-0.5, 2.5, -0.5)) gl.glMultMatrixf(mat.transpose()) gl.glColor3f(1.0, 1.0, 1.0) gl.glTranslatef(0, float(self.shift) / self.waterfall.history, 0) gl.glScalef(1.0, 5.0, 1.0) self.waterfall.draw_scroll(self.color_offset, self.color_scale) gl.glMatrixMode(gl.GL_PROJECTION) gl.glPushMatrix() gl.glLoadIdentity() gluOrtho2D(0.0, self.screen_size[0], 0.0, self.screen_size[1]) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() gl.glDisable(gl.GL_DEPTH_TEST) gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) # gl.glPushMatrix() # gl.glTranslatef(100, 100, 0) # # gl.glColor4f(1.0, 1.0, 1.0, 1.0) # # gl.glBegin(gl.GL_LINES) # gl.glVertex2i(10, 8) # gl.glVertex2i(0, 4) # gl.glVertex2i(10, 0) # gl.glVertex2i(0, 4) # # gl.glVertex2i(35, 8) # gl.glVertex2i(45, 4) # gl.glVertex2i(35, 0) # gl.glVertex2i(45, 4) # gl.glEnd() # # gl.glPopMatrix() for a in self.sliders: a.draw() gl.glDisable(gl.GL_BLEND) gl.glEnable(gl.GL_DEPTH_TEST) gl.glMatrixMode(gl.GL_PROJECTION) gl.glPopMatrix() gl.glMatrixMode(gl.GL_MODELVIEW)
def draw_gl_points_norm(pos,size=20,color=(1.,0.5,0.5,.5)): glMatrixMode(GL_PROJECTION) glPushMatrix() glLoadIdentity() gluOrtho2D(0, 1, 0, 1) # gl coord convention glMatrixMode(GL_MODELVIEW) glPushMatrix() glLoadIdentity() draw_gl_points(pos,size,color) glMatrixMode(GL_PROJECTION) glPopMatrix() glMatrixMode(GL_MODELVIEW) glPopMatrix()
def draw_gl_points_norm(pos, size=20, color=(1., 0.5, 0.5, .5)): glMatrixMode(GL_PROJECTION) glPushMatrix() glLoadIdentity() gluOrtho2D(0, 1, 0, 1) # gl coord convention glMatrixMode(GL_MODELVIEW) glPushMatrix() glLoadIdentity() draw_gl_points(pos, size, color) glMatrixMode(GL_PROJECTION) glPopMatrix() glMatrixMode(GL_MODELVIEW) glPopMatrix()
def draw_gl_point_norm(pos,color): glMatrixMode(GL_PROJECTION) glPushMatrix() glLoadIdentity() gluOrtho2D(-1, 1, -1, 1) #origin at the center positive up, positve right glMatrixMode(GL_MODELVIEW) glPushMatrix() glLoadIdentity() draw_gl_point(pos,color) glMatrixMode(GL_PROJECTION) glPopMatrix() glMatrixMode(GL_MODELVIEW) glPopMatrix()
def gl_display_cache_bars(self): """ """ padding = 20. # Lines for areas that have be cached cached_ranges = [] for r in self.cache.visited_ranges: # [[0,1],[3,4]] cached_ranges += (r[0], 0), (r[1], 0) #[(0,0),(1,0),(3,0),(4,0)] # Lines where surfaces have been found in video cached_surfaces = [] for s in self.surfaces: found_at = [] if s.cache is not None: for r in s.cache.positive_ranges: # [[0,1],[3,4]] found_at += (r[0], 0), (r[1], 0 ) #[(0,0),(1,0),(3,0),(4,0)] cached_surfaces.append(found_at) glMatrixMode(GL_PROJECTION) glPushMatrix() glLoadIdentity() width, height = self.win_size h_pad = padding * (self.cache.length - 2) / float(width) v_pad = padding * 1. / (height - 2) gluOrtho2D( -h_pad, (self.cache.length - 1) + h_pad, -v_pad, 1 + v_pad ) # ranging from 0 to cache_len-1 (horizontal) and 0 to 1 (vertical) glMatrixMode(GL_MODELVIEW) glPushMatrix() glLoadIdentity() color = (8., .6, .2, 8.) draw_gl_polyline(cached_ranges, color=color, type='Lines', thickness=4) color = (0., .7, .3, 8.) for s in cached_surfaces: glTranslatef(0, .02, 0) draw_gl_polyline(s, color=color, type='Lines', thickness=2) glMatrixMode(GL_PROJECTION) glPopMatrix() glMatrixMode(GL_MODELVIEW) glPopMatrix()
def setupProjection(cls, w, h): """ \: setupProjection (void; float w, float h) { glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0, w-1, 0.0, h-1); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } """ glMatrixMode(GL_PROJECTION) glLoadIdentity() gluOrtho2D(0.0, w - 1, 0.0, h - 1) glMatrixMode(GL_MODELVIEW) glLoadIdentity()
def tick(self, interval): """ Functions called every frame """ Game.tick(self, interval) cam = self.camera # Draw Backgrounds glColor4f(1.0, 1.0, 1.0, self.background_alpha) left, right, bottom, top = cam.get_coords(self.background.width, self.background.height, False) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluOrtho2D(left, right, bottom, top) self.background.draw() left, right, bottom, top = cam.get_coords(self.background2.width, self.background2.height, False) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluOrtho2D(left, right, bottom, top) self.background2.draw() left, right, bottom, top = cam.get_coords(self.background3.width, self.background3.height, False) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluOrtho2D(left, right, bottom, top) self.background3.draw() # Move view to camera left, right, bottom, top = cam.get_coords(self.level.width, self.level.height) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluOrtho2D(left, right, bottom, top) # Draw Level self.level_pages_dev.draw(cam) self.level_pages.draw(cam) glColor4f(1.0, 1.0, 1.0, self.player_alpha) self.g_actors.draw() if self.transition_in: glColor4f(1.0, 1.0, 1.0, self.transition_alpha) self.g_transition.draw() glColor4f(1.0, 1.0, 1.0, 1.0) self.g_layer2.draw() # Zoom Out glMatrixMode(GL_PROJECTION) glLoadIdentity() gluOrtho2D(0.0, self.width, 0.0, self.height)
def gl_display_cache_bars(self): """ """ padding = 20. # Lines for areas that have been cached cached_ranges = [] for r in self.cache.visited_ranges: # [[0,1],[3,4]] cached_ranges += (r[0],0),(r[1],0) #[(0,0),(1,0),(3,0),(4,0)] # Lines where surfaces have been found in video cached_surfaces = [] for s in self.surfaces: found_at = [] if s.cache is not None: for r in s.cache.positive_ranges: # [[0,1],[3,4]] found_at += (r[0],0),(r[1],0) #[(0,0),(1,0),(3,0),(4,0)] cached_surfaces.append(found_at) glMatrixMode(GL_PROJECTION) glPushMatrix() glLoadIdentity() width,height = self.win_size h_pad = padding * (self.cache.length-2)/float(width) v_pad = padding* 1./(height-2) gluOrtho2D(-h_pad, (self.cache.length-1)+h_pad, -v_pad, 1+v_pad) # ranging from 0 to cache_len-1 (horizontal) and 0 to 1 (vertical) glMatrixMode(GL_MODELVIEW) glPushMatrix() glLoadIdentity() color = (8.,.6,.2,8.) draw_gl_polyline(cached_ranges,color=color,type='Lines',thickness=4) color = (0.,.7,.3,8.) for s in cached_surfaces: glTranslatef(0,.02,0) draw_gl_polyline(s,color=color,type='Lines',thickness=2) glMatrixMode(GL_PROJECTION) glPopMatrix() glMatrixMode(GL_MODELVIEW) glPopMatrix()
def gl_display(self): glMatrixMode(GL_PROJECTION) glPushMatrix() glLoadIdentity() gluOrtho2D( -self.h_pad, (self.frame_count) + self.h_pad, -self.v_pad, 1 + self.v_pad ) # ranging from 0 to cache_len-1 (horizontal) and 0 to 1 (vertical) glMatrixMode(GL_MODELVIEW) glPushMatrix() glLoadIdentity() # custom events for e in self.custom_events: draw_polyline([(e, .06), (e, .005)], color=RGBA(.8, .8, .8, .8)) size = len(self.custom_events) if size > 1: for i, e in enumerate(self.custom_events): draw_points([(e, .03)], size=5, color=RGBA(.1, .5, .5, 1.)) i = 0 while True: if i == 0: draw_polyline([(self.custom_events[i], .03), (self.custom_events[i + 1], 0.03)], color=RGBA(.8, .8, .8, .8)) elif (i > 0) and (i < (size - 1)): draw_polyline([(self.custom_events[i] + 1, .03), (self.custom_events[i + 1], 0.03)], color=RGBA(.8, .8, .8, .8)) if 'chain' in self.mode: i += 1 elif 'in out pairs' in self.mode: i += 2 if i > (size - 1): break glMatrixMode(GL_PROJECTION) glPopMatrix() glMatrixMode(GL_MODELVIEW) glPopMatrix()
def gl_display_in_window(self): active_window = glfwGetCurrentContext() glfwMakeContextCurrent(self._window) clear_gl_screen() # Set Matrix unsing gluOrtho2D to include padding for the marker of radius r # ############################ # r # # 0,0##################w,h # # # # # # # # # #r# #r# # # # # # # # # # 0,h##################w,h # # r # ############################ r = 60 gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() p_window_size = glfwGetWindowSize(self._window) # compensate for radius of marker gluOrtho2D( -r, p_window_size[0] + r, p_window_size[1] + r, -r) # origin in the top left corner just like the img np-array # Switch back to Model View Matrix gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() screen_pos = denormalize(self.display_pos, p_window_size, flip_y=True) draw_marker(screen_pos) #some feedback on the detection state if self.detected and self.on_position: draw_gl_point(screen_pos, 5, (0., 1., 0., 1.)) else: draw_gl_point(screen_pos, 5, (1., 0., 0., 1.)) glfwSwapBuffers(self._window) glfwMakeContextCurrent(active_window)
def reshapeWindow(self, w, h): # preventing dividing by zero if window height is 0 if h == 0: h = 1 # coefficient to maintain the proportions of the picture when resizing the window coef = float(w) / h glViewport(0, 0, w, h) glMatrixMode(GL_PROJECTION) glLoadIdentity() if w > h: gluOrtho2D(-20.0 * coef, 20.0 * coef, -20.0, 20.0) else: gluOrtho2D(-20.0, 20.0, -20.0 / coef, 20.0 / coef) glMatrixMode(GL_MODELVIEW) glLoadIdentity()
def gl_display_in_window(self): active_window = glfwGetCurrentContext() glfwMakeContextCurrent(self._window) clear_gl_screen() # Set Matrix unsing gluOrtho2D to include padding for the marker of radius r # ############################ # r # # 0,0##################w,h # # # # # # # # # #r# #r# # # # # # # # # # 0,h##################w,h # # r # ############################ r = 60 gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() p_window_size = glfwGetWindowSize(self._window) # compensate for radius of marker gluOrtho2D(-r,p_window_size[0]+r,p_window_size[1]+r, -r) # origin in the top left corner just like the img np-array # Switch back to Model View Matrix gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() screen_pos = denormalize(self.display_pos,p_window_size,flip_y=True) draw_marker(screen_pos) #some feedback on the detection state if self.detected and self.on_position: draw_gl_point(screen_pos, 5, (0.,1.,0.,1.)) else: draw_gl_point(screen_pos, 5, (1.,0.,0.,1.)) glfwSwapBuffers(self._window) glfwMakeContextCurrent(active_window)
def ayuda(): glMatrixMode(GL_PROJECTION) glPushMatrix() glLoadIdentity() gluOrtho2D(0.0, ventana_tam_x, 0.0, ventana_tam_y) glMatrixMode(GL_MODELVIEW) glPushMatrix() glLoadIdentity() glColor3f(1.0, 0.0, 0.0) num_lineas = 0 for s in strings_ayuda: glWindowPos2i(10, ventana_tam_y - 15*(num_lineas + 1)) for c in s: glutBitmapCharacter(GLUT_BITMAP_9_BY_15, ord(c)); num_lineas += 1 glMatrixMode(GL_MODELVIEW) glPopMatrix() glMatrixMode(GL_PROJECTION) glPopMatrix()
def ayuda(): glMatrixMode(GL_PROJECTION) glPushMatrix() glLoadIdentity() gluOrtho2D(0.0, ventana_tam_x, 0.0, ventana_tam_y) glMatrixMode(GL_MODELVIEW) glPushMatrix() glLoadIdentity() glColor3f(1.0, 0.0, 0.0) num_lineas = 0 for s in strings_ayuda: glWindowPos2i(10, ventana_tam_y - 15 * (num_lineas + 1)) for c in s: glutBitmapCharacter(GLUT_BITMAP_9_BY_15, ord(c)) num_lineas += 1 glMatrixMode(GL_MODELVIEW) glPopMatrix() glMatrixMode(GL_PROJECTION) glPopMatrix()
def gl_display_in_window(self,surface): """ here we map a selected surface onto a seperate window. """ active_window = glfwGetCurrentContext() glfwMakeContextCurrent(self._window) clear_gl_screen() # cv uses 3x3 gl uses 4x4 tranformation matricies m = cvmat_to_glmat(surface.m_from_screen) glMatrixMode(GL_PROJECTION) glPushMatrix() glLoadIdentity() gluOrtho2D(0, 1, 0, 1) # gl coord convention glMatrixMode(GL_MODELVIEW) glPushMatrix() #apply m to our quad - this will stretch the quad such that the ref suface will span the window extends glLoadMatrixf(m) #redraw will use the most recent world img texture and use it again. redraw_gl_texture( ((0,0),(1,0),(1,1),(0,1)) ) glMatrixMode(GL_PROJECTION) glPopMatrix() glMatrixMode(GL_MODELVIEW) glPopMatrix() #now lets get recent pupil positions on this surface: try: gaze_on_surface = [p['realtime gaze on '+surface.name] for p in self.recent_pupil_positions] except KeyError: gaze_on_surface = [] draw_gl_points_norm(gaze_on_surface,color=(0.,8.,.5,.8), size=80) glfwSwapBuffers(self._window) glfwMakeContextCurrent(active_window)
def gl_display_in_window(self, surface): """ here we map a selected surface onto a seperate window. """ active_window = glfwGetCurrentContext() glfwMakeContextCurrent(self._window) clear_gl_screen() # cv uses 3x3 gl uses 4x4 tranformation matricies m = cvmat_to_glmat(surface.m_from_screen) glMatrixMode(GL_PROJECTION) glPushMatrix() glLoadIdentity() gluOrtho2D(0, 1, 0, 1) # gl coord convention glMatrixMode(GL_MODELVIEW) glPushMatrix() #apply m to our quad - this will stretch the quad such that the ref suface will span the window extends glLoadMatrixf(m) draw_named_texture(self.g_pool.image_tex) glMatrixMode(GL_PROJECTION) glPopMatrix() glMatrixMode(GL_MODELVIEW) glPopMatrix() #now lets get recent pupil positions on this surface: try: gaze_on_surface = [ p['realtime gaze on ' + surface.name] for p in self.recent_pupil_positions ] except KeyError: gaze_on_surface = [] draw_gl_points_norm(gaze_on_surface, color=(0., 8., .5, .8), size=80) glfwSwapBuffers(self._window) glfwMakeContextCurrent(active_window)
def gl_display(self): glMatrixMode(GL_PROJECTION) glPushMatrix() glLoadIdentity() gluOrtho2D( -self.h_pad, (self.frame_count) + self.h_pad, -self.v_pad, 1 + self.v_pad ) # ranging from 0 to cache_len-1 (horizontal) and 0 to 1 (vertical) glMatrixMode(GL_MODELVIEW) glPushMatrix() glLoadIdentity() # if self.drag_mode: # color1 = (0.,.8,.5,.5) # color2 = (0.,.8,.5,1.) # else: color1 = (1, 1, 1, 0.4) #(.25,.8,.8,.5) color2 = (1, 1, 1, 1.) #(.25,.8,.8,1.) thickness = 10. draw_polyline(verts=[(0, 0), (self.current_frame_index, 0)], thickness=thickness, color=RGBA(*color1)) draw_polyline(verts=[(self.current_frame_index, 0), (self.frame_count, 0)], thickness=thickness, color=RGBA(*color1)) if not self.drag_mode: draw_points([(self.current_frame_index, 0)], color=RGBA(*color1), size=30) draw_points([(self.current_frame_index, 0)], color=RGBA(*color2), size=20) glMatrixMode(GL_PROJECTION) glPopMatrix() glMatrixMode(GL_MODELVIEW) glPopMatrix()
def gl_display_metrics(self): if self.metrics_texture and self.detected: # cv uses 3x3 gl uses 4x4 tranformation matricies m = cvmat_to_glmat(self.m_to_screen) glMatrixMode(GL_PROJECTION) glPushMatrix() glLoadIdentity() gluOrtho2D(0, 1, 0, 1) # gl coord convention glMatrixMode(GL_MODELVIEW) glPushMatrix() # apply m to our quad - this will stretch the quad such that the ref suface will span the window extends glLoadMatrixf(m) draw_named_texture(self.metrics_texture) glMatrixMode(GL_PROJECTION) glPopMatrix() glMatrixMode(GL_MODELVIEW) glPopMatrix()
def gl_display(self): glMatrixMode(GL_PROJECTION) glPushMatrix() glLoadIdentity() gluOrtho2D(-self.h_pad, (self.frame_count)+self.h_pad, -self.v_pad, 1+self.v_pad) # ranging from 0 to cache_len-1 (horizontal) and 0 to 1 (vertical) glMatrixMode(GL_MODELVIEW) glPushMatrix() glLoadIdentity() color1 = (.1,.9,.2,.5) color2 = (.1,.9,.2,.5) if self.in_mark != 0 or self.out_mark != self.frame_count: draw_gl_polyline( [(self.in_mark,0),(self.out_mark,0)],color=(.1,.9,.2,.5),thickness=2) draw_gl_point((self.in_mark,0),color=color2,size=10) draw_gl_point((self.out_mark,0),color=color2,size=10) glMatrixMode(GL_PROJECTION) glPopMatrix() glMatrixMode(GL_MODELVIEW) glPopMatrix()
def gl_display_metrics(self): if self.metrics_texture and self.detected: # cv uses 3x3 gl uses 4x4 tranformation matricies m = cvmat_to_glmat(self.m_to_screen) glMatrixMode(GL_PROJECTION) glPushMatrix() glLoadIdentity() gluOrtho2D(0, 1, 0, 1) # gl coord convention glMatrixMode(GL_MODELVIEW) glPushMatrix() #apply m to our quad - this will stretch the quad such that the ref suface will span the window extends glLoadMatrixf(m) draw_named_texture(self.metrics_texture) glMatrixMode(GL_PROJECTION) glPopMatrix() glMatrixMode(GL_MODELVIEW) glPopMatrix()
def gl_display(self): glMatrixMode(GL_PROJECTION) glPushMatrix() glLoadIdentity() gluOrtho2D(-self.h_pad, (self.frame_count)+self.h_pad, -self.v_pad, 1+self.v_pad) # ranging from 0 to cache_len-1 (horizontal) and 0 to 1 (vertical) glMatrixMode(GL_MODELVIEW) glPushMatrix() glLoadIdentity() # custom events for e in self.custom_events: draw_polyline([(e,.06),(e,.005)], color = RGBA(.8, .8, .8, .8)) size = len(self.custom_events) if size > 1: for i, e in enumerate(self.custom_events): draw_points([(e, .03)], size = 5, color = RGBA(.1, .5, .5, 1.)) i = 0 while True: if i == 0: draw_polyline([(self.custom_events[i],.03),(self.custom_events[i+1],0.03)], color = RGBA(.8, .8, .8, .8)) elif (i > 0) and (i < (size-1)): draw_polyline([(self.custom_events[i] +1,.03),(self.custom_events[i+1],0.03)], color = RGBA(.8, .8, .8, .8)) if 'chain' in self.mode: i += 1 elif 'in out pairs' in self.mode: i += 2 if i > (size-1): break glMatrixMode(GL_PROJECTION) glPopMatrix() glMatrixMode(GL_MODELVIEW) glPopMatrix()
def draw_gl_texture(image,interpolation=True,update=True): """ We draw the image as a texture on a quad from 0,0 to img.width,img.height. to save cpu power, update can be false and we will reuse the old img instead of uploading the new. """ height, width, channels = image.shape if channels == 3: gl_blend = GL_BGR gl_blend_init = GL_RGB else: gl_blend = GL_BGRA gl_blend_init = GL_RGBA glPixelStorei(GL_UNPACK_ALIGNMENT,1) global texture_id if texture_id is None: texture_id = glGenTextures(1) glBindTexture(GL_TEXTURE_2D, texture_id) # Create Texture glTexImage2D(GL_TEXTURE_2D, 0, gl_blend_init, width, height, 0, gl_blend, GL_UNSIGNED_BYTE, image) glBindTexture(GL_TEXTURE_2D, texture_id) # update texture if update: glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, gl_blend, GL_UNSIGNED_BYTE, image) glEnable(GL_TEXTURE_2D) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) # interpolation here if not interpolation: glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); # Set Projection Matrix glMatrixMode(GL_PROJECTION) glLoadIdentity() gluOrtho2D(0, width, height, 0) # origin in the top left corner just like the img np-array # Switch back to Model View Matrix glMatrixMode(GL_MODELVIEW) glLoadIdentity() # someday replace with this: # glEnableClientState(GL_VERTEX_ARRAY) # glEnableClientState(GL_TEXTURE_COORD_ARRAY) # Varray = numpy.array([[0,0],[0,1],[1,1],[1,0]],numpy.float) # glVertexPointer(2,GL_FLOAT,0,Varray) # glTexCoordPointer(2,GL_FLOAT,0,Varray) # indices = [0,1,2,3] # glDrawElements(GL_QUADS,1,GL_UNSIGNED_SHORT,indices) glColor4f(1.0,1.0,1.0,1.0) # Draw textured Quad. glBegin(GL_QUADS) glTexCoord2f(0.0, 0.0) glVertex2f(0.0, 0.0) glTexCoord2f(1.0, 0.0) glVertex2f(width, 0.0) glTexCoord2f(1.0, 1.0) glVertex2f(width, height) glTexCoord2f(0.0, 1.0) glVertex2f(0.0, height) glEnd() glDisable(GL_TEXTURE_2D)
glBegin(GL_LINE_STRIP) elif type == 'Polygon': glBegin(GL_POLYGON) else: glBegin(GL_LINES) for x, y in positions: glVertex3f(x, y, 0.0) glEnd() def draw_gl_polyline_norm((positions), color, type='Loop', thickness=1): glMatrixMode(GL_PROJECTION) glPushMatrix() glLoadIdentity() gluOrtho2D(0, 1, 0, 1) # gl coord convention glMatrixMode(GL_MODELVIEW) glPushMatrix() glLoadIdentity() draw_gl_polyline(positions, color, type, thickness) glMatrixMode(GL_PROJECTION) glPopMatrix() glMatrixMode(GL_MODELVIEW) glPopMatrix() # def draw_gl_point((x,y),size=20,color=(1.,0.5,0.5,.5)): # glColor4f(*color) # glPointSize(int(size)) # glBegin(GL_POINTS) # glVertex3f(x,y,0.0)
def draw(self): """ Draws the rulers. """ width = self.glpane.width height = self.glpane.height # These 3 attrs (scale, aspect, and ruler_position) are checked to # determine if they've changed. If any of them have, # getRulerDrawingParameters() must be called to get new drawing parms. if ( (self.scale != self.glpane.scale) or (self.zoomFactor != self.glpane.zoomFactor) or (self.aspect != self.glpane.aspect) or (self.ruler_position != env.prefs[rulerPosition_prefs_key]) ): self.scale = self.glpane.scale self.zoomFactor = self.glpane.zoomFactor self.aspect = self.glpane.aspect self.ruler_position = env.prefs[rulerPosition_prefs_key] self.ruler_drawing_params = getRulerDrawingParameters( width, height, self.aspect, self.scale, self.zoomFactor, self.ruler_position ) ( draw_ticks_and_text, units_text, units_format, units_scale, unit_label_inc, long_tickmark_inc, medium_tickmark_inc, num_vert_ticks, num_horz_ticks, tickmark_spacing_multiplier, ruler_origin, ruler_start_pt, units_text_origin, origin_square_pt1, origin_square_pt2, vr_thickness, vr_tickmark_spacing, vr_long_tick_len, vr_medium_tick_len, vr_short_tick_len, vr_rect_pt1, vr_rect_pt2, vr_line_pt1, vr_line_pt2, vr_units_x_offset, vr_units_y_offset, hr_thickness, hr_tickmark_spacing, hr_long_tick_len, hr_medium_tick_len, hr_short_tick_len, hr_rect_pt1, hr_rect_pt2, hr_line_pt1, hr_line_pt2, hr_units_x_offset, hr_units_y_offset, ) = self.ruler_drawing_params ruler_color = env.prefs[rulerColor_prefs_key] ruler_opacity = env.prefs[rulerOpacity_prefs_key] # These may become user preferences in the future. tickmark_color = darkgray text_color = black # Set up 2D (window) coordinate system. # Bruce - please review this section. glMatrixMode(GL_MODELVIEW) glPushMatrix() glLoadIdentity() glMatrixMode(GL_PROJECTION) glPushMatrix() glLoadIdentity() # needed! gluOrtho2D(0.0, float(width), 0.0, float(height)) glMatrixMode(GL_MODELVIEW) # About this glMatrixMode(GL_MODELVIEW) call, Bruce wrote in a review: # The only reason this is desirable (it's not really needed) is if, # when someone is editing the large body of drawing code after this, # they inadvertently do something which is only correct if the matrix # mode is GL_MODELVIEW (e.g. if they use glTranslate to shift a ruler # or tickmark position). Most of our drawing code assumes it can do # this, so a typical NE1 OpenGL programmer may naturally assume this, # which is why it's good to leave the matrix mode as GL_MODELVIEW when # entering into a large hunk of ordinary drawing code (especially if # it might call other drawing functions, now or in the future). # Mark 2008-03-03 glDisable(GL_LIGHTING) glDisable(GL_DEPTH_TEST) # Note: disabling GL_DEPTH_TEST is not honored by the 3d version of # glpane.renderText -- ruler text can still be obscured by # previously drawn less-deep model objects. # But the 2d call of renderText in part.py does honor this. # The Qt doc says why, if I guess how to interpret it: # http://doc.trolltech.com/4.3/qglwidget.html#renderText # says, for the 3d version of renderText only (passing three model # coords as opposed to two window coords): # Note that this function only works properly if GL_DEPTH_TEST # is enabled, and you have a properly initialized depth buffer. # Possible fixes: # - revise ruler_origin to be very close to the screen, # and hope that this disable is merely ignored, not a messup; # - or, use the 2d version of the function. # I did the latter fix (2d renderText, below) and it seems to work. # [bruce 081204 comment] # Suppress writing into the depth buffer so anything behind the ruler # can still be highlighted/selected. glDepthMask(GL_FALSE) # == Draw v/h ruler rectangles in the user defined color and opacity. glColor4fv(list(ruler_color) + [ruler_opacity]) glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) glRectf(origin_square_pt1[0], origin_square_pt1[1], origin_square_pt2[0], origin_square_pt2[1]) # Origin square if env.prefs[displayVertRuler_prefs_key]: glRectf(vr_rect_pt1[0], vr_rect_pt1[1], vr_rect_pt2[0], vr_rect_pt2[1]) # Vertical ruler if env.prefs[displayHorzRuler_prefs_key]: glRectf(hr_rect_pt1[0], hr_rect_pt1[1], hr_rect_pt2[0], hr_rect_pt2[1]) # Horizontal ruler glDisable(GL_BLEND) # Set color of ruler lines, tick marks and text. glColor3fv(tickmark_color) self.glpane.qglColor(RGBf_to_QColor(text_color)) # Draw unit of measurement in corner (A or nm). # piotr 080326: replaced drawText with drawCenteredText self.drawCenteredText(units_text, units_text_origin) # Kludge alert. Finish drawing ruler edge(s) if we will not be # drawing the ruler tick marks and text (only happens when the user # is zoomed out to an "absurd scale factor"). if not draw_ticks_and_text: if env.prefs[displayVertRuler_prefs_key]: self.drawLine(vr_line_pt1, vr_line_pt2) if env.prefs[displayHorzRuler_prefs_key]: self.drawLine(hr_line_pt1, hr_line_pt2) # == Draw vertical ruler line(s) and tick marks if env.prefs[displayVertRuler_prefs_key] and draw_ticks_and_text: # Draw vertical line along right/left edge of ruler. self.drawLine(vr_line_pt1, vr_line_pt2) # Initialize pt1 and pt2, the tick mark endpoints. The first tick # mark will span the entire width of the ruler, which serves as a # divider b/w the unit of measure text (i.e. A or nm) and the rest # of the ruler. pt1 = ruler_start_pt pt2 = ruler_start_pt + V(-vr_thickness, 0.0, 0.0) # Draw vertical ruler tickmarks, including numeric unit labels for tick_num in range(num_horz_ticks + 1): # pt1 and pt2 are modified by each iteration of the loop. self.drawLine(pt1, pt2) # Draw units number beside long tickmarks. if not tick_num % unit_label_inc: units_num_origin = pt1 + V(vr_units_x_offset, vr_units_y_offset, 0.0) units_num = units_format % (tick_num * units_scale) self.drawText(units_num, units_num_origin) # Update tickmark endpoints for next tickmark. pt1 = ruler_start_pt + V(0.0, vr_tickmark_spacing * tickmark_spacing_multiplier * (tick_num + 1), 0.0) if not (tick_num + 1) % long_tickmark_inc: pt2 = pt1 + V(vr_long_tick_len, 0.0, 0.0) elif not (tick_num + 1) % medium_tickmark_inc: pt2 = pt1 + V(vr_medium_tick_len, 0.0, 0.0) else: pt2 = pt1 + V(vr_short_tick_len, 0.0, 0.0) # End vertical ruler # == Draw horizontal ruler line(s) and tick marks if env.prefs[displayHorzRuler_prefs_key] and draw_ticks_and_text: # Draw horizontal line along top/bottom edge of ruler. self.drawLine(hr_line_pt1, hr_line_pt2) # Initialize pt1 and pt2, the tick mark endpoints. The first tick # mark will span the entire width of the ruler, which serves as a # divider b/w the unit of measure text (i.e. A or nm) and the rest # of the ruler. pt1 = ruler_start_pt pt2 = ruler_start_pt + V(0.0, -hr_thickness, 0.0) # Draw horizontal ruler (with vertical) tickmarks, including its # numeric unit labels for tick_num in range(num_vert_ticks + 1): # pt1 and pt2 are modified by each iteration of the loop. self.drawLine(pt1, pt2) # Draw units number beside long tickmarks. if not tick_num % unit_label_inc: units_num_origin = pt1 + V(hr_units_x_offset, hr_units_y_offset, 0.0) units_num = units_format % (tick_num * units_scale) self.drawText(units_num, units_num_origin) # Update tickmark endpoints for next tickmark. pt1 = ruler_start_pt + V(hr_tickmark_spacing * tickmark_spacing_multiplier * (tick_num + 1), 0.0, 0.0) if not (tick_num + 1) % long_tickmark_inc: pt2 = pt1 + V(0.0, hr_long_tick_len, 0.0) elif not (tick_num + 1) % medium_tickmark_inc: pt2 = pt1 + V(0.0, hr_medium_tick_len, 0.0) else: pt2 = pt1 + V(0.0, hr_short_tick_len, 0.0) # End horizontal ruler # Restore OpenGL state. glDepthMask(GL_TRUE) glEnable(GL_DEPTH_TEST) glEnable(GL_LIGHTING) glDepthMask(GL_TRUE) glMatrixMode(GL_PROJECTION) glPopMatrix() glMatrixMode(GL_MODELVIEW) glPopMatrix() return # from drawRulers
def set_window(self, l, r, b, t): glMatrixMode(GL_PROJECTION) glLoadIdentity() gluOrtho2D(l, r, b, t)
def make_coord_system_norm_based(): glMatrixMode(GL_PROJECTION) glLoadIdentity() gluOrtho2D(0, 1, 0, 1) # gl coord convention glMatrixMode(GL_MODELVIEW) glLoadIdentity()
elif type=='Strip': glBegin(GL_LINE_STRIP) elif type=='Polygon': glBegin(GL_POLYGON) else: glBegin(GL_LINES) for x,y in positions: glVertex3f(x,y,0.0) glEnd() def draw_gl_polyline_norm((positions),color,type='Loop'): glMatrixMode(GL_PROJECTION) glPushMatrix() glLoadIdentity() gluOrtho2D(0, 1, 0, 1) # gl coord convention glMatrixMode(GL_MODELVIEW) glPushMatrix() glLoadIdentity() draw_gl_polyline(positions,color,type) glMatrixMode(GL_PROJECTION) glPopMatrix() glMatrixMode(GL_MODELVIEW) glPopMatrix() # def draw_gl_point((x,y),size=20,color=(1.,0.5,0.5,.5)): # glColor4f(*color) # glPointSize(int(size)) # glBegin(GL_POINTS) # glVertex3f(x,y,0.0)
def main(): pygame.init() edit_mode = False width = 800 height = 600 screen = pygame.display.set_mode((width, height), pygame.OPENGL|pygame.DOUBLEBUF) vertex_circle = GLSolidCircle(4) fp_circle = GLCircle(10) marker_circle = GLSolidCircle(3) glClearColor(*background_color) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluOrtho2D(0.0, width, height, 0.0) level = Level() groups = [ { 'dudes': [Dude( 250, 250, 1 ), Dude( 275, 250, 1 ), Dude( 250, 275, 1 ), Dude( 275, 275, 1 ) ]}, { 'dudes': [Dude( 500, 250, 0 ), Dude( 525, 250, 0 ), Dude( 500, 275, 0 ), Dude( 525, 275, 0 ) ]}, ] sel_group = 0 sel_dude = 0 mx, my = 0, 0 def validate_sel_dude(x,z): n = len( groups[ sel_group ][ 'dudes' ] ) return x if x < n else z while True: global poly glClear(GL_COLOR_BUFFER_BIT) glMatrixMode(GL_MODELVIEW) glLoadIdentity() for event in pygame.event.get(): if event.type == QUIT: return if event.type == KEYDOWN: if event.key == K_e: edit_mode = not edit_mode if poly != None and len(level.polys[poly]) < 3: level.del_poly(poly) poly = None elif event.key == K_TAB: sel_group = (sel_group + 1) % len(groups) # don't let the dindex point to an invalid dude. sel_dude = validate_sel_dude( sel_dude, 0 ) elif edit_mode: if event.key == K_s: level.save_file( 'level.dat' ) if event.key == K_l: poly = None level.load_file('level.dat') elif event.key == K_w: sel_dude = validate_sel_dude( 0, sel_dude ) elif event.key == K_a: sel_dude = validate_sel_dude( 1, sel_dude ) elif event.key == K_s: sel_dude = validate_sel_dude( 2, sel_dude ) elif event.key == K_d: sel_dude = validate_sel_dude( 3, sel_dude ) if event.type == MOUSEBUTTONDOWN: if edit_mode: do_edit_action( level, event ) elif event.button == 1: for dude in groups[sel_group]['dudes']: dude.issue_orders() if event.type == MOUSEMOTION: mx, my = event.pos if edit_mode: pass #screen.blit(text, text.get_rect()) # draw level.draw( screen, edit_mode ) for index,group in enumerate(groups): for dindex, dude in enumerate(group['dudes']): sel_level = 0 if sel_group == index: sel_level = 2 if dindex == sel_dude else 1 dude.draw( screen, sel_level ) if edit_mode: level.draw_vertex_circle( mx, my , vertex_circle) level.draw_poly_vertex( poly, marker_circle ) level.draw_guide_line( poly, mx, my ) else: # show movement plan # todo: get this crap out of the renderer fp = level.get_firing_position_near( mx, my ) if fp != None: for i, dude in enumerate(groups[sel_group]['dudes']): f = intvec( get_position_for( fp, i ) ) color = (0.78, 0.59, 0.0) if i == 0 else (0.43, 0.27, 0.0) fp_circle.draw( f, color ) path = level.get_path( dude.pos, f ) dude.set_possible_orders( path ) draw_path( path ) else: for dude in groups[sel_group]['dudes']: dude.set_possible_orders( None ) pygame.display.flip()
def draw(self): """ Draws the rulers. """ width = self.glpane.width height = self.glpane.height # These 3 attrs (scale, aspect, and ruler_position) are checked to # determine if they've changed. If any of them have, # getRulerDrawingParameters() must be called to get new drawing parms. if (self.scale != self.glpane.scale) or \ (self.zoomFactor != self.glpane.zoomFactor) or \ (self.aspect != self.glpane.aspect) or \ (self.ruler_position != env.prefs[rulerPosition_prefs_key]): self.scale = self.glpane.scale self.zoomFactor = self.glpane.zoomFactor self.aspect = self.glpane.aspect self.ruler_position = env.prefs[rulerPosition_prefs_key] self.ruler_drawing_params = \ getRulerDrawingParameters(width, height, self.aspect, self.scale, self.zoomFactor, self.ruler_position) (draw_ticks_and_text, units_text, units_format, units_scale, unit_label_inc, long_tickmark_inc, medium_tickmark_inc, num_vert_ticks, num_horz_ticks, tickmark_spacing_multiplier, ruler_origin, ruler_start_pt, units_text_origin, origin_square_pt1, origin_square_pt2, vr_thickness, vr_tickmark_spacing, vr_long_tick_len, vr_medium_tick_len, vr_short_tick_len, vr_rect_pt1, vr_rect_pt2, vr_line_pt1, vr_line_pt2, vr_units_x_offset, vr_units_y_offset, hr_thickness, hr_tickmark_spacing, hr_long_tick_len, hr_medium_tick_len, hr_short_tick_len, hr_rect_pt1, hr_rect_pt2, hr_line_pt1, hr_line_pt2, hr_units_x_offset, hr_units_y_offset) = self.ruler_drawing_params ruler_color = env.prefs[rulerColor_prefs_key] ruler_opacity = env.prefs[rulerOpacity_prefs_key] # These may become user preferences in the future. tickmark_color = darkgray text_color = black # Set up 2D (window) coordinate system. # Bruce - please review this section. glMatrixMode(GL_MODELVIEW) glPushMatrix() glLoadIdentity() glMatrixMode(GL_PROJECTION) glPushMatrix() glLoadIdentity() # needed! gluOrtho2D(0.0, float(width), 0.0, float(height)) glMatrixMode(GL_MODELVIEW) # About this glMatrixMode(GL_MODELVIEW) call, Bruce wrote in a review: # The only reason this is desirable (it's not really needed) is if, # when someone is editing the large body of drawing code after this, # they inadvertently do something which is only correct if the matrix # mode is GL_MODELVIEW (e.g. if they use glTranslate to shift a ruler # or tickmark position). Most of our drawing code assumes it can do # this, so a typical NE1 OpenGL programmer may naturally assume this, # which is why it's good to leave the matrix mode as GL_MODELVIEW when # entering into a large hunk of ordinary drawing code (especially if # it might call other drawing functions, now or in the future). # Mark 2008-03-03 glDisable(GL_LIGHTING) glDisable(GL_DEPTH_TEST) # Suppress writing into the depth buffer so anything behind the ruler # can still be highlighted/selected. glDepthMask(GL_FALSE) # Draw v/h ruler rectangles in the user defined color and opacity. ##### glColor4fv(list(ruler_color) + [ruler_opacity]) glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) glRectf(origin_square_pt1[0], origin_square_pt1[1], origin_square_pt2[0], origin_square_pt2[1]) # Origin square if env.prefs[displayVertRuler_prefs_key]: glRectf(vr_rect_pt1[0], vr_rect_pt1[1], vr_rect_pt2[0], vr_rect_pt2[1]) # Vertical ruler if env.prefs[displayHorzRuler_prefs_key]: glRectf(hr_rect_pt1[0], hr_rect_pt1[1], hr_rect_pt2[0], hr_rect_pt2[1]) # Horizontal ruler glDisable(GL_BLEND) # Set color of ruler lines, tick marks and text. glColor3fv(tickmark_color) self.glpane.qglColor(RGBf_to_QColor(text_color)) # Draw unit of measurement in corner (A or nm). # piotr 080326: replaced drawText with drawCenteredText self.drawCenteredText(units_text, units_text_origin) # Kludge alert. Finish drawing ruler edge(s) if we will not be # drawing the ruler tick marks and text (only happens when the user # is zoomed out to an "absured scale factor". if not draw_ticks_and_text: if env.prefs[displayVertRuler_prefs_key]: self.drawLine(vr_line_pt1, vr_line_pt2) if env.prefs[displayHorzRuler_prefs_key]: self.drawLine(hr_line_pt1, hr_line_pt2) # Draw vertical ruler line(s) and tick marks ########################## if env.prefs[displayVertRuler_prefs_key] and draw_ticks_and_text: # Draw vertical line along right/left edge of ruler. self.drawLine(vr_line_pt1, vr_line_pt2) # Initialize pt1 and pt2, the tick mark endpoints. The first tick # mark will span the entire width of the ruler, which serves as a # divider b/w the unit of measure text (i.e. A or nm) and the rest # of the ruler. pt1 = ruler_start_pt pt2 = ruler_start_pt + V(-vr_thickness, 0.0, 0.0) # Draw vertical ruler tickmarks, including numeric unit labels for tick_num in range(num_horz_ticks + 1): # pt1 and pt2 are modified by each iteration of the loop. self.drawLine(pt1, pt2) # Draw units number beside long tickmarks. if not tick_num % unit_label_inc: units_num_origin = pt1 \ + V(vr_units_x_offset, vr_units_y_offset, 0.0) units_num = units_format % (tick_num * units_scale) self.drawText(units_num, units_num_origin) # Update tickmark endpoints for next tickmark. pt1 = ruler_start_pt + \ V(0.0, vr_tickmark_spacing * tickmark_spacing_multiplier * (tick_num + 1), 0.0) if not (tick_num + 1) % long_tickmark_inc: pt2 = pt1 + V(vr_long_tick_len, 0.0, 0.0) elif not (tick_num + 1) % medium_tickmark_inc: pt2 = pt1 + V(vr_medium_tick_len, 0.0, 0.0) else: pt2 = pt1 + V(vr_short_tick_len, 0.0, 0.0) # End vertical ruler # Draw horizontal ruler line(s) and tick marks ######################### if env.prefs[displayHorzRuler_prefs_key] and draw_ticks_and_text: # Draw horizontal line along top/bottom edge of ruler. self.drawLine(hr_line_pt1, hr_line_pt2) # Initialize pt1 and pt2, the tick mark endpoints. The first tick # mark will span the entire width of the ruler, which serves as a # divider b/w the unit of measure text (i.e. A or nm) and the rest # of the ruler. pt1 = ruler_start_pt pt2 = ruler_start_pt + V(0.0, -hr_thickness, 0.0) # Draw horizontal ruler (with vertical) tickmarks, including its # numeric unit labels for tick_num in range(num_vert_ticks + 1): # pt1 and pt2 are modified by each iteration of the loop. self.drawLine(pt1, pt2) # Draw units number beside long tickmarks. if not tick_num % unit_label_inc: units_num_origin = pt1 \ + V(hr_units_x_offset, hr_units_y_offset, 0.0) units_num = units_format % (tick_num * units_scale) self.drawText(units_num, units_num_origin) # Update tickmark endpoints for next tickmark. pt1 = \ ruler_start_pt + \ V(hr_tickmark_spacing * tickmark_spacing_multiplier * (tick_num + 1), 0.0, 0.0) if not (tick_num + 1) % long_tickmark_inc: pt2 = pt1 + V(0.0, hr_long_tick_len, 0.0) elif not (tick_num + 1) % medium_tickmark_inc: pt2 = pt1 + V(0.0, hr_medium_tick_len, 0.0) else: pt2 = pt1 + V(0.0, hr_short_tick_len, 0.0) # End horizontal ruler # Restore OpenGL state. glDepthMask(GL_TRUE) glEnable(GL_DEPTH_TEST) glEnable(GL_LIGHTING) glDepthMask(GL_TRUE) glMatrixMode(GL_PROJECTION) glPopMatrix() glMatrixMode(GL_MODELVIEW) glPopMatrix() return # from drawRulers
def set_window(self,l,r,b,t): glMatrixMode(GL_PROJECTION) glLoadIdentity() gluOrtho2D(l,r,b,t)
def init(): glClearColor(0, 0, 0, 0.0) glMatrixMode(GL_PROJECTION) gluOrtho2D(0.0, 300, 0.0, 300)
def player(g_pool,size): """player - Shows 9 point calibration pattern - Plays a source video synchronized with world process - Get src videos from directory (glob) - Iterate through videos on each record event """ grid = make_grid() grid *=2.5###scale to fit # player object player = Temp() player.play_list = glob('src_video/*') path_parent = os.path.dirname( os.path.abspath(sys.argv[0])) player.playlist = [os.path.join(path_parent, path) for path in player.play_list] player.captures = [autoCreateCapture(src) for src in player.playlist] print "Player found %i videos in src_video"%len(player.captures) player.captures = [c for c in player.captures if c is not None] print "Player sucessfully loaded %i videos in src_video"%len(player.captures) # for c in player.captures: c.auto_rewind = False player.current_video = 0 # Callbacks def on_resize(w, h): adjust_gl_view(w,h) def on_key(key, pressed): if key == GLFW_KEY_ESC: on_close() def on_char(char, pressed): if pressed: g_pool.player_input.value = char def on_close(): g_pool.quit.value = True print "Player Process closing from window" def draw_circle(pos,r,c): pts = cv2.ellipse2Poly(tuple(pos),(r,r),0,0,360,10) draw_gl_polyline(pts,c,'Polygon') def draw_marker(pos): pos = int(pos[0]),int(pos[1]) black = (0.,0.,0.,1.) white = (1.,1.,1.,1.) for r,c in zip((50,40,30,20,10),(black,white,black,white,black)): draw_circle(pos,r,c) # Initialize glfw glfwInit() glfwOpenWindow(size[0], size[1], 0, 0, 0, 8, 0, 0, GLFW_WINDOW) glfwSetWindowTitle("Player") glfwSetWindowPos(100,0) glfwDisable(GLFW_AUTO_POLL_EVENTS) # Callbacks glfwSetWindowSizeCallback(on_resize) glfwSetWindowCloseCallback(on_close) glfwSetKeyCallback(on_key) glfwSetCharCallback(on_char) # gl state settings gl.glEnable(gl.GL_POINT_SMOOTH) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) gl.glEnable(gl.GL_BLEND) gl.glDisable (gl.GL_DEPTH_TEST) gl.glClearColor(1.,1.,1.,0.) while glfwGetWindowParam(GLFW_OPENED) and not g_pool.quit.value: glfwPollEvents() if g_pool.player_refresh.wait(0.01): g_pool.player_refresh.clear() clear_gl_screen() if g_pool.marker_state.value !=0: # Set Matrix unsing gluOrtho2D to include padding for the marker of radius r # ############################ # r # # 0,0##################w,h # # # # # # # # # #r# #r# # # # # # # # # # 0,h##################w,h # # r # ############################ r = 60 gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() # compensate for radius of marker gluOrtho2D(-r,glfwGetWindowSize()[0]+r,glfwGetWindowSize()[1]+r, -r) # origin in the top left corner just like the img np-array # Switch back to Model View Matrix gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() screen_pos = denormalize(g_pool.marker[:],glfwGetWindowSize(),flip_y=True) #some feedback on the detection state draw_marker(screen_pos) if g_pool.ref[:] == [0.,0.]: # world ref is detected draw_gl_point(screen_pos, 5.0, (1.,0.,0.,1.)) else: draw_gl_point(screen_pos, 5.0, (0.,1.,0.,1.)) elif g_pool.play.value: if len(player.captures): frame = player.captures[player.current_video].get_frame() img = frame.img if img: draw_gl_texture(img) else: player.captures[player.current_video].rewind() player.current_video +=1 if player.current_video >= len(player.captures): player.current_video = 0 g_pool.play.value = False else: print 'PLAYER: Warning: No Videos available to play. Please put your vidoes into a folder called "src_video" in the Capture folder.' g_pool.play.value = False glfwSwapBuffers() glfwCloseWindow() glfwTerminate() print "PLAYER Process closed"
def run(self): """ Run game Initialize pygame, the game window, and the game clock. Run main loop until self.running is no longer True. Exit program when main loop finishes. """ width = self.width height = self.height res_x = self.res_x res_y = self.res_y # Pygame if pygame.image.get_extended() == False: error_message = "Your build of pygame does not support extended \ image formats" sys.exit(error_message) pygame.init() pygame.font.init() pygame.mixer.init() self.screen = pygame.display.set_mode((res_x, res_y), OPENGL | DOUBLEBUF) pygame.display.set_caption('Robot Toast') # JOYSTIX!! self.stick = False numstix = pygame.joystick.get_count() for s in range(numstix): stik = pygame.joystick.Joystick(s) stik.init() print stik.get_id() print stik.get_numaxes() if not stik.get_numaxes(): # skip this stick pass else: self.stick = stik break # OpenGL viewport glViewport(0, 0, res_x, res_y) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluOrtho2D(0.0, float(width), 0.0, float(height)) glMatrixMode(GL_MODELVIEW) glLoadIdentity() # OpenGL options glEnable(GL_TEXTURE_2D) glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) glShadeModel(GL_SMOOTH) glClearColor(0.0, 0.0, 0.0, 0.0) glClearDepth(1.0) glEnable(GL_DEPTH_TEST) glDepthFunc(GL_LEQUAL) #OpenGL.error.ErrorChecker.registerChecker(error.ErrorChecker.nullGetError) # Clock clock = pygame.time.Clock() last_tick = this_tick = pygame.time.get_ticks() timer = 0 fps_timer = 0 self.ticklock = True # Initial Game State self.add_game_state(MenuLoader(self)) self.running = True while self.running: # Do every frame if self.ticklock: interval = clock.tick(60) else: interval = clock.tick() if interval > 50: interval = 50 timer += interval self.tick(interval) [self.handle_event(event) for event in pygame.event.get()] # Print FPS fps_timer += interval if fps_timer >= 1000: #print self.active_state print "%2d FPS" % clock.get_fps() fps_timer = 0 pygame.quit()