当前位置: 代码迷 >> python >> python-osc调度程序调用时,pygame opengl窗口未更新
  详细解决方案

python-osc调度程序调用时,pygame opengl窗口未更新

热度:111   发布时间:2023-06-27 21:48:37.0

我目前正在对几个点进行3D可视化,通过osc(python-osc)接收数据并使用pygame显示它们。 基于一个有效的多维数据集pygame示例,我添加了osc调度程序函数来接收数据,然后调用这些函数以更新这些调度程序函数的显示。 但是,从分派的evevent中调用这些功能时,显示不会更新。 最初调用相同功能(而不是调度程序)时,显示仍在更新。 我仍在使用多维数据集进行测试,而不显示接收到的数据。

我是python的新手-目前找不到这种行为的原因有点无奈。 我猜想调度程序和主程序的上下文不一样或类似的东西-但不知道如何调试...这是我用来更新gl显示的代码:

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()

在花了一些时间研究如何处理gl上下文之后,我放弃了。 显然,在GLUT中不可能轻松地获取和设置窗口上下文,但是存在一些黑客。 我在创建第二个线程时发现了自己的解决方案,该线程创建并更新了显示功能,因此上下文保留在该线程中:

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()