-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject_handling.py
170 lines (144 loc) · 8.04 KB
/
object_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
162
163
164
165
166
167
168
169
from OpenGL.GL import *
import numpy
import context
class TronObject:
def __init__(self, structure_id):
self.hided = 0
self.position = None
self.structure = None
self.structure_id = structure_id
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)
self.instance_vbo = glGenBuffers(1)
self.rotation_vbo = glGenBuffers(1)
self.resize_vbo = glGenBuffers(1)
self.update_buffers()
context.main_context.objects.append(self)
def describe_buffers(self):
glBindBuffer(GL_ARRAY_BUFFER, self.instance_vbo)
# instance - 6
glVertexAttribPointer(6, 3, GL_FLOAT, GL_FALSE, 0, ctypes.c_void_p(0))
glEnableVertexAttribArray(6)
glVertexAttribDivisor(6, 1)
glBindBuffer(GL_ARRAY_BUFFER, self.rotation_vbo)
# rotation - 7
glVertexAttribPointer(7, 3, GL_FLOAT, GL_FALSE, 0, ctypes.c_void_p(0))
glEnableVertexAttribArray(7)
glVertexAttribDivisor(7, 1)
glBindBuffer(GL_ARRAY_BUFFER, self.resize_vbo)
# resize - 8
glVertexAttribPointer(8, 1, GL_FLOAT, GL_FALSE, 0, ctypes.c_void_p(0))
glEnableVertexAttribArray(8)
glVertexAttribDivisor(8, 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
def shade_draw(self):
struct = context.main_context.structures[self.structure_id]
self.update_buffers()
for i in struct.subobjects:
i.update_buffers()
for j in i.parts:
if context.main_context.materials[j.material_id].texture_id is not None:
glBindVertexArray(j.vao)
i.describe_buffers()
self.describe_buffers()
count_objects = 1
glDrawArraysInstanced(GL_TRIANGLES, 0, len(j.points), count_objects)
def real_draw(self):
cam = context.main_context.cameras[context.main_context.current_camera]
context.main_context.shader_texture.bind()
uniform = glGetUniformLocation(context.main_context.shader_texture.get_shader(), "num_active_lights")
glUniform1i(uniform, context.main_context.num_active_lights)
count_added = 0
for i in range(len(context.main_context.lights)):
if context.main_context.lights[i].hided == 0:
context.main_context.lights[i].set_shader_uniforms(context.main_context.shader_texture, count_added, 1)
count_added += 1
glUniformMatrix4fv(context.main_context.shader_texture.view_uniform_location, 1, GL_FALSE, cam.camera_view_matrix)
glUniformMatrix4fv(context.main_context.shader_texture.projection_uniform_location, 1, GL_FALSE,
cam.camera_projection_matrix)
glUniform1i(glGetUniformLocation(context.main_context.shader_texture.get_shader(), "tex_sampler"), 0)
count_added = 0
for k in range(len(context.main_context.lights)):
if context.main_context.lights[k].hided == 0:
glUniform1i(glGetUniformLocation(context.main_context.shader_texture.get_shader(),
"shadowMap[" + str(count_added) + "]"), count_added + 1)
count_added += 1
textures_array = []
for i in context.main_context.lights:
if i.hided == 0:
textures_array.append(i.depth_map)
glBindTextures(1, context.main_context.num_active_lights, textures_array)
struct = context.main_context.structures[self.structure_id]
self.update_buffers()
for i in struct.subobjects:
i.update_buffers()
for j in i.parts:
if context.main_context.materials[j.material_id].texture_id is not None:
context.main_context.shader_texture.bind()
glActiveTexture(GL_TEXTURE0)
context.main_context.textures[context.main_context.materials[j.material_id].texture_id].bind()
glBindVertexArray(j.vao)
# TODO: do we need this as part buffers, or we can make them subobject buffers?
i.describe_buffers()
self.describe_buffers()
count_objects = 1
glDrawArraysInstanced(GL_TRIANGLES, 0, len(j.points), count_objects)
context.main_context.shader_common.bind()
uniform = glGetUniformLocation(context.main_context.shader_common.get_shader(), "num_active_lights")
glUniform1i(uniform, context.main_context.num_active_lights)
count_added = 0
for i in range(len(context.main_context.lights)):
if context.main_context.lights[i].hided == 0:
context.main_context.lights[i].set_shader_uniforms(context.main_context.shader_common, count_added, 0)
count_added += 1
glUniformMatrix4fv(context.main_context.shader_common.view_uniform_location, 1, GL_FALSE, cam.camera_view_matrix)
glUniformMatrix4fv(context.main_context.shader_common.projection_uniform_location, 1, GL_FALSE,
cam.camera_projection_matrix)
count_added = 0
for k in range(len(context.main_context.lights)):
if context.main_context.lights[k].hided == 0:
glUniform1i(glGetUniformLocation(context.main_context.shader_common.get_shader(),
"shadowMap[" + str(count_added) + "]"), count_added)
count_added += 1
textures_array = []
for i in context.main_context.lights:
if i.hided == 0:
textures_array.append(i.depth_map)
glBindTextures(0, context.main_context.num_active_lights, textures_array)
glUniformMatrix4fv(context.main_context.shader_common.view_uniform_location, 1, GL_FALSE, cam.camera_view_matrix)
glUniformMatrix4fv(context.main_context.shader_common.projection_uniform_location, 1, GL_FALSE,
cam.camera_projection_matrix)
self.update_buffers()
for i in struct.subobjects:
i.update_buffers()
for j in i.parts:
if context.main_context.materials[j.material_id].texture_id is None:
context.main_context.shader_common.bind()
glBindVertexArray(j.vao)
i.describe_buffers()
self.describe_buffers()
count_objects = 1
glDrawArraysInstanced(GL_TRIANGLES, 0, len(j.points), count_objects)
def draw(self, rotation_array, instance_array, resize_array):
self.rotation_array = rotation_array
self.instance_array = instance_array
self.resize_array = resize_array