Skip to content

Commit eaa8bce

Browse files
committed
First rushed attempt getting os x frame working
1 parent 7ede18e commit eaa8bce

File tree

2 files changed

+144
-3
lines changed

2 files changed

+144
-3
lines changed

pyopengltk/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
if sys.platform.startswith('win32'):
2727
from pyopengltk.win32 import OpenGLFrame
2828

29-
# if sys.platform.startswith('darwin'):
30-
# from pyopengltk.darwin import OpenGLFrame
29+
if sys.platform.startswith('darwin'):
30+
from pyopengltk.darwin import OpenGLFrame
3131

3232
# opengl
3333
from pyopengltk.opengl import RawOpengl

pyopengltk/darwin.py

+142-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,142 @@
1-
"""Currently not implemented"""
1+
"""
2+
OS X implementation of the opengl frame
3+
4+
Resources:
5+
https://github.com/apitrace/apitrace/blob/master/specs/cglapi.py
6+
http://mirror.informatimago.com/next/developer.apple.com/documentation/GraphicsImaging/Conceptual/OpenGL/chap5/chapter_5_section_41.html#//apple_ref/doc/uid/TP30000136/BDJHHIDE
7+
https://stackoverflow.com/questions/5523777/opengl-on-mac-operation
8+
9+
https://stackoverflow.com/questions/11402472/is-there-a-way-to-iterate-over-all-open-windows-in-mac-os-x
10+
https://chromium.googlesource.com/external/p3/regal/+/b79468111af42248fdb0e5684820e39bba2bef7a/src/apitrace/specs/cglapi.py
11+
"""
12+
from __future__ import print_function
13+
14+
import sys, time
15+
from ctypes import c_int, c_char_p, c_void_p, cdll, POINTER, util, \
16+
pointer, CFUNCTYPE, c_bool, byref
17+
18+
from pyopengltk.base import BaseOpenGLFrame
19+
20+
CGL_RESULT = {
21+
10000: 'Invalid pixel format attribute',
22+
10001: 'Invalid renderer property',
23+
10002: 'Invalid pixel format object',
24+
10003: 'Invalid renderer information object',
25+
10004: 'Invalid context object',
26+
10005: 'Invalid drawable',
27+
10006: 'Invalid display',
28+
10007: 'Invalid context state',
29+
10008: 'Invalid numerical value',
30+
10009: 'Invalid share context',
31+
10010: 'Invalid enumerant',
32+
10011: 'Invalid off-screen drawable',
33+
10012: 'Invalid full-screen drawable',
34+
10013: 'Invalid window',
35+
10014: 'Invalid memory address',
36+
10015: 'Invalid code module',
37+
10016: 'Invalid memory allocation',
38+
10017: 'Invalid Core Graphics connection',
39+
}
40+
41+
class CGLPixelFormatAttribute(object):
42+
"""
43+
The following constants are used by CGLChoosePixelFormat and CGLDescribePixelFormat.
44+
The existence of a Boolean attribute in the attribute array of CGLChoosePixelFormat
45+
implies a true value. Integer attribute constants must be followed by a value.
46+
"""
47+
kCGLPFAAllRenderers = 1
48+
kCGLPFADoubleBuffer = 5
49+
kCGLPFAStereo = 6
50+
kCGLPFAAuxBuffers = 7
51+
kCGLPFAColorSize = 8
52+
kCGLPFAAlphaSize = 11
53+
kCGLPFADepthSize = 12
54+
kCGLPFAStencilSize = 13
55+
kCGLPFAAccumSize = 14
56+
kCGLPFAMinimumPolicy = 51
57+
kCGLPFAMaximumPolicy = 52
58+
kCGLPFAOffScreen = 53
59+
kCGLPFAFullScreen = 54
60+
kCGLPFARendererID = 70
61+
kCGLPFASingleRenderer = 71
62+
kCGLPFANoRecovery = 72
63+
kCGLPFAAccelerated = 73
64+
kCGLPFAClosestPolicy = 74
65+
kCGLPFARobust = 75
66+
kCGLPFABackingStore = 76
67+
kCGLPFAMPSafe = 78
68+
kCGLPFAWindow = 80
69+
kCGLPFAMultiScreen = 81
70+
kCGLPFACompliant = 83
71+
kCGLPFADisplayMask = 84
72+
kCGLPFAOpenGLProfile = 99
73+
kCGLPFAVirtualScreenCount = 128
74+
75+
kCGLOGLPVersion_Legacy = 0x1000 # choose a renderer compatible with GL1.0
76+
kCGLOGLPVersion_3_2_Core = 0x3200 # choose a renderer capable of GL3.2 or later
77+
kCGLOGLPVersion_GL3_Core = 0x3200 # choose a renderer capable of GL3.2 or later
78+
kCGLOGLPVersion_GL4_Core = 0x410 # choose a renderer capable of GL4.1 or later
79+
80+
81+
_gllib = cdll.LoadLibrary(util.find_library("OpenGL"))
82+
CGLGetCurrentContext = _gllib.CGLGetCurrentContext # returns CGLContextObj
83+
# CGLChoosePixelFormat (attribs, &pixelFormat, &numPixelFormats);
84+
CGLChoosePixelFormat = _gllib.CGLChoosePixelFormat
85+
# CGLCreateContext(pixelFormat, NULL, &cglContext1);
86+
CGLCreateContext = _gllib.CGLCreateContext
87+
CGLSetCurrentContext = _gllib.CGLSetCurrentContext
88+
CGLSetSurface = _gllib.CGLSetSurface
89+
CGSMainConnectionID = _gllib.CGSMainConnectionID
90+
91+
92+
class OpenGLFrame(BaseOpenGLFrame):
93+
94+
def tkCreateContext(self):
95+
print('winfo_id', self.winfo_id())
96+
try:
97+
attribs = c_int * 9
98+
pfo = attribs(
99+
CGLPixelFormatAttribute.kCGLPFAOpenGLProfile, kCGLOGLPVersion_Legacy,
100+
CGLPixelFormatAttribute.kCGLPFAWindow, 1,
101+
CGLPixelFormatAttribute.kCGLPFAColorSize, 24,
102+
CGLPixelFormatAttribute.kCGLPFADepthSize, 16,
103+
# CGLPixelFormatAttribute.kCGLPFAAccelerated,
104+
# CGLPixelFormatAttribute.kCGLPFAFullScreen,
105+
# CGLPixelFormatAttribute.kCGLPFADoubleBuffer,
106+
0,
107+
)
108+
pixel_format_obj = c_void_p()
109+
num_pixel_formats = c_int(0)
110+
CGLChoosePixelFormat(pfo, byref(pixel_format_obj), byref(num_pixel_formats))
111+
if num_pixel_formats.value == 0:
112+
raise ValueError("No pixel formats detected")
113+
114+
print('num_pixel_formats', num_pixel_formats.value)
115+
print('pixel_format_obj', pixel_format_obj)
116+
117+
self.__context = c_void_p()
118+
result = CGLCreateContext(pixel_format_obj, None, byref(self.__context))
119+
print('result', type(result), result)
120+
print('contect', type(self.__context), self.__context.value)
121+
err = CGL_RESULT.get(result)
122+
if err:
123+
raise ValueError('Error while creating context: %s' % err)
124+
125+
test = CGSMainConnectionID()
126+
print("CGSMainConnectionID", test)
127+
# # Somehow we need to find the window and surface_id
128+
# CGSWindow = c_int(self.winfo_id())
129+
# CGLSetSurface(cgl_context, connection_id, window, surface_id)
130+
131+
print('context', type(self.__context), self.__context)
132+
CGLSetCurrentContext(self.__context)
133+
except Exception as ex:
134+
print(ex)
135+
exit(-1)
136+
137+
def tkMakeCurrent(self):
138+
if self.winfo_ismapped():
139+
CGLSetCurrentContext(self.__context)
140+
141+
def tkSwapBuffers(self):
142+
pass

0 commit comments

Comments
 (0)