【问题标题】:pygame opengl window not updating when called by python-osc dispatcherpython-osc调度程序调用pygame opengl窗口时不更新
【发布时间】:2019-02-28 00:14:42
【问题描述】:

我目前正在对几个点进行 3d 可视化,通过 osc (python-osc) 接收数据并使用 pygame 显示它们。基于一个立方体的工作 pygame 示例,我添加了 osc 调度程序函数来接收数据,然后调用这些函数来更新这些调度程序函数的显示。但是,当从调度的事件调用这些函数时,显示不会更新。当最初调用相同的函数时(不是由调度程序调用),显示仍在更新。我仍在使用多维数据集进行测试,而不是显示接收到的数据。

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

【问题讨论】:

  • 在我看来,您没有对到达 printPoints 函数的值做任何事情(除了将它们打印到控制台窗口之外)。
  • printPoints 点在不同的线程中执行,OpenGL 上下文不是该线程的当前上下文 - 请参阅OpenGL and multithreading。所以你不能从这个线程调用任何OpenGL指令。

标签: python python-3.x pygame pyopengl osc


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-13
    • 2014-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-30
    • 1970-01-01
    • 2018-01-28
    相关资源
    最近更新 更多