demos/lworeader.py
author Radek Brich <radek.brich@devl.cz>
Mon, 05 May 2008 15:31:14 +0200
branchpyrit
changeset 92 9af5c039b678
parent 90 f6a72eb99631
child 97 2a853d284a6a
permissions -rw-r--r--
add MSVC compiler support, make it default for Windows new header file simd.h for SSE abstraction and helpers add mselect pseudo instruction for common or(and(...), andnot(...)) replace many SSE intrinsics with new names new MemoryPool class (mempool.h) for faster KdNode allocation remove setMaxDepth() from Octree and KdTree, make max_depth const, it should be defined in constructor and never changed, change after building tree would cause error in traversal modify DefaultSampler to generate nice 2x2 packets of samples for packet tracing optimize Box and BBox::intersect_packet add precomputed invdir attribute to RayPacket scons build system: check for pthread library on Windows check for SDL generate include/config.h with variables detected by scons configuration move auxiliary files to build/ add sanity checks add writable operator[] to Vector
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 *
90
f6a72eb99631 rename Python module from 'raytracer' to 'pyrit'
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
     5
from pyrit import Triangle, NormalVertex, Material
72
7c3f38dff082 kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents: 69
diff changeset
     6
from vector import dot
58
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     7
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     8
def read_int4(f):
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     9
	return unpack('>i', f.read(4))[0]
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    10
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    11
def read_int2(f):
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    12
	return unpack('>h', f.read(2))[0]
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    13
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    14
def read_float4(f):
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    15
	return unpack('>f', f.read(4))[0]
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    16
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    17
def read_string(f):
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    18
	res = ''
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    19
	b = f.read(1)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    20
	l = 1
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    21
	while ( b != '\0' ):
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    22
		res += b
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    23
		b = f.read(1)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    24
		l += 1
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    25
	if (l % 2 != 0):
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    26
		f.read(1)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    27
		l += 1
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    28
	return (res,l)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    29
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    30
def read_chunk(f):
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    31
	ID = f.read(4)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    32
	if (ID == ''):
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    33
		return ('',0)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    34
	size = read_int4(f)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    35
	return (ID, size)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    36
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    37
def read_subchunk(f):
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    38
	ID = f.read(4)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    39
	size = read_int2(f)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    40
	return (ID, size)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    41
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    42
def read_lwo(filename):
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    43
	points = []
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    44
	faces = []
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    45
	tags = []
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    46
	surfaces = []
65
242839c6d27d basic detection of compiler (GCC or ICC) and CPU capabilities
Radek Brich <radek.brich@devl.cz>
parents: 59
diff changeset
    47
	f = file(filename, 'rb')
58
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    48
	(ID,size) = read_chunk(f)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    49
	form = f.read(4)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    50
	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
    51
		print 'unknown format'
58
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    52
		return
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    53
	(ID,size) = read_chunk(f)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    54
	while (ID != ''):
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    55
		#print ID,size
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    56
		if (ID == 'PNTS'):
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    57
			while (size > 0):
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    58
				p1 = read_float4(f)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    59
				p2 = read_float4(f)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    60
				p3 = read_float4(f)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    61
				points.append((p1,p2,p3))
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    62
				size -= 12
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    63
		elif (ID == 'SRFS'):
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    64
			while (size > 0):
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    65
				(s,l) = read_string(f)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    66
				size -= l
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    67
				tags.append(s)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    68
		elif (ID == 'POLS'):
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    69
			while (size > 0):
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    70
				vertex_count = read_int2(f)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    71
				size -= 2
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    72
				inds = []
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    73
				for i in range(vertex_count):
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    74
					index = read_int2(f)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    75
					inds.append(index)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    76
					size -= 2
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    77
				surf = read_int2(f)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    78
				size -= 2
69
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
    79
				if surf < 0:
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
    80
					# detail polygons
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
    81
					surf = abs(surf)
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
    82
					count = read_int2(f)
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
    83
					size -= 2
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
    84
					# ... FIXME
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
    85
				#print size, vertex_count
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
    86
				if vertex_count >= 3:
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
    87
					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
    88
				i = 0
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    89
				while (vertex_count > 3):
69
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
    90
					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
    91
					vertex_count -= 1
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    92
					i += 1
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    93
		elif (ID == 'SURF'):
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    94
			(name,l) = read_string(f)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    95
			size -= l
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    96
			surf = {}
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    97
			while (size > 0):
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    98
				(subID,subsize) = read_subchunk(f)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    99
				size -= 6
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   100
				if (subID == 'COLR'):
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   101
					col = f.read(subsize)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   102
					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
   103
				elif (subID == 'FLAG'):
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   104
					flags = read_int2(f)
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   105
					surf['luminous'] = (flags >> 0) & 1;
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   106
					surf['outline'] = (flags >> 1) & 1;
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   107
					surf['smooth'] = (flags >> 2) & 1;
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   108
					surf['color-highlights'] = (flags >> 3) & 1;
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   109
					surf['color-filter'] = (flags >> 4) & 1;
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   110
					surf['opaque-edge'] = (flags >> 5) & 1;
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   111
					surf['transparent-edge'] = (flags >> 6) & 1;
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   112
					surf['sharp-terminator'] = (flags >> 7) & 1;
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   113
					surf['double-sided'] = (flags >> 8) & 1;
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   114
					surf['additive'] = (flags >> 9) & 1;
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   115
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   116
				# Base Shading (float)
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   117
				elif (subID == 'VDIF'):
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   118
					surf['diffuse'] = read_float4(f)
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   119
				elif (subID == 'VSPC'):
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   120
					surf['specular'] = read_float4(f)
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   121
				elif (subID == 'VRFL'):
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   122
					surf['reflection'] = read_float4(f)
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   123
				elif (subID == 'VTRN'):
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   124
					surf['transparency'] = read_float4(f)
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   125
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   126
				# Base Shading (short)
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   127
				elif (subID == 'DIFF'):
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   128
					if not surf.has_key('diffuse'):
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   129
						surf['diffuse'] = read_int2(f)/255.
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   130
				elif (subID == 'SPEC'):
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   131
					if not surf.has_key('specular'):
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   132
						surf['specular'] = read_int2(f)/255.
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   133
				elif (subID == 'REFL'):
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   134
					if not surf.has_key('reflection'):
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   135
						surf['reflection'] = read_int2(f)/255.
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   136
				elif (subID == 'TRAN'):
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   137
					if not surf.has_key('transparency'):
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   138
						surf['transparency'] = read_int2(f)/255.
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   139
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   140
				elif (subID == 'RIND'):
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   141
					surf['refractive-index'] = read_float4(f)
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   142
				elif (subID == 'GLOS'):
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   143
					surf['glossiness'] = read_int2(f)
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   144
				elif (subID == 'SMAN'):
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   145
					surf['smoothing-max-angle'] = read_float4(f)
58
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   146
				else:
69
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   147
					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
   148
					f.read(subsize)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   149
				size -= subsize
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   150
			surfaces.append(surf)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   151
		else:
69
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   152
			print "Warning: chunk", ID,"("+str(size),"B) ignored"
58
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   153
			f.read(size)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   154
		(ID,size) = read_chunk(f)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   155
	return (points, faces, tags, surfaces)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   156
69
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   157
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
   158
	if (type(scale) == float or type(scale) == int):
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   159
		scale = (scale,)*3
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   160
	(points, faces, tags, surfaces) = read_lwo(filename)
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   161
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   162
	vertices = []
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   163
	normals = []
69
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   164
	vertex_faces = []
59
64e456ab823d add color support to lwo reader
Radek Brich <radek.brich@devl.cz>
parents: 58
diff changeset
   165
	materials = []
64e456ab823d add color support to lwo reader
Radek Brich <radek.brich@devl.cz>
parents: 58
diff changeset
   166
64e456ab823d add color support to lwo reader
Radek Brich <radek.brich@devl.cz>
parents: 58
diff changeset
   167
	for surf in surfaces:
64e456ab823d add color support to lwo reader
Radek Brich <radek.brich@devl.cz>
parents: 58
diff changeset
   168
		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
   169
		if surf.has_key('smooth'):
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   170
			mat.setSmooth(surf['smooth'])
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   171
		diff = 1.
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   172
		if surf.has_key('diffuse'):
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   173
			diff = surf['diffuse']
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   174
		spec = 0.
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   175
		if surf.has_key('specular'):
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   176
			spec = surf['specular']
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   177
		gloss = 1.0
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   178
		if surf.has_key('glossiness'):
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   179
			gloss = surf['glossiness']
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   180
		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
   181
		refl = 0.
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   182
		if surf.has_key('reflection'):
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   183
			refl = surf['reflection']
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   184
		mat.setReflectivity(refl)
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   185
		transp = 0.
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   186
		if surf.has_key('transparency'):
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   187
			transp = surf['transparency']
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   188
		rindex = 1.0
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   189
		if surf.has_key('refractive-index'):
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   190
			rindex = surf['refractive-index']
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   191
		mat.setTransmissivity(transp, rindex)
59
64e456ab823d add color support to lwo reader
Radek Brich <radek.brich@devl.cz>
parents: 58
diff changeset
   192
		materials.append(mat)
58
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   193
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   194
	for point in points:
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   195
		v0 = scale[0]*point[0] + trans[0]
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   196
		v1 = scale[1]*point[1] + trans[1]
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   197
		v2 = scale[2]*point[2] + trans[2]
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   198
		vertices.append(NormalVertex((v2,v1,v0)))
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   199
		normals.append([0.,0.,0.])
69
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   200
		vertex_faces.append([])
58
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   201
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   202
	for f in faces:
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   203
		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
   204
			vertex_faces[x].append(f)
58
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   205
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   206
	# interpolate normals at vertices
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   207
	num = 0
69
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   208
	for vfaces in vertex_faces:
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   209
		vert = vertices[num]
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   210
		edges = {}
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   211
		N = [0,0,0]
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   212
		for f in vfaces:
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   213
			for fvert in f[0:3]:
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   214
				if edges.has_key(str(fvert)):
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   215
					edges[str(fvert)].append(f)
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   216
				else:
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   217
					edges[str(fvert)] = [f]
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   218
		for f in vfaces:
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   219
			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
   220
			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
   221
			for i in range(3):
69
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   222
				N[i] += fN[i]
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   223
			surf = surfaces[f[3]-1]
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   224
			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
   225
				continue
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   226
			fNvert = list(fN)
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   227
			Ncount = 1
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   228
			copy_vertex = False
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   229
			for fvert in f[0:3]:
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   230
				for ef in edges[str(fvert)]:
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   231
					if ef == f:
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   232
						continue
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   233
					# f - ref. face; ef - other face
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   234
					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
   235
					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
   236
					d = dot(fN, efN)
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   237
					if d > 1:
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   238
						d = 1
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   239
					if d < -1:
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   240
						d = -1
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   241
					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
   242
						for i in range(3):
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   243
							fNvert[i] += efN[i]
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   244
						Ncount += 1
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   245
					else:
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   246
						copy_vertex = True
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   247
			# 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
   248
			if copy_vertex:
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   249
				for i in range(3):
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   250
					fNvert[i] /= Ncount
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   251
				new_vert = NormalVertex(vert)
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   252
				new_vert.setNormal(tuple(fNvert))
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   253
				f.append(f[0])
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   254
				f.append(f[1])
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   255
				f.append(f[2])
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   256
				vertices.append(new_vert)
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   257
				for i in range(3):
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   258
					if f[i] == num:
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   259
						f[i+4] = len(vertices)-1
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   260
		if (len(vfaces) > 0):
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   261
			for i in range(3):
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   262
				N[i] /= len(vfaces)
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   263
		vertices[num].setNormal(tuple(N))
58
cfe98f5c0ae9 new python demo: car.py
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
   264
		num += 1
69
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   265
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   266
	for f in faces:
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   267
		if len(f) > 4:
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   268
			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
   269
		else:
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   270
			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
   271
		matidx = f[3]-1
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 65
diff changeset
   272
		face = Triangle(v[0], v[1], v[2], materials[matidx])
75
20dee9819b17 unify capitalization of method names in C++ and Python
Radek Brich <radek.brich@devl.cz>
parents: 72
diff changeset
   273
		rt.addShape(face)