From: Svjatoslav Agejenko Date: Thu, 26 Jun 2025 13:45:37 +0000 (+0300) Subject: Better code readability. X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=commitdiff_plain;h=e6fe84d6cd34ba58a684bfaf44fb545d06a65399;p=qbasicapps.git Better code readability. --- diff --git a/2D GFX/Presentations/AI/AI demo.bas b/2D GFX/Presentations/AI/AI demo.bas index 10b1216..306e0bc 100644 --- a/2D GFX/Presentations/AI/AI demo.bas +++ b/2D GFX/Presentations/AI/AI demo.bas @@ -58,60 +58,60 @@ Scene7 Scene8 Scene9 - -SUB ComputeShadeValue (txOne, tyOne, tzOne, txTwo, tyTwo, tzTwo, txThree, tyThree, tzThree, colorResult) +SUB ComputeShadeValue (point1X, point1Y, point1Z, point2X, point2Y, point2Z, point3X, point3Y, point3Z, shadeValue) ' -' Computes a "shade" or brightness value stored in 'colorResult' by: -' 1) Assigning local copies of the point coordinates. -' 2) Rotating them around multiple axes via GetAngle and RotatePoint. -' 3) Computing a distance 'distCalc' to adjust brightness. +' Computes a brightness value for a 3D triangle based on distance from viewer. +' The algorithm: +' 1. Makes local copies of the three points +' 2. Applies three sequential rotations to simulate 3D perspective +' 3. Calculates distance from viewer to determine shading ' - xLocalOne = txOne - yLocalOne = tyOne - zLocalOne = tzOne + xLocalOne = point1X + yLocalOne = point1Y + zLocalOne = point1Z - xLocalTwo = txTwo - yLocalTwo = tyTwo - zLocalTwo = tzTwo + xLocalTwo = point2X + yLocalTwo = point2Y + zLocalTwo = point2Z - xLocalThree = txThree - yLocalThree = tyThree - zLocalThree = tzThree + xLocalThree = point3X + yLocalThree = point3Y + zLocalThree = point3Z - ' First rotation + ' First rotation around Y-axis GetAngle xLocalOne, yLocalOne, xLocalTwo, yLocalTwo, angleTemp1 RotatePoint xLocalOne, yLocalOne, xLocalTwo, yLocalTwo, -angleTemp1 RotatePoint xLocalOne, yLocalOne, xLocalThree, yLocalThree, -angleTemp1 - ' Second rotation + ' Second rotation around X-axis GetAngle yLocalOne, zLocalOne, yLocalTwo, zLocalTwo, angleTemp2 angleTemp2 = angleTemp2 + globalPi / 2 RotatePoint yLocalOne, zLocalOne, yLocalTwo, zLocalTwo, -angleTemp2 RotatePoint yLocalOne, zLocalOne, yLocalThree, zLocalThree, -angleTemp2 - ' Third rotation + ' Third rotation around Z-axis GetAngle xLocalOne, zLocalOne, xLocalThree, zLocalThree, angleTemp3 angleTemp3 = angleTemp3 + globalPi / 2 RotatePoint xLocalOne, zLocalOne, xLocalTwo, zLocalTwo, -angleTemp3 RotatePoint xLocalOne, zLocalOne, xLocalThree, zLocalThree, -angleTemp3 - xOffset = xLocalOne - yOffset = yLocalOne - zOffset = zLocalOne + 30 - - RotatePoint xLocalOne, zLocalOne, xOffset, zOffset, angleTemp3 - RotatePoint yLocalOne, zLocalOne, yOffset, zOffset, angleTemp2 - RotatePoint xLocalOne, yLocalOne, xOffset, yOffset, angleTemp1 - - ' The distance 'distCalc' is used to compute 'colorResult' - xLocalOne = txOne + 20 - yLocalOne = tyOne + 10 - distCalc = SQR((xLocalOne - xOffset) ^ 2 + (yLocalOne - yOffset) ^ 2) - colorResult = 49 - distCalc - IF colorResult < 0 THEN colorResult = 0 + ' Calculate distance from viewer to determine shading + viewerX = xLocalOne + viewerY = yLocalOne + viewerZ = zLocalOne + 30 + + RotatePoint xLocalOne, zLocalOne, viewerX, viewerZ, angleTemp3 + RotatePoint yLocalOne, zLocalOne, viewerY, viewerZ, angleTemp2 + RotatePoint xLocalOne, yLocalOne, viewerX, viewerY, angleTemp1 + + ' Compute distance from viewer to first point + distanceX = point1X + 20 + distanceY = point1Y + 10 + distance = SQR((distanceX - viewerX) ^ 2 + (distanceY - viewerY) ^ 2) + shadeValue = 49 - distance + IF shadeValue < 0 THEN shadeValue = 0 END SUB - SUB DrawRoundedBox (xOne, yOne, xTwo, yTwo) ' ' Draws a soft-edged rectangular box by fading pixel colors around its edges.