pythonosc调度调用时pygame opengl窗口未更新

2024-04-26 12:34:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在开发一个三维可视化的几个点,通过osc(python-osc)接收数据并用pygame显示它们。基于一个立方体的pygame示例,我添加了osc dispatcher函数来接收数据,然后调用这些函数来更新这些dispatcher函数的显示。但是,当从分派的事件调用这些函数时,显示不会更新。当最初调用相同的函数时(不是由调度器调用),显示仍在更新。我仍然使用立方体进行测试,没有显示接收到的数据。你知道吗

我对python还不熟悉,所以现在有点无助于找到这种行为的原因。我猜调度程序和主程序的上下文不一样,但不知道如何调试它。。。 下面是我用来更新总账显示的代码:

glRotatef(1, 3, 1, 1)
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
cube()
pygame.display.flip()

下面是整个代码:

import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
from pythonosc import dispatcher
from pythonosc import osc_server
import argparse

class Displ:
    def __init__ (self):
        self.verticies = (
            (1, -1, -1),
            (1, 1, -1),
            (-1, 1, -1),
            (-1, -1, -1),
            (1, -1, 1),
            (1, 1, 1),
            (-1, -1, 1),
            (-1, 1, 1)
            )
        self.edges = (
            (0,1),
            (0,3),
            (0,4),
            (2,1),
            (2,3),
            (2,7),
            (6,3),
            (6,4),
            (6,7),
            (5,1),
            (5,4),
            (5,7)
            )

    def Cube(self):
        glBegin(GL_LINES)
        for edge in self.edges:
            for vertex in edge:
                glVertex3fv(self.verticies[vertex])
        glEnd()

    def makeWindow(self):
        pygame.init()
        display = (800,600)
        screen = pygame.display.set_mode(display, OPENGL)
        gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)
        glTranslatef(0.0,0.0, -5)

    def drawUpdate(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
        glRotatef(1, 3, 1, 1)
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
        self.Cube()
        pygame.display.flip()
        #pygame.time.wait(10)

     def printPoints (addr, x,y,z):
        print ("Point {} {}".format( str(addr), str([x,y,z])))
        displ.drawUpdate()

if __name__ == "__main__":
  parser = argparse.ArgumentParser()
  parser.add_argument("--ip",
      default="127.0.0.1", help="The ip to listen on")
  parser.add_argument("--port",
      type=int, default=9003, help="The port to listen on")
  args = parser.parse_args()

  dispatcher = dispatcher.Dispatcher()
  dispatcher.map("/buoy/p1", printPoints)
  server = osc_server.ThreadingOSCUDPServer((args.ip, args.port), dispatcher)
  print("Serving on {}".format(server.server_address))

  displ = Displ()
  displ.makeWindow()
  displ.drawUpdate() # just for testing: this gets updated
  displ.drawUpdate() # and this...
  displ.drawUpdate() # but not if drawUpdate() is called by the dispatcher.
  server.serve_forever()

Tags: 函数fromimportselfserverdefbufferdisplay
1条回答
网友
1楼 · 发布于 2024-04-26 12:34:08

在找到一些时间研究如何处理gl上下文之后,我放弃了。显然,在GLUT内部不可能轻松地获取和设置窗口上下文,但是存在一些黑客攻击。我在创建第二个线程时找到了自己的解决方案,该线程创建并更新display函数,以便上下文留在线程中:

import threading
import time

def loop():
    displ = Displ()
    displ.makeWindow()
    displ.drawUpdate()

    while(1):
        time.sleep(0.1)
        displ.drawUpdate()

threading.Thread(target=loop).start()

相关问题 更多 >