forked from pchalas1/libWall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gl_model.py
147 lines (108 loc) · 3.89 KB
/
gl_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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env python
import numpy
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
import pyassimp
from pyassimp.postprocess import *
from pyassimp.helper import *
import random
from random import *
from gl_camera import *
import gl_utils
class Model(object):
"""@author : Preetham Chalasani, Ceasar Hernandez
@brief : Model Loader
"""
def __init__(self, parent = None):
super(Model, self).__init__()
self.__scene = None
self.__angle = 1.0
pass
def load_model(self,path):
"""Load model from the specified path
@param path : Location of the model in the directory
"""
self.__scene = pyassimp.load(path)
scene = self.__scene
for index, mesh in enumerate(scene.meshes):
self.__prepare_gl_buffers(mesh)
pyassimp.release(scene)
pass
def __prepare_gl_buffers(self,mesh):
"""Prepare buffer to store mesha values
@param mesh
"""
mesh.gl = {}
mesh.gl["vertices"] = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, mesh.gl["vertices"])
glBufferData(GL_ARRAY_BUFFER, mesh.vertices, GL_STATIC_DRAW)
mesh.gl["normals"] = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, mesh.gl["normals"])
glBufferData(GL_ARRAY_BUFFER, mesh.normals, GL_STATIC_DRAW)
mesh.gl["triangles"] = glGenBuffers(1)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh.gl["triangles"])
glBufferData(GL_ELEMENT_ARRAY_BUFFER, mesh.faces, GL_STATIC_DRAW)
glBindBuffer(GL_ARRAY_BUFFER,0)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0)
pass
def render(self, child = None):
"""Render the model from the buffer
"""
if child == None:
node = self.__scene.rootnode
else:
node = child
# glPushMatrix()
# m = node.transformation.transpose()
# glMultMatrixf(m)
for mesh in node.meshes:
self.__apply_material(mesh.material)
glBindBuffer(GL_ARRAY_BUFFER, mesh.gl["vertices"])
glEnableClientState(GL_VERTEX_ARRAY)
glVertexPointer(3, GL_FLOAT, 0, None)
glBindBuffer(GL_ARRAY_BUFFER, mesh.gl["normals"])
glEnableClientState(GL_NORMAL_ARRAY)
glNormalPointer(GL_FLOAT, 0, None)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh.gl["triangles"])
glDrawElements(GL_TRIANGLES,len(mesh.faces) * 3, GL_UNSIGNED_INT, None)
glDisableClientState(GL_VERTEX_ARRAY)
glDisableClientState(GL_NORMAL_ARRAY)
glBindBuffer(GL_ARRAY_BUFFER, 0)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0)
for child in node.children:
self.render(child)
# glPopMatrix()
pass
def __apply_material(self, mat):
"""Apply material properties the model
@param mat : mesh.material
"""
if not hasattr(mat, "gl_mat"):
diffuse = numpy.array(mat.properties.get("diffuse", [0.8, 0.8, 0.8, 1.0]))
specular = numpy.array(mat.properties.get("specular", [0., 0., 0., 1.0]))
ambient = numpy.array(mat.properties.get("ambient", [0.6, 0.6, 0.6, 1.0]))
emissive = numpy.array(mat.properties.get("emissive", [0., 0., 0., 1.0]))
shininess = min(mat.properties.get("shininess", 1.0), 128)
wireframe = mat.properties.get("wireframe", 0)
twosided = mat.properties.get("twosided", 0)
setattr(mat, "gl_mat", glGenLists(1))
glNewList(mat.gl_mat, GL_COMPILE)
# glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse)
# glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular)
# glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ambient)
# glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, emissive)
# glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess)
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, [ 1.0, 1.0, 1.0, 1.0 ])
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 30)
glLightfv(GL_LIGHT0, GL_AMBIENT, [0.0, 0.0, 0.0, -1.0] )
glLightfv(GL_LIGHT0, GL_POSITION, [ 1.0, 1.0, -1.0, 0.0 ])
glEnable(GL_LIGHTING)
glEnable(GL_LIGHT0)
glEnable(GL_DEPTH_TEST)
glEnable(GL_CULL_FACE)
# glPolygonMode(GL_FRONT_AND_BACK, GL_LINE if wireframe else GL_FILL)
# glDisable(GL_CULL_FACE) if twosided else glEnable(GL_CULL_FACE)
glEndList()
glCallList(mat.gl_mat)
pass