09-04-2024, 11:41 PM
Hey @bplus I've been playing around with your image to sphere code. (cool by the way!)
I've whittled it down to the bare essentials and understand most of it. Could you please explain to me what lines 54 and 57 of the code are doing?
I've whittled it down to the bare essentials and understand most of it. Could you please explain to me what lines 54 and 57 of the code are doing?
Code: (Select All)
CONST wW = 1280 ' screen width
CONST wH = 720 ' screen height
DIM x AS INTEGER ' x center of sphere
DIM y AS INTEGER ' y center of sphere
DIM map AS LONG ' map image
SCREEN _NEWIMAGE(wW, wH, 32)
map = _LOADIMAGE("worldmap.png")
x = wW / 2 ' center sphere horizontally on screen
y = wH / 2 ' center sphere vertically on screen
WHILE _KEYDOWN(27) = 0 ' ESC key pressed?
xoff = (xoff + 4) MOD (_WIDTH(map) + 1) ' no, rotate left (can't go right because of MOD use)
projectImagetoSphere map, x, y, 300, xoff ' map image to sphere
_DISPLAY
_LIMIT 60
WEND
SUB projectImagetoSphere (image AS LONG, x0 AS INTEGER, y0 AS INTEGER, sr AS INTEGER, xo AS INTEGER)
' image : image passed in
' x0 : center x location of sphere
' y0 : center y location of sphere
' sr : sphere radius
' xo : image x offset
' Note: use x1 = _HYPOT(r, y) to map to a funnel structure
DIM iW AS INTEGER ' width of image
DIM iH AS INTEGER ' height of image
DIM r AS SINGLE ' half image height
DIM scale AS SINGLE ' proportion of sphere radius to image height
DIM x AS INTEGER ' location along horizontal line within sphere
DIM y AS INTEGER ' vertical location within image
DIM x1 AS SINGLE ' length of horizontal line within sphere
DIM tv AS SINGLE
DIM tu AS SINGLE
DIM pc AS _UNSIGNED LONG ' POINT on image
DIM oSource AS LONG ' calling SOURCE
oSource = _SOURCE ' get calling source
_SOURCE image ' POINT data will be retrieved from image
iW = _WIDTH(image) ' get width of image
iH = _HEIGHT(image) ' get height of image
r = iH / 2 ' calculate image half height
scale = sr / r ' calculate proportion of radius to image height
y = -r ' start at top of image
DO ' begin vertical loop
x1 = SQR(r * r - y * y) ' length of line across sphere at y location
tv = (_ASIN(y / r) + 1.5) / 3
x = -x1 + 1 ' start at left side of sphere
DO ' begin horizontal loop
tu = (_ASIN(x / x1) + 1.5) / 6
pc = POINT((xo + tu * iW) MOD iW, tv * iH)
PSET (x * scale + x0, y * scale + y0), pc
x = x + 1 ' move one pixel across image to the right
LOOP UNTIL x > x1 ' leave when right side of image reached
y = y + 1 ' move one pixel down the image
LOOP UNTIL y > r ' leave when bottom of image reached
_SOURCE oSource ' restore calling source
END SUB