From 4700b78a9ee685bf84867aa7108c362b9332cf85 Mon Sep 17 00:00:00 2001 From: Svjatoslav Agejenko Date: Sun, 18 Aug 2024 12:54:27 +0300 Subject: [PATCH] Better directory names --- Graphics/3D/3dland.bas | 169 ++++++++++++++++++++++++----------------- 1 file changed, 101 insertions(+), 68 deletions(-) diff --git a/Graphics/3D/3dland.bas b/Graphics/3D/3dland.bas index eac4cfb..5d6e4d2 100755 --- a/Graphics/3D/3dland.bas +++ b/Graphics/3D/3dland.bas @@ -1,81 +1,114 @@ -' Svjatoslav Agejenko -' year 1999 - -DECLARE SUB setpal () -DEFINT A-Y -DECLARE SUB box (x1, y1, x2, y2, x3, y3, x4, y4, c) 'draw filled - 'box using 4 cordinates - '(sometimes don't work - 'correctly, but fast. - '(PAINT command used)) -DIM SHARED x1(1 TO 40, 1 TO 40) ' X & Y cordinates -DIM SHARED y1(1 TO 40, 1 TO 40) ' +DECLARE SUB DrawBox (x1%, y1%, x2%, y2%, x3%, y3%, x4%, y4%, c1%) -SCREEN 12 - -setpal -zfa = 1.5 -1 -FOR b = 1 TO 40 -FOR a = 1 TO 40 - -x = 120 + (a * 10) -y = 200 + (b * 3) - -y = y - COS(SQR((a - 20) ^ 2 + (b - 20) ^ 2) / zfa) * 20 - -x = (x - 320) * (b + 50) / 50 + 320 -y = (y - 240) * (b + 50) / 50 + 240 - -x1(a, b) = x -y1(a, b) = y +' Program to render 3D shaded landscape with perspective and distortion effects. +' By Svjatoslav Agejenko. +' Email: svjatoslav@svjatoslav.eu +' Homepage: http://www.svjatoslav.eu +' +' Changelog: +' 1999, Initial version +' 2024.08, Improved program readability using AI -NEXT a -NEXT b +DECLARE SUB SetPalette () +DEFINT A-Z +' Declare shared arrays for X and Y coordinates +DIM SHARED xCoordinates(1 TO 40, 1 TO 40) +DIM SHARED yCoordinates(1 TO 40, 1 TO 40) +' Set screen mode to 12 +SCREEN 12 -FOR b = 1 TO 39 -FOR a = 1 TO 39 -IF (a + b) \ 2 = (a + b + 1) \ 2 THEN c = 0 ELSE c = 5 -kz = b + (a / 3) - -box x1(a, b), y1(a, b), x1(a + 1, b), y1(a + 1, b), x1(a, b + 1), y1(a, b + 1), x1(a + 1, b + 1), y1(a + 1, b + 1), c -NEXT a -NEXT b - -a$ = INPUT$(1) -zfa = zfa * 1.9 +' Initialize color palette +SetPalette + +' Set initial scaling factor +scalingFactor = 1.5 + +' Main loop start +1 : +' Loop through each point in the grid +FOR rowIndex = 1 TO 40 + FOR colIndex = 1 TO 40 + ' Calculate the position with distortion + xPosition = 120 + (colIndex * 10) + yPosition = 200 + (rowIndex * 3) + + ' Apply a cosine distortion based on distance from center + yPosition = yPosition - COS(SQR((colIndex - 20) ^ 2 + (rowIndex - 20) ^ 2) / scalingFactor) * 20 + + ' Apply perspective transformation + xPosition = (xPosition - 320) * (rowIndex + 50) / 50 + 320 + yPosition = (yPosition - 240) * (rowIndex + 50) / 50 + 240 + + ' Store the transformed coordinates + xCoordinates(colIndex, rowIndex) = xPosition + yCoordinates(colIndex, rowIndex) = yPosition + NEXT colIndex +NEXT rowIndex + +' Draw boxes based on the stored coordinates +FOR rowIndex = 1 TO 39 + FOR colIndex = 1 TO 39 + ' Alternate colors for each box + IF (colIndex + rowIndex) \ 2 <> (colIndex + rowIndex + 1) \ 2 THEN colorIndex = 0 ELSE colorIndex = 5 + ' Calculate a brightness factor + brightnessFactor = rowIndex + (colIndex / 3) + + ' Draw the box with the calculated color and brightness + DrawBox xCoordinates(colIndex, rowIndex), yCoordinates(colIndex, rowIndex), xCoordinates(colIndex + 1, rowIndex), yCoordinates(colIndex + 1, rowIndex), xCoordinates(colIndex, rowIndex + 1), yCoordinates(colIndex, rowIndex + 1), xCoordinates( _ +colIndex + 1, rowIndex + 1), yCoordinates(colIndex + 1, rowIndex + 1), colorIndex + NEXT colIndex +NEXT rowIndex + +' Wait for user input and adjust the scaling factor +userInput$ = INPUT$(1) +scalingFactor = scalingFactor * 1.9 + +' Clear the screen for the next iteration CLS -IF zfa > 10 THEN SYSTEM -GOTO 1 - -SUB box (x1, y1, x2, y2, x3, y3, x4, y4, c1) -c1 = c1 + (y2 - y1) / 3.5 + (kz / 8) + 4 +' If the scaling factor is too large, exit the program +IF scalingFactor > 10 THEN SYSTEM -IF c1 < 0 THEN c1 = 0 -IF c1 > 15 THEN c1 = 15 +' Jump back to the main loop start +GOTO 1 -a = SQR((x1 - x2) ^ 2 + (y1 - y2) ^ 2) -b = SQR((x3 - x4) ^ 2 + (y3 - y4) ^ 2) -IF b < a THEN b = a -FOR a = 1 TO b -x5 = (x2 - x1) * a / b + x1 -y5 = (y2 - y1) * a / b + y1 -x6 = (x4 - x3) * a / b + x3 -y6 = (y4 - y3) * a / b + y3 -LINE (x5, y5)-(x6, y6), c1 -LINE (x5 + 1, y5)-(x6 + 1, y6), c1 -NEXT a +' Subroutine to draw a filled box using line drawing +SUB DrawBox (x1, y1, x2, y2, x3, y3, x4, y4, c1) + ' Adjust color index based on position and brightness factor + c1 = c1 + (y2 - y1) / 3.5 + (brightnessFactor / 8) + 4 + + ' Ensure the color index is within valid range + IF c1 < 0 THEN c1 = 0 + IF c1 > 15 THEN c1 = 15 + + ' Calculate the length of the longer side + a = SQR((x1 - x2) ^ 2 + (y1 - y2) ^ 2) + b = SQR((x3 - x4) ^ 2 + (y3 - y4) ^ 2) + IF b < a THEN b = a + + ' Draw the box using lines + FOR lineIndex = 1 TO b + x5 = (x2 - x1) * lineIndex / b + x1 + y5 = (y2 - y1) * lineIndex / b + y1 + x6 = (x4 - x3) * lineIndex / b + x3 + y6 = (y4 - y3) * lineIndex / b + y3 + + ' Draw two adjacent lines to create a filled effect + LINE (x5, y5)-(x6, y6), c1 + LINE (x5 + 1, y5)-(x6 + 1, y6), c1 + NEXT lineIndex END SUB -SUB setpal -FOR a = 0 TO 16 -OUT &H3C8, a -OUT &H3C9, a * 4 -OUT &H3C9, a * 4 -OUT &H3C9, a * 3 -NEXT +' Subroutine to initialize color palette +SUB SetPalette + FOR paletteIndex = 1 TO 16 + ' Set the color values for each palette entry + OUT &H3C8, paletteIndex + OUT &H3C9, paletteIndex * 4 + OUT &H3C9, paletteIndex * 4 + OUT &H3C9, paletteIndex * 3 + NEXT paletteIndex END SUB -- 2.20.1