-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModelTank.py
executable file
·106 lines (83 loc) · 3.65 KB
/
ModelTank.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
"""
Model tank component, represented as a GL_WIREFRAME object
Daniel Scrivener 08/2022
"""
from Displayable import Displayable
from Component import Component
from GLBuffer import VAO, VBO, lineEBO
import numpy as np
import ColorType
from collada import *
class Tank(Component):
def __init__(self, position, shaderProg, scale):
self.species_id = -1
self.default_color = ColorType.PINK
self.mesh = DisplayableTank(shaderProg, scale, color=ColorType.PINK)
super(Tank, self).__init__(position, self.mesh)
class DisplayableTank(Displayable):
vao = None
vbo = None
ebo = None
shaderProg = None
vertices = None # array to store vertices information
indices = None # stores triangle indices to vertices
vCenter = None # the "center" of the mesh, approximated as the average vertex
defaultColor = None
def __init__(self, shaderProg, scale, color=ColorType.PINK):
super(DisplayableTank, self).__init__()
assert(len(scale) == 3)
self.defaultColor = np.array(color.getRGB())
self.shaderProg = shaderProg
self.shaderProg.use()
self.vao = VAO()
self.vbo = VBO() # vbo can only be initiate with glProgram activated
self.ebo = lineEBO()
# construct vertex list
self.vertices = np.array([
-scale[0] / 2, -scale[1] / 2, -scale[2] / 2, 0, 0, 0, *self.defaultColor, 0, 0,
-scale[0] / 2, -scale[1] / 2, scale[2] / 2, 0, 0, 0, *self.defaultColor, 0, 0,
-scale[0] / 2, scale[1] / 2, -scale[2] / 2, 0, 0, 0, *self.defaultColor, 0, 0,
-scale[0] / 2, scale[1] / 2, scale[2] / 2, 0, 0, 0, *self.defaultColor, 0, 0,
scale[0] / 2, -scale[1] / 2, -scale[2] / 2, 0, 0, 0, *self.defaultColor, 0, 0,
scale[0] / 2, -scale[1] / 2, scale[2] / 2, 0, 0, 0, *self.defaultColor, 0, 0,
scale[0] / 2, scale[1] / 2, -scale[2] / 2, 0, 0, 0, *self.defaultColor, 0, 0,
scale[0] / 2, scale[1] / 2, scale[2] / 2, 0, 0, 0, *self.defaultColor, 0, 0,
])
# construct indices
self.indices = np.array([
0, 1,
0, 2,
0, 4,
1, 5,
1, 3,
2, 3,
2, 6,
3, 7,
4, 5,
4, 6,
5, 7,
6, 7
])
# self.vertices should be an n x 11 array, where n is the number of mesh vertices
# self.indices should be a flat array of length n*2
def draw(self):
self.vao.bind()
self.ebo.draw()
self.vao.unbind()
def initialize(self):
"""
Remember to bind VAO before this initialization. If VAO is not bind, program might throw an error
in systems that don't enable a default VAO after GLProgram compilation
"""
self.vao.bind()
self.vbo.setBuffer(self.vertices, 11)
self.ebo.setBuffer(self.indices)
self.vbo.setAttribPointer(self.shaderProg.getAttribLocation("vertexPos"),
stride=11, offset=0, attribSize=3)
self.vbo.setAttribPointer(self.shaderProg.getAttribLocation("vertexNormal"),
stride=11, offset=3, attribSize=3)
self.vbo.setAttribPointer(self.shaderProg.getAttribLocation("vertexColor"),
stride=11, offset=6, attribSize=3)
self.vbo.setAttribPointer(self.shaderProg.getAttribLocation("vertexTexture"),
stride=11, offset=9, attribSize=2)
self.vao.unbind()