-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
124 lines (91 loc) · 4.72 KB
/
model.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
import pygame as pg
import numpy as np
import glm
import random
import os
shadersDirectory = "shaders"
class Cube :
def __init__(self, app, pos=(0,0,0), rot=(0,0,0), textures=["test.png","test.png","test.png","test.png","test.png","test.png"]) -> None:
self.faces = []
self.faces.append({"object": Face(app, pos=(pos[0]+0.5, pos[1]+0, pos[2]+0), rot=(0,90,0), brightness=1.0, texture=textures[0]), "visible": True})# +x
self.faces.append({"object": Face(app, pos=(pos[0]-0.5, pos[1]+0, pos[2]+0), rot=(0,-90,0), brightness=0.75, texture=textures[1]), "visible": True})# -x
self.faces.append({"object": Face(app, pos=(pos[0]+0, pos[1]+0.5, pos[2]+0), rot=(-90,0,0), brightness=1.0, texture=textures[2]), "visible": True})# +y
self.faces.append({"object": Face(app, pos=(pos[0]+0, pos[1]-0.5, pos[2]+0), rot=(90,0,0), brightness=0.75, texture=textures[3]), "visible": True})# -y
self.faces.append({"object": Face(app, pos=(pos[0]+0, pos[1]+0, pos[2]+0.5), rot=(0,0,0), brightness=0.75, texture=textures[4]), "visible": True})# +z
self.faces.append({"object": Face(app, pos=(pos[0]+0, pos[1]+0, pos[2]-0.5), rot=(0,180,0), brightness=1.0, texture=textures[5]), "visible": True})# -z
def render(self) :
for face in self.faces :
if face["visible"] :
face["object"].render()
def destroy(self) :
for face in self.faces :
face["object"].destroy()
class Billboard :
def __init__(self, app, pos=(0,0,0), rot=(0,0,0), textures=["test.png","test.png"]) -> None:
self.faces = []
self.faces.append({"object": Face(app, pos=(pos[0], pos[1], pos[2]), rot=(0,45,0), texture=textures[0]), "visible": True})
self.faces.append({"object": Face(app, pos=(pos[0], pos[1], pos[2]), rot=(0,-45,0), texture=textures[1]), "visible": True})
self.faces.append({"object": Face(app, pos=(pos[0], pos[1], pos[2]), rot=(0,225,0), texture=textures[1]), "visible": True})
self.faces.append({"object": Face(app, pos=(pos[0], pos[1], pos[2]), rot=(0,-225,0), texture=textures[1]), "visible": True})
def render(self) :
for face in self.faces :
if face["visible"] :
face["object"].render()
def destroy(self) :
for face in self.faces :
face["object"].destroy()
class Face :
def __init__(self, app, pos=(0,0,0), rot=(0,0,0), brightness=1, texture="test.png") -> None:
self.app = app
self.ctx = app.ctx
self.textureMan = app.textureMan
self.vbo = self.getVbo()
self.shaderProgram = app.shaderMan.getShaderProgram("default", texture)
self.vao = self.getVao()
self.pos = pos
self.setRotation(rot)
self.textureID = texture
self.brightness = glm.c_float(brightness)
self.modelM = self.getModelMatrix()
def update(self) :
self.modelM = self.getModelMatrix()
self.shaderProgram['m_model'].write(self.modelM)
self.shaderProgram['m_view'].write(self.app.camera.viewM)
self.shaderProgram['in_brightness'].write(self.brightness)
def getModelMatrix(self) :
modelMatrix = glm.mat4()
#Translate
modelMatrix = glm.translate(modelMatrix, self.pos)
#Rotate
modelMatrix = glm.rotate(modelMatrix, self.rot.x, glm.vec3(1, 0, 0))
modelMatrix = glm.rotate(modelMatrix, self.rot.y, glm.vec3(0, 1, 0))
modelMatrix = glm.rotate(modelMatrix, self.rot.z, glm.vec3(0, 0, 1))
return modelMatrix
def render(self) :
self.update()
self.vao.render()
def destroy(self) :
self.vbo.release()
self.vao.release()
def getVao(self) :
vao = self.ctx.vertex_array(self.shaderProgram, [(self.vbo, '2f 3f', 'in_texcoord_0', 'in_position')])
return vao
def getVertexData(self) :
verticies = [ (-0.5, -0.5, 0), (0.5, -0.5, 0), (0.5, 0.5, 0), (-0.5, 0.5, 0) ]
indices = [ (0, 1, 2), (2, 3, 0) ]
vertexData = self.get_data(verticies, indices)
textureCoords = [ (0, 0), (1, 0), (1, 1), (0, 1) ]
textureCoordsIndices = [ (0, 1, 2), (2, 3, 0) ]
textureCoordsData = self.get_data(textureCoords, textureCoordsIndices)
vertexData = np.hstack([textureCoordsData, vertexData])
return vertexData
@staticmethod
def get_data(verticies, indices) :
data = [verticies[ind] for triangle in indices for ind in triangle]
return np.array(data, dtype='f4')
def getVbo(self) :
vertexData = self.getVertexData()
vbo = self.ctx.buffer(vertexData)
return vbo
def setRotation(self, rot) :
self.rot = glm.vec3([glm.radians(a) for a in rot])