def makestock(name, output): if name in output.subst: return None objname = name + '.obj' if not objname in listdir('Resources'): return None tex = None vt = [] idx = [] obj = file(join('Resources', objname), 'rU') if obj.next() not in ['A\n', 'I\n']: return None if not obj.next().startswith('800'): return None if obj.next() != 'OBJ\n': return None for line in obj: tokens = line.split() if not tokens: continue if tokens[0] == 'TEXTURE' and len(tokens) == 2: if output.dds and tokens[1][-4:] == '.png': tex = 'Resources/' + tokens[1][:-4] + '.dds' else: tex = 'Resources/' + tokens[1] elif tokens[0] == 'VT' and len(tokens) == 9: vt.append((float(tokens[1]), float(tokens[2]), float(tokens[3]), float(tokens[4]), float(tokens[5]), float(tokens[6]), float(tokens[7]), float(tokens[8]))) elif tokens[0] == 'IDX' and len(tokens) == 2: idx.append(int(tokens[1])) elif tokens[0] == 'IDX10' and len(tokens) == 11: idx.extend([ int(tokens[1]), int(tokens[2]), int(tokens[3]), int(tokens[4]), int(tokens[5]), int(tokens[6]), int(tokens[7]), int(tokens[8]), int(tokens[9]), int(tokens[10]) ]) obj.close() obj = Object( name, "(c) Jonathan Harris 2008. http://creativecommons.org/licenses/by-sa/3.0/", None, None) obj.addgeometry(Material(output.xpver, None), vt, idx) return obj
def makestock(name, output): if name in output.subst: return None objname=name+'.obj' if not objname in listdir('Resources'): return None tex=None vt=[] idx=[] obj=file(join('Resources', objname), 'rU') if obj.next() not in ['A\n', 'I\n']: return None if not obj.next().startswith('800'): return None if obj.next()!='OBJ\n': return None for line in obj: tokens=line.split() if not tokens: continue if tokens[0]=='TEXTURE' and len(tokens)==2: if output.dds and tokens[1][-4:]=='.png': tex='Resources/'+tokens[1][:-4]+'.dds' else: tex='Resources/'+tokens[1] elif tokens[0]=='VT' and len(tokens)==9: vt.append((float(tokens[1]), float(tokens[2]), float(tokens[3]), float(tokens[4]), float(tokens[5]), float(tokens[6]), float(tokens[7]), float(tokens[8]))) elif tokens[0]=='IDX' and len(tokens)==2: idx.append(int(tokens[1])) elif tokens[0]=='IDX10' and len(tokens)==11: idx.extend([int(tokens[1]), int(tokens[2]), int(tokens[3]), int(tokens[4]), int(tokens[5]), int(tokens[6]), int(tokens[7]), int(tokens[8]), int(tokens[9]), int(tokens[10])]) obj.close() obj=Object(name, "(c) Jonathan Harris 2008. http://creativecommons.org/licenses/by-sa/3.0/", None, None) obj.addgeometry(Material(output.xpver, None), vt, idx) return obj
def makegenquad(name, output, x, z, incx, incz, heights, texs, roof): # roof types: 0=flat, 1=pointy, 2=gabled, 3=slanty cumheight = 0 vt = [] idx = [] rgbs = [] for i in range(len(texs)): rgbs.append(genrgb(i, texs[i])) sign = [(1, 1), (1, -1), (-1, -1), (-1, 1), (1, 1)] n = [(cos(incx), sin(incx), 0), (0, sin(incz), -cos(incz)), (-cos(incx), sin(incx), 0), (0, sin(incz), cos(incz))] for story in range(3): if not heights[story]: continue (u, v) = rgb2uv(rgbs[story]) x0 = x / 2 - sin(incx) * cumheight z0 = z / 2 - sin(incz) * cumheight x1 = x / 2 - sin(incx) * (cumheight + heights[story]) z1 = z / 2 - sin(incz) * (cumheight + heights[story]) for side in range(4): (nx, ny, nz) = n[side] base = len(vt) for corner in [side, side + 1]: (sx, sz) = sign[corner] vt.extend([(sx * x0, cumheight, sz * z0, nx, ny, nz, u, v), (sx * x1, cumheight + heights[story], sz * z1, nx, ny, nz, u, v)]) idx.extend( [base, base + 1, base + 2, base + 3, base + 2, base + 1]) cumheight += heights[story] # roof x0 = x / 2 - sin(incx) * cumheight z0 = z / 2 - sin(incz) * cumheight (u, v) = rgb2uv(rgbs[3]) base = len(vt) if roof == 0 or heights[3] == 0: # simplify if actually flat vt.extend([(x0, cumheight, z0, 0, 1, 0, u, v), (x0, cumheight, -z0, 0, 1, 0, u, v), (-x0, cumheight, -z0, 0, 1, 0, u, v), (-x0, cumheight, z0, 0, 1, 0, u, v)]) idx.extend([base + 2, base + 1, base, base, base + 3, base + 2]) elif roof == 1: # pointy topheight = cumheight + heights[3] incx = atan(x / (2 * heights[3])) incz = atan(z / (2 * heights[3])) vt.extend([(x0, cumheight, z0, cos(incx), sin(incx), 0, u, v), (0, topheight, 0, cos(incx), sin(incx), 0, u, v), (x0, cumheight, -z0, cos(incx), sin(incx), 0, u, v), (-x0, cumheight, z0, -cos(incx), sin(incx), 0, u, v), (0, topheight, 0, -cos(incx), sin(incx), 0, u, v), (-x0, cumheight, -z0, -cos(incx), sin(incx), 0, u, v), (x0, cumheight, z0, 0, sin(incz), cos(incz), u, v), (0, topheight, 0, 0, sin(incz), cos(incz), u, v), (-x0, cumheight, z0, 0, sin(incz), cos(incz), u, v), (x0, cumheight, -z0, 0, sin(incz), -cos(incz), u, v), (0, topheight, 0, 0, sin(incz), -cos(incz), u, v), (-x0, cumheight, -z0, 0, sin(incz), -cos(incz), u, v)]) idx.extend([ base, base + 1, base + 2, base + 5, base + 4, base + 3, base + 8, base + 7, base + 6, base + 9, base + 10, base + 11 ]) elif roof == 2: # gabled topheight = cumheight + heights[3] incz = atan(z / (2 * heights[3])) ny = sin(incz) nz = cos(incz) #roof vt.extend([(x0, cumheight, z0, 0, ny, nz, u, v), (x0, topheight, 0, 0, ny, nz, u, v), (-x0, topheight, 0, 0, ny, nz, u, v), (-x0, cumheight, z0, 0, ny, nz, u, v), (x0, cumheight, -z0, 0, ny, -nz, u, v), (x0, topheight, 0, 0, ny, -nz, u, v), (-x0, topheight, 0, 0, ny, -nz, u, v), (-x0, cumheight, -z0, 0, ny, -nz, u, v)]) idx.extend([ base + 2, base + 1, base, base, base + 3, base + 2, base + 4, base + 5, base + 6, base + 6, base + 7, base + 4 ]) #gables (u, v) = rgb2uv(rgbs[4]) base = len(vt) vt.extend([(x0, cumheight, z0, 1, 0, 0, u, v), (x0, topheight, 0, 1, 0, 0, u, v), (x0, cumheight, -z0, 1, 0, 0, u, v), (-x0, cumheight, z0, -1, 0, 0, u, v), (-x0, topheight, 0, -1, 0, 0, u, v), (-x0, cumheight, -z0, -1, 0, 0, u, v)]) idx.extend([base, base + 1, base + 2, base + 5, base + 4, base + 3]) elif roof == 3: # slanty topheight = cumheight + heights[3] incz = atan(z / heights[3]) ny = sin(incz) nz = cos(incz) #roof vt.extend([(x0, cumheight, z0, 0, ny, nz, u, v), (x0, topheight, -z0, 0, ny, nz, u, v), (-x0, topheight, -z0, 0, ny, nz, u, v), (-x0, cumheight, z0, 0, ny, nz, u, v)]) idx.extend([base + 2, base + 1, base, base, base + 3, base + 2]) #gables (u, v) = rgb2uv(rgbs[4]) base = len(vt) vt.extend([(x0, topheight, -z0, 1, 0, 0, u, v), (x0, cumheight, -z0, 1, 0, 0, u, v), (x0, cumheight, z0, 1, 0, 0, u, v), (-x0, topheight, -z0, -1, 0, 0, u, v), (-x0, cumheight, -z0, -1, 0, 0, u, v), (-x0, cumheight, z0, -1, 0, 0, u, v)]) idx.extend([base, base + 1, base + 2, base + 5, base + 4, base + 3]) #face (u, v) = rgb2uv(rgbs[5]) base = len(vt) vt.extend([(x0, topheight, -z0, 0, 0, -1, u, v), (x0, cumheight, -z0, 0, 0, -1, u, v), (-x0, cumheight, -z0, 0, 0, -1, u, v), (-x0, topheight, -z0, 0, 0, -1, u, v)]) idx.extend([base + 2, base + 1, base, base, base + 3, base + 2]) obj = Object(name, "Generic building", None, None) obj.addgeometry(Material(output.xpver, None), vt, idx) return obj
def makegenmulti(name, output, sides, x, z, heights, texs): # building fits inside an oval inscribed in a x*z box slice = 2 * pi / sides halfslice = pi / sides sides2 = sides * 2 cumheight = 0 vt = [] idx = [] rgbs = [] for i in range(len(texs)): rgbs.append(genrgb(i, texs[i])) for story in range(3): (u, v) = rgb2uv(rgbs[story]) base = len(vt) for corner in range(sides): angle = corner * slice - halfslice nx = sin(angle) nz = cos(angle) x0 = x * nx / 2 z0 = z * nz / 2 vt.append((x0, cumheight, z0, nx, 0, nz, u, v)) vt.append((x0, cumheight + heights[story], z0, nx, 0, nz, u, v)) idx.extend([ base + corner * 2, base + (corner * 2 + 1) % sides2, base + (corner * 2 + 2) % sides2, base + (corner * 2 + 3) % sides2, base + (corner * 2 + 2) % sides2, base + (corner * 2 + 1) % sides2 ]) cumheight += heights[story] # roof (u, v) = rgb2uv(rgbs[3]) base = len(vt) if heights[3] == 0: # simplify if actually flat for corner in range(sides): angle = corner * slice - halfslice x0 = sin(angle) * x / 2 z0 = cos(angle) * z / 2 vt.append((x0, cumheight, z0, 0, 1, 0, u, v)) vt.append((0, cumheight, 0, 0, 1, 0, u, v)) idx.extend([ base + corner * 2, base + (corner * 2 + 1) % sides2, base + (corner * 2 + 2) % sides2 ]) else: incx = atan(x / (2 * heights[3])) incz = atan(z / (2 * heights[3])) for corner in range(sides): # FIXME: normal calculation is incorrect angle = corner * slice - halfslice nx = cos(incx) * sin(angle) ny = sin(incx) * sin(angle) + sin(incz) * cos(angle) nz = cos(incz) * cos(angle) x0 = sin(angle) * x / 2 z0 = cos(angle) * z / 2 vt.append((x0, cumheight, z0, nx, ny, nz, u, v)) angle = corner * slice nx = cos(incx) * sin(angle) ny = sin(incx) * sin(angle) + sin(incz) * cos(angle) nz = cos(incz) * cos(angle) vt.append((0, cumheight + heights[3], 0, nx, ny, nz, u, v)) idx.extend([ base + corner * 2, base + (corner * 2 + 1) % sides2, base + (corner * 2 + 2) % sides2 ]) obj = Object(name, "Generic building", None, None) obj.addgeometry(Material(output.xpver, None), vt, idx) return obj
def makegenquad(name, output, x, z, incx, incz, heights, texs, roof): # roof types: 0=flat, 1=pointy, 2=gabled, 3=slanty cumheight=0 vt=[] idx=[] rgbs=[] for i in range(len(texs)): rgbs.append(genrgb(i, texs[i])) sign=[(1,1), (1,-1), (-1,-1), (-1,1), (1,1)] n=[( cos(incx),sin(incx),0), (0,sin(incz),-cos(incz)), (-cos(incx),sin(incx),0), (0,sin(incz), cos(incz))] for story in range(3): if not heights[story]: continue (u,v)=rgb2uv(rgbs[story]) x0=x/2-sin(incx)*cumheight z0=z/2-sin(incz)*cumheight x1=x/2-sin(incx)*(cumheight+heights[story]) z1=z/2-sin(incz)*(cumheight+heights[story]) for side in range(4): (nx,ny,nz)=n[side] base=len(vt) for corner in [side, side+1]: (sx,sz)=sign[corner] vt.extend([(sx*x0, cumheight, sz*z0, nx,ny,nz, u,v), (sx*x1, cumheight+heights[story], sz*z1, nx,ny,nz, u,v)]) idx.extend([base,base+1,base+2, base+3,base+2,base+1]) cumheight += heights[story] # roof x0=x/2-sin(incx)*cumheight z0=z/2-sin(incz)*cumheight (u,v)=rgb2uv(rgbs[3]) base=len(vt) if roof==0 or heights[3]==0: # simplify if actually flat vt.extend([( x0, cumheight, z0, 0,1,0, u,v), ( x0, cumheight, -z0, 0,1,0, u,v), (-x0, cumheight, -z0, 0,1,0, u,v), (-x0, cumheight, z0, 0,1,0, u,v)]) idx.extend([base+2,base+1,base, base,base+3,base+2]) elif roof==1: # pointy topheight=cumheight+heights[3] incx=atan(x/(2*heights[3])) incz=atan(z/(2*heights[3])) vt.extend([( x0, cumheight, z0, cos(incx),sin(incx),0, u,v), ( 0, topheight, 0, cos(incx),sin(incx),0, u,v), ( x0, cumheight, -z0, cos(incx),sin(incx),0, u,v), (-x0, cumheight, z0, -cos(incx),sin(incx),0, u,v), ( 0, topheight, 0, -cos(incx),sin(incx),0, u,v), (-x0, cumheight, -z0, -cos(incx),sin(incx),0, u,v), ( x0, cumheight, z0, 0,sin(incz), cos(incz), u,v), ( 0, topheight, 0, 0,sin(incz), cos(incz), u,v), (-x0, cumheight, z0, 0,sin(incz), cos(incz), u,v), ( x0, cumheight, -z0, 0,sin(incz),-cos(incz), u,v), ( 0, topheight, 0, 0,sin(incz),-cos(incz), u,v), (-x0, cumheight, -z0, 0,sin(incz),-cos(incz), u,v)]) idx.extend([ base,base+1,base+2, base+5,base+4,base+3, base+8,base+7,base+6, base+9,base+10,base+11]) elif roof==2: # gabled topheight=cumheight+heights[3] incz=atan(z/(2*heights[3])) ny=sin(incz) nz=cos(incz) #roof vt.extend([( x0, cumheight, z0, 0,ny,nz, u,v), ( x0, topheight, 0, 0,ny,nz, u,v), (-x0, topheight, 0, 0,ny,nz, u,v), (-x0, cumheight, z0, 0,ny,nz, u,v), ( x0, cumheight, -z0, 0,ny,-nz, u,v), ( x0, topheight, 0, 0,ny,-nz, u,v), (-x0, topheight, 0, 0,ny,-nz, u,v), (-x0, cumheight, -z0, 0,ny,-nz, u,v)]) idx.extend([base+2,base+1,base, base,base+3,base+2, base+4,base+5,base+6, base+6,base+7,base+4]) #gables (u,v)=rgb2uv(rgbs[4]) base=len(vt) vt.extend([( x0, cumheight, z0, 1,0,0, u,v), ( x0, topheight, 0, 1,0,0, u,v), ( x0, cumheight, -z0, 1,0,0, u,v), (-x0, cumheight, z0, -1,0,0, u,v), (-x0, topheight, 0, -1,0,0, u,v), (-x0, cumheight, -z0, -1,0,0, u,v)]) idx.extend([base,base+1,base+2, base+5,base+4,base+3]) elif roof==3: # slanty topheight=cumheight+heights[3] incz=atan(z/heights[3]) ny=sin(incz) nz=cos(incz) #roof vt.extend([( x0, cumheight, z0, 0,ny,nz, u,v), ( x0, topheight, -z0, 0,ny,nz, u,v), (-x0, topheight, -z0, 0,ny,nz, u,v), (-x0, cumheight, z0, 0,ny,nz, u,v)]) idx.extend([base+2,base+1,base, base,base+3,base+2]) #gables (u,v)=rgb2uv(rgbs[4]) base=len(vt) vt.extend([( x0, topheight, -z0, 1,0,0, u,v), ( x0, cumheight, -z0, 1,0,0, u,v), ( x0, cumheight, z0, 1,0,0, u,v), (-x0, topheight, -z0, -1,0,0, u,v), (-x0, cumheight, -z0, -1,0,0, u,v), (-x0, cumheight, z0, -1,0,0, u,v)]) idx.extend([base,base+1,base+2, base+5,base+4,base+3]) #face (u,v)=rgb2uv(rgbs[5]) base=len(vt) vt.extend([( x0, topheight, -z0, 0,0,-1, u,v), ( x0, cumheight, -z0, 0,0,-1, u,v), (-x0, cumheight, -z0, 0,0,-1, u,v), (-x0, topheight, -z0, 0,0,-1, u,v)]) idx.extend([base+2,base+1,base, base,base+3,base+2]) obj=Object(name, "Generic building", None, None) obj.addgeometry(Material(output.xpver, None), vt, idx) return obj
def makegenmulti(name, output, sides, x, z, heights, texs): # building fits inside an oval inscribed in a x*z box slice=2*pi/sides halfslice=pi/sides sides2=sides*2 cumheight=0 vt=[] idx=[] rgbs=[] for i in range(len(texs)): rgbs.append(genrgb(i, texs[i])) for story in range(3): (u,v)=rgb2uv(rgbs[story]) base=len(vt) for corner in range(sides): angle=corner*slice-halfslice nx=sin(angle) nz=cos(angle) x0=x*nx/2 z0=z*nz/2 vt.append((x0, cumheight, z0, nx,0,nz, u,v)) vt.append((x0, cumheight+heights[story], z0, nx,0,nz, u,v)) idx.extend([base + corner*2, base +(corner*2+1)%sides2, base +(corner*2+2)%sides2, base +(corner*2+3)%sides2, base +(corner*2+2)%sides2, base +(corner*2+1)%sides2]) cumheight += heights[story] # roof (u,v)=rgb2uv(rgbs[3]) base=len(vt) if heights[3]==0: # simplify if actually flat for corner in range(sides): angle=corner*slice-halfslice x0=sin(angle)*x/2 z0=cos(angle)*z/2 vt.append((x0, cumheight, z0, 0,1,0, u,v)) vt.append((0, cumheight, 0, 0,1,0, u,v)) idx.extend([base +corner*2, base+(corner*2+1)%sides2, base+(corner*2+2)%sides2]) else: incx=atan(x/(2*heights[3])) incz=atan(z/(2*heights[3])) for corner in range(sides): # FIXME: normal calculation is incorrect angle=corner*slice-halfslice nx=cos(incx)*sin(angle) ny=sin(incx)*sin(angle)+sin(incz)*cos(angle) nz=cos(incz)*cos(angle) x0=sin(angle)*x/2 z0=cos(angle)*z/2 vt.append((x0, cumheight, z0, nx,ny,nz, u,v)) angle=corner*slice nx=cos(incx)*sin(angle) ny=sin(incx)*sin(angle)+sin(incz)*cos(angle) nz=cos(incz)*cos(angle) vt.append((0, cumheight+heights[3], 0, nx,ny,nz, u,v)) idx.extend([base +corner*2, base+(corner*2+1)%sides2, base+(corner*2+2)%sides2]) obj=Object(name, "Generic building", None, None) obj.addgeometry(Material(output.xpver, None), vt, idx) return obj