demos/lworeader.py
author Radek Brich <radek.brich@devl.cz>
Sat, 19 Apr 2008 18:00:27 +0200
branchpyrit
changeset 71 4fedf7290929
parent 69 303583d2fb97
child 72 7c3f38dff082
permissions -rw-r--r--
simplify kd-tree building, it's also much faster now
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
58
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     1
# LightWave .lwo file loader
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     2
69
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
     3
from math import *
58
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     4
from struct import *
59
64e456ab823d add color support to lwo reader
Radek Brich <radek.brich@devl.cz>
parents: 58
diff changeset
     5
from raytracer import Triangle, NormalVertex, Material
58
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     6
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     7
def read_int4(f):
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     8
	return unpack('>i', f.read(4))[0]
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     9
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    10
def read_int2(f):
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    11
	return unpack('>h', f.read(2))[0]
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    12
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    13
def read_float4(f):
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    14
	return unpack('>f', f.read(4))[0]
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    15
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    16
def read_string(f):
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    17
	res = ''
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    18
	b = f.read(1)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    19
	l = 1
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    20
	while ( b != '\0' ):
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    21
		res += b
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    22
		b = f.read(1)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    23
		l += 1
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    24
	if (l % 2 != 0):
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    25
		f.read(1)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    26
		l += 1
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    27
	return (res,l)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    28
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    29
def read_chunk(f):
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    30
	ID = f.read(4)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    31
	if (ID == ''):
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    32
		return ('',0)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    33
	size = read_int4(f)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    34
	return (ID, size)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    35
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    36
def read_subchunk(f):
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    37
	ID = f.read(4)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    38
	size = read_int2(f)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    39
	return (ID, size)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    40
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    41
def read_lwo(filename):
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    42
	points = []
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    43
	faces = []
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    44
	tags = []
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    45
	surfaces = []
65
242839c6d27d basic detection of compiler (GCC or ICC) and CPU capabilities
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    46
	f = file(filename, 'rb')
58
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    47
	(ID,size) = read_chunk(f)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    48
	form = f.read(4)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    49
	if (ID != 'FORM' or form != 'LWOB'):
69
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
    50
		print 'unknown format'
58
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    51
		return
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    52
	(ID,size) = read_chunk(f)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    53
	while (ID != ''):
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    54
		#print ID,size
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    55
		if (ID == 'PNTS'):
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    56
			while (size > 0):
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    57
				p1 = read_float4(f)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    58
				p2 = read_float4(f)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    59
				p3 = read_float4(f)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    60
				points.append((p1,p2,p3))
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    61
				size -= 12
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    62
		elif (ID == 'SRFS'):
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    63
			while (size > 0):
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    64
				(s,l) = read_string(f)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    65
				size -= l
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    66
				tags.append(s)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    67
		elif (ID == 'POLS'):
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    68
			while (size > 0):
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    69
				vertex_count = read_int2(f)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    70
				size -= 2
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    71
				inds = []
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    72
				for i in range(vertex_count):
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    73
					index = read_int2(f)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    74
					inds.append(index)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    75
					size -= 2
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    76
				surf = read_int2(f)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    77
				size -= 2
69
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
    78
				if surf < 0:
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
    79
					# detail polygons
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
    80
					surf = abs(surf)
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
    81
					count = read_int2(f)
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
    82
					size -= 2
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
    83
					# ... FIXME
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
    84
				#print size, vertex_count
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
    85
				if vertex_count >= 3:
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
    86
					faces.append([inds[0], inds[1], inds[2], surf])
58
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    87
				i = 0
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    88
				while (vertex_count > 3):
69
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
    89
					faces.append([inds[0], inds[2+i], inds[3+i], surf])
58
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    90
					vertex_count -= 1
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    91
					i += 1
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    92
		elif (ID == 'SURF'):
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    93
			(name,l) = read_string(f)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    94
			size -= l
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    95
			surf = {}
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    96
			while (size > 0):
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    97
				(subID,subsize) = read_subchunk(f)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    98
				size -= 6
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    99
				if (subID == 'COLR'):
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   100
					col = f.read(subsize)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   101
					surf['color'] = (unpack('BBB',col[:3]))
69
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   102
				elif (subID == 'FLAG'):
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   103
					flags = read_int2(f)
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   104
					surf['luminous'] = (flags >> 0) & 1;
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   105
					surf['outline'] = (flags >> 1) & 1;
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   106
					surf['smooth'] = (flags >> 2) & 1;
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   107
					surf['color-highlights'] = (flags >> 3) & 1;
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   108
					surf['color-filter'] = (flags >> 4) & 1;
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   109
					surf['opaque-edge'] = (flags >> 5) & 1;
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   110
					surf['transparent-edge'] = (flags >> 6) & 1;
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   111
					surf['sharp-terminator'] = (flags >> 7) & 1;
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   112
					surf['double-sided'] = (flags >> 8) & 1;
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   113
					surf['additive'] = (flags >> 9) & 1;
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   114
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   115
				# Base Shading (float)
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   116
				elif (subID == 'VDIF'):
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   117
					surf['diffuse'] = read_float4(f)
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   118
				elif (subID == 'VSPC'):
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   119
					surf['specular'] = read_float4(f)
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   120
				elif (subID == 'VRFL'):
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   121
					surf['reflection'] = read_float4(f)
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   122
				elif (subID == 'VTRN'):
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   123
					surf['transparency'] = read_float4(f)
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   124
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   125
				# Base Shading (short)
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   126
				elif (subID == 'DIFF'):
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   127
					if not surf.has_key('diffuse'):
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   128
						surf['diffuse'] = read_int2(f)/255.
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   129
				elif (subID == 'SPEC'):
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   130
					if not surf.has_key('specular'):
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   131
						surf['specular'] = read_int2(f)/255.
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   132
				elif (subID == 'REFL'):
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   133
					if not surf.has_key('reflection'):
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   134
						surf['reflection'] = read_int2(f)/255.
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   135
				elif (subID == 'TRAN'):
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   136
					if not surf.has_key('transparency'):
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   137
						surf['transparency'] = read_int2(f)/255.
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   138
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   139
				elif (subID == 'RIND'):
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   140
					surf['refractive-index'] = read_float4(f)
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   141
				elif (subID == 'GLOS'):
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   142
					surf['glossiness'] = read_int2(f)
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   143
				elif (subID == 'SMAN'):
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   144
					surf['smoothing-max-angle'] = read_float4(f)
58
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   145
				else:
69
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   146
					print "Warning: SURF sub chunk", subID,"("+str(subsize),"B) ignored"
58
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   147
					f.read(subsize)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   148
				size -= subsize
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   149
			surfaces.append(surf)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   150
		else:
69
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   151
			print "Warning: chunk", ID,"("+str(size),"B) ignored"
58
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   152
			f.read(size)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   153
		(ID,size) = read_chunk(f)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   154
	return (points, faces, tags, surfaces)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   155
69
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   156
def dot(a,b):
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   157
	sum = 0
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   158
	for i in range(min(len(a),len(b))):
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   159
		sum += a[i]*b[i]
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   160
	return sum
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   161
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   162
def LoadLightwaveLwoFile(rt, filename, scale=(1,1,1), trans=(0,0,0)):
58
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   163
	if (type(scale) == float or type(scale) == int):
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   164
		scale = (scale,)*3
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   165
	(points, faces, tags, surfaces) = read_lwo(filename)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   166
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   167
	vertices = []
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   168
	normals = []
69
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   169
	vertex_faces = []
59
64e456ab823d add color support to lwo reader
Radek Brich <radek.brich@devl.cz>
parents: 58
diff changeset
   170
	materials = []
64e456ab823d add color support to lwo reader
Radek Brich <radek.brich@devl.cz>
parents: 58
diff changeset
   171
64e456ab823d add color support to lwo reader
Radek Brich <radek.brich@devl.cz>
parents: 58
diff changeset
   172
	for surf in surfaces:
64e456ab823d add color support to lwo reader
Radek Brich <radek.brich@devl.cz>
parents: 58
diff changeset
   173
		mat = Material(colour=tuple(float(x)/255. for x in surf['color']))
69
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   174
		if surf.has_key('smooth'):
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   175
			mat.setSmooth(surf['smooth'])
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   176
		diff = 1.
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   177
		if surf.has_key('diffuse'):
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   178
			diff = surf['diffuse']
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   179
		spec = 0.
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   180
		if surf.has_key('specular'):
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   181
			spec = surf['specular']
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   182
		gloss = 1.0
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   183
		if surf.has_key('glossiness'):
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   184
			gloss = surf['glossiness']
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   185
		mat.setPhong(0.1, diff, spec, gloss)
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   186
		refl = 0.
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   187
		if surf.has_key('reflection'):
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   188
			refl = surf['reflection']
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   189
		mat.setReflectivity(refl)
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   190
		transp = 0.
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   191
		if surf.has_key('transparency'):
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   192
			transp = surf['transparency']
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   193
		rindex = 1.0
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   194
		if surf.has_key('refractive-index'):
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   195
			rindex = surf['refractive-index']
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   196
		mat.setTransmissivity(transp, rindex)
59
64e456ab823d add color support to lwo reader
Radek Brich <radek.brich@devl.cz>
parents: 58
diff changeset
   197
		materials.append(mat)
58
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   198
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   199
	for point in points:
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   200
		v0 = scale[0]*point[0] + trans[0]
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   201
		v1 = scale[1]*point[1] + trans[1]
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   202
		v2 = scale[2]*point[2] + trans[2]
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   203
		vertices.append(NormalVertex((v2,v1,v0)))
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   204
		normals.append([0.,0.,0.])
69
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   205
		vertex_faces.append([])
58
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   206
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   207
	for f in faces:
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   208
		for x in f[0:3]:
69
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   209
			vertex_faces[x].append(f)
58
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   210
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   211
	# interpolate normals at vertices
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   212
	num = 0
69
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   213
	for vfaces in vertex_faces:
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   214
		vert = vertices[num]
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   215
		edges = {}
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   216
		N = [0,0,0]
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   217
		for f in vfaces:
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   218
			for fvert in f[0:3]:
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   219
				if edges.has_key(str(fvert)):
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   220
					edges[str(fvert)].append(f)
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   221
				else:
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   222
					edges[str(fvert)] = [f]
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   223
		for f in vfaces:
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   224
			vv = [vertices[x] for x in f[0:3]]
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   225
			fN = Triangle(vv[0], vv[1], vv[2], materials[f[3]-1]).getNormal()
58
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   226
			for i in range(3):
69
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   227
				N[i] += fN[i]
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   228
			surf = surfaces[f[3]-1]
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   229
			if not surf.has_key('smoothing-max-angle'):
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   230
				continue
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   231
			fNvert = list(fN)
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   232
			Ncount = 1
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   233
			copy_vertex = False
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   234
			for fvert in f[0:3]:
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   235
				for ef in edges[str(fvert)]:
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   236
					if ef == f:
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   237
						continue
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   238
					# f - ref. face; ef - other face
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   239
					vv = [vertices[x] for x in ef[0:3]]
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   240
					efN = Triangle(vv[0], vv[1], vv[2], materials[ef[3]-1]).getNormal()
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   241
					d = dot(fN, efN)
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   242
					if d > 1:
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   243
						d = 1
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   244
					if d < -1:
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   245
						d = -1
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   246
					if acos(d) < surf['smoothing-max-angle']:
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   247
						for i in range(3):
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   248
							fNvert[i] += efN[i]
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   249
						Ncount += 1
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   250
					else:
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   251
						copy_vertex = True
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   252
			# here fNvert is normal for num'th vertex in face f
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   253
			if copy_vertex:
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   254
				for i in range(3):
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   255
					fNvert[i] /= Ncount
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   256
				new_vert = NormalVertex(vert)
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   257
				new_vert.setNormal(tuple(fNvert))
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   258
				f.append(f[0])
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   259
				f.append(f[1])
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   260
				f.append(f[2])
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   261
				vertices.append(new_vert)
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   262
				for i in range(3):
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   263
					if f[i] == num:
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   264
						f[i+4] = len(vertices)-1
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   265
		if (len(vfaces) > 0):
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   266
			for i in range(3):
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   267
				N[i] /= len(vfaces)
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   268
		vertices[num].setNormal(tuple(N))
58
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   269
		num += 1
69
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   270
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   271
	for f in faces:
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   272
		if len(f) > 4:
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   273
			v = [vertices[x] for x in f[4:7]]
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   274
		else:
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   275
			v = [vertices[x] for x in f[0:3]]
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   276
		matidx = f[3]-1
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   277
		face = Triangle(v[0], v[1], v[2], materials[matidx])
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   278
		rt.addshape(face)