GuvaCode / ray4laz

A complete header translation (binding) of the raylib 5.0 to Pascal. Without any funky helper functions for memory alignment issues. Inspired and partially based on the drezgames/raylib-pascal binding, however a little cleaner and more recent, with FPC 3.2.0 and up support.
https://www.raylib.com/
zlib License
98 stars 18 forks source link

iqm animation/DrawModelEx spinning bug #11

Closed ghost closed 2 years ago

ghost commented 2 years ago

When loading an animation and using DrawModelEx the model keeps pivoting around its root position. When I use DrawModel and DrawModelWiresEx the model stays where it should.

Thanks

GuvaCode commented 2 years ago

Yes, I know that. But this behavior is only on Windows. In Linux ase everything works fine. As soon as I get a chance I will try to solve it.

GuvaCode commented 2 years ago

There is probably an error in raylib. You need to compile examples from raylib in Windows and check. For example there was a bug loading voxel models on linux.

ghost commented 2 years ago

Ok, thanks for the support. I will be using Ray4Laz extensively and will try to help out wherever I can.

ghost commented 2 years ago

Following code using DrawModelEx fixes the spinning for me on Windows:

unit frm1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs,
  ray_header;

type

  { TForm1 }

  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private

  public
    FCamera:TCamera;
    FModel:TModel;
    FTexture:TTexture2D;
    FPosition:TVector3;
    FAnimationCount, FAnimationFrameCounter:Integer;
    FAnimations:PModelAnimation;
  end;

var
  Form1: TForm1;

procedure DrawModel(AModel:TModel; APosition:TVector3; AAxis:TVector3; AAngle:Single; AScale:Single; ATint:TColor);

implementation

{$R *.lfm}

procedure DrawModel(AModel:TModel; APosition:TVector3; AAxis:TVector3; AAngle:Single; AScale:Single; ATint:TColor);
var
  Scale, Axis:TVector3;
begin
  Scale:=Vector3Create(AScale,AScale,AScale);
  DrawModelEx(AModel, APosition, AAxis, AAngle, Scale, ATint);
end;

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
var
  I:Integer;
begin
  InitWindow(1280, 720, 'Animation Test');
  FCamera.Position:=Vector3Create(15.0, 15.0, 15.0);
  FCamera.Target:=Vector3Create(0.0,0.0,0.0);
  FCamera.Up:=Vector3Create(0.0, 1.0, 0.0);
  FCamera.FoVY:=45;
  FCamera.Projection:=CAMERA_PERSPECTIVE;
  FModel:=LoadModel('guy.iqm');
  FTexture:=LoadTexture('guytex.png');
  SetMaterialTexture(@FModel.Materials[0], MATERIAL_MAP_DIFFUSE, FTexture);
  FPosition:=Vector3Create(0,0,0);
  FAnimationCount:=0;
  FAnimations:=LoadModelAnimations('guy.iqm', @FAnimationCount);
  FAnimationFrameCounter:=0;
  WriteLn('animation count: ', FAnimationCount);
  SetCameraMode(FCamera, CAMERA_FREE);
  SetTargetFPS(60);
  while not WindowShouldClose() do
  begin
  UpdateCamera(@FCamera);
  if IsKeyDown(KEY_SPACE)
  then
  begin
    Inc(FAnimationFrameCounter);
    UpdateModelAnimation(FModel, FAnimations[0], FAnimationFrameCounter);
  if FAnimationFrameCounter>=FAnimations[0].FrameCount
  then
    FAnimationFrameCounter:=0;
  end;
  BeginDrawing();
  ClearBackground(RAYWHITE);
    BeginMode3d(FCamera);
      DrawModel(FModel, FPosition, Vector3Create(1.0, 0.0, 0.0), -45, 1, WHITE);
      for I:=0 to FModel.BoneCount - 1 do
      begin
      DrawCube(FAnimations[0].FramePoses[FAnimationFrameCounter][I].Translation, 0.2, 0.2, 0.2, RED);
      end;
      DrawGrid(10,1);
    EndMode3D();
  DrawFPS(10,10);
  EndDrawing();
  end;
  UnloadTexture(FTexture);
  for I:=0 to FAnimationCount - 1 do
  UnloadModelAnimation(FAnimations[I]);
  UnloadModel(FModel);
  CloseWindow();
end;

end.    
GuvaCode commented 2 years ago

Thank you. but why are you using this to create a form? If you need it to be OOP style, look here

https://github.com/GuvaCode/Ray4Laz-Extras

https://github.com/GuvaCode/Ray4Laz-Extras/blob/master/source/ray_application.pas

ghost commented 2 years ago

No problem, thanks for the link. I just had this up where I reformatted it, but was meaning to update it anyway :)