Package MeatEngine :: Package RayTrace :: Module test
[hide private]
[frames] | no frames]

Source Code for Module MeatEngine.RayTrace.test

  1  import MeatEngine.Math.vector as vector 
  2  import triangle 
  3  import Image 
  4  import math 
  5  import intersect 
  6  import light 
  7   
  8  verts=[vector.Vec3f(-1.0, -1.0, -1.0), 
  9         vector.Vec3f(-1.0, -1.0,  1.0), 
 10         vector.Vec3f(-1.0,  1.0, -1.0), 
 11         vector.Vec3f(-1.0,  1.0,  1.0), 
 12         vector.Vec3f( 1.0, -1.0, -1.0), 
 13         vector.Vec3f( 1.0, -1.0,  1.0), 
 14         vector.Vec3f( 1.0,  1.0, -1.0), 
 15         vector.Vec3f( 1.0,  1.0,  1.0), 
 16   
 17         vector.Vec3f(-1.0, 3.0, -1.0), 
 18         vector.Vec3f(-1.0, 5.0, -1.0), 
 19         vector.Vec3f(0.73, 4.0, -1.0), 
 20         vector.Vec3f(-.42, 4.0, 0.63), 
 21   
 22         vector.Vec3f(-5.5, -5.5, -1.0), 
 23         vector.Vec3f(-5.5,  5.5, -1.0), 
 24         vector.Vec3f( 5.5, -5.5, -1.0), 
 25         vector.Vec3f( 5.5,  5.5, -1.0), 
 26         ] 
 27   
 28  tris=[(2, 0, 1), 
 29        (1, 3, 2), 
 30        (6, 4, 0), 
 31        (0, 2, 6), 
 32        (6, 2, 3), 
 33        (3, 7, 6), 
 34        (5, 4, 6), 
 35        (6, 7, 5), 
 36        (1, 0, 4), 
 37        (4, 5, 1), 
 38        (1, 5, 7), 
 39        (7, 3, 1), 
 40   
 41        (8, 9, 10), 
 42        (8, 10, 11), 
 43        (9, 11, 10), 
 44        (8, 11, 9), 
 45   
 46        (12, 14, 13), 
 47        (14, 15, 13) 
 48        ] 
 49   
 50  colors=[(240, 0, 0), 
 51          (240, 0, 0), 
 52          (0, 240, 0), 
 53          (0, 240, 0), 
 54          (0, 0, 240), 
 55          (0, 0, 240), 
 56          (180, 180, 0), 
 57          (180, 180, 0), 
 58          (180, 0, 180), 
 59          (180, 0, 180), 
 60          (0, 180, 180), 
 61          (0, 180, 180), 
 62   
 63          (0, 0, 0), 
 64          (200, 200, 200), 
 65          (200, 200, 200), 
 66          (250, 250, 250), 
 67   
 68          (220, 220, 220), 
 69          (220, 220, 220)] 
 70   
 71   
 72  TOT_FRAMES=150 
 73   
 74  WIDTH=400 
 75  HEIGHT=200 
 76   
 77  FOV=math.radians(50) 
 78  NEAR=0.5 
 79   
 80  LIGHT_DIR=vector.Vec3f(-0.5,-0.5, -1.0).norm() 
 81  dirLight=light.DirectionalLight(LIGHT_DIR, 
 82                                  (1.0, 1.0, 1.0)) 
 83   
 84  AMBIENT=0.25 
 85  LIGHT_VAL=0.75 
 86   
87 -def trace(base, frameNum):
88 print "Frame:",frameNum 89 radius=9.0 90 x=radius*math.cos(2*math.pi*frameNum/TOT_FRAMES) 91 y=radius*math.sin(2*math.pi*frameNum/TOT_FRAMES) 92 z=3.0 93 94 eye=vector.Vec3f(x, y, z) 95 at=vector.Vec3f(0.0, 0.0, 0.0) 96 worldUp=vector.Vec3f(0.0, 0.0, 1.0) 97 98 aspect=float(WIDTH)/HEIGHT 99 100 halfFOVangle=FOV/2.0 101 step=math.tan(halfFOVangle)*NEAR/(WIDTH/2.0) 102 103 eyeForward=at.sub(eye).norm().mul(NEAR) 104 eyeRight=eyeForward.cross(worldUp).norm().mul(step) 105 eyeUp=eyeRight.cross(eyeForward).norm().mul(step) 106 107 im=Image.new("RGB", (WIDTH,HEIGHT)) 108 109 for x in range(WIDTH): 110 xVec=eyeRight.mul(x-WIDTH/2) 111 112 for y in range(HEIGHT): 113 yVec=eyeUp.mul(y-HEIGHT/2) 114 115 dir=eyeForward.add(xVec.add(yVec)) 116 117 closestDepth=None 118 bestColor=(200,200,200) 119 120 for triIndex, tverts in enumerate(tris): 121 c=colors[triIndex] 122 123 triVerts=[verts[i] for i in tverts] 124 125 ix=triangle.intersectRayTriangle(eye, 126 dir, 127 triVerts[0], 128 triVerts[1], 129 triVerts[2], 130 True) 131 132 if ix: 133 t=ix.dist 134 u,v=ix.uv 135 136 iNorm=ix.normal.norm() 137 138 lightDot=-iNorm.dot(LIGHT_DIR) 139 140 if lightDot>0: 141 # now raycast to see if we're occluded 142 for lightTriIndex,lightTVerts in enumerate(tris): 143 if lightTriIndex==triIndex: 144 continue 145 lightTriVerts=[verts[i] for i in lightTVerts] 146 lx=triangle.intersectRayTriangle(ix.pos, 147 LIGHT_DIR.mul(-1.0), 148 lightTriVerts[0], 149 lightTriVerts[1], 150 lightTriVerts[2], 151 True) 152 if lx and lx.dist>0: 153 lightDot=0 154 break 155 156 if lightDot<0: 157 lightDot=0 158 159 totalLight=AMBIENT+LIGHT_VAL*lightDot 160 161 if (closestDepth is None or 162 (t>0 and t<closestDepth)): 163 closestDepth=t 164 cr,cg,cb=c 165 166 #bestColor=(cr, 167 # 120+int(128*u), 168 # 120+int(128*v)) 169 bestColor=c 170 171 172 bestColor=(cr*totalLight, 173 cg*totalLight, 174 cb*totalLight) 175 176 im.putpixel((x,HEIGHT-y-1), bestColor) 177 178 im.save("%s%04d.png"%(base,frameNum))
179 180 181 if __name__=="__main__": 182 for i in range(TOT_FRAMES): 183 trace("light", i) 184