-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDisplayableMesh.py
123 lines (96 loc) · 3.91 KB
/
DisplayableMesh.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
"""
Implements the Displayable class by providing import functions for .dae meshes
:author: micou(Zezhou Sun)
:version: 2021.1.1
Modified by Daniel Scrivener 07/22
"""
from random import random
from typing import Union, List, Tuple
from GLProgram import GLProgram
from Point import Point
from Displayable import Displayable
from GLBuffer import VAO, VBO, EBO
import numpy as np
import ColorType
from collada import *
try:
import OpenGL
try:
import OpenGL.GL as gl
import OpenGL.GLU as glu
except ImportError:
from ctypes import util
orig_util_find_library = util.find_library
def new_util_find_library(name):
res = orig_util_find_library(name)
if res:
return res
return '/System/Library/Frameworks/' + name + '.framework/' + name
util.find_library = new_util_find_library
import OpenGL.GL as gl
import OpenGL.GLU as glu
except ImportError:
raise ImportError("Required dependency PyOpenGL not present")
class DisplayableMesh(Displayable):
vao = None
vbo = None
ebo = None
shaderProg = None
vertices = None # array to store vertex information
indices = None # stores triangle indices to vertices
defaultColor = None
def __init__(self,
shaderProg: GLProgram,
scale: Union[List[float], Tuple[float, float, float], np.ndarray],
vertexData,
indexData,
color: ColorType.ColorType = ColorType.BLUE):
"""
:param shaderProg: compiled shader program
:type shaderProg: GLProgram
:param scale: set of three scale factors to be applied to each vertex
:type scale: list or tuple
:param filename: .dae file to import
:type filename: string
:param color: vertex color to be applied uniformly
:type color: ColorType
"""
super(DisplayableMesh, 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 = EBO()
self.indices = indexData
self.vertices = vertexData
for i in range(len(self.vertices) // 11):
i = i * 11
self.vertices[i] = self.vertices[i] * scale[0]
self.vertices[i + 1] = self.vertices[i + 1] * scale[1]
self.vertices[i + 2] = self.vertices[i + 2] * scale[2]
self.vertices[i + 5] = self.defaultColor[0]
self.vertices[i + 6] = self.defaultColor[1]
self.vertices[i + 7] = self.defaultColor[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()