'=========================================================================== ' Subject: 3D LANDSCAPE W/MOVING VIEWPOINT Date: 07-25-99 (12:02) ' Author: Andries Mooij Code: QB, QBasic, PDS ' Origin: santa@tir.com Packet: GRAPHICS.ABC '=========================================================================== DEFINT A-Z TYPE Vector X AS INTEGER Y AS INTEGER Z AS INTEGER XW AS INTEGER YW AS INTEGER ZW AS INTEGER XV AS INTEGER YV AS INTEGER OX AS INTEGER OY AS INTEGER END TYPE TYPE SimpleVector X AS INTEGER Y AS INTEGER Z AS INTEGER END TYPE TYPE LineType P1 AS INTEGER P2 AS INTEGER END TYPE TYPE Colors R AS INTEGER G AS INTEGER B AS INTEGER END TYPE RANDOMIZE TIMER Dots = 400: SQRD = INT(SQR(Dots)): div! = 2 DIM Object(Dots) AS Vector DIM Lines(Dots) AS LineType DIM Sine(450) AS LONG DIM CameraAdds(2000) AS SimpleVector DIM Pal(256) AS Colors, Order(Dots), ZC(Dots) FOR I = 0 TO Dots - 1 IF (I + 1) \ SQRD = (I + 1) / SQRD THEN Tg = -1 IF NOT Tg THEN Lines(I).P1 = I + 1 ELSE Lines(I).P1 = I Tg = NOT Tg Tg2 = NOT Tg2 END IF IF I <= Dots - 1 - SQRD THEN Lines(I).P2 = I + SQRD ELSE Lines(I).P2 = I NEXT '256/Dots *2 div! = 3 Rad! = 5.72958 / div! FOR X = 0 TO SQRD - 1 FOR Z = 0 TO SQRD - 1 I = X * SQRD + Z Object(I).X = X * 4 * div! Object(I).Y = 30 * (SIN(X / Rad!) + SIN(Z / Rad!)) Object(I).Z = Z * 4 * div! NEXT NEXT FOR I = 0 TO Dots - 1 X& = X& + Object(I).X Y& = Y& + Object(I).Y Z& = Z& + Object(I).Z NEXT X& = X& \ Dots: Y& = Y& \ Dots: Z& = Z& \ Dots FOR I = 0 TO Dots - 1 Object(I).X = Object(I).X - X& Object(I).Y = Object(I).Y - Y& Object(I).Z = Object(I).Z - Z& Object(I).YW = Object(I).Y NEXT I = 0 FOR I! = 0 TO 449 / 57.2958 STEP 1 / 57.2958 Sine(I) = SIN(I!) * 1024: I = I + 1 NEXT EyeX = 0 EyeY = 0 EyeZ = 400 SCREEN 13 FOR I = 0 TO 31 Pal(I).B = I * 2 NEXT FOR I = 0 TO 31 Pal(I + 32).R = I * 2 Pal(I + 32).G = I * 2 Pal(I + 32).B = 63 - (I * 2) NEXT FOR I = 0 TO 31 Pal(I + 64).R = 63 - (I * 2) Pal(I + 64).G = 63 NEXT FOR I = 0 TO 31 Pal(I + 96).R = I * 1.354839 Pal(I + 96).G = 63 - I / 1.47619 NEXT FOR I = 0 TO 31 R = 42 + I * 1.3125 G = 42 + I * 1.3125 B = I * 4 IF R > 63 THEN R = 63 IF G > 63 THEN G = 63 IF B > 63 THEN B = 63 Pal(I + 128).R = R Pal(I + 128).G = G Pal(I + 128).B = B NEXT FOR ix = 0 TO 63 OUT &H3C8, 0 FOR I = 0 TO 255 OUT &H3C9, ix OUT &H3C9, ix OUT &H3C9, ix NEXT NEXT A3 = 0 GOSUB SetupCameraMovement GotoNormal = -1 Frame& = 0 RE: xc! = TIMER DO IF GotoNormal THEN OUT &H3C8, 0 FOR I = 0 TO 255 R = ix + Pal(I).R G = ix + Pal(I).G B = ix + Pal(I).B IF R > 63 THEN R = 63 IF G > 63 THEN G = 63 IF B > 63 THEN B = 63 OUT &H3C9, R OUT &H3C9, G OUT &H3C9, B NEXT ix = ix - 1 IF ix < 0 THEN GotoNormal = NOT GotoNormal END IF IF GotoWhite THEN OUT &H3C8, 0 FOR I = 0 TO 255 R = Pal(I).R + ix G = Pal(I).G + ix B = Pal(I).B + ix IF R > 63 THEN R = 63 IF G > 63 THEN G = 63 IF B > 63 THEN B = 63 OUT &H3C9, R OUT &H3C9, G OUT &H3C9, B NEXT ix = ix + 1 IF ix >= 64 THEN GOTO StopIt END IF EyeX = EyeX + CameraAdds(CurFrame).X EyeY = EyeY + CameraAdds(CurFrame).Y EyeZ = EyeZ + CameraAdds(CurFrame).Z CurFrame = (CurFrame + 1) MOD 2001 IF CurFrame < 1001 THEN a1 = (a1 + 1) MOD 360 ELSE a1 = a1 - 1 IF a1 < 0 THEN a1 = a1 + 360 END IF s1& = Sine(a1): c1& = Sine(a1 + 90) FOR I = 0 TO Dots - 1 XO = Object(I).X: YO = Object(I).Y: ZO = Object(I).Z Object(I).XW = (XO * c1& - ZO * s1&) \ 1024 Object(I).ZW = (XO * s1& + ZO * c1&) \ 1024 Object(I).OX = Object(I).XV Object(I).OY = Object(I).YV z4 = Object(I).ZW - EyeZ IF z4 <> 0 THEN X! = (Object(I).XW - EyeX) / z4 Y! = (Object(I).YW - EyeY) / z4 IF X! > -90 AND X! < 90 THEN Object(I).XV = X! * 320 + 160 IF Y! > -90 AND Y! < 90 THEN Object(I).YV = Y! * 200 + 100 END IF NEXT FOR I = 0 TO Dots - 1 P1 = Lines(I).P1: P2 = Lines(I).P2: P3 = I Order(I) = I: ZC(I) = Object(P1).ZW + Object(P2).ZW + Object(P3).ZW NEXT Dotz = Dots - 1 Mid = (Dotz + 1) \ 2 DO FOR a = 0 TO Dotz - Mid CompareLow = a CompareHigh = a + Mid DO WHILE ZC(Order(CompareLow)) > ZC(Order(CompareHigh)) SWAP Order(CompareLow), Order(CompareHigh) CompareHigh = CompareLow CompareLow = CompareLow - Mid IF CompareLow < 0 THEN EXIT DO LOOP NEXT Mid = Mid \ 2 LOOP WHILE Mid > 0 FOR I = 0 TO Dots - 2 It = Order(I) P1 = Lines(It).P1: P2 = Lines(It).P2: P3 = It x1 = Object(P1).OX: x2 = Object(P2).OX: x3 = Object(P3).OX y1 = Object(P1).OY: y2 = Object(P2).OY: y3 = Object(P3).OY dp1 = x1 < 0 OR x1 > 319 AND y1 < 0 OR y1 > 319 dp2 = x2 < 0 OR x2 > 319 AND y2 < 0 OR y2 > 319 dp3 = x3 < 0 OR x3 > 319 AND y3 < 0 OR y3 > 319 IF NOT dp1 OR NOT dp3 THEN LINE (x3, y3)-(x1, y1), 0 IF NOT dp2 OR NOT dp3 THEN LINE (x3, y3)-(x2, y2), 0 IF Object(P1).ZW < EyeZ AND Object(P2).ZW < EyeZ AND Object(P3).ZW < EyeZ THEN x1 = Object(P1).XV: x2 = Object(P2).XV: x3 = Object(P3).XV y1 = Object(P1).YV: y2 = Object(P2).YV: y3 = Object(P3).YV c = ((Object(P3).YW + 72) * 14) \ 10 IF c > 143 THEN c = 143 LINE (x3, y3)-(x1, y1), c LINE (x3, y3)-(x2, y2), c END IF NEXT I$ = INKEY$ IF GotoWhite OR GotoNormal THEN I$ = "" LOOP WHILE I$ = "" GotoWhite = -1: ix = 1 GOTO RE StopIt: FOR ix = 63 TO 0 STEP -1 OUT &H3C8, 0 FOR I = 0 TO 255 OUT &H3C9, ix OUT &H3C9, ix OUT &H3C9, ix NEXT NEXT SCREEN 0: WIDTH 80 PRINT "Hill by Illegal Opcode [aka Andries Mooij] (C) 1998" PRINT "A simple example of 3d landscapes and a moving viewpoint" PRINT "Want to see more cool stuff ? Visit my homepage" PRINT "www.geocities.com/SiliconValley/Orchard/5183" END SetupCameraMovement: FOR I = 0 TO 80 CameraAdds(I).Z = -2 NEXT FOR I = 81 TO 140 CameraAdds(I).Z = -4 IF I < 95 THEN CameraAdds(I).X = -2 IF I >= 95 AND I < 110 THEN CameraAdds(I).X = -1 IF I >= 110 THEN CameraAdds(I).X = 1 NEXT FOR I = 141 TO 182 IF I <= 152 THEN CameraAdds(I).X = 1 CameraAdds(I).Z = 1 NEXT FOR I = 265 TO 330 CameraAdds(I).Z = -3 IF I < 301 THEN CameraAdds(I).X = 1 IF I >= 300 THEN CameraAdds(I).Y = 1 NEXT CameraAdds(330).X = 250 CameraAdds(330).Z = 400 FOR I = 331 TO 480 CameraAdds(I).X = -2 NEXT FOR I = 481 TO 600 CameraAdds(I).Y = 1 IF I > 540 THEN CameraAdds(I).Z = 2 IF I < 499 THEN CameraAdds(I).X = 1 NEXT FOR I = 601 TO 676 CameraAdds(I).Y = -2 CameraAdds(I).Z = 2 NEXT FOR I = 678 TO 760 IF I <= 759 THEN CameraAdds(I).Z = -6 IF I >= 730 AND I <= 750 THEN CameraAdds(I).X = -2 IF I >= 753 THEN CameraAdds(I).X = 5 NEXT FOR I = 761 TO 800 CameraAdds(I).Y = -1 NEXT FOR I = 800 TO 1000 IF I < 862 THEN CameraAdds(I).Y = 1 IF I > 861 THEN CameraAdds(I).Z = -4 NEXT CameraAdds(1000).X = -282 CameraAdds(1000).Z = 827 CameraAdds(1000).Y = 32 FOR I = 1001 TO 1140 CameraAdds(I).X = 2 IF I < 1028 THEN CameraAdds(I).Y = -2 NEXT FOR I = 1141 TO 1500 CameraAdds(I).Y = COS((I - 1141) / 5.72958) * 10 NEXT FOR I = 1501 TO 1680 CameraAdds(I).X = COS((I - 1501) / 5.72958) * 10 NEXT FOR I = 1681 TO 1880 CameraAdds(I).Z = 1 NEXT FOR I = 1881 TO 1980 CameraAdds(I).Z = -1 NEXT RETURN '-12,-2