Skip to content

Commit 9c71912

Browse files
committed
Patches for older pyopengl on python3
1 parent 22bfbde commit 9c71912

File tree

2 files changed

+44
-10
lines changed

2 files changed

+44
-10
lines changed

pyopengltk.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ def tkSwapBuffers( self ):
223223
class OpenGLFrame( baseOpenGLFrame ):
224224

225225
def tkCreateContext( self ):
226-
self.__window = XOpenDisplay( os.environ.get("DISPLAY") )
226+
self.__window = XOpenDisplay( self.winfo_screen().encode('utf-8'))
227227
# Check glx version:
228228
major = c_int(0)
229229
minor = c_int(0)

shader_example.py

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,43 @@
1414
else:
1515
import Tkinter as tk
1616

17-
vertex_shader = """
18-
#version 130
17+
#
18+
# Avoiding glitches in pyopengl-3.0.x and python3.4
19+
def bytestr(s):
20+
return s.encode("utf-8")+b"\000"
21+
#
22+
# Avoiding glitches in pyopengl-3.0.x and python3.4
23+
def compileShader( source, shaderType ):
24+
"""
25+
Compile shader source of given type
26+
source -- GLSL source-code for the shader
27+
shaderType -- GLenum GL_VERTEX_SHADER, GL_FRAGMENT_SHADER, etc,
28+
returns GLuint compiled shader reference
29+
raises RuntimeError when a compilation failure occurs
30+
"""
31+
if isinstance(source, str):
32+
source = [source]
33+
elif isinstance(source, bytes):
34+
source = [source.decode('utf-8')]
35+
36+
shader = GL.glCreateShader(shaderType)
37+
GL.glShaderSource(shader, source)
38+
GL.glCompileShader(shader)
39+
result = GL.glGetShaderiv(shader, GL.GL_COMPILE_STATUS)
40+
if not(result):
41+
# TODO: this will be wrong if the user has
42+
# disabled traditional unpacking array support.
43+
raise RuntimeError(
44+
"""Shader compile failure (%s): %s"""%(
45+
result,
46+
GL.glGetShaderInfoLog( shader ),
47+
),
48+
source,
49+
shaderType,
50+
)
51+
return shader
52+
53+
vertex_shader = """#version 130
1954
in vec3 position;
2055
varying vec3 vertex_color;
2156
uniform mat3 proj;
@@ -27,8 +62,7 @@
2762
}
2863
"""
2964

30-
fragment_shader = """
31-
#version 130
65+
fragment_shader = """#version 130
3266
varying vec3 vertex_color;
3367
void main()
3468
{
@@ -48,7 +82,7 @@ def create_object(shader):
4882
vertex_buffer = GL.glGenBuffers(1)
4983
GL.glBindBuffer(GL.GL_ARRAY_BUFFER, vertex_buffer)
5084
# Get the position of the 'position' in parameter of our shader and bind it.
51-
position = GL.glGetAttribLocation(shader, 'position')
85+
position = GL.glGetAttribLocation(shader, bytestr('position') )
5286
GL.glEnableVertexAttribArray(position)
5387
# Describe the position data layout in the buffer
5488
GL.glVertexAttribPointer(position, 3, GL.GL_FLOAT, False, 0, ctypes.c_void_p(0))
@@ -78,17 +112,17 @@ def rot(a,b,c):
78112
class ShaderFrame(pyopengltk.OpenGLFrame):
79113

80114
def initgl(self):
81-
GLUT.glutInit()
115+
GLUT.glutInit(sys.argv)
82116
GL.glClearColor(0.15, 0.15, 0.15, 1.0)
83117
GL.glEnable(GL.GL_DEPTH_TEST)
84118
GL.glEnable(GL.GL_PROGRAM_POINT_SIZE)
85119
if not hasattr(self, "shader"):
86120
self.shader = OpenGL.GL.shaders.compileProgram(
87-
OpenGL.GL.shaders.compileShader(vertex_shader, GL.GL_VERTEX_SHADER),
88-
OpenGL.GL.shaders.compileShader(fragment_shader, GL.GL_FRAGMENT_SHADER)
121+
compileShader(vertex_shader, GL.GL_VERTEX_SHADER),
122+
compileShader(fragment_shader, GL.GL_FRAGMENT_SHADER)
89123
)
90124
self.vertex_array_object = create_object(self.shader)
91-
self.proj = GL.glGetUniformLocation( self.shader, 'proj')
125+
self.proj = GL.glGetUniformLocation( self.shader, bytestr('proj'))
92126
self.nframes = 0
93127
self.start = time.time()
94128

0 commit comments

Comments
 (0)