demos/lworeader.py
branchpyrit
changeset 59 64e456ab823d
parent 58 cfe98f5c0ae9
child 65 242839c6d27d
equal deleted inserted replaced
58:cfe98f5c0ae9 59:64e456ab823d
     1 # LightWave .lwo file loader
     1 # LightWave .lwo file loader
     2 
     2 
     3 from struct import *
     3 from struct import *
     4 from raytracer import Triangle, NormalVertex
     4 from raytracer import Triangle, NormalVertex, Material
     5 
     5 
     6 def read_int4(f):
     6 def read_int4(f):
     7 	return unpack('>i', f.read(4))[0]
     7 	return unpack('>i', f.read(4))[0]
     8 
     8 
     9 def read_int2(f):
     9 def read_int2(f):
   107 	(points, faces, tags, surfaces) = read_lwo(filename)
   107 	(points, faces, tags, surfaces) = read_lwo(filename)
   108 
   108 
   109 	vertices = []
   109 	vertices = []
   110 	normals = []
   110 	normals = []
   111 	vertex_face_num = []
   111 	vertex_face_num = []
       
   112 	materials = []
       
   113 
       
   114 	for surf in surfaces:
       
   115 		mat = Material(colour=tuple(float(x)/255. for x in surf['color']))
       
   116 		materials.append(mat)
   112 
   117 
   113 	for point in points:
   118 	for point in points:
   114 		v0 = scale[0]*point[0] + trans[0]
   119 		v0 = scale[0]*point[0] + trans[0]
   115 		v1 = scale[1]*point[1] + trans[1]
   120 		v1 = scale[1]*point[1] + trans[1]
   116 		v2 = scale[2]*point[2] + trans[2]
   121 		v2 = scale[2]*point[2] + trans[2]
   117 		vertices.append(NormalVertex((v2,v1,v0)))
   122 		vertices.append(NormalVertex((v2,v1,v0)))
   118 		normals.append([0.,0.,0.])
   123 		normals.append([0.,0.,0.])
   119 		vertex_face_num.append(0)
   124 		vertex_face_num.append(0)
   120 
   125 
   121 
       
   122 	for f in faces:
   126 	for f in faces:
   123 		v = [vertices[x] for x in f[0:3]]
   127 		v = [vertices[x] for x in f[0:3]]
   124 		surf = f[3]
   128 		matidx = f[3]-1
   125 		mat = defmat
   129 		face = Triangle(v[0], v[1], v[2], materials[matidx])
   126 		face = Triangle(v[0], v[1], v[2], mat)
       
   127 		n = face.getNormal()
   130 		n = face.getNormal()
   128 		for x in f[0:3]:
   131 		for x in f[0:3]:
   129 			for i in range(3):
   132 			for i in range(3):
   130 				normals[x][i] += n[i]
   133 				normals[x][i] += n[i]
   131 			vertex_face_num[x] += 1
   134 			vertex_face_num[x] += 1