Next, we create two helper classes to allocate and structure memory to hold the data to be used by the shaders. The shader code we will write will replace all the OpenGL drawing code we've written to date. In some ways, the shader code will also simplify the OpenGL drawing code, but to work, the shaders still need to know about vertices, colors, projection and view matrices, and other data. The first class we create will be in a script called GraphicsData. py. Create this file and add the following code to it:
from OpenGL.GL import *
import numpy as np
class GraphicsData():
def_init_(self, data_type, data):
self.data_type = data_type
self.data $=$ data
self.buffer_ref = glGenBuffers(1)
self.load()
def load(self) :
data $=\mathrm{np} \cdot \operatorname{array}($ self.data, np.float32)
glBindBuffer(GL_ARRAY_BUFFER, self.buffer_ref)
glBufferData (GL_ARRAY_BUFFER, data.ravel (),
GL_STATIC_DRAW)
def create_variable(self, program_id,
variable_name) :
variable_id = glGetAttribLocation(program_id,
variable_name)
glBindBuffer(GL_ARRAY_BUFFER, self.buffer_ref)
if self.data_type $==$ "vec3":
glVertexAttribPointer(variable_id, 3,
GL_FLOAT,
False, 0, None)
elif self.data_type == "vec2" :
glVertexAttribPointer(variable_id, 2,
GL_FLOAT,
False, 0, None)
glEnableVertexAttribArray (variable_id)