Skip to content

Commit aba3360

Browse files
committed
shader_example.py: basic pep8
1 parent 9eaaebe commit aba3360

File tree

1 file changed

+51
-44
lines changed

1 file changed

+51
-44
lines changed

shader_example.py

Lines changed: 51 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1+
"""Example rotating a point cloud containing 100.000 points"""
22
from __future__ import print_function, division
33

44
from OpenGL import GL, GLUT
@@ -14,13 +14,14 @@
1414
else:
1515
import Tkinter as tk
1616

17-
#
17+
1818
# Avoiding glitches in pyopengl-3.0.x and python3.4
1919
def bytestr(s):
20-
return s.encode("utf-8")+b"\000"
21-
#
20+
return s.encode("utf-8") + b"\000"
21+
22+
2223
# Avoiding glitches in pyopengl-3.0.x and python3.4
23-
def compileShader( source, shaderType ):
24+
def compileShader(source, shaderType):
2425
"""
2526
Compile shader source of given type
2627
source -- GLSL source-code for the shader
@@ -41,15 +42,16 @@ def compileShader( source, shaderType ):
4142
# TODO: this will be wrong if the user has
4243
# disabled traditional unpacking array support.
4344
raise RuntimeError(
44-
"""Shader compile failure (%s): %s"""%(
45+
"""Shader compile failure (%s): %s""" % (
4546
result,
46-
GL.glGetShaderInfoLog( shader ),
47+
GL.glGetShaderInfoLog(shader),
4748
),
4849
source,
4950
shaderType,
5051
)
51-
return shader
52-
52+
return shader
53+
54+
5355
vertex_shader = """#version 130
5456
in vec3 position;
5557
varying vec3 vertex_color;
@@ -66,53 +68,57 @@ def compileShader( source, shaderType ):
6668
varying vec3 vertex_color;
6769
void main()
6870
{
69-
gl_FragColor = vec4(vertex_color,0.25f);
71+
gl_FragColor = vec4(vertex_color,0.25f);
7072
}
7173
"""
72-
NPTS=100000
74+
NPTS = 100000
75+
76+
vertices = (numpy.random.random(NPTS * 3).astype(numpy.float32)-.5) * 1.5
77+
vertices.shape = NPTS, 3
7378

74-
vertices = (numpy.random.random( NPTS*3 ).astype(numpy.float32)-.5)*1.5
75-
vertices.shape = NPTS,3
7679

7780
def create_object(shader):
7881
# Create a new VAO (Vertex Array Object) and bind it
7982
vertex_array_object = GL.glGenVertexArrays(1)
80-
GL.glBindVertexArray( vertex_array_object )
83+
GL.glBindVertexArray(vertex_array_object)
8184
# Generate buffers to hold our vertices
8285
vertex_buffer = GL.glGenBuffers(1)
8386
GL.glBindBuffer(GL.GL_ARRAY_BUFFER, vertex_buffer)
84-
# Get the position of the 'position' in parameter of our shader and bind it.
85-
position = GL.glGetAttribLocation(shader, bytestr('position') )
87+
# Get the position of the 'position' in parameter of our shader
88+
# and bind it.
89+
position = GL.glGetAttribLocation(shader, bytestr('position'))
8690
GL.glEnableVertexAttribArray(position)
8791
# Describe the position data layout in the buffer
88-
GL.glVertexAttribPointer(position, 3, GL.GL_FLOAT, False, 0, ctypes.c_void_p(0))
92+
GL.glVertexAttribPointer(position, 3, GL.GL_FLOAT, False,
93+
0, ctypes.c_void_p(0))
8994
# Send the data over to the buffer (bytes)
9095
vs = vertices.tostring()
9196
GL.glBufferData(GL.GL_ARRAY_BUFFER, len(vs), vs, GL.GL_STATIC_DRAW)
9297
# Unbind the VAO first (Important)
93-
GL.glBindVertexArray( 0 )
98+
GL.glBindVertexArray(0)
9499
# Unbind other stuff
95100
GL.glDisableVertexAttribArray(position)
96101
GL.glBindBuffer(GL.GL_ARRAY_BUFFER, 0)
97102
return vertex_array_object
98-
99-
def rot(a,b,c):
100-
s=numpy.sin(a)
101-
c=numpy.cos(a)
102-
am = numpy.array((( c,s,0),(-s,c,0),(0,0,1)), numpy.float32)
103-
s=numpy.sin(b)
104-
c=numpy.cos(b)
105-
bm = numpy.array((( c,0,s),(0,1,0),(-s,0,c)), numpy.float32)
106-
s=numpy.sin(c)
107-
c=numpy.cos(c)
108-
cm = numpy.array((( 1,0,0 ),(0,c,s),(0,-s,c)), numpy.float32)
109-
return numpy.dot(numpy.dot( am, bm ), cm )
103+
104+
105+
def rot(a, b, c):
106+
s = numpy.sin(a)
107+
c = numpy.cos(a)
108+
am = numpy.array(((c, s, 0), (-s, c, 0), (0, 0, 1)), numpy.float32)
109+
s = numpy.sin(b)
110+
c = numpy.cos(b)
111+
bm = numpy.array(((c, 0, s), (0, 1, 0), (-s, 0, c)), numpy.float32)
112+
s = numpy.sin(c)
113+
c = numpy.cos(c)
114+
cm = numpy.array(((1, 0, 0), (0, c, s), (0, -s, c)), numpy.float32)
115+
return numpy.dot(numpy.dot(am, bm), cm)
110116

111117

112118
class ShaderFrame(pyopengltk.OpenGLFrame):
113119

114120
def initgl(self):
115-
# GLUT.glutInit(sys.argv)
121+
# GLUT.glutInit(sys.argv)
116122
GL.glClearColor(0.15, 0.15, 0.15, 1.0)
117123
GL.glEnable(GL.GL_DEPTH_TEST)
118124
GL.glEnable(GL.GL_PROGRAM_POINT_SIZE)
@@ -122,7 +128,7 @@ def initgl(self):
122128
compileShader(fragment_shader, GL.GL_FRAGMENT_SHADER)
123129
)
124130
self.vertex_array_object = create_object(self.shader)
125-
self.proj = GL.glGetUniformLocation( self.shader, bytestr('proj'))
131+
self.proj = GL.glGetUniformLocation(self.shader, bytestr('proj'))
126132
self.nframes = 0
127133
self.start = time.time()
128134

@@ -131,29 +137,30 @@ def redraw(self):
131137
GL.glUseProgram(self.shader)
132138
t = time.time()-self.start
133139
s = 2.
134-
p = rot(t*s/5.,t*s/6.,t*s/7.)
140+
p = rot(t*s/5., t*s/6., t*s/7.)
135141
GL.glUniformMatrix3fv(self.proj, 1, GL.GL_FALSE, p)
136-
GL.glBindVertexArray( self.vertex_array_object )
142+
GL.glBindVertexArray(self.vertex_array_object)
137143
GL.glDrawArrays(GL.GL_POINTS, 0, NPTS)
138-
GL.glBindVertexArray( 0 )
139-
GL.glUseProgram( 0 )
140-
GL.glRasterPos2f(-0.99,-0.99);
144+
GL.glBindVertexArray(0)
145+
GL.glUseProgram(0)
146+
GL.glRasterPos2f(-0.99, -0.99)
141147
if self.nframes > 1:
142148
t = time.time()-self.start
143-
fps = "fps: %5.2f frames: %d"%(self.nframes / t, self.nframes)
144-
# for c in fps:
145-
# GLUT.glutBitmapCharacter(GLUT.GLUT_BITMAP_HELVETICA_18, ord(c));
149+
fps = "fps: %5.2f frames: %d" % (self.nframes / t, self.nframes)
150+
# for c in fps:
151+
# GLUT.glutBitmapCharacter(GLUT.GLUT_BITMAP_HELVETICA_18, ord(c));
146152
self.nframes += 1
147153

148154

149155
def main():
150156
root = tk.Tk()
151-
app = ShaderFrame(root, width=512,height=512)
157+
app = ShaderFrame(root, width=512, height=512)
152158
app.pack(fill=tk.BOTH, expand=tk.YES)
153159
app.after(100, app.printContext)
154-
app.animate=1000//60
155-
app.animate=1
160+
app.animate = 1000 // 60
161+
app.animate = 1
156162
app.mainloop()
163+
164+
157165
if __name__ == '__main__':
158166
main()
159-

0 commit comments

Comments
 (0)