def on_resize(self, width, height): """Calculate the new viewport preserving aspect ratio""" aspect = float(WIDTH)/HEIGHT self.viewport_width = int(min(width, height*aspect)) self.viewport_height = int(min(height, width/aspect)) self.viewport_x_offs = (width-self.viewport_width) // 2 self.viewport_y_offs = (height-self.viewport_height) // 2 x = (width-WIDTH) / 2 gl.glViewport(self.viewport_x_offs, self.viewport_y_offs, self.viewport_width, self.viewport_height, ) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.glOrtho(0, self.viewport_width, 0, self.viewport_height, -1, 1) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() logging.debug("Viewport: %s, %s, %s, %s" % (self.viewport_x_offs, self.viewport_y_offs, self.viewport_width, self.viewport_height, )) # adjust elements depending on the new viewport self.label.x = self.viewport_width // 2 self.label.y = self.viewport_height // 2
def on_draw(self): self.window.clear() gl.glMatrixMode(gl.GL_PROJECTION) gl.glPushMatrix() gl.glLoadIdentity() self.camera() gl.glEnable(self.grass.target) gl.glEnable(gl.GL_BLEND) gl.glBindTexture(self.grass.target, self.grass.id) W = 10000. graphics.draw(4, gl.GL_QUADS, ('v2f', (-W, -W, W, -W, W, W, -W, W)), ('t2f', (0., 0., W*5., 0., W*5., W*5., 0., W*5.)) ) gl.glDisable(self.grass.target) for lane in self.lanes: self.draw_lane_surface(lane) for lane in self.lanes: self.draw_lane_lines(lane) for obj in self.objects: self.draw_object(obj) for car in self.cars: if car!=self.main_car and car not in self.visible_cars: self.draw_car(self.anim_x[car], car.color) if self.heat is not None: self.draw_heatmap() for car in self.cars: if car==self.main_car or car in self.visible_cars: self.draw_car(self.anim_x[car], car.color) gl.glPopMatrix() if isinstance(self.main_car, Car): self.label.text = 'Speed: %.2f'%self.anim_x[self.main_car][3] self.label.draw() if self.output is not None: pyglet.image.get_buffer_manager().get_color_buffer().save(self.output.format(self.frame))
def world_projection(self, aspect): """ Sets OpenGL projection and modelview matrices such that the window is centered on self.(x,y), shows at least scale world units in every direction, and is oriented by angle. """ glMatrixMode(GL_PROJECTION) glLoadIdentity() if aspect < 1: gluOrtho2D( -self.scale, +self.scale, -self.scale / aspect, +self.scale / aspect) else: gluOrtho2D( -self.scale * aspect, +self.scale * aspect, -self.scale, +self.scale) glMatrixMode(GL_MODELVIEW) glLoadIdentity() gluLookAt( self.x, self.y, +1.0, self.x, self.y, -1.0, sin(self.angle), cos(self.angle), 0.0)
def select_object(x, y, objects=None): from miru.context import context if objects is None: objects = context.camera.objects # following technique is adapted from # http://www.cse.msu.edu/~cse872/tutorial9.html w = context.window.width h = context.window.height select_buffer = ctypes.cast((100 * gl.GLuint)(), ctypes.POINTER(gl.GLuint)) gl.glSelectBuffer(100, select_buffer) viewport = (4 * gl.GLint)() gl.glGetIntegerv(gl.GL_VIEWPORT, viewport) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() # rotate the camera first angle = context.camera.angle gl.glRotatef(angle.z, 0, 0, 1) gl.glRotatef(angle.y, 0, 1, 0) gl.glRotatef(angle.x, 1, 0, 0) gl.gluPickMatrix(x, y, 3, 3, viewport) gl.glRenderMode(gl.GL_SELECT) gl.gluPerspective(45., w / float(h), 0.1, 1000.) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glInitNames() gl.glPushName(-1) context.camera.render(select_pass=1, visible=objects) gl.glFlush() hits = gl.glRenderMode(gl.GL_RENDER) gl.glPopName() selected = None if hits: try: m = sys.maxint << 100 idx = 0 for i in range(0, 100, 4): if not select_buffer[i]: selected = objects[idx] break m = min(select_buffer[i+1], m) if m == select_buffer[i+1]: idx = select_buffer[i+3] except IndexError: pass context.window.on_resize(context.window.width, context.window.height) return selected
def set_viewport(left, right, bottom, top): """ This sets what coordinates appear on the window. Note: It is recommended to only set the viewport to integer values that line up with the pixels on the screen. Otherwise if making a tiled game the blocks may not line up well, creating rectangle artifacts. >>> import arcade >>> arcade.open_window("Drawing Example", 800, 600) >>> set_viewport(-1, 1, -1, 1) >>> arcade.quick_run(0.25) """ global _left global _right global _bottom global _top _left = left _right = right _bottom = bottom _top = top # GL.glViewport(0, 0, _window.height, _window.height) GL.glMatrixMode(GL.GL_PROJECTION) GL.glLoadIdentity() GL.glOrtho(_left, _right, _bottom, _top, -1, 1) GL.glMatrixMode(GL.GL_MODELVIEW) GL.glLoadIdentity()
def on_draw(): pyglet.clock.tick() window.clear() gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() (w, h) = window.get_size() gl.glScalef( float(min(w, h))/w, -float(min(w, h))/h, 1 ) gl.gluPerspective(45.0, 1, 0.1, 1000.0) gl.gluLookAt(0, 0, 2.4, 0, 0, 0, 0, 1, 0) for vision in window.visions.values(): gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() vision() buf = pyglet.image.get_buffer_manager().get_color_buffer() rawimage = buf.get_image_data() window.texture = rawimage.get_texture()
def set_3d(self): """ Configure OpenGL to draw in 3d. """ width, height = self.get_size() gl.glEnable(gl.GL_DEPTH_TEST) gl.glViewport(0, 0, width, height) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.gluPerspective(65.0, width / float(height), 0.1, DIST) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() x, y = self.rotation gl.glRotatef(x, 0, 1, 0) gl.glRotatef(-y, math.cos(math.radians(x)), 0, math.sin(math.radians(x))) x, y, z = self.position gl.glTranslatef(-x, -y, -z) gl.glEnable(gl.GL_LIGHTING) gl.glLightModelfv(gl.GL_LIGHT_MODEL_AMBIENT, GLfloat4(0.05,0.05,0.05,1.0)) gl.glEnable(gl.GL_COLOR_MATERIAL) gl.glColorMaterial(gl.GL_FRONT, gl.GL_AMBIENT_AND_DIFFUSE) #gl.glLightfv(gl.GL_LIGHT1,gl.GL_SPOT_DIRECTION, GLfloat3(0,0,-1)) gl.glLightfv(gl.GL_LIGHT1, gl.GL_AMBIENT, GLfloat4(0.5,0.5,0.5,1.0)) gl.glLightfv(gl.GL_LIGHT1, gl.GL_DIFFUSE, GLfloat4(1.0,1.0,1.0,1.0)) gl.glLightfv(gl.GL_LIGHT1, gl.GL_POSITION, GLfloat4(0.35,1.0,0.65,0.0)) #gl.glLightfv(gl.GL_LIGHT0,gl.GL_SPECULAR, GLfloat4(1,1,1,1)) gl.glDisable(gl.GL_LIGHT0) gl.glEnable(gl.GL_LIGHT1)
def on_resize(self, width, height): ''' calculate perspective matrix ''' v_ar = width/float(height) usableWidth = int(min(width, height*v_ar)) usableHeight = int(min(height, width/v_ar)) ox = (width - usableWidth) // 2 oy = (height - usableHeight) // 2 glViewport(ox, oy, usableWidth, usableHeight) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(60, usableWidth/float(usableHeight), 0.1, 3000.0) ''' set camera position on modelview matrix ''' glMatrixMode(GL_MODELVIEW) glLoadIdentity() gluLookAt(width/2.0, height/2.0, height/1.1566, width/2.0, height/2.0, 0, 0.0, 1.0, 0.0) ''' update scene controller with size ''' self.controller.resize(width, height) #clears to a grey. glClearColor(0.4,0.4,0.4,0.) return pyglet.event.EVENT_HANDLED
def on_resize(self, width, height): glViewport(0, 0, width, height) glMatrixMode(GL_PROJECTION) glLoadIdentity() glOrtho(0, width, 0, height, -1000, 1000) glMatrixMode(GL_MODELVIEW) return pyglet.event.EVENT_HANDLED
def _set_texture(self, t): self._texture = t from pyglet import gl try: gl.glFramebufferTexture2DEXT( gl.GL_FRAMEBUFFER_EXT, gl.GL_COLOR_ATTACHMENT0_EXT, t.target, t.id, 0, ) except gl.GLException: # HACK: Some Intel card return errno == 1286L # which means GL_INVALID_FRAMEBUFFER_OPERATION_EXT # but IT ACTUALLY WORKS FINE!! pass gl.glViewport(0, 0, t.width, t.height) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.gluOrtho2D(0, t.width, 0, t.height) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() # ATI cards hack gl.glBegin(gl.GL_LINES) gl.glEnd()
def on_draw(): gl.glClear(gl.GL_COLOR_BUFFER_BIT) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() for block in blocks: draw_block(block)
def on_draw(self): self.clear() gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() (w, h) = self.get_size() gl.glScalef( float(min(w, h))/w, -float(min(w, h))/h, 1 ) gl.gluPerspective(45.0, 1, 0.1, 1000.0) gl.gluLookAt(0, 0, 2.4, 0, 0, 0, 0, 1, 0) global render_texture render_texture = self.texture for v in self.visions.values(): gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() v.iteration() buf = pyglet.image.get_buffer_manager().get_color_buffer() rawimage = buf.get_image_data() self.texture = rawimage.get_texture() clock.tick()
def _reset_projection(self): if self.fullcanvas: if self._pygimage is None: return width, height = self._pygimage.width, self._pygimage.height else: size = self.GetClientSize() width, height = size.width, size.height b = 0 t = height if self.flip_lr: l = width r = 0 else: l = 0 r = width if self.rotate_180: l,r=r,l b,t=t,b if width==0 or height==0: # prevent OpenGL error return self.wxcontext.SetCurrent() gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.glOrtho(l,r,b,t, -1, 1) gl.glMatrixMode(gl.GL_MODELVIEW)
def world_projection(self): """ Sets OpenGL projection and modelview matrices such that the window is centered on self.(x,y), shows at least 'scale' world units in every direction, and is oriented by rot. """ left = bottom = -self.scale right = top = self.scale aspect = self.width / self.height if aspect >= 1: # landscape left *= aspect right *= aspect else: # portrait bottom /= aspect top /= aspect glMatrixMode(GL_PROJECTION) glLoadIdentity() gluOrtho2D(left, right, bottom, top) glMatrixMode(GL_MODELVIEW) glLoadIdentity() gluLookAt( self.x, self.y, +1.0, self.x, self.y, -1.0, sin(self.rot), cos(self.rot), 0.0)
def draw(self): # set up projection gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.glViewport(self.x, self.y, self.width, self.height) gl.glOrtho(0, self.width, 0, self.height, self.near, self.far) gl.glMatrixMode(gl.GL_MODELVIEW) fx, fy = self._determine_focus() w2 = self.width / 2 h2 = self.height / 2 x1, y1 = fx - w2, fy - h2 x2, y2 = fx + w2, fy + h2 gl.glPushMatrix() gl.glTranslatef(self.width / 2 - fx, self.height / 2 - fy, 0) for layer in self.layers: if hasattr(layer, 'x'): translate = layer.x or layer.y else: translate = False if translate: gl.glPushMatrix() gl.glTranslatef(layer.x, layer.y, 0) layer.draw() if translate: gl.glPopMatrix() gl.glPopMatrix()
def _layerProjectLocalToScreenUndo(self): """Undoes the _layerProjectLocalToScreen() operation.""" if self._scissorBox is None and self.localBounds is None: return gl.glMatrixMode(gl.GL_PROJECTION) gl.glPopMatrix() gl.glMatrixMode(gl.GL_MODELVIEW)
def SetOrigin(self): size = self.GetVirtualSize() self.SetScrollbar(wx.HORIZONTAL, self.GetScrollPos(wx.HORIZONTAL), size[0], self.map.width * 32 * self.zoom, refresh=True) self.SetScrollbar(wx.VERTICAL, self.GetScrollPos(wx.VERTICAL), size[1], self.map.height * 32 * self.zoom, refresh=True) size = self.GetGLExtents() if size.width <= 0: size.width = 1 if size.height <= 0: size.height = 1 self.tilemap.updateDimmingSprite( int(size.width) + 2, int(size.height) + 2, 1 / self.zoom) gl.glViewport(0, 0, size.width, size.height) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.glOrtho( 0, size.width / self.zoom, 0, size.height / self.zoom, -1, 1) x = (-self.GetScrollPos(wx.HORIZONTAL)) / self.zoom y = ((-(self.map.height * 32) + size.height / self.zoom) + self.GetScrollPos(wx.VERTICAL) / self.zoom) gl.glTranslatef(x, y, 0) self.translateX = -x + size.width / 2 / self.zoom self.translateY = -y + size.height / 2 / self.zoom self.onscreenwidth = int(size.width / self.zoom) self.onscreenheight = int(size.height / self.zoom) self.tilemap.setDimXY(self.translateX - 1, self.translateY + 1) gl.glMatrixMode(gl.GL_MODELVIEW)
def hud_mode(self): "Set matrices ready for drawing HUD, like fps counter" glMatrixMode(GL_PROJECTION) glLoadIdentity() gluOrtho2D(0, self.win_width, 0, self.win_height) glMatrixMode(GL_MODELVIEW) glLoadIdentity()
def on_resize(self, width, height): gl.glViewport(0, 0, width, height) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.gluPerspective(60., width / float(height), .01, 1000.) gl.glMatrixMode(gl.GL_MODELVIEW) self.view['ball'].place([width/2, height/2], (width+height)/2)
def _resize(self, width, height): aspect = float(self._width)/self._height self._viewport_width = int(min(width, height*aspect)) self._viewport_height = int(min(height, width/aspect)) self._viewport_x_offs = (width-self._viewport_width) // 2 self._viewport_y_offs = (height-self._viewport_height) // 2 x = (width-self._width) / 2 gl.glViewport(self._viewport_x_offs, self._viewport_y_offs, self._viewport_width, self._viewport_height, ) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.glOrtho(0, self._viewport_width, 0, self._viewport_height, -1, 1) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() logging.debug("Viewport: %s, %s, %s, %s" % (self._viewport_x_offs, self._viewport_y_offs, self._viewport_width, self._viewport_height, ))
def get_vlist(geom, batch=None, group=None, data=None): """Get a "new" C{pyglet.graphics.VertexList} for this geometry. If a batch is given, vertex list will be added to the batch """ # projection with one world coordinate unit equal to one screen pixel # glMatrixMode (GL_PROJECTION); glLoadIdentity (); gluOrtho2D (0, windowWidth, 0, windowHeight); window = pyglet.window.get_platform().get_default_display().get_windows()[0] gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.gluOrtho2D(0, window.width, 0, window.height) _data = [('v3f', geom.vertex_data)] if hasattr(geom, 'normal_data'): _data.append(('n3f', geom.normal_data)) if hasattr(geom, 'texcoord_data'): _data.append(('t2f', geom.texcoord_data)) if data: _data.extend(data) count = len(geom.vertex_data) // geom.vertex_pitch indexed = hasattr(geom, 'indices') if batch: if indexed: return batch.add_indexed( count, geom.drawing_mode, group, geom.indices, *_data) return batch.add( count, geom.drawing_mode, group, *_data) if indexed: return pyglet.graphics.vertex_list_indexed( count, geom.indices, *_data ) return pyglet.graphics.vertex_list( count, *_data)
def predraw(w,h): gl.glLightfv(gl.GL_LIGHT0, gl.GL_POSITION,vec(1,1,10, 3)) gl.glLightModelfv( gl.GL_LIGHT_MODEL_AMBIENT|gl.GL_LIGHT_MODEL_TWO_SIDE, vec(1,1,1, 1.0) ) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() #glOrtho(-1, 1, -1, 1, -1, 1) #(w,h) = self.get_size() gl.glScalef( float(min(w,h))/w, -float(min(w,h))/h, 1 ) gl.gluPerspective(45.0, 1, 0.1, 1000.0) gl.gluLookAt( camera.x, camera.y, camera.z, 0,0,0, camera.up[0], camera.up[1], camera.up[2] )
def on_resize(width, height): gl.glViewport(0, 0, width, height) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.glOrtho(-width/2, width/2, -height/2, height/2, -1, 1) gl.glMatrixMode(gl.GL_MODELVIEW) return EVENT_HANDLED
def gl_setup(): # general GL setup glMatrixMode(GL_PROJECTION) glMatrixMode(GL_MODELVIEW) gluOrtho2D(0, WIDTH, 0, HEIGHT) # dont understand, check this # TODO glLoadIdentity() glTranslatef(CENTX, CENTY, 0) glClear(GL_COLOR_BUFFER_BIT)
def on_draw(): update_grid() window.clear() gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.glOrtho(0, 200, 200, 0, 0, 1) grid.draw()
def _layerProjectLocalToScreen(self): """Maps self.coordLocal to self.coordScreen within OpenGL.""" if self._scissorBox is None and self.localBounds is None: return gl.glMatrixMode(gl.GL_PROJECTION) gl.glPushMatrix() gl.glLoadIdentity() # gluOrtho2D declares the render space for the corners of the window. # So, we want to set it up in such a way that our layer renders in # the right place. In other words, determine window corners that # map cl -> cs. All clipping, etc is already handled by the coordsLocal # getter. cs = self.coordsScreen cl = self.coordsLocal sw = self.scene.width sh = self.scene.height # Determine the window width and height. We want a local-sized chunk # of this to correspond to a screen-sized chunk of the screen. That is, # cl[2] / ww == cs[2] / sw ww = cl[2] * sw / cs[2] wh = cl[3] * sh / cs[3] # cs[0] / sw = (x - nx) / ww nx = cl[0] - cs[0] * ww / sw ny = cl[1] - cs[1] * wh / sh gl.gluOrtho2D(nx, nx+ww, ny, ny+wh) gl.glMatrixMode(gl.GL_MODELVIEW)
def update(self): self.x += (self.target_x - self.x) * 0.1 self.y += (self.target_y - self.y) * 0.1 self.scale += (self.target_scale - self.scale) * 0.1 self.angle += (self.target_angle - self.angle) * 0.1 "Set projection and modelview matrices ready for rendering" glMatrixMode(GL_PROJECTION) glLoadIdentity() gluOrtho2D( -self.scale * self.aspect, +self.scale * self.aspect, -self.scale, +self.scale) # Set modelview matrix to move, scale & rotate to camera position" glMatrixMode(GL_MODELVIEW) glLoadIdentity() gluLookAt( self.x, self.y, +1.0, self.x, self.y, -1.0, sin(self.angle), cos(self.angle), 0.0) print 'gluLookAt:', self.x,self.y, self.angle
def on_draw(): gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() # Gradient sky l, b = world_to_screen((0, 0)) r, t = world_to_screen((W, H)) horizon = 177 / 255.0, 202 / 255.0, 1.0 zenith = 68 / 255.0, 0.5, 1.0 pyglet.graphics.draw(4, gl.GL_QUADS, ('v2f', [l, b, l, t, r, t, r, b]), ('c3f', sum([horizon, zenith, zenith, horizon], ())), ) cx, cy = camera tx, ty = world_to_screen((-cx + W * 0.5, -cy + H * 0.5)) gl.glTranslatef(tx, ty, 0) batch.draw() # Water l, b = world_to_screen((0, 0)) r, t = world_to_screen((1000, WATER_LEVEL)) gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) pyglet.graphics.draw(4, gl.GL_QUADS, ('v2f', [l, b, l, t, r, t, r, b]), ('c4f', [0, 0.2, 0.8, 0.5] * 4), )
def handle_resize(w, h): gl.glViewport(0, 0, w, h) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.glOrtho(0, w, h, 0, 0, 1) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity()
def on_draw(): gl.glClear(gl.GL_COLOR_BUFFER_BIT) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() # draw_obj(bear, bear_tex) draw_obj(ball, ball_tex)
def zoom(self, factor, to=None): glMatrixMode(GL_MODELVIEW) if to: delta_x = to[0] delta_y = to[1] glTranslatef(delta_x, delta_y, 0) glScalef(factor, factor, 1) self.zoom_factor *= factor if to: glTranslatef(-delta_x, -delta_y, 0) wx.CallAfter(self.Refresh)
def focus(self, width, height): """Sets the projection matrix. Usefull for window.on_resize event @param width: width of resized window @param height: height of resized window""" self.width = width self.height = height glViewport(0, 0, width, height) glMatrixMode(GL_PROJECTION) glLoadIdentity() aspect = width / float(height) gluPerspective(self.fovy, aspect, 0.01, 1024.0)
def init_perspective(self): gl.glEnable(gl.GL_DEPTH_TEST) gl.glEnable(gl.GL_LIGHTING) # Init Projection gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() GLU.gluPerspective(45.0, float(self.width) / float(self.height), 0.1, 100.0) # Initialize ModelView matrix gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity()
def set_2d(self): """ Configure OpenGL to draw in 2d. """ width, height = self.get_size() glDisable(GL_DEPTH_TEST) glViewport(0, 0, width, height) glMatrixMode(GL_PROJECTION) glLoadIdentity() glOrtho(0, width, 0, height, -1, 1) glMatrixMode(GL_MODELVIEW) glLoadIdentity()
def setup_projection_matrix(self): gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.glOrtho( # WIDTH * -0.5, WIDTH * 0.5, 0, WIDTH, 0, HEIGHT, # HEIGHT * -0.5, HEIGHT * 0.5, -100, 100 ) gl.glMatrixMode(gl.GL_MODELVIEW)
def reset_mview(self, factor): glMatrixMode(GL_MODELVIEW) glLoadIdentity() self.setup_lights() if self.orthographic: wratio = self.width / self.dist hratio = self.height / self.dist minratio = float(min(wratio, hratio)) self.zoom_factor = 1.0 self.zoomed_width = wratio / minratio self.zoomed_height = hratio / minratio glScalef(factor * minratio, factor * minratio, 1)
def on_resize(self, width, height): # scale is distance from screen centre to top or bottom, in world coords scale = 110 glMatrixMode(GL_PROJECTION) glLoadIdentity() aspect = width / height gluOrtho2D( -scale * aspect, +scale * aspect, -scale, +scale) return EVENT_HANDLED
def on_resize(width, height): gl.glViewport(0, 0, width, height) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.glOrtho( 0., self.rendering_config.visible_area_width, -0.1 * float(height) / width * self.rendering_config.visible_area_width, 0.9 * float(height) / width * self.rendering_config.visible_area_width, -1., 1.) gl.glMatrixMode(gl.GL_MODELVIEW) return pyglet.event.EVENT_HANDLED
def ui_mode(self): gl.glDisable(gl.GL_DEPTH_TEST) gl.glDisable(gl.GL_CULL_FACE) gl.glDisable(gl.GL_TEXTURE_2D) gl.glDisable(gl.GL_LIGHTING) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.gluOrtho2D(-self.w / 2, self.w / 2, -self.h / 2, self.h / 2) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity()
def _unset_view(self): gl.glMatrixMode(gl.GL_PROJECTION) gl.glPopMatrix() gl.glMatrixMode(self._mode.value) gl.glViewport( self._viewport[0], self._viewport[1], self._viewport[2], self._viewport[3], ) gl.glPopAttrib()
def focus(self, width, height): "Set projection and model view matrices ready for rendering" # Set projection matrix suitable for 2D rendering" glMatrixMode(GL_PROJECTION) glLoadIdentity() aspect = width / height gluOrtho2D(-self.scale * aspect, +self.scale * aspect, -self.scale, +self.scale) # Set model view matrix to move, scale & rotate to camera position" glMatrixMode(GL_MODELVIEW) glLoadIdentity() gluLookAt(self.x, self.y, 1.0, self.x, self.y, -1.0, 0.0, 1.0, 0.0)
def on_mouse_drag(x, y, dx, dy, button, modifiers): sprite.x = int((x/float(window.width)) * width)-sprite.width//2 sprite.y = int((y/float(window.height)) * height)-sprite.height//2 # Compute gl.glViewport(0, 0, width, height) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.glOrtho(0, width, 0, height, -1, 1) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, framebuffer) sprite.draw() gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, 0)
def set_2d(self): """ Configure Opengl.GL to draw in 2d. """ width, height = self.get_size() gl.glDisable(gl.GL_DEPTH_TEST) viewport = self.get_viewport_size() gl.glViewport(0, 0, max(1, viewport[0]), max(1, viewport[1])) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.glOrtho(0, max(1, width), 0, max(1, height), -1, 1) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity()
def world_projection(self): """ Project the world. """ gl.glDisable(gl.GL_DEPTH_TEST) gl.glViewport(0, 0, self.win.width, self.win.height) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() width_ratio = self.win.width / self.win.height gl.gluOrtho2D(-self.zoom * width_ratio, self.zoom * width_ratio, -self.zoom, self.zoom) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity()
def _setup_2d(self): w, h = self.get_size() gl.glDisable(gl.GL_DEPTH_TEST) viewport = self.get_viewport_size() gl.glViewport(0, 0, max(1, viewport[0]), max(1, viewport[1])) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.glOrtho(0, max(1, w), 0, max(1, h), -1, 1) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity()
def focus(self, width, height): aspect = width / height glMatrixMode(GL_PROJECTION) glLoadIdentity() glViewport(0, 0, width, height) gluPerspective(self._fov * self._zoom, aspect, self._near, self._far) glScalef(self._scalex, self._scaley, self._scalez) glMatrixMode(GL_MODELVIEW) glLoadIdentity() gluLookAt(self._x, self._y, self._z, self._target.x, self._target.y, self._target.z, 0.0, 1.0, 0.0)
def display(self, width, height): gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.gluPerspective(60., width / float(height), .1, 1000.) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() gl.glTranslatef(self.trans[0], self.trans[1], self.trans[2]) gl.glRotatef(self.rot[0], 1.0, 0.0, 0.0) gl.glRotatef(self.rot[1], 0.0, 1.0, 0.0) gl.glRotatef(self.rz, 0, 0, 1) gl.glRotatef(self.ry, 0, 1, 0) gl.glRotatef(self.rx, 1, 0, 0) gl.glEnable(gl.GL_LIGHTING) gl.glColor3f(1, 0, 0) self.torus.draw() gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.glOrtho(0, width, 0, height, -1, 1) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() gl.glDisable(gl.GL_LIGHTING) gl.glColor3f(0, 1, 0) self.lowerlabel.text = 'Rx %.2f Ry %.2f Rz %.2f' % (self.rx, self.ry, self.rz) self.lowerlabel.draw() self.upperlabel.text = time.strftime('Now is %H:%M:%S') self.upperlabel.x = width - self.upperlabel.content_width - 5 self.upperlabel.y = height - self.upperlabel.content_height self.upperlabel.draw()
def set_3d(self): width, height = self.get_size() glEnable(GL_DEPTH_TEST) glViewport(0, 0, width, height) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluPerspective(65.0, width / float(height), 0.1, 60.0) glMatrixMode(GL_MODELVIEW) glLoadIdentity() x, y = self.rotation glRotatef(x, 0, 1, 0) glRotatef(-y, math.cos(math.radians(x)), 0, math.sin(math.radians(x))) x, y, z = self.position glTranslatef(-x, -y, -z)
def _drawLUTtoScreen(self): """(private) Used to set the LUT in 'bits++' mode. Should not be needed by user if attached to a ``psychopy.visual.Window()`` since this will automatically draw the LUT as part of the screen refresh. """ # push the projection matrix and set to orthorgaphic GL.glMatrixMode(GL.GL_PROJECTION) GL.glPushMatrix() GL.glLoadIdentity() # this also sets the 0,0 to be top-left GL.glOrtho(0, self.win.size[0], self.win.size[1], 0, 0, 1) # but return to modelview for rendering GL.glMatrixMode(GL.GL_MODELVIEW) GL.glLoadIdentity() # draw the pixels GL.glActiveTextureARB(GL.GL_TEXTURE0_ARB) GL.glEnable(GL.GL_TEXTURE_2D) GL.glBindTexture(GL.GL_TEXTURE_2D, 0) GL.glActiveTextureARB(GL.GL_TEXTURE1_ARB) GL.glEnable(GL.GL_TEXTURE_2D) GL.glBindTexture(GL.GL_TEXTURE_2D, 0) GL.glRasterPos2i(0, 1) GL.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1) GL.glDrawPixels(len(self._HEADandLUT), 1, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, self._HEADandLUTstr) # GL.glDrawPixels(524,1, GL.GL_RGB,GL.GL_UNSIGNED_BYTE, # self._HEADandLUTstr) # return to 3D mode (go and pop the projection matrix) GL.glMatrixMode(GL.GL_PROJECTION) GL.glPopMatrix() GL.glMatrixMode(GL.GL_MODELVIEW)
def on_draw(): gl.glClearColor(1.0,1.0,1.0,1.0) window.clear() # Compute gl.glViewport(0, 0, width, height) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.glOrtho(0, 1, 0, 1, -1, 1) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glActiveTexture( gl.GL_TEXTURE1 ) gl.glBindTexture(texture_s.target, texture_s.id) gl.glActiveTexture( gl.GL_TEXTURE0 ) gl.glBindTexture(texture_uv.target, texture_uv.id) gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, framebuffer) reaction_shader.bind() texture_uv.blit(x=0.0, y=0.0, width=1.0, height=1.0) reaction_shader.unbind() gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, 0) # Render gl.glViewport(0, 0, window.width, window.height) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.glOrtho(0, 1, 0, 1, -1, 1) gl.glMatrixMode(gl.GL_MODELVIEW) color_shader.bind() texture_uv.blit(x=0.0, y=0.0, width=1.0, height=1.0) color_shader.bind()
def draw(self, camera): # set up projection matrix gl.glMatrixMode(gl.GL_PROJECTION) gl.glPushMatrix() gl.glLoadIdentity() gl.gluPerspective(FOVY, camera.aspect(), NEAR_PLANE, FAR_PLANE) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glEnable(gl.GL_DEPTH_TEST) # compute correct clip rect on far plane l, b = camera.position + camera.offset * FAR_PLANE_SCALE r, t = camera.position - camera.offset * FAR_PLANE_SCALE # Create blocks l = int(floor(l / BLOCK_SIZE)) r = int(ceil(r / BLOCK_SIZE)) b = int(floor(b / BLOCK_SIZE)) t = int(ceil(t / BLOCK_SIZE)) for y in range(b, t): for x in range(l, r): c = v(x, y) * BLOCK_SIZE gl.glPushMatrix() gl.glTranslatef(c.x, c.y, 0) self.batch.draw() gl.glPopMatrix() gl.glMatrixMode(gl.GL_PROJECTION) gl.glPopMatrix() gl.glMatrixMode(gl.GL_MODELVIEW) gl.glDisable(gl.GL_DEPTH_TEST)
def on_draw(self): """Draw window.""" self.clear() viewport = self.get_viewport_size() gl.glViewport(0, 0, max(1, viewport[0]), max(1, viewport[1])) # set projection for billiard table gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.glOrtho( self.center[0] - self.extent / 2, self.center[0] + self.extent / 2, self.center[1] - self.extent / (2 * self.aspect_ratio), self.center[1] + self.extent / (2 * self.aspect_ratio), -1, 1, ) # draw balls and obstacles gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() self.obs_batch.draw() self.ball_batch.draw() # set projection for GUI gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.glOrtho(0, max(1, self.width), 0, max(1, self.height), -1, 1) # draw GUI gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() self.gui_batch.draw()
def __init__(self): self.window = Window(visible=False, fullscreen=False) self.window.on_resize = self.on_resize self.window.on_draw = self.on_draw self.window.on_key_press = self.on_key_press self.files = SvgFiles() glMatrixMode(GL_MODELVIEW) glLoadIdentity() gluLookAt( 0.0, -0.0, 1.0, # eye 0.0, -0.0, -1.0, # lookAt 0.0, 1.0, 0.0) # up
def on_resize(width, height): # sets the viewport gl.glViewport(0, 0, width, height) # sets the projection gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() glu.gluPerspective(60.0, width / float(height), 0.1, 1000.0) # sets the model view gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() return pyglet.event.EVENT_HANDLED
def set_viewport(left: Number, right: Number, bottom: Number, top: Number): """ This sets what coordinates the window will cover. By default, the lower left coordinate will be (0, 0) and the top y coordinate will be the height of the window in pixels, and the right x coordinate will be the width of the window in pixels. If a program is making a game where the user scrolls around a larger world, this command can help out. Note: It is recommended to only set the view port to integer values that line up with the pixels on the screen. Otherwise if making a tiled game the blocks may not line up well, creating rectangle artifacts. Args: :left: Left-most (smallest) x value. :right: Right-most (largest) x value. :bottom: Bottom (smallest) y value. :top: Top (largest) y value. Returns: None Raises: None :Example: >>> import arcade >>> arcade.open_window("Drawing Example", 800, 600) >>> set_viewport(-1, 1, -1, 1) >>> arcade.quick_run(0.25) """ global _left global _right global _bottom global _top _left = left _right = right _bottom = bottom _top = top # gl.glViewport(0, 0, _window.height, _window.height) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.glOrtho(_left, _right, _bottom, _top, -1, 1) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity()
def on_draw(): gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.gluOrtho2D(0, 1, 1, 0) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() gl.glViewport(0, 0, self.width, self.height) gl.glEnable(gl.GL_TEXTURE_2D) gl.glBindTexture(self.texture.target, self.texture.id) self.draw(0, 0, 1, 1) gl.glDisable(gl.GL_TEXTURE_2D)
def draw(self, camera): gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) vp = camera.viewport() vp = vp.translate(v(-vp.l, -vp.b)) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.glOrtho(vp.l, vp.r, vp.b, vp.t, -1000, 1000) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity() self.draw_health(vp) self.draw_gold(vp)
def on_resize(self, width, height): # Resize GL viewport gl.glViewport(0, 0, width, height) gl.glMatrixMode(gl.GL_PROJECTION) gl.glLoadIdentity() gl.glOrtho(0, width, 0, height, -1, 1) gl.glMatrixMode(gl.GL_MODELVIEW) # Inform all items of the new dimensions and adjust their positions accordingly Shape.tellScreenBounds(width, height) for item in self.items: item.adjustBounds() # Update button positions: they are supposed to be dynamically sized self.update_buttons()
def setup_projection(self): pgl.glMatrixMode(pgl.GL_PROJECTION) pgl.glLoadIdentity() if self.ortho: # yep, this is pseudo ortho (don't tell anyone) pgl.gluPerspective( 0.3, float(self.window.width) / float(self.window.height), self.min_ortho_dist - 0.01, self.max_ortho_dist + 0.01) else: pgl.gluPerspective( 30.0, float(self.window.width) / float(self.window.height), self.min_dist - 0.01, self.max_dist + 0.01) pgl.glMatrixMode(pgl.GL_MODELVIEW)
def init_ortho(self): # disable shaders gl.glUseProgram(0) gl.glDisable(gl.GL_LIGHTING) # store the projection matrix to restore later gl.glMatrixMode(gl.GL_PROJECTION) # load orthographic projection matrix gl.glLoadIdentity() gl.glOrtho(0, self.width, 0, self.height, -1, 500) # reset modelview gl.glMatrixMode(gl.GL_MODELVIEW) gl.glLoadIdentity()