Author Topic: A little shape interpolation snippet  (Read 6762 times)

0 Members and 1 Guest are viewing this topic.

Offline Hezad

  • Sponsor
  • Pentium
  • *******
  • Posts: 613
  • Karma: 44
  • I believe .. in Patrick.
    • View Profile
    • Hezad.com Web hosting
A little shape interpolation snippet
« on: September 30, 2008 »
Hey again :P

I wanted to code something with interpolation (Since I discovered it, I just loved it :D) to decompress a bit between two code sessions on my 3d engine.

So here it is, nothing's outstanding, it's not optimized AT ALL nor efficiently designed but I wanted to share it :)

Code: [Select]
Const MAX_PNT = 20

Randomize timer

Type PntType
    as single x,y
End Type

Type ShapeType
    as integer NbPoints
   
    as PntType Center
   
    as PntType V
   
    as PntType Pnt0(MAX_PNT)
    as PntType Pnt(MAX_PNT)
   
End Type

Sub InterpolateShape(ByRef S1 as ShapeType, Byref S2 as ShapeType, ByVal t as single)
   
    Dim as integer dX,dY
   
    If S1.nbPoints<>S2.NbPoints then exit sub
   
    For i as integer = 1 to S1.NbPoints
       
        dX = S2.Pnt0(i).x - S1.Pnt0(i).x
        dY = S2.Pnt0(i).y - S1.Pnt0(i).y
       
        S1.Pnt(i).x = S1.Pnt0(i).x + t*dX
        S1.Pnt(i).y = S1.Pnt0(i).y + t*Dy
       
    next
   
End sub

Sub Clip(S as ShapeType)
   
    For i as integer = 1 to S.NbPoints
       
        If S.Center.x+S.pnt(i).x>320 or S.Center.x+S.pnt(i).x<0 then
            S.V.X*=-1
            S.Center.x+=S.v.x
        end if
       
        If S.Center.y+S.pnt(i).y>240 or S.Center.y+S.pnt(i).y<0 then
            S.V.Y*=-1
            S.Center.y+=S.v.y
        end if
       
    next

End sub

Sub RecallShape(S as ShapeType)
   
    For i as integer = 1 to S.NbPoints-1
       
        S.pnt(i) = S.pnt0(i)

    next

End Sub

Sub RenderShape(Byref S as ShapeType,t as single)
   
    For i as integer = 1 to S.NbPoints-1
       
        Line(S.Center.x+S.Pnt(i).x,S.Center.y+S.Pnt(i).y)-(S.Center.x+S.Pnt(i+1).x,S.Center.y+S.Pnt(i+1).y),rgb(t*250,100+150*t,200-100*t)

    next

    Line(S.Center.x+S.Pnt(S.NbPoints).x,S.Center.y+S.Pnt(S.NbPoints).y)-(S.Center.x+S.Pnt(1).x,S.Center.y+S.Pnt(1).y),rgb(t*250,100+150*t,200-100*t)

    Paint(S.Center.X,S.Center.Y),rgb(t*250,100+150*t,200-100*t)
End sub

Screenres 320,240,32,2

Dim as ShapeType Shape1, Shape2

Shape1.NbPoints = 8
Shape2.NbPoints = 8

Shape1.V=type(-.5+rnd/2,-.5+rnd/2)

Shape1.Center = type(150,150)
Shape2.Center = type(150,150)

Shape1.Pnt0(1) = type(-50,-50)
Shape1.Pnt0(2) = type(0,-50)
Shape1.Pnt0(3) = type(50,-50)
Shape1.Pnt0(4) = type(50,0)
Shape1.Pnt0(5) = type(50,50)
Shape1.Pnt0(6) = type(0,50)
Shape1.Pnt0(7) = type(-50,50)
Shape1.Pnt0(8) = type(-50,0)

For i as integer = 1 to 8
    Shape2.Pnt0(9-i) = Type(50*cos(i*(6.28/8)+3*3.14/2),50*sin(i*(6.28/8)-3*3.14/2))
next

dim as single t,sign=1

Do : screenlock : cls
   
    Shape1.Center.x+=Shape1.v.x
    Shape1.Center.y+=Shape1.v.y
   
    clip Shape1
   
    InterpolateShape(Shape1,Shape2,t)
       
    RenderShape Shape1,t
   
    t+=.0008*sign : if t>=1 or t<=0 then sign*=-1
   
    Screenunlock : sleep 1,1
Loop until multikey(&h01)

Binaries attached.

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: A little shape interpolation snippet
« Reply #1 on: September 30, 2008 »
Congrats on your progress dude, liking the morphing shape.

Welldone,
Clyde.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: A little shape interpolation snippet
« Reply #2 on: September 30, 2008 »
Cute :)

Thanks for sharing, have some Karma.
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Pixel_Outlaw

  • Pentium
  • *****
  • Posts: 1382
  • Karma: 83
    • View Profile
Re: A little shape interpolation snippet
« Reply #3 on: September 30, 2008 »
I don't code in freebasic but yes interpolation is a very cool thing to know and use. There are different kinds of interpolation too!

Basically they all go from one result to another but some have unique properties. The most basic of course is the linear interpolation. Things get very interesting when you have weighted interpolation and other fancy curve based interpolations. It is very cool when you start looking at interpolation between unlike shapes. If you wanted to connect unlike shapes you can use interpolation. You can form a surface that connects very unlike shapes like a circular base to a rectangular base. Interpolation is not restrained to a single dimension however as you have shown. It gets very useful in creating organic looking forms from ridged and easily defined hulls. You might have seen a 3D technique called "Block Modeling". In this process the artist can take a relatively simple shape such as a dodecahedron and easily convert it to a sphere like shape. Why is this useful to the demo maker? Well we only need a model of a simple shape and we use interpolation surfaces to make a very complex shape at runtime. Would you rather store 8 verticies for a cube or a model consisting of hundreds of faces? Of course I'm speaking out of my ass a little here since I've never preformed 3D interpolation but I have seen and read how it is done. The point is that between point a and b there is one hell of a journey. There are a few different paths to take, much like those pick your own adventure books we all used to read.

If you haven't already, you might next study things like bezier curves and other 2d interpolations. Most of the math is again a little beyond my current ablilty but there are good folk here that can help you there too.

Once some time ago I was writing a program that produced gradients for shading. A fellow made a great post about using log interpolation. I made it into a PDF for save keeping:

|
V
« Last Edit: September 30, 2008 by Pixel_Outlaw »
Challenge Trophies Won:

Offline Hezad

  • Sponsor
  • Pentium
  • *******
  • Posts: 613
  • Karma: 44
  • I believe .. in Patrick.
    • View Profile
    • Hezad.com Web hosting
Re: A little shape interpolation snippet
« Reply #4 on: September 30, 2008 »
thanks mates :)

Pixel_outlaw >
Quote
You might have seen a 3D technique called "Block Modeling". In this process the artist can take a relatively simple shape such as a dodecahedron and easily convert it to a sphere like shape. Why is this useful to the demo maker? Well we only need a model of a simple shape and we use interpolation surfaces to make a very complex shape at runtime. Would you rather store 8 verticies for a cube or a model consisting of hundreds of faces?

I admit I didn't know this technique :P It seems really interesting, I'll fish some more informations about it in the wikipedia's ocean :D



Quote
If you haven't already, you might next study things like bezier curves and other 2d interpolations.
Well in fact, I already coded bezier curves some time ago, when I discovered on wikipedia it wasn't as difficult to code as it seems :P It's really nice to see the curve being deformed while you move the control points !


Quote
Once some time ago I was writing a program that produced gradients for shading. A fellow made a great post about using log interpolation. I made it into a PDF for save keeping:
Yeh, cool PDF ! thanks for sharing it :)

Offline relsoft

  • DBF Aficionado
  • ******
  • Posts: 3303
  • Karma: 47
    • View Profile
Re: A little shape interpolation snippet
« Reply #5 on: October 01, 2008 »
Yay!! Nice!! Reminds me of a 3d windows screensaver that morphs.

Here was my take on the subject:

http://rel.betterwebber.com/junk.php?id=61
Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: A little shape interpolation snippet
« Reply #6 on: October 01, 2008 »
Actually when I saw Hezads morph routine it put me in mind of your morphing torus Rel :) When I saw you were online I guessed that you'd find this topic :D
Shockwave ^ Codigos
Challenge Trophies Won:

Offline relsoft

  • DBF Aficionado
  • ******
  • Posts: 3303
  • Karma: 47
    • View Profile
Re: A little shape interpolation snippet
« Reply #7 on: October 01, 2008 »
Hey shock! you're still sick?
Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: A little shape interpolation snippet
« Reply #8 on: October 01, 2008 »
Lol I have so much steel in my legs that my value has doubled :) I'll be off work for several more weeks I think.
Shockwave ^ Codigos
Challenge Trophies Won:

Offline relsoft

  • DBF Aficionado
  • ******
  • Posts: 3303
  • Karma: 47
    • View Profile
Re: A little shape interpolation snippet
« Reply #9 on: October 01, 2008 »
Aw man!!!
Accident?

I was actually offline for a month because of an illness.
Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: A little shape interpolation snippet
« Reply #10 on: October 01, 2008 »
Fell over and broke my leg in 4 places!

Hope you're ok now Rel? Sounds serious to be off a whole month!
Shockwave ^ Codigos
Challenge Trophies Won:

Offline relsoft

  • DBF Aficionado
  • ******
  • Posts: 3303
  • Karma: 47
    • View Profile
Re: A little shape interpolation snippet
« Reply #11 on: October 01, 2008 »
I almost died.  Bad case of allergies and an adult onset asthma which I didn't have for 32 years.  I can't breathe I almost fainted at the mall. :*(

I'm wearing mask all the time and I'm on steroids.  It sucks being sick.  Hope you get well soon mate.
Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: A little shape interpolation snippet
« Reply #12 on: October 01, 2008 »
Oh my.

That sounds really bad :( I won't complain too much about my leg after reading that..
It's funny, I was talking to Pixel Outlaw about you and Adigun a couple of days ago and we wondered where you'd got to.
Shockwave ^ Codigos
Challenge Trophies Won:

Offline relsoft

  • DBF Aficionado
  • ******
  • Posts: 3303
  • Karma: 47
    • View Profile
Re: A little shape interpolation snippet
« Reply #13 on: October 01, 2008 »
Home playing PS2 games. LOL

But I'm a little bit better now.  Though I was absent from work again this week because of another attack. :*(


Has Adigun disappeared too?
Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: A little shape interpolation snippet
« Reply #14 on: October 01, 2008 »
He posted something on Deviantart but has not been around the forums lately :(
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17409
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: A little shape interpolation snippet
« Reply #15 on: October 01, 2008 »
Btw I should probably split this topic as we appear to have hijacked it. Sorry Hezad!
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Hezad

  • Sponsor
  • Pentium
  • *******
  • Posts: 613
  • Karma: 44
  • I believe .. in Patrick.
    • View Profile
    • Hezad.com Web hosting
Re: A little shape interpolation snippet
« Reply #16 on: October 01, 2008 »
Shockwave > No problem, don't worry for that :)

rel > Thanks :D And awesome morpher !! Sorry to hear about your allergies and asthma attacks though :/

Quote
Home playing PS2 games. LOL
Sounds like it have good sides too .. (tacky humor?)