Skip to content

Commit 7b6f71e

Browse files
committed
tidying
1 parent 3a27427 commit 7b6f71e

File tree

1 file changed

+59
-52
lines changed

1 file changed

+59
-52
lines changed

pyopengltk.py

Lines changed: 59 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,15 @@ def tkMap( self, evt ):
6161
self.tkCreateContext( )
6262
self.initgl()
6363

64-
def printContext(self):
64+
def printContext(self, extns=False):
6565
""" For debugging """
66-
print("Extension list:")
6766
exts = GL.glGetString(GL.GL_EXTENSIONS)
68-
for e in sorted(exts.split()):
69-
print( "\t", e )
67+
if extns:
68+
print("Extension list:")
69+
for e in sorted(exts.split()):
70+
print( "\t", e )
71+
else:
72+
print("Number of extensions:",len(exts.split()))
7073
print( "GL_VENDOR :",GL.glGetString(GL.GL_VENDOR))
7174
print( "GL_RENDERER:",GL.glGetString(GL.GL_RENDERER))
7275
print( "GL_VERSION :",GL.glGetString(GL.GL_VERSION))
@@ -190,34 +193,29 @@ def tkSwapBuffers( self ):
190193
pointer, CFUNCTYPE, c_bool
191194
from OpenGL import GLX
192195
from OpenGL.raw._GLX import Display
193-
# import tk_read_XWindowAttributes
194196

195197
_x11lib = cdll.LoadLibrary(util.find_library( "X11" ) )
196198
XOpenDisplay = _x11lib.XOpenDisplay
197199
XOpenDisplay.argtypes = [c_char_p]
198200
XOpenDisplay.restype = POINTER(Display)
199-
200-
Colormap = c_void_p
201201

202+
Colormap = c_void_p
203+
# Attributes for old style creation
202204
att = [ GLX.GLX_RGBA, GLX.GLX_DOUBLEBUFFER,
203-
GLX.GLX_RED_SIZE, 4,
205+
GLX.GLX_RED_SIZE, 4,
204206
GLX.GLX_GREEN_SIZE, 4,
205-
GLX.GLX_BLUE_SIZE, 4,
207+
GLX.GLX_BLUE_SIZE, 4,
206208
GLX.GLX_DEPTH_SIZE, 16,
207209
0,
208210
]
211+
# Attributes for newer style creations
209212
fbatt = [ GLX.GLX_X_RENDERABLE , 1,
210213
GLX.GLX_DRAWABLE_TYPE , GLX.GLX_WINDOW_BIT,
211214
GLX.GLX_RENDER_TYPE , GLX.GLX_RGBA_BIT,
212-
GLX.GLX_RED_SIZE , 4,
213-
GLX.GLX_GREEN_SIZE , 4,
214-
GLX.GLX_BLUE_SIZE , 4,
215-
# GLX.GLX_ALPHA_SIZE , 4,
216-
GLX.GLX_DEPTH_SIZE , 16,
215+
GLX.GLX_RED_SIZE , 1,
216+
GLX.GLX_GREEN_SIZE , 1,
217+
GLX.GLX_BLUE_SIZE , 1,
217218
GLX.GLX_DOUBLEBUFFER , 1,
218-
# GLX.GLX_STENCIL_SIZE , 8,
219-
# GLX.GLX_X_VISUAL_TYPE , GLX.GLX_TRUE_COLOR,
220-
# GLX.GLX_CONFIG_CAVEAT , GLX.GLX_NONE,
221219
0,
222220
]
223221

@@ -226,6 +224,7 @@ class OpenGLFrame( baseOpenGLFrame ):
226224

227225
def tkCreateContext( self ):
228226
self.__window = XOpenDisplay( os.environ.get("DISPLAY") )
227+
# Check glx version:
229228
major = c_int(0)
230229
minor = c_int(0)
231230
GLX.glXQueryVersion( self.__window, major, minor )
@@ -239,44 +238,61 @@ def tkCreateContext( self ):
239238
visual,
240239
None,
241240
GL.GL_TRUE)
241+
GLX.glXMakeCurrent(self.__window, self._wid, self.__context)
242+
return # OUT HERE FOR 1.2 and less
242243
else:
243-
ncfg = GL.GLint(0)
244+
# 1.3 or higher
245+
# which screen - should it be winfo_screen instead ??
244246
XDefaultScreen = _x11lib.XDefaultScreen
245247
XDefaultScreen.argtypes = [POINTER(Display)]
246248
XDefaultScreen.restype = c_int
247249
screen = XDefaultScreen( self.__window )
248250
print("Screen is ",screen)
251+
# Look at framebuffer configs
252+
ncfg = GL.GLint(0)
249253
cfgs = GLX.glXChooseFBConfig( self.__window,
250254
screen,
251255
(GL.GLint * len(fbatt))(* fbatt),
252256
ncfg )
253-
print( "Number of configs",ncfg.value )
254-
# xwa = tk_read_XWindowAttributes.getXWA(self._wid)
255-
# print("xwa....id" ,xwa.visual.contents.visualid)
256-
# ideal = xwa.visual.contents.visualid
257-
ideal = self.winfo_visualid()
257+
print( "Number of FBconfigs",ncfg.value )
258+
#
259+
# Try to match to the current window
260+
# ... might also be possible to set this for the frame
261+
# ... but for now we just take what Tk gave us
262+
ideal = int(self.winfo_visualid(), 16) # convert from hex
258263
best = -1
259264
for i in range(ncfg.value):
260265
vis = GLX.glXGetVisualFromFBConfig(self.__window, cfgs[i])
261-
# print("i %d visualid %d:" %(i,vis.contents.visualid))
262266
if ideal == vis.contents.visualid:
263267
best = i
268+
# print(type(vis.contents.visualid))
264269
if best >= 0:
265-
print("Got a matching visual?",best )
270+
print("Got a matching visual: index %d xid %s"%(best,hex(ideal) ))
266271
else:
267-
print("OH dear - visual does not match?" )
272+
print("oh dear - visual does not match" )
273+
# Take the first in the list (should be another I guess)
268274
best=0
275+
# Here we insist on RGBA - but didn't check earlier
276+
self.__context = GLX.glXCreateNewContext(self.__window,
277+
cfgs[best],
278+
GLX.GLX_RGBA_TYPE,
279+
None, # share list
280+
GL.GL_TRUE) # direct
281+
print("Is Direct?: ", GLX.glXIsDirect( self.__window, self.__context ))
282+
# Not creating another window ... some tutorials do
283+
# print("wid: ",self._wid)
284+
# self._wid = GLX.glXCreateWindow( self.__window, cfgs[best], self._wid, None)
285+
# print("wid: ",self._wid)
286+
GLX.glXMakeContextCurrent( self.__window, self._wid, self._wid,
287+
self.__context )
288+
print("Done making a first context")
269289
extensions = GLX.glXQueryExtensionsString(self.__window, screen)
270-
if "GLX_ARB_create_context" not in extensions or True: # FIXME HERE old style then:
271-
typ = GLX.GLX_RGBA_TYPE #
272-
self.__context = GLX.glXCreateNewContext(self.__window,
273-
cfgs[best],
274-
typ,
275-
None, # share list
276-
GL.GL_TRUE) # direct
277-
else:
290+
# Here we quit - getting a modern context needs further work below
291+
return
292+
if "GLX_ARB_create_context" in extensions:
293+
# We can try to upgrade it ??
294+
print("Trying to upgrade context")
278295
s = "glXCreateContextAttribsARB"
279-
# FAILING HERE
280296
p = GLX.glXGetProcAddress( c_char_p( s ) )
281297

282298
print(p)
@@ -304,15 +320,6 @@ def tkCreateContext( self ):
304320
# pdb.set_trace()
305321
self.__context = p( self.__window, cfgs[best], None, GL.GL_TRUE,
306322
(GL.GLint * len(arb_attrs))(* arb_attrs) )
307-
if not self.__context:
308-
print("Failed to create context")
309-
print("Is Direct?: ", GLX.glXIsDirect( self.__window, self.__context ))
310-
# print("wid: ",self._wid)
311-
# self._wid = GLX.glXCreateWindow( self.__window, cfgs[best], self._wid, None)
312-
# print("wid: ",self._wid)
313-
GLX.glXMakeContextCurrent( self.__window, self._wid, self._wid,
314-
self.__context )
315-
print("Done making context")
316323

317324

318325
def tkMakeCurrent( self ):
@@ -628,11 +635,11 @@ def tkTranslate(self, event):
628635
# Scale mouse translations to object viewplane so object tracks with mouse
629636

630637
win_height = max( 1,self.winfo_height() )
631-
obj_c = ( self.xcenter, self.ycenter, self.zcenter )
632-
win = GLU.gluProject( obj_c[0], obj_c[1], obj_c[2])
633-
obj = GLU.gluUnProject( win[0], win[1] + 0.5 * win_height, win[2])
634-
dist = math.sqrt( v3distsq( obj, obj_c ) )
635-
scale = abs( dist / ( 0.5 * win_height ) )
638+
obj_c = ( self.xcenter, self.ycenter, self.zcenter )
639+
win = GLU.gluProject( obj_c[0], obj_c[1], obj_c[2])
640+
obj = GLU.gluUnProject( win[0], win[1] + 0.5 * win_height, win[2])
641+
dist = math.sqrt( v3distsq( obj, obj_c ) )
642+
scale = abs( dist / ( 0.5 * win_height ) )
636643

637644
glTranslateScene(scale, event.x, event.y, self.xmouse, self.ymouse)
638645
self.tkRedraw()
@@ -645,7 +652,7 @@ def tkRedraw(self, *dummy):
645652
if not self.initialised: return
646653
self.activate()
647654

648-
GL.glPushMatrix() # Protect our matrix
655+
GL.glPushMatrix() # Protect our matrix
649656
self.update_idletasks()
650657
self.activate()
651658
w = self.winfo_width()
@@ -676,8 +683,8 @@ def tkRedraw(self, *dummy):
676683

677684
# Call objects redraw method.
678685
self.redraw(self)
679-
GL.glFlush() # Tidy up
680-
GL.glPopMatrix() # Restore the matrix
686+
GL.glFlush() # Tidy up
687+
GL.glPopMatrix() # Restore the matrix
681688

682689
self.tkSwapBuffers()
683690

0 commit comments

Comments
 (0)