3
from objloader import *
5
from OpenGL.GL import *
9
def __init__(self,filename="",groupname = None,model = None,textureFileName = None) :
11
This holds all informations relative to a 3D (lightwave) model. Developers can use an object of this class to represent immobile 3D models. For mobile models use MobileModel instead. To create a composite model, specify it's filename. For the components, specify the groupname and the model's model (model). model is specified for a construction by copy.
15
raise 'No model model specified for the group',groupname
17
print 'creation of model ',groupname
19
self.groups = {groupname : model.groups[groupname]}
20
del model.groups[groupname]
21
model.buildModelList()
23
self.min = self.groups[groupname][MINIMA]
24
self.max = self.groups[groupname][MAXIMA]
25
self.center = self.groups[groupname][CENTER]
26
self.boundingbox = [self.min,self.max]
27
self.modelList = glGenLists(1)
31
print 'creation of model', filename
32
self.filename = filename
33
self.materialFileName = ''
34
self.textureFileName = textureFileName
35
self.materialFile = ''
46
self.materials = {} # dictionary
47
self.groups = {} # dictionary
49
self.min = [INFINITY,INFINITY,INFINITY]
50
self.max = [-INFINITY,-INFINITY,-INFINITY]
51
self.loader = ObjLoader(model=self)
52
self.boundingBox = [self.min,self.max]
53
self.boundingBoxShowed = False
54
self.drawing = Drawing()
55
self.modelList = glGenLists(1)
58
if self.modelList == 0 :
59
print 'Error in MobileModel constructor, self.modelList is', self.modelList, '. returning.'
65
for i,j in model.__dict__.items() :
68
def buildModelList(self):
69
glNewList(self.modelList,GL_COMPILE)
70
if self.textureFileName :
71
glEnable(GL_TEXTURE_2D)
72
self.drawing.useTexture(self.textureFileName)
74
for g in self.groups.values():
75
if not self.textureFileName :
77
self.useMaterial(g[MATERIAL])
80
print "Warning : a group has no material in ", self.model.filename,"!"
82
#FACES is an enumeration. It's value is 1
84
if len(f) == 3 * self.nb_elements :
86
elif len(f) == 4 * self.nb_elements :
90
for i in range(0,len(f),self.nb_elements):
91
#Keep in mind that first line is line 1, not 0
92
j = i + self.nb_elements - 1
94
glNormal(self.normals[f[j]-1][X],
95
self.normals[f[j]-1][Y],
96
self.normals[f[j]-1][Z])
97
glVertex(self.vertices[f[i]-1][X],
98
self.vertices[f[i]-1][Y],
99
self.vertices[f[i]-1][Z])
100
if self.textureFileName and self.nb_elements == 3 :
101
## print self.nb_elements
104
## print self.textures[f[k] - 1]
106
glTexCoord(self.textures[f[k]-1][X],
107
self.textures[f[k]-1][Y])
108
##self.textures[f[k]-1][Z])
112
if self.textureFileName :
113
glDisable(GL_TEXTURE_2D)
116
def useMaterial(self,materialName):
117
for param in [GL_AMBIENT,GL_SPECULAR,GL_DIFFUSE,GL_SHININESS]:
118
try: # sometimes not all values are defined, so put this line in a try block
119
glMaterialfv(GL_FRONT,param,self.materials[materialName][param])
125
glCallList(self.modelList)
126
if(self.boundingBoxShowed):
127
self.drawBoundingBox()
130
def buildBoundingBoxList(self):
131
self.boundingBoxList = glGenLists(1)
132
glNewList(self.boundingBoxList,GL_COMPILE)
133
glDisable(GL_LIGHTING)
135
glTranslate(self.center[X],self.center[Y],self.center[Z])
136
glScale(self.max[X]-self.min[X],self.max[Y]-self.min[Y],self.max[Z]-self.min[Z])
139
glEnable(GL_LIGHTING)
142
def drawBoundingBox(self):
144
glCallList(self.boundingBoxList)
147
def showBoundingBox(self,showing):
149
self.boundingBoxShowed = True
150
self.buildBoundingBoxList()
152
self.boundingBoxShowed= False
153
glDeleteLists(self.boundingBoxList,1)
155
def loader_test(filename='',groupname='') :
156
if (filename == '' or groupname == ''):
157
print "usage : loader_test (filename,groupname)"
160
model = Model(filename)
161
for name,g in model.groups.items() :
162
#print 'group ', name
164
#print 'faces are', f
165
for i in range(0,len(f),model.nb_elements):
166
#Keep in mind that first line is line 1, not 0
167
#print 'i is',i,' and len(f) is', len(f)
168
#print 'len(normals) is',len(l.normals),'and f[i+1]-1 is ', f[i+1] - 1,'sans le -1',f[i+1]
169
#print 'normals are',l.normals[f[i+2]-1][X],l.normals[f[i+2]-1][Y],l.normals[f[i+2]-1][Z]
170
#print 'vertices are',l.vertices[f[i]-1][X],l.vertices[f[i]-1][Y],l.vertices[f[i]-1][Z]
172
if not model.groups.has_key(groupname) :
173
print "error, no group named", groupname, "in model. Returning."
175
model.computeBoundingBox(groupname)
176
print 'min du ',groupname,' est', model.groups[groupname][MINIMA]
177
print 'max du ',groupname,' est', model.groups[groupname][MAXIMA]
178
print 'centre du ',groupname,' est', model.groups[groupname][CENTER]
179
print 'min du ', filename.split(".")[0],model.min
180
print 'max du ',filename.split(".")[0] ,model.max
181
print 'centre du ',filename.split(".")[0],model.center
182
#####################
183
##loader_test("tank.obj",'Canon')
186
model1 = Model(filename = 'jeep.txt')
187
model2 = Model(filename = 'tank.obj')
189
print ObjLoader.singleton