python opengl examples (multiple windows, multithreading)

This commit is contained in:
Matthias Hochsteger 2014-10-10 14:23:27 +00:00
parent d8d525205e
commit 0565fb9cd5
3 changed files with 153 additions and 79 deletions

View File

@ -2,112 +2,76 @@ from netgen.csg import *
import netgen.meshing as meshing
import libvisual
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
# import Window class from other file
from opengl_window import Window
sp1 = Sphere (Pnt(0,0,0), 0.2)
sp2 = Sphere (Pnt(0.5,0,0), 0.2)
all = sp1+sp2
geom = CSGeometry()
geom.Add (all)
param = meshing.MeshingParameters()
param.maxh = 0.1
m1 = GenerateMesh (geom, param)
vismesh = libvisual.meshvis.VS(m1)
vis = VS(geom)
# vis.Draw()
window = 0 # glut window number
width, height = 500, 500
# window callback functions
def mydraw():
vis.Draw()
glutSwapBuffers()
def mymouse(xold, yold, x, y, mode):
MouseMove(vis,xold,yold, x,y, mode)
xold = -1;
yold = -1;
mode = 'r'
# init glut and create window
glutInit("mainwin")
win_geom = Window( name=b"ngs", drawfunc=mydraw, mousefunc=mymouse)
def myMotionHandler( x, y ):
global xold, yold
MouseMove(vis,xold,yold, x,y, mode) # 'm','z'
xold = x
yold = y
glutPostRedisplay()
# mydraw()
###########################################
# press ctrl+c to break loop
try:
while(True):
for i in range(10000):
glutMainLoopEvent()
print('press ctrl+c to create second window!')
except:
pass
def myPassiveMotionHandler( x, y ):
global xold, yold
xold = x
yold = y
###########################################
# create second window (transformation matrices are shared)
vismesh = libvisual.meshvis.VS(m1)
def mydraw2():
vismesh.Draw()
glutSwapBuffers()
def myMouseHandler( button, state, x, y ):
print(button,state,x,y)
modes = {0:'r', 1:'m', 2:'z'}
global mode
if button<3:
if state==0:
mode = modes[button]
else:
mode = 'r'
win_mesh = Window( name=b"ngs2", drawfunc=mydraw2, mousefunc=mymouse)
###########################################
# press ctrl+c to break loop
try:
while(True):
for i in range(10000):
glutMainLoopEvent()
print('press ctrl+c to hide/destroy first window!')
except:
pass
###########################################
glutInit("mainwin") # initialize glut
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH)
glutInitWindowSize(width, height) # set window size
glutInitWindowPosition(0, 0) # set window position
window = glutCreateWindow(b"ngs") # create window with title
## BREAKS (on numericus) as soon as mouse touches remaining window ("Error of failed request: GLXBadContextTag")
# glutDestroyWindow(win_geom.handle)
glutMotionFunc(myMotionHandler)
glutMouseFunc(myMouseHandler)
glutPassiveMotionFunc(myPassiveMotionHandler)
glutDisplayFunc(mydraw) # set draw function callback
# glutIdleFunc(mydraw) # draw all the time
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
pnear = 0.1;
pfar = 10;
gluPerspective(20.0, 1.0*width / height, pnear, pfar);
glViewport(0, 0, width, height);
glMatrixMode(GL_MODELVIEW);
glutMainLoop()
# import threading
# threading.start_new_thread (glutMainLoop, [])
# from threading import Thread
# thread = Thread(target = glutMainLoop)
# thread.start()
# thread.join()
# param = meshing.MeshingParameters(maxh=0.2)
# mesh = GenerateMesh (geom, param)
# mesh.Save ("test.vol")
## WORKS
glutHideWindow(win_geom.handle)
try:
while(True):
glutMainLoopEvent()
except:
pass

View File

@ -0,0 +1,53 @@
from netgen.csg import *
import netgen.meshing as meshing
import libvisual
from OpenGL.GLUT import *
# import Window class from other file
from opengl_window import Window
sp1 = Sphere (Pnt(0,0,0), 0.2)
sp2 = Sphere (Pnt(0.5,0,0), 0.2)
all = sp1+sp2
geom = CSGeometry()
geom.Add (all)
param = meshing.MeshingParameters()
param.maxh = 0.1
m1 = GenerateMesh (geom, param)
vis = VS(geom)
# window callback functions
def mydraw():
vis.Draw()
glutSwapBuffers()
def mymouse(xold, yold, x, y, mode):
MouseMove(vis,xold,yold, x,y, mode)
###########################################
glutInit("mainwin")
# IMPORTANT: create window in the mainloop - thread!
## not working:
#win_geom = Window( name=b"ngs", drawfunc=mydraw, mousefunc=mymouse)
def runVisualization():
## working:
win_geom = Window( name=b"ngs", drawfunc=mydraw, mousefunc=mymouse)
glutMainLoop()
# create thread
from threading import Thread
thread = Thread(target = runVisualization)
thread.start()
thread.join()

View File

@ -0,0 +1,57 @@
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
class Window():
xold = -1;
yold = -1;
mode = 'r'
modes = {0:'r', 1:'m', 2:'z'}
drawfunc = None
mousefunc = None
def draw(self):
glutSetWindow(self.handle)
self.drawfunc()
def __init__( self, name=b"Window", width=500, height=500, drawfunc=None, mousefunc=None ):
glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,GLUT_ACTION_GLUTMAINLOOP_RETURNS)
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH)
glutInitWindowSize(width, height) # set window size
glutInitWindowPosition(0, 0) # set window position
self.handle = glutCreateWindow(b"ngs") # create window with title
glutMotionFunc(self.motionHandler)
glutMouseFunc(self.mouseHandler)
glutPassiveMotionFunc(self.passiveMotionHandler)
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
pnear = 0.1;
pfar = 10;
gluPerspective(20.0, 1.0*width / height, pnear, pfar);
glViewport(0, 0, width, height);
glMatrixMode(GL_MODELVIEW);
self.drawfunc = drawfunc
self.mousefunc = mousefunc
if drawfunc:
glutDisplayFunc(self.draw) # set draw function callback
def motionHandler(self, x, y ):
self.mousefunc(self.xold,self.yold, x,y, self.mode) # 'm','z'
self.xold = x
self.yold = y
glutPostRedisplay()
def passiveMotionHandler(self, x, y ):
self.xold = x
self.yold = y
def mouseHandler(self, button, state, x, y ):
print(button,state,x,y)
if button<3:
if state==0:
self.mode = self.modes[button]
else:
self.mode = 'r'
glutInit("mainwin") # initialize glut