1
1
from my_engine .component import Component
2
- from my_engine .material import Material
2
+ from my_engine .material import Material , TYPE_TO_LENGTH
3
3
from my_engine .mesh import Mesh
4
4
from my_engine .camera import Camera
5
5
from typing import Tuple , Union
6
6
from collections import defaultdict
7
- from OpenGL .GL import glGenBuffers , glClearColor , glEnable , glBlendFunc , GL_DEPTH_TEST , GL_BLEND , GL_SRC_ALPHA , GL_ONE_MINUS_SRC_ALPHA
7
+ from OpenGL .GL import glGenBuffers , glBindBuffer , glBufferData , glClearColor , glEnable , glBlendFunc , GL_DEPTH_TEST , \
8
+ GL_BLEND , GL_SRC_ALPHA , GL_ONE_MINUS_SRC_ALPHA , GL_ARRAY_BUFFER , GL_ELEMENT_ARRAY_BUFFER , GL_STATIC_DRAW , \
9
+ glEnableVertexAttribArray , glVertexAttribPointer , GL_FALSE , GL_FLOAT , GL_SAMPLER_2D , glUniformMatrix4fv , glDrawElements , \
10
+ GL_TRIANGLES , GL_UNSIGNED_INT , glDrawRangeElements
11
+ import numpy as np
12
+ import ctypes
8
13
9
14
10
15
class Renderer (Component ):
11
16
def __init__ (self , obj , active_camera : Camera = None , enable : Tuple = (GL_DEPTH_TEST ,),
12
- blend_settings : Union [Tuple [ 2 ] , None ] = (GL_SRC_ALPHA , GL_ONE_MINUS_SRC_ALPHA ),
13
- clear_color : Tuple [ 4 ] = (0.1 , 0.1 , 0.1 , 1 )):
17
+ blend_settings : Union [Tuple , None ] = (GL_SRC_ALPHA , GL_ONE_MINUS_SRC_ALPHA ),
18
+ clear_color : Tuple = (0.1 , 0.1 , 0.1 , 1 )):
14
19
super ().__init__ ('Renderer' , obj )
15
20
self .__active_camera = active_camera
16
21
self .__enable = enable
@@ -23,6 +28,9 @@ def __init__(self, obj, active_camera: Camera=None, enable: Tuple = (GL_DEPTH_TE
23
28
# glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO)
24
29
# glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.nbytes, indices, GL_STATIC_DRAW)
25
30
self .__all_objects = {}
31
+ self .__data_for_render = {}
32
+ self .__vertices = np .array ([])
33
+ self .__indices = np .array ([])
26
34
27
35
@property
28
36
def active_camera (self ):
@@ -33,17 +41,15 @@ def active_camera(self, val):
33
41
self .__active_camera = val
34
42
35
43
def get_all_objects (self ):
36
- descendants = {child : child .matrix for child in self .__obj .children }
37
- unchecked_children = [child1 for child in self .__obj .children for child1 in child .children ]
44
+ descendants = {child : child .matrix for child in self .obj .children }
45
+ unchecked_children = [child1 for child in self .obj .children for child1 in child .children ]
38
46
while unchecked_children :
39
47
child = unchecked_children .pop ()
40
- descendants [child ] = descendants [child .parent ] * child .matrix
48
+ descendants [child ] = descendants [child .parent ] @ child .matrix
41
49
unchecked_children .extend (child .children )
42
50
return descendants
43
51
44
52
def start (self ):
45
- self .__VBO = glGenBuffers (1 )
46
- self .__EBO = glGenBuffers (1 )
47
53
glClearColor (* self .__clear_color )
48
54
for setting in self .__enable :
49
55
glEnable (setting )
@@ -53,9 +59,58 @@ def start(self):
53
59
54
60
self .__all_objects = [(key , val ) for key , val in self .get_all_objects ().items () if 'Material' in key .components and 'Mesh' in key .components ]
55
61
56
- shaders = defaultdict (list )
62
+ vertices = []
63
+ indices = []
57
64
65
+ shaders = defaultdict (list )
66
+ verts_len = 0
58
67
for obj in self .__all_objects :
59
68
material = obj [0 ].components ['Material' ]
60
69
mesh = obj [0 ].components ['Mesh' ]
61
- shaders [material ].append ((mesh .get_data_positioned_to_material (material ), mesh .indices , obj [1 ]))
70
+ vertices .extend (list (mesh .get_data_positioned_to_material (material )))
71
+ object_data = {'pointer' : len (indices ), 'length' : mesh .indices .size , 'matrix' : obj [1 ], 'object' : obj [0 ]}
72
+ indices .extend (list (mesh .indices .flatten () + verts_len ))
73
+ verts_len += len (mesh .vertices )
74
+ if 'Texture' in obj [0 ].components :
75
+ object_data ['texture' ] = obj [0 ].components ['Texture' ]
76
+ shaders [material ].append (object_data )
77
+
78
+ self .__data_for_render = shaders
79
+
80
+ self .__vertices = np .array (vertices , dtype = np .float32 )
81
+ self .__indices = np .array (indices , dtype = np .uint32 )
82
+
83
+ self .__VBO = glGenBuffers (1 )
84
+ self .__EBO = glGenBuffers (1 )
85
+
86
+ glBindBuffer (GL_ARRAY_BUFFER , self .__VBO )
87
+ glBufferData (GL_ARRAY_BUFFER , self .__vertices .nbytes , self .__vertices , GL_STATIC_DRAW )
88
+
89
+ glBindBuffer (GL_ELEMENT_ARRAY_BUFFER , self .__EBO )
90
+ glBufferData (GL_ELEMENT_ARRAY_BUFFER , self .__indices .nbytes , self .__indices , GL_STATIC_DRAW )
91
+
92
+ def update (self ):
93
+ for material , data in self .__data_for_render .items ():
94
+ num_of_items = sum (TYPE_TO_LENGTH [type_ ] for attrib , type_ in material .attributes_types .items ())
95
+ pointer = 0
96
+ for attrib , index in sorted (material .attributes .items (), key = lambda x : x [1 ]):
97
+ length = TYPE_TO_LENGTH [material .attributes_types [attrib ]]
98
+ glEnableVertexAttribArray (index )
99
+ glVertexAttribPointer (index , length , GL_FLOAT , GL_FALSE , self .__vertices .itemsize * num_of_items , ctypes .c_void_p (pointer * 4 ))
100
+ pointer += length
101
+ material .use_program ()
102
+ glUniformMatrix4fv (material .uniforms [material .view_matrix_name ], 1 , GL_FALSE , self .__active_camera .obj .inverse_matrix )
103
+ glUniformMatrix4fv (material .uniforms [material .projection_matrix_name ], 1 , GL_FALSE , self .__active_camera .projection )
104
+ for dat in data :
105
+ for uniform , index in material .uniforms .items ():
106
+ if material .uniforms_types [uniform ] == GL_SAMPLER_2D :
107
+ pass
108
+ elif material .model_matrix_name == uniform :
109
+ glUniformMatrix4fv (index , 1 , GL_FALSE , dat ['matrix' ])
110
+ elif material .view_matrix_name == uniform :
111
+ pass
112
+ elif material .projection_matrix_name == uniform :
113
+ pass
114
+ # glDrawElements(GL_TRIANGLES, dat['length'], GL_UNSIGNED_INT, ctypes.c_void_p(dat['pointer'] * 0))
115
+ glDrawElements (GL_TRIANGLES , len (self .__indices ), GL_UNSIGNED_INT , None )
116
+ # glDrawRangeElements(GL_TRIANGLES, len(self.__indices), GL_UNSIGNED_INT, None)
0 commit comments