def __init__(self, width=1024, height=768): super(VisualizerWindow, self).__init__(width=width, height=height, resizable=False) squirtle.setup_gl() self.half_width = width / 2 self.half_height = height / 2 helv = font.load('Helvetica', width / 50.0) self.label = font.Text(helv, 'Hello, World!', x=width / 2, y=height / 2, halign=font.Text.CENTER, valign=font.Text.CENTER, color=(1, 0, 0, 1), ) pyglet.clock.schedule_interval(self.update,1/30.0) glClearColor(255,255,255,255) glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) glEnable(GL_DEPTH_TEST) glDepthFunc(GL_LEQUAL) self.set_3D() self.modelViewMatrix = (GLdouble * 16)() self.projectionMatrix = (GLdouble * 16)() self.viewport = (GLint * 4)() glGetDoublev(GL_MODELVIEW_MATRIX, self.modelViewMatrix) glGetDoublev(GL_PROJECTION_MATRIX, self.projectionMatrix) glGetIntegerv(GL_VIEWPORT, self.viewport) self.buttons = Buttons(os.path.join(sys.path[0], './theme'), self) self.control_width = 0.37*width self.input_height = 0.37*width self.sun = squirtle.SVG('sun.svg', anchor_x='center', anchor_y='center') self.mouse_x = 0 self.mouse_y = 0
class VisualizerWindow(pyglet.window.Window): def __init__(self, width=1024, height=768): super(VisualizerWindow, self).__init__(width=width, height=height, resizable=False) squirtle.setup_gl() self.half_width = width / 2 self.half_height = height / 2 helv = font.load('Helvetica', width / 50.0) self.label = font.Text(helv, 'Hello, World!', x=width / 2, y=height / 2, halign=font.Text.CENTER, valign=font.Text.CENTER, color=(1, 0, 0, 1), ) pyglet.clock.schedule_interval(self.update,1/30.0) glClearColor(255,255,255,255) glEnable(GL_BLEND) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) glEnable(GL_DEPTH_TEST) glDepthFunc(GL_LEQUAL) self.set_3D() self.modelViewMatrix = (GLdouble * 16)() self.projectionMatrix = (GLdouble * 16)() self.viewport = (GLint * 4)() glGetDoublev(GL_MODELVIEW_MATRIX, self.modelViewMatrix) glGetDoublev(GL_PROJECTION_MATRIX, self.projectionMatrix) glGetIntegerv(GL_VIEWPORT, self.viewport) self.buttons = Buttons(os.path.join(sys.path[0], './theme'), self) self.control_width = 0.37*width self.input_height = 0.37*width self.sun = squirtle.SVG('sun.svg', anchor_x='center', anchor_y='center') self.mouse_x = 0 self.mouse_y = 0 def draw_divisions(self): glBegin(GL_LINES) glColor4f(0.7,0.7,0.7,1.) glVertex2f(self.control_width, 0.) glVertex2f(self.control_width, self.height) glVertex2f(0., self.input_height) glVertex2f(self.control_width, self.input_height) glColor4f(0.5,0.5,0.5,1.) glVertex2f(self.control_width-1, 0.) glVertex2f(self.control_width-1, self.height) glVertex2f(0., self.input_height-1) glVertex2f(self.control_width, self.input_height-1) glEnd() def draw_mask(self): glBegin(GL_TRIANGLES) glColor4f(1.,1.,1.,1.) glVertex2f(0.,0.) glVertex2f(self.control_width, 0.) glVertex2f(self.control_width, self.input_height) glVertex2f(0.,0.) glVertex2f(self.control_width, self.input_height) glVertex2f(0., self.input_height) glVertex2f(self.control_width, 0.) glVertex2f(self.width, 0.) glVertex2f(self.width, self.height) glVertex2f(self.control_width,0.) glVertex2f(self.width, self.height) glVertex2f(self.control_width, self.height) glEnd() def on_draw(self): glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT) self.set_2D() glDisable(GL_BLEND) self.sun.draw(self.mouse_x, self.mouse_y, scale=(1,-1)) self.draw_mask() self.buttons.draw() self.draw_divisions() glDisable(GL_TEXTURE_3D) glEnable(GL_TEXTURE_2D) glEnable(GL_BLEND) self.label.draw() self.set_3D() #~ self.rectangle.draw() #~ Draw 3D objects def on_mouse_press(self, x, y, button, modifiers): if button == mouse.LEFT: self.mouse_x = x self.mouse_y = y self.label.text = "Mouse pressed on %s, %s"%(x, y) def on_mouse_release(self, x, y, button, modifiers): if button == mouse.LEFT: self.label.text = "Hello, World!!!" def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers): self.on_mouse_press(x, y, buttons, modifiers) def set_3D(self): glEnable(GL_DEPTH_TEST) glDisable(GL_BLEND) width, height = self.width, self.height glMatrixMode(GL_PROJECTION) glLoadIdentity() height = 1. if height == 0 else float(height) aspect_ratio = width/height gluPerspective(60., aspect_ratio, 0.1, 1000.) glMatrixMode(GL_MODELVIEW) glLoadIdentity() def set_2D(self): glDisable(GL_DEPTH_TEST) width, height = self.width, self.height glMatrixMode(GL_PROJECTION) glLoadIdentity() glOrtho(0, width, 0, height, -1., 1.) glMatrixMode(GL_MODELVIEW) glLoadIdentity() def update(self, dt): pass def screenToWorldCoords(self, screen_x, screen_y, plane_z=0.0): modelViewMatrix = self.modelViewMatrix projectionMatrix = self.projectionMatrix viewport = self.viewport near_x, near_y, near_z = (GLdouble * 1)(), (GLdouble * 1)(), (GLdouble * 1)() screen_z = 0.0 gluUnProject(screen_x, screen_y, screen_z, modelViewMatrix, projectionMatrix, viewport, near_x, near_y, near_z) if plane_z == 0: return near_x[0], near_y[0], near_z[0] else: x_v = near_x[0] y_v = near_y[0] z_v = -near_z[0] if z_v == 0: z_v = 0.0000000001 s = plane_z/z_v return x_v*s, y_v*s , plane_z