From: Svjatoslav Agejenko Date: Tue, 29 Oct 2024 21:06:00 +0000 (+0200) Subject: Add bump mapping video X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=commitdiff_plain;h=8682221eab09187d753bb2ca3aa36840f2309d53;p=qbasicapps.git Add bump mapping video --- diff --git a/Graphics/Bump mapping.bas b/Graphics/Bump mapping.bas new file mode 100755 index 0000000..65303ae --- /dev/null +++ b/Graphics/Bump mapping.bas @@ -0,0 +1,124 @@ +' Program render bump-map animation. +' By Svjatoslav Agejenko. +' Email: svjatoslav@svjatoslav.eu +' Homepage: http://www.svjatoslav.eu + +' Changelog: +' ?, Initial version +' 2024.08, Improved program readability using AI + +DECLARE SUB makeSurface () +DECLARE SUB animate () +DECLARE SUB makeDot (x!, y!) +DECLARE SUB paintImage () +SCREEN 13 + +' surface height map +DIM SHARED imgHeight(0 TO 50, 0 TO 50) + +PAINT (0, 0), 1 + +makeSurface +paintImage +animate + +SUB animate + + frame = 0 +1 + ' Increment the frame counter + frame = frame + 5 + + ' Calculate light position based on sine functions + lightX = SIN(frame / 100) * 20 + 25 + lightY = SIN(frame / 71.32) * 20 + 25 + lightX = lightX + SIN(frame / 34) * 10 + lightY = lightY + SIN(frame / 45) * 10 + + ' Calculate brightness for each pixel based on distance and angle from light + FOR y = 2 TO 48 + FOR x = 2 TO 48 + distance = SQR((x - lightX) ^ 2 + (y - lightY) ^ 2) + brightness = (30 - distance) / 4 + + ' Calculate surface inclination relative to light location + value = imgHeight(x - 1, y) - imgHeight(x, y) + brightnessX = (lightX - x) * value + + value = imgHeight(x, y - 1) - imgHeight(x, y) + brightnessY = (lightY - y) * value + + brightness = brightness + (brightnessX + brightnessY) / (distance / 2) + + ' Clamp brightness within valid range + IF brightness < 0 THEN brightness = 0 + IF brightness > 15 THEN brightness = 15 + + ' Set pixel color based on brightness + PSET (x + 150, y), 16 + brightness + NEXT x + NEXT y + + ' Draw light source as a circle + CIRCLE (lightX + 150, lightY), 2, 12 + + ' Loop back to the start of animation + GOTO 1 + +END SUB + +SUB makeDot (x, y) + + ' Create a dot with Gaussian distribution + FOR x1 = -10 TO 10 + FOR y1 = -10 TO 10 + dist = SQR(x1 * x1 + y1 * y1) + power = 4 - dist + IF power < 0 THEN power = 0 + + ' Calculate image coordinates + imgX = x1 + x + imgY = y1 + y + + ' Ensure coordinates are within bounds + IF imgX < 0 THEN imgX = 0 + IF imgY < 0 THEN imgY = 0 + IF imgX > 50 THEN imgX = 50 + IF imgY > 50 THEN imgY = 50 + + ' Add power to the height map + imgHeight(imgX, imgY) = imgHeight(imgX, imgY) + power + NEXT y1 + NEXT x1 + +END SUB + +SUB makeSurface + + ' Generate random dots on the surface + FOR x = 0 TO 10 + CALL makeDot(RND * 50, RND * 50) + NEXT x + + ' Add some structured dots to create a pattern + FOR x = 0 TO 45 STEP 2 + CALL makeDot(x, x / 2 + 5) + NEXT x + + FOR x = 5 TO 30 STEP 2 + CALL makeDot(x, -x / 1.2 + 30) + NEXT x + +END SUB + +SUB paintImage + + ' Paint the image based on height map + FOR x = 0 TO 50 + FOR y = 0 TO 50 + clr = imgHeight(x, y) + 16 + PSET (x, y), clr + NEXT y + NEXT x + +END SUB \ No newline at end of file diff --git a/Graphics/Bump mapping.webm b/Graphics/Bump mapping.webm new file mode 100644 index 0000000..4bf57e0 Binary files /dev/null and b/Graphics/Bump mapping.webm differ diff --git a/Graphics/bumpmap.bas b/Graphics/bumpmap.bas deleted file mode 100755 index 65303ae..0000000 --- a/Graphics/bumpmap.bas +++ /dev/null @@ -1,124 +0,0 @@ -' Program render bump-map animation. -' By Svjatoslav Agejenko. -' Email: svjatoslav@svjatoslav.eu -' Homepage: http://www.svjatoslav.eu - -' Changelog: -' ?, Initial version -' 2024.08, Improved program readability using AI - -DECLARE SUB makeSurface () -DECLARE SUB animate () -DECLARE SUB makeDot (x!, y!) -DECLARE SUB paintImage () -SCREEN 13 - -' surface height map -DIM SHARED imgHeight(0 TO 50, 0 TO 50) - -PAINT (0, 0), 1 - -makeSurface -paintImage -animate - -SUB animate - - frame = 0 -1 - ' Increment the frame counter - frame = frame + 5 - - ' Calculate light position based on sine functions - lightX = SIN(frame / 100) * 20 + 25 - lightY = SIN(frame / 71.32) * 20 + 25 - lightX = lightX + SIN(frame / 34) * 10 - lightY = lightY + SIN(frame / 45) * 10 - - ' Calculate brightness for each pixel based on distance and angle from light - FOR y = 2 TO 48 - FOR x = 2 TO 48 - distance = SQR((x - lightX) ^ 2 + (y - lightY) ^ 2) - brightness = (30 - distance) / 4 - - ' Calculate surface inclination relative to light location - value = imgHeight(x - 1, y) - imgHeight(x, y) - brightnessX = (lightX - x) * value - - value = imgHeight(x, y - 1) - imgHeight(x, y) - brightnessY = (lightY - y) * value - - brightness = brightness + (brightnessX + brightnessY) / (distance / 2) - - ' Clamp brightness within valid range - IF brightness < 0 THEN brightness = 0 - IF brightness > 15 THEN brightness = 15 - - ' Set pixel color based on brightness - PSET (x + 150, y), 16 + brightness - NEXT x - NEXT y - - ' Draw light source as a circle - CIRCLE (lightX + 150, lightY), 2, 12 - - ' Loop back to the start of animation - GOTO 1 - -END SUB - -SUB makeDot (x, y) - - ' Create a dot with Gaussian distribution - FOR x1 = -10 TO 10 - FOR y1 = -10 TO 10 - dist = SQR(x1 * x1 + y1 * y1) - power = 4 - dist - IF power < 0 THEN power = 0 - - ' Calculate image coordinates - imgX = x1 + x - imgY = y1 + y - - ' Ensure coordinates are within bounds - IF imgX < 0 THEN imgX = 0 - IF imgY < 0 THEN imgY = 0 - IF imgX > 50 THEN imgX = 50 - IF imgY > 50 THEN imgY = 50 - - ' Add power to the height map - imgHeight(imgX, imgY) = imgHeight(imgX, imgY) + power - NEXT y1 - NEXT x1 - -END SUB - -SUB makeSurface - - ' Generate random dots on the surface - FOR x = 0 TO 10 - CALL makeDot(RND * 50, RND * 50) - NEXT x - - ' Add some structured dots to create a pattern - FOR x = 0 TO 45 STEP 2 - CALL makeDot(x, x / 2 + 5) - NEXT x - - FOR x = 5 TO 30 STEP 2 - CALL makeDot(x, -x / 1.2 + 30) - NEXT x - -END SUB - -SUB paintImage - - ' Paint the image based on height map - FOR x = 0 TO 50 - FOR y = 0 TO 50 - clr = imgHeight(x, y) + 16 - PSET (x, y), clr - NEXT y - NEXT x - -END SUB \ No newline at end of file