我目前正在对一些点进行三维可视化,通过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()