pygame opengl window not updating when called by python-osc dispatcher

638 Views Asked by At

I'm currently working an a 3d visualization of a few points, receiving data via osc (python-osc) and displaying them using pygame. Based on a working pygame example of a cube I added the osc dispatcher functions to receive data, then calling the functions to update the display from those dispatcher functions as well. However the display won't update when those functions are called from the dispatched evevents. Display is still updating when the same functions get called initially (not by the dispatcher). I was still using the cube for testing, not displaying received data.

I'm new to python - so a little helpless at the moment finding the reason for that behavior. I guess that the context of the dispatcher and the main program aren't the same or something like that - but no clue how to debug that... here's the code I'm using to update the gl display:

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

and here the whole code:

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

There are 1 best solutions below

0
On

after finding some time to research on how to deal with the gl context I gave up. Apparently it's not possible inside GLUT to get and set the window context easily, but some hacks exist. I found my own solution in creating a second thread that creates and updates the display function so the context stays in the thread:

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