-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstructure_handling.py
161 lines (135 loc) · 6.11 KB
/
structure_handling.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
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
from OpenGL.GL import *
import numpy
import context
from PIL import Image
class TronMaterial:
def __init__(self):
self.id = len(context.main_context.materials)
self.name = ""
self.ns = 0
self.kd = []
self.ka = []
self.ks = []
self.ni = 0
self.d = 0
self.illum = 0
self.map_kd = ""
self.map_ks = ""
self.map_ka = ""
self.map_bump = ""
self.map_d = ""
self.texture_id = None
class TronTexture:
def __init__(self):
self.id = len(context.main_context.textures)
self.opengl_id = glGenTextures(1)
self.name = None
def load(self, file_location):
self.name = file_location
glBindTexture(GL_TEXTURE_2D, self.opengl_id)
# Set the texture wrapping parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
# Set texture filtering parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
# load image
image = Image.open(file_location)
# TODO: speed up this line:
img_data = numpy.array(list(image.getdata()), numpy.uint8)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image.width, image.height, 0, GL_RGB, GL_UNSIGNED_BYTE, img_data)
glBindTexture(GL_TEXTURE_2D, 0)
return self.id
def bind(self):
glBindTexture(GL_TEXTURE_2D, self.opengl_id)
class TronPart:
def __init__(self):
self.material_id = None
self.points = []
self.vao = glGenVertexArrays(1)
self.points_vbo = glGenBuffers(1)
def fill_buffers(self):
glBindVertexArray(self.vao)
glBindBuffer(GL_ARRAY_BUFFER, self.points_vbo)
if context.main_context.materials[self.material_id].texture_id is not None:
glBufferData(GL_ARRAY_BUFFER,
self.points.itemsize * len(self.points),
self.points, GL_STATIC_DRAW)
# position - 0
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, self.points.itemsize * 8,
ctypes.c_void_p(0))
glEnableVertexAttribArray(0)
# textures - 1
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, self.points.itemsize * 8,
ctypes.c_void_p(3 * 4))
glEnableVertexAttribArray(1)
# normals - 2
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, self.points.itemsize * 8,
ctypes.c_void_p((3 + 2) * 4))
glEnableVertexAttribArray(2)
else:
glBufferData(GL_ARRAY_BUFFER,
self.points.itemsize * len(self.points),
self.points, GL_STATIC_DRAW)
# position - 0
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, self.points.itemsize * 10,
ctypes.c_void_p(0))
glEnableVertexAttribArray(0)
# color - 1
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, self.points.itemsize * 10,
ctypes.c_void_p(3 * 4))
glEnableVertexAttribArray(1)
# normals - 2
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, self.points.itemsize * 10,
ctypes.c_void_p((3 + 4) * 4))
glEnableVertexAttribArray(2)
class TronSubobject:
def __init__(self):
self.parts = []
self.name = None
self.count_parts = 0
self.instance_vbo = glGenBuffers(1)
self.rotation_vbo = glGenBuffers(1)
self.resize_vbo = glGenBuffers(1)
self.rotation_array = numpy.array([0, 0, 0], numpy.float32)
self.instance_array = numpy.array([0, 0, 0], numpy.float32)
self.resize_array = numpy.array([1], numpy.float32)
self.rotation_array_previous = numpy.array([0, 0, 0], numpy.float32)
self.instance_array_previous = numpy.array([0, 0, 0], numpy.float32)
self.resize_array_previous = numpy.array([0], numpy.float32)
def describe_buffers(self):
glBindBuffer(GL_ARRAY_BUFFER, self.instance_vbo)
# instance - 3
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, 0, ctypes.c_void_p(0))
glEnableVertexAttribArray(3)
glVertexAttribDivisor(3, 1)
glBindBuffer(GL_ARRAY_BUFFER, self.rotation_vbo)
# rotation - 4
glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, 0, ctypes.c_void_p(0))
glEnableVertexAttribArray(4)
glVertexAttribDivisor(4, 1)
glBindBuffer(GL_ARRAY_BUFFER, self.resize_vbo)
# resize - 5
glVertexAttribPointer(5, 1, GL_FLOAT, GL_FALSE, 0, ctypes.c_void_p(0))
glEnableVertexAttribArray(5)
glVertexAttribDivisor(5, 1)
def update_buffers(self):
if not numpy.array_equal(self.rotation_array, self.rotation_array_previous) or \
not numpy.array_equal(self.instance_array, self.instance_array_previous) or \
not numpy.array_equal(self.resize_array, self.resize_array_previous):
glBindBuffer(GL_ARRAY_BUFFER, self.rotation_vbo)
glBufferData(GL_ARRAY_BUFFER, self.rotation_array.itemsize * len(self.rotation_array),
self.rotation_array, GL_DYNAMIC_DRAW)
glBindBuffer(GL_ARRAY_BUFFER, self.instance_vbo)
glBufferData(GL_ARRAY_BUFFER, self.instance_array.itemsize * len(self.instance_array),
self.instance_array, GL_DYNAMIC_DRAW)
glBindBuffer(GL_ARRAY_BUFFER, self.resize_vbo)
glBufferData(GL_ARRAY_BUFFER, self.resize_array.itemsize * len(self.resize_array),
self.resize_array, GL_DYNAMIC_DRAW)
self.rotation_array_previous = self.rotation_array
self.instance_array_previous = self.instance_array
self.resize_array_previous = self.resize_array
class TronStructure:
def __init__(self):
self.id = len(context.main_context.structures)
self.subobjects = []