-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffers.py
75 lines (55 loc) · 2.26 KB
/
buffers.py
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
from pyglet.gl import *
from ctypes import sizeof
class Buffer:
def __init__(self):
self.id = GLuint(0)
def get_id(self):
return self.id
class VertexBuffer(Buffer):
def __init__(self, data):
Buffer.__init__(self)
self.data_gl = (GLfloat * len(data))(*data)
self.__create_buffer()
def __create_buffer(self):
# generate the vert buffer
glGenBuffers(1, self.id)
# use the buffer
glBindBuffer(GL_ARRAY_BUFFER, self.id)
# allocate memory in the buffer and populate with data
glBufferData(GL_ARRAY_BUFFER, sizeof(self.data_gl), self.data_gl, GL_STATIC_DRAW)
# tell opengl how data is packed in buffer
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0)
# enable vertexattrib array at position 0 so shader can read it
glEnableVertexAttribArray(0)
# def set_attribute(self, handle, index_str, size, type, normalized, stride, pointer):
# index = glGetAttribLocation(handle, index_str)
# if index > -1:
# glEnableVertexAttribArray(index)
# glVertexAttribPointer(index, size, type, normalized, stride, pointer)
def set_attribute(self, index, size, type, normalized, stride, pointer):
glEnableVertexAttribArray(index)
glVertexAttribPointer(index, size, type, normalized, stride, pointer)
class IndexBuffer(Buffer):
def __init__(self, data):
Buffer.__init__(self)
self.data_gl = (GLuint * len(data))(*data)
self.__create_buffer()
def __create_buffer(self):
# generate the index buffer
glGenBuffers(1, self.id)
# use the buffer
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, self.id)
# allocate memory in the buffer and populate with data
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(self.data_gl), self.data_gl, GL_STATIC_DRAW)
class FrameBuffer(Buffer):
def __init__(self):
Buffer.__init__(self)
def __create_buffer(self):
# generate the frame buffer
glGenFramebuffers(1, self.id)
# use frame buffer
glBindFramebuffer(GL_FRAMEBUFFER, self.id)
def bind(self):
glBindFramebuffer(GL_FRAMEBUFFER, self.id)
def unbind(self):
glBindFramebuffer(GL_FRAMEBUFFER, self.id)