Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Working on ways to map images onto balls
#1
Was adding more textures to my BALL sub Here, and thought why not just use textured images on the balls instead.   Spherical mapping is a little above me, but here's 2 ways I've worked out so far that gives and illusion that an image is wrapped around the ball.  Will post them here for input (advice/help is more like it...)

There are 2 SUBs.  One maps the image in a circular method from the center point, the other SUB just stamps the image it plain jane on the front of the ball.  The demo makes a texture& image to use, but can load one yourself instead.  Image textures look better with the circular method I think.

if anyone have some routines for doing something like this, could you share them?  Thanks!

- Dav

Code: (Select All)
'ImageBalls.bas

'Working on a couple ways to put an image on a ball.
'Two SUB's - either pset it straight, or in a circular method from the center point.
'Coded by Dav, SEP/2023

_Icon

'Get a bird& image from the built-in ICON
bird& = _NewImage(192, 192, 32): _Dest bird&
Cls , _RGB(200, 200, 0)
_PutImage (0, 0)-(192, 192), -11: _Dest 0

Screen _NewImage(800, 600, 32)

'Make sample texture& image to use, or load your own with _LOADIMAGE
texture& = _NewImage(200, 200, 32)
_Dest texture&
Cls , _RGB(33, 66, 99)
For x = 0 To _Width Step 25
    For y = 0 To _Height Step 25
        Line (x, y)-Step(50, 50), _RGBA(Rnd * 255, Rnd * 255, Rnd * 255, Rnd * 255), BF
    Next
Next
'_PutImage (0, 0), bird&
_PrintMode _KeepBackground
_PrintString (50, 90), "QB64-PE POWER"
_Dest 0

'Or load your own image like below
'_FreeImage texture&
'texture& = _LoadImage("brickwall.jpg", 32)

Do
    ImageBall Rnd * _Width, Rnd * _Height, 50 + Rnd * 150, texture&
    ImageBall2 Rnd * _Width, Rnd * _Height, 50 + Rnd * 150, texture&
    ImageBall2 Rnd * _Width, Rnd * _Height, 50 + Rnd * 50, bird&
    _Limit 5
Loop Until _KeyHit

Sub ImageBall (x, y, size, image&)
    'Puts image in a center circle
    orig& = _Source
    _Source image&
    For y2 = y - size To y + size
        For x2 = x - size To x + size
            dis = Sqr((x2 - x) ^ 2 + (y2 - y) ^ 2)
            If dis <= size Then
                ix = ((_Atan2(y2 - y, x2 - x) + _Pi) / (2 * _Pi)) * (_Width(image&) - 1)
                iy = ((_Acos((dis / size) * 2 - 1)) / _Pi) * (_Height(image&) - 1)
                c& = Point(ix, iy): r = _Red32(c&): g = _Green32(c&): b = _Blue32(c&)
                PSet (x2, y2), _RGB(r - dis, g - dis, b - dis)
            End If
        Next
    Next
    _Source orig&
End Sub

Sub ImageBall2 (x, y, size, image&)

    'puts image on straight.
    'To make it look better, make a temp image to match the size of the ball
    temp& = _NewImage(size * 2, size * 2, 32)
    'put the image& into temp&, stretch to fill
    _PutImage (0, 0)-(_Width(temp&), _Height(temp&)), image&, temp&

    orig& = _Source
    _Source temp&
    For y2 = y - size To y + size
        For x2 = x - size To x + size
            If Sqr((x2 - x) ^ 2 + (y2 - y) ^ 2) <= size Then
                clr = (size - (Sqr((x2 - x) * (x2 - x) + (y2 - y) * (y2 - y)))) / size
                c& = Point(x2 - x + size, y2 - y + size): r = _Red32(c&): g = _Green32(c&): b = _Blue32(c&)
                PSet (x2, y2), _RGB(clr * r, clr * g, clr * b)
            End If
        Next
    Next
    _FreeImage temp&
    _Source orig&
End Sub

Find my programs here in Dav's QB64 Corner
Reply
#2
(10-04-2023, 11:48 PM)Dav Wrote: Was adding more textures to my BALL sub Here, and thought why not just use textured images on the balls instead.   Spherical mapping is a little above me, but here's 2 ways I've worked out so far that gives and illusion that an image is wrapped around the ball.  Will post them here for input (advice/help is more like it...)

There are 2 SUBs.  One maps the image in a circular method from the center point, the other SUB just stamps the image it plain jane on the front of the ball.  The demo makes a texture& image to use, but can load one yourself instead.  Image textures look better with the circular method I think.

if anyone have some routines for doing something like this, could you share them?  Thanks!

- Dav

Code: (Select All)
'ImageBalls.bas

'Working on a couple ways to put an image on a ball.
'Two SUB's - either pset it straight, or in a circular method from the center point.
'Coded by Dav, SEP/2023

_Icon

'Get a bird& image from the built-in ICON
bird& = _NewImage(192, 192, 32): _Dest bird&
Cls , _RGB(200, 200, 0)
_PutImage (0, 0)-(192, 192), -11: _Dest 0

Screen _NewImage(800, 600, 32)

'Make sample texture& image to use, or load your own with _LOADIMAGE
texture& = _NewImage(200, 200, 32)
_Dest texture&
Cls , _RGB(33, 66, 99)
For x = 0 To _Width Step 25
    For y = 0 To _Height Step 25
        Line (x, y)-Step(50, 50), _RGBA(Rnd * 255, Rnd * 255, Rnd * 255, Rnd * 255), BF
    Next
Next
'_PutImage (0, 0), bird&
_PrintMode _KeepBackground
_PrintString (50, 90), "QB64-PE POWER"
_Dest 0

'Or load your own image like below
'_FreeImage texture&
'texture& = _LoadImage("brickwall.jpg", 32)

Do
    ImageBall Rnd * _Width, Rnd * _Height, 50 + Rnd * 150, texture&
    ImageBall2 Rnd * _Width, Rnd * _Height, 50 + Rnd * 150, texture&
    ImageBall2 Rnd * _Width, Rnd * _Height, 50 + Rnd * 50, bird&
    _Limit 5
Loop Until _KeyHit

Sub ImageBall (x, y, size, image&)
    'Puts image in a center circle
    orig& = _Source
    _Source image&
    For y2 = y - size To y + size
        For x2 = x - size To x + size
            dis = Sqr((x2 - x) ^ 2 + (y2 - y) ^ 2)
            If dis <= size Then
                ix = ((_Atan2(y2 - y, x2 - x) + _Pi) / (2 * _Pi)) * (_Width(image&) - 1)
                iy = ((_Acos((dis / size) * 2 - 1)) / _Pi) * (_Height(image&) - 1)
                c& = Point(ix, iy): r = _Red32(c&): g = _Green32(c&): b = _Blue32(c&)
                PSet (x2, y2), _RGB(r - dis, g - dis, b - dis)
            End If
        Next
    Next
    _Source orig&
End Sub

Sub ImageBall2 (x, y, size, image&)

    'puts image on straight.
    'To make it look better, make a temp image to match the size of the ball
    temp& = _NewImage(size * 2, size * 2, 32)
    'put the image& into temp&, stretch to fill
    _PutImage (0, 0)-(_Width(temp&), _Height(temp&)), image&, temp&

    orig& = _Source
    _Source temp&
    For y2 = y - size To y + size
        For x2 = x - size To x + size
            If Sqr((x2 - x) ^ 2 + (y2 - y) ^ 2) <= size Then
                clr = (size - (Sqr((x2 - x) * (x2 - x) + (y2 - y) * (y2 - y)))) / size
                c& = Point(x2 - x + size, y2 - y + size): r = _Red32(c&): g = _Green32(c&): b = _Blue32(c&)
                PSet (x2, y2), _RGB(clr * r, clr * g, clr * b)
            End If
        Next
    Next
    _FreeImage temp&
    _Source orig&
End Sub

Wow, very impressive - even to someone with my superior skillset!   Rolleyes
If that's your early attempt, I can't wait to see where it will go!
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, W.A.) Big Grin
Please visit my Website at: http://oldendayskids.blogspot.com/
Reply




Users browsing this thread: 1 Guest(s)