如何在2D模式下正确平移和缩放?

如何在2D模式下正确平移和缩放?,第1张

如何在2D模式下正确平移缩放

经过一天的苦难,我终于找到了解决方案:在2D模式下,基于鼠标坐标(枢轴点)的缩放和不带跳转的单击鼠标拖拽平移的最简单方法是使用该

glOrtho()
功能更改投影矩阵。

下面是我的原代码的简化版本-
如果你正在使用Pyglet数据的seriuos金额,你应该考虑使用组和批次,但为便于理解我所用

glBegin()
glColor()
glVertex()
glEnd()
这里的功能来绘制。

import pygletfrom pyglet.gl import *# Zooming constantsZOOM_IN_FACTOR = 1.2ZOOM_OUT_FACTOR = 1/ZOOM_IN_FACTORclass App(pyglet.window.Window):    def __init__(self, width, height, *args, **kwargs):        conf = Config(sample_buffers=1,samples=4,depth_size=16,double_buffer=True)        super().__init__(width, height, config=conf, *args, **kwargs)        #Initialize camera values        self.left   = 0        self.right  = width        self.bottom = 0        self.top    = height        self.zoom_level = 1        self.zoomed_width  = width        self.zoomed_height = height    def init_gl(self, width, height):        # Set clear color        glClearColor(0/255, 0/255, 0/255, 0/255)        # Set antialiasing        glEnable( GL_LINE_SMOOTH )        glEnable( GL_POLYGON_SMOOTH )        glHint( GL_LINE_SMOOTH_HINT, GL_NICEST )        # Set alpha blending        glEnable( GL_BLEND )        glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA )        # Set viewport        glViewport( 0, 0, width, height )    def on_resize(self, width, height):        # Set window values        self.width  = width        self.height = height        # Initialize OpenGL context        self.init_gl(width, height)    def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):        # Move camera        self.left   -= dx*self.zoom_level        self.right  -= dx*self.zoom_level        self.bottom -= dy*self.zoom_level        self.top    -= dy*self.zoom_level    def on_mouse_scroll(self, x, y, dx, dy):        # Get scale factor        f = ZOOM_IN_FACTOR if dy > 0 else ZOOM_OUT_FACTOR if dy < 0 else 1        # If zoom_level is in the proper range        if .2 < self.zoom_level*f < 5: self.zoom_level *= f mouse_x = x/self.width mouse_y = y/self.height mouse_x_in_world = self.left   + mouse_x*self.zoomed_width mouse_y_in_world = self.bottom + mouse_y*self.zoomed_height self.zoomed_width  *= f self.zoomed_height *= f self.left   = mouse_x_in_world - mouse_x*self.zoomed_width self.right  = mouse_x_in_world + (1 - mouse_x)*self.zoomed_width self.bottom = mouse_y_in_world - mouse_y*self.zoomed_height self.top    = mouse_y_in_world + (1 - mouse_y)*self.zoomed_height    def on_draw(self):        # Initialize Projection matrix        glMatrixMode( GL_PROJECTION )        glLoadIdentity()        # Initialize Modelview matrix        glMatrixMode( GL_MODELVIEW )        glLoadIdentity()        # Save the default modelview matrix        glPushMatrix()        # Clear window with ClearColor        glClear( GL_COLOR_BUFFER_BIT )        # Set orthographic projection matrix        glOrtho( self.left, self.right, self.bottom, self.top, 1, -1 )        # Draw quad        glBegin( GL_QUADS )        glColor3ub( 0xFF, 0, 0 )        glVertex2i( 10, 10 )        glColor3ub( 0xFF, 0xFF, 0 )        glVertex2i( 110, 10 )        glColor3ub( 0, 0xFF, 0 )        glVertex2i( 110, 110 )        glColor3ub( 0, 0, 0xFF )        glVertex2i( 10, 110 )        glEnd()        # Remove default modelview matrix        glPopMatrix()    def run(self):        pyglet.app.run()App(500, 500).run()


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5655436.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存