§ Иллюстрация
Сфера и плоскость с теневой функцией кохлеарной репагулярности биполярной математики
§ Код
1SCREEN 13 2 3TYPE vec3 4 x AS SINGLE 5 y AS SINGLE 6 z AS SINGLE 7END TYPE 8 9DIM cam AS vec3, ctr AS vec3, ctn AS vec3 10DIM lpt AS vec3, lno AS vec3, ltn AS vec3 11DIM nor AS vec3, dir AS vec3 12DIM ref AS vec3, pln AS vec3 13 14SetPal 15 16' Directional light 17dir.x = 2 18dir.y = 2 19dir.z = 0 20normvec3 dir 21 22' Landscape 23pln.y = -4 24 25' Center of sphere 26ctr.x = 0 27ctr.y = 0 28ctr.z = 16 29 30r = 3 31speed = 1 / 30 32 33WHILE 1 34 35 ' Light point 36 lpt.x = 8 * COS(move * speed) 37 lpt.y = 1 38 lpt.z = 9 39 40 ' Ball position 41 ctr.y = ABS(r * COS(move * speed)) + pln.y + r 42 43 FOR y = 100 TO -99 STEP -1 44 FOR x = -160 TO 160 45 46 sx = x + 160 47 sy = 100 - y 48 49 ' Init cam vector 50 cam.x = x / 200 51 cam.y = y / 200 52 cam.z = 1 53 54 ' Search sphere intersect 55 t = intersec(cam, ctr, r) 56 57 ' Found 58 IF t > 0 AND cam.y * t > pln.y THEN 59 60 ' Calculate normal and point of intersect 61 mul3vec cam, t ' cam *= t 62 sub3vec nor, cam, ctr ' nor = cam - ctr 63 sub3vec lno, lpt, cam ' lno = lpt - cam 64 mul3vec cam, -1 ' cam = -cam 65 66 ' Normalize vectors 67 normvec3 nor 68 normvec3 cam 69 normvec3 lno 70 71 ' Reflect camera & normal 72 refl = 2 * mul3(cam, nor) 73 ref.x = refl * nor.x - cam.x 74 ref.y = refl * nor.y - cam.y 75 ref.z = refl * nor.z - cam.z 76 normvec3 ref 77 78 ' Directional and specular 79 ldir = mul3(nor, dir): IF ldir < 0 THEN ldir = 0 80 lspe = mul3(ref, lno): IF lspe < 0 THEN lspe = 0 ELSE lspe = lspe ^ 7 81 82 ' Total Light 83 lv = ldir * .8 + lspe * .5 84 IF lv < 0 THEN lv = 0 85 IF lv > 1 THEN lv = 1 86 87 PSET (sx, sy), lv * 63 + 64 88 89 ELSE 90 91 ' Only floor 92 IF y < 0 THEN 93 94 IF cam.y = 0 THEN cam.y = 1 95 96 ' Point of intersection of floor 97 pln.z = pln.y / cam.y 98 pln.x = cam.x * pln.z 99 100 u = INT(pln.x) 101 v = INT(pln.z) 102 c = ((v + move) XOR (u + 16)) AND 63 103 104 c = 16 * c / pln.z 105 IF c > 63 THEN c = 63 106 107 ' Search intersection of ball, light and floor 108 sub3vec ctn, ctr, pln 109 sub3vec ltn, lpt, pln 110 111 ' -------------------------------------- 112 ' LTN = LPT (Light Pointer) - Plane(XYZ) 113 ' CTN = CTR (Center Sphere) - Plane(XYZ) 114 ' -------------------------------------- 115 116 t = intersec(ltn, ctn, r) 117 IF t > 0 THEN c = c \ 2 118 119 PSET (sx, sy), 64 + c 120 121 ELSE 122 123 PSET (sx, sy), 0 124 125 END IF 126 127 END IF 128 129 NEXT 130 NEXT 131 132 ' I like'd moved moved! 133 move = move + 1 134 135WEND 136 137FUNCTION intersec (cam AS vec3, ctr AS vec3, r AS SINGLE) 138 139 a = mul3(cam, cam) 140 b = -mul3(cam, ctr) * 2 141 c = mul3(ctr, ctr) - r ^ 2 142 d = b ^ 2 - 4 * a * c 143 t = 0 144 145 IF d >= 0 THEN 146 147 d = SQR(d) 148 t1 = (-b - d) / (2 * a) 149 t2 = (-b + d) / (2 * a) 150 151 IF t1 < 0 AND t2 < 0 THEN t = -1 152 IF t1 < 0 AND t2 > 0 THEN t = t2 153 IF t1 > 0 AND t2 < 0 THEN t = t1 154 IF t1 > 0 AND t2 > 0 THEN 155 IF t1 < t2 THEN t = t1 ELSE t = t2 156 END IF 157 158 END IF 159 160 intersec = t 161 162END FUNCTION 163 164FUNCTION length (a AS vec3) 165 length = SQR(mul3(a, a)) 166END FUNCTION 167 168FUNCTION mul3 (a AS vec3, b AS vec3) 169 mul3 = a.x * b.x + a.y * b.y + a.z * b.z 170END FUNCTION 171 172SUB mul3vec (v AS vec3, k AS SINGLE) 173 v.x = v.x * k 174 v.y = v.y * k 175 v.z = v.z * k 176END SUB 177 178SUB sub3vec (c AS vec3, a AS vec3, b AS vec3) 179 c.x = a.x - b.x 180 c.y = a.y - b.y 181 c.z = a.z - b.z 182END SUB 183 184SUB normvec3 (v AS vec3) 185 mul3vec v, 1 / length(v) 186END SUB 187 188SUB SetPal 189FOR i = 0 TO 63 190 OUT 968, i + 64 191 OUT 969, i 192 OUT 969, i 193 OUT 969, i 194NEXT 195END SUB