forked from adamlwgriffiths/PyGLy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenderable_cube.py
More file actions
213 lines (162 loc) · 5.35 KB
/
renderable_cube.py
File metadata and controls
213 lines (162 loc) · 5.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import textwrap
import numpy
from OpenGL import GL
from pygly.shader import Shader, VertexShader, FragmentShader, ShaderProgram
from pygly.vertex_buffer import VertexBuffer, BufferAttributes, GenericAttribute, VertexAttribute, ColourAttribute
from pygly.vertex_array import VertexArray
from pyrr import geometry
rgba=(
(0.0, 1.0, 0.0,),
(1.0, 0.5, 0.0,),
(0.0, 0.0, 1.0,),
(1.0, 0.0, 1.0,),
(1.0, 1.0, 0.0,),
(1.0, 0.0, 0.0,),
)
vertices, indices = geometry.create_cube(scale=(2.0,2.0,2.0), rgba=rgba, dtype='float32')
vertices = vertices[indices]
vertices.dtype = [
('position', 'float32', (3,)),
('colour', 'float32', (3,)),
]
def create( core_profile = True ):
if core_profile:
return CoreCube()
else:
return LegacyCube()
class CoreCube( object ):
vertex_shader = textwrap.dedent( """
#version 150
// input
in vec3 in_position;
in vec3 in_colour;
uniform mat4 model_view;
uniform mat4 projection;
// shared
out vec3 ex_colour;
void main(void)
{
// apply projection and model view matrix to vertex
gl_Position = projection * model_view * vec4( in_position, 1.0 );
ex_colour = in_colour;
}
""" )
fragment_shader = textwrap.dedent( """
#version 150
// shared
in vec3 ex_colour;
// output
out vec4 fragColor;
void main(void)
{
// set colour of each fragment
fragColor = vec4( ex_colour, 1.0 );
}
""" )
def __init__( self ):
super( CoreCube, self ).__init__()
global vertices
# create our shader
self.shader = ShaderProgram(
VertexShader( self.vertex_shader ),
FragmentShader( self.fragment_shader )
)
# create a vertex buffer
# we pass in a list of regions we want to define
# we only have 1 region here
# for each region, we pass in how many rows, and the dtype
self.buffer = VertexBuffer(
GL.GL_ARRAY_BUFFER,
GL.GL_STATIC_DRAW,
data = vertices,
)
# pass the shader and region to our VAO
# and bind each of the attributes to a VAO index
# the shader name is the variable name used in the shader
# the buffer name is the name of the property in our vertex dtype
self.buffer_attributes = BufferAttributes()
self.buffer_attributes[ 'position' ] = GenericAttribute.from_dtype(
self.buffer,
vertices.dtype,
'position',
location = self.shader.attributes[ 'in_position' ]
)
self.buffer_attributes[ 'colour' ] = GenericAttribute.from_dtype(
self.buffer,
vertices.dtype,
'colour',
location = self.shader.attributes[ 'in_colour' ]
)
# create our vertex array
self.vao = VertexArray()
self.vao.bind()
self.buffer.bind()
self.buffer_attributes.set()
self.buffer.unbind()
self.vao.unbind()
def draw( self, projection, model_view ):
global vertices
self.shader.bind()
self.shader.uniforms[ 'projection' ].value = projection
self.shader.uniforms[ 'model_view' ].value = model_view
self.vao.bind()
GL.glDrawArrays( GL.GL_TRIANGLES, 0, len( vertices ) )
self.vao.unbind()
self.shader.unbind()
class LegacyCube( object ):
vertex_shader = textwrap.dedent( """
#version 120
void main(void)
{
// apply projection and model view matrix to vertex
gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex;
gl_FrontColor = gl_Color;
}
""" )
fragment_shader = textwrap.dedent( """
#version 120
void main(void)
{
// set colour of each fragment
gl_FragColor = gl_Color;
}
""" )
def __init__( self ):
super( LegacyCube, self ).__init__()
global vertices
self.use_shaders = True
# create our shader
self.shader = ShaderProgram(
VertexShader( self.vertex_shader ),
FragmentShader( self.fragment_shader )
)
# create our vertex buffer
self.buffer = VertexBuffer(
GL.GL_ARRAY_BUFFER,
GL.GL_STATIC_DRAW,
data = vertices,
)
self.buffer_attributes = BufferAttributes()
self.buffer_attributes[ 'position' ] = VertexAttribute.from_dtype(
self.buffer,
vertices.dtype,
'position'
)
self.buffer_attributes[ 'colour' ] = ColourAttribute.from_dtype(
self.buffer,
vertices.dtype,
'colour'
)
def draw( self ):
global vertices
if self.use_shaders:
self.shader.bind()
self.buffer_attributes.push_attributes()
# set the vertex pointer to the position data
self.buffer.bind()
self.buffer_attributes.set()
self.buffer.unbind()
GL.glDrawArrays( GL.GL_TRIANGLES, 0, len( vertices ) )
self.buffer_attributes.pop_attributes()
if self.use_shaders:
self.shader.unbind()