angros47 / OpenB3D

GNU Lesser General Public License v2.1
1 stars 0 forks source link

SetAnimKey and AddAnimSeq issue #11

Closed blitzcoder closed 11 months ago

blitzcoder commented 1 year ago

Tried porting and testing the AddAnimSeq and SetAnimKey example here: https://kippykip.com/b3ddocs/commands/3d_commands/AddAnimSeq.htm

but getting a stopped frame as per screenshot below, the debug values (frame, bloat and flatten) are the same with b3d:

frame bloat flatten
1 0 0.05
2 0 0.1
3 0 0.15
4 0 0.2
5 0 0.25
6 0 0.3
7 0 0.35
8 0 0.4
9 1.5 0.15
10 3 -0.1

Untitled

angros47 commented 1 year ago

Ok, I ported it to OpenB3D in FreeBasic:

` #include "openb3d.bi"

screen 18,  32, , &h10002 

Graphics3d 640,480,32,1,1

var camera=CreateCamera()
PositionEntity camera,0,12,-12
RotateEntity camera,-35,0,0
var light=CreateLight(2)
PositionEntity light,1000,1000,-1000
var ground=CreatePlane(2)
EntityAlpha ground,0.5
EntityColor ground,0,0,255

'Lets make a bouncing ball that squashes on impact with the floor.
var ball=CreateSphere(16)
EntityShininess ball,1
EntityColor ball,255,0,0

' Lets animate him and "record" the 3D animation for later playback

dim as single bloat=0 , flatten=0 , ypos=10
var seq = AddAnimSeq(ball,32)
dim spd as single

For frame as integer=1 To 10
    'Drop the ball from height 10 to 2
    ypos = ypos - spd
    spd=spd+.2
    PositionEntity ball,0,ypos,0
    ScaleEntity ball,1+bloat,1+flatten,1+bloat

    'If the ball is low enough make it look increasingly squashed
    If frame>8 then
        bloat=bloat+1.5
        flatten=flatten-.25
    Else
        flatten=flatten+.05
    EndIf

    'Record the frame!
    SetAnimKey ball,frame
Next

'Now we need to add the frames we've just made to the sequence of "film"!
seq = ExtractAnimSeq(ball,1,10) ' total number of frames
PositionEntity ball,0,0,0
ScaleEntity ball,1,1,1

'Play it back ping-pong!
Animate ball,2,0.15,1

do
UpdateWorld
RenderWorld
Flip
loop until multikey(1)

`

Basically, you need to create the animated sequence before using SetAnimKey, to allocate the space for the animation, and then you can add the animation keys. Also, the first frame of the sequence is the mesh at its regular scale, with no rotation nor translation. So, since, in the example, we want the sphere to start in position 0,10,0 and fall, we need to use the ExtractAnimSequence to create an animated sequence that doesn't include the first frame.

I also noticed a little bug in SetAnimKey, in entity.cpp

blitzcoder commented 12 months ago

great, will try this again.