author | Radek Brich <radek.brich@devl.cz> |
Tue, 08 Apr 2008 01:05:12 +0200 | |
branch | pyrit |
changeset 60 | a23b5089b9c3 |
parent 59 | 64e456ab823d |
child 65 | 242839c6d27d |
permissions | -rw-r--r-- |
58 | 1 |
# LightWave .lwo file loader |
2 |
||
3 |
from struct import * |
|
59
64e456ab823d
add color support to lwo reader
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
4 |
from raytracer import Triangle, NormalVertex, Material |
58 | 5 |
|
6 |
def read_int4(f): |
|
7 |
return unpack('>i', f.read(4))[0] |
|
8 |
||
9 |
def read_int2(f): |
|
10 |
return unpack('>h', f.read(2))[0] |
|
11 |
||
12 |
def read_float4(f): |
|
13 |
return unpack('>f', f.read(4))[0] |
|
14 |
||
15 |
def read_string(f): |
|
16 |
res = '' |
|
17 |
b = f.read(1) |
|
18 |
l = 1 |
|
19 |
while ( b != '\0' ): |
|
20 |
res += b |
|
21 |
b = f.read(1) |
|
22 |
l += 1 |
|
23 |
if (l % 2 != 0): |
|
24 |
f.read(1) |
|
25 |
l += 1 |
|
26 |
return (res,l) |
|
27 |
||
28 |
def read_chunk(f): |
|
29 |
ID = f.read(4) |
|
30 |
if (ID == ''): |
|
31 |
return ('',0) |
|
32 |
size = read_int4(f) |
|
33 |
return (ID, size) |
|
34 |
||
35 |
def read_subchunk(f): |
|
36 |
ID = f.read(4) |
|
37 |
size = read_int2(f) |
|
38 |
return (ID, size) |
|
39 |
||
40 |
def read_lwo(filename): |
|
41 |
points = [] |
|
42 |
faces = [] |
|
43 |
tags = [] |
|
44 |
surfaces = [] |
|
45 |
f = file(filename) |
|
46 |
(ID,size) = read_chunk(f) |
|
47 |
form = f.read(4) |
|
48 |
if (ID != 'FORM' or form != 'LWOB'): |
|
49 |
print 'not lwo file' |
|
50 |
return |
|
51 |
(ID,size) = read_chunk(f) |
|
52 |
while (ID != ''): |
|
53 |
#print ID,size |
|
54 |
if (ID == 'PNTS'): |
|
55 |
while (size > 0): |
|
56 |
p1 = read_float4(f) |
|
57 |
p2 = read_float4(f) |
|
58 |
p3 = read_float4(f) |
|
59 |
points.append((p1,p2,p3)) |
|
60 |
size -= 12 |
|
61 |
elif (ID == 'SRFS'): |
|
62 |
while (size > 0): |
|
63 |
(s,l) = read_string(f) |
|
64 |
size -= l |
|
65 |
tags.append(s) |
|
66 |
elif (ID == 'POLS'): |
|
67 |
while (size > 0): |
|
68 |
vertex_count = read_int2(f) |
|
69 |
size -= 2 |
|
70 |
inds = [] |
|
71 |
for i in range(vertex_count): |
|
72 |
index = read_int2(f) |
|
73 |
inds.append(index) |
|
74 |
size -= 2 |
|
75 |
surf = read_int2(f) |
|
76 |
size -= 2 |
|
77 |
faces.append((inds[0], inds[1], inds[2], surf)) |
|
78 |
i = 0 |
|
79 |
while (vertex_count > 3): |
|
80 |
faces.append((inds[0], inds[2+i], inds[3+i], surf)) |
|
81 |
vertex_count -= 1 |
|
82 |
i += 1 |
|
83 |
elif (ID == 'SURF'): |
|
84 |
(name,l) = read_string(f) |
|
85 |
size -= l |
|
86 |
surf = {} |
|
87 |
while (size > 0): |
|
88 |
(subID,subsize) = read_subchunk(f) |
|
89 |
size -= 6 |
|
90 |
if (subID == 'COLR'): |
|
91 |
col = f.read(subsize) |
|
92 |
surf['color'] = (unpack('BBB',col[:3])) |
|
93 |
else: |
|
94 |
print "Warning:", subID,"("+str(subsize),"B) ignored" |
|
95 |
f.read(subsize) |
|
96 |
size -= subsize |
|
97 |
surfaces.append(surf) |
|
98 |
else: |
|
99 |
print "Warning:", ID,"("+str(size),"B) ignored" |
|
100 |
f.read(size) |
|
101 |
(ID,size) = read_chunk(f) |
|
102 |
return (points, faces, tags, surfaces) |
|
103 |
||
104 |
def LoadLightwaveLwoFile(rt, filename, defmat, smooth, scale=(1,1,1), trans=(0,0,0)): |
|
105 |
if (type(scale) == float or type(scale) == int): |
|
106 |
scale = (scale,)*3 |
|
107 |
(points, faces, tags, surfaces) = read_lwo(filename) |
|
108 |
||
109 |
vertices = [] |
|
110 |
normals = [] |
|
111 |
vertex_face_num = [] |
|
59
64e456ab823d
add color support to lwo reader
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
112 |
materials = [] |
64e456ab823d
add color support to lwo reader
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
113 |
|
64e456ab823d
add color support to lwo reader
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
114 |
for surf in surfaces: |
64e456ab823d
add color support to lwo reader
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
115 |
mat = Material(colour=tuple(float(x)/255. for x in surf['color'])) |
64e456ab823d
add color support to lwo reader
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
116 |
materials.append(mat) |
58 | 117 |
|
118 |
for point in points: |
|
119 |
v0 = scale[0]*point[0] + trans[0] |
|
120 |
v1 = scale[1]*point[1] + trans[1] |
|
121 |
v2 = scale[2]*point[2] + trans[2] |
|
122 |
vertices.append(NormalVertex((v2,v1,v0))) |
|
123 |
normals.append([0.,0.,0.]) |
|
124 |
vertex_face_num.append(0) |
|
125 |
||
126 |
for f in faces: |
|
127 |
v = [vertices[x] for x in f[0:3]] |
|
59
64e456ab823d
add color support to lwo reader
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
128 |
matidx = f[3]-1 |
64e456ab823d
add color support to lwo reader
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
129 |
face = Triangle(v[0], v[1], v[2], materials[matidx]) |
58 | 130 |
n = face.getNormal() |
131 |
for x in f[0:3]: |
|
132 |
for i in range(3): |
|
133 |
normals[x][i] += n[i] |
|
134 |
vertex_face_num[x] += 1 |
|
135 |
if (smooth): |
|
136 |
face.setSmooth() |
|
137 |
rt.addshape(face) |
|
138 |
||
139 |
# interpolate normals at vertices |
|
140 |
num = 0 |
|
141 |
for vf in vertex_face_num: |
|
142 |
if (vertex_face_num[num] > 0): |
|
143 |
for i in range(3): |
|
144 |
normals[num][i] /= vertex_face_num[num] |
|
145 |
vertices[num].setNormal(tuple(normals[num])) |
|
146 |
num += 1 |