社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

pygame opengl窗口在被python osc dispatcher调用时未更新

Martin • 4 年前 • 734 次点击  

我目前正在对一些点进行三维可视化,通过OSC(python OSC)接收数据并使用pygame显示它们。基于一个立方体的Pygame示例,我添加了OSC调度器函数来接收数据,然后调用函数来更新这些调度器函数的显示。但是,当从调度的evevents调用这些函数时,显示将不会更新。当最初调用相同的函数(而不是调度器)时,显示仍在更新。我仍在使用多维数据集进行测试,没有显示收到的数据。

我对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()
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/38123
 
734 次点击  
文章 [ 1 ]  |  最新文章 4 年前
Martin
Reply   •   1 楼
Martin    5 年前

在找到时间研究如何处理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()