Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Requesting example of how to load/draw a model #10

Open
shelvick opened this issue Jan 23, 2021 · 3 comments
Open

Requesting example of how to load/draw a model #10

shelvick opened this issue Jan 23, 2021 · 3 comments

Comments

@shelvick
Copy link

shelvick commented Jan 23, 2021

When I try to load and draw a simple model, such as in this example, I get pointer errors in (draw-model):

The value
  (1065353216 0 #.(SB-SYS:INT-SAP #X00000000)
   #.(SB-SYS:INT-SAP #X3F80000000000000)
   #.(SB-SYS:INT-SAP #X00000000) #.(SB-SYS:INT-SAP #X00000000)
   #.(SB-SYS:INT-SAP #X3F800000) #.(SB-SYS:INT-SAP #X00000000)
   #.(SB-SYS:INT-SAP #X3F80000000000000)
   #.(SB-SYS:INT-SAP #X10000001)
   #.(SB-SYS:INT-SAP #X7FAA9C405CF0)
   #.(SB-SYS:INT-SAP #X7FAA9C406E40)
   #.(SB-SYS:INT-SAP #X7FAA9C406FD0) 0
   #.(SB-SYS:INT-SAP #X00000000))

is not of type
  SB-SYS:SYSTEM-AREA-POINTER

This works in C so at least on some level I am using the library correctly. Do you have a working Lisp example? Am I doing something wrong or is this related to #3?

@longlene
Copy link
Owner

longlene commented Jan 25, 2021

Sorry for that.
As the raylib library use the struct and pointer to the struct at the same time, some APIs may not work as expected.
Those APIs may be could be rewrote with Common Lisp. I'll try it when I have time.

@kiran-kp
Copy link
Contributor

The issue here might just be that the bones and bind-pose members are declared as structs CL but they are pointers in the C struct. The code is likely just reading invalid memory.

;;
;;// Model, meshes, materials and animation data
;;typedef struct Model {
;;    Matrix transform;       // Local transform matrix
;;
;;    int meshCount;          // Number of meshes
;;    int materialCount;      // Number of materials
;;    Mesh *meshes;           // Meshes array
;;    Material *materials;    // Materials array
;;    int *meshMaterial;      // Mesh material number
;;
;;    // Animation data
;;    int boneCount;          // Number of bones
;;    BoneInfo *bones;        // Bones information (skeleton)
;;    Transform *bindPose;    // Bones base transformation (pose)
;;} Model;
(defcstruct (%model :class model-type)
 "Model type"
 (transform (:struct %matrix))
 (mesh-count :int)
 (material-count :int)
 (meshes (:pointer (:struct %mesh)))
 (materials (:pointer (:struct %material)))
 (mesh-material (:pointer :int))
 (bone-count :int)
 (bones (:struct %bone-info))
 (bind-pose (:struct %transform)))

@eepyamre
Copy link

Sorry to bump an old thread, butt the problem is still exist. I managed to find a way to draw a model, by replacing bones and bind-pose with pointers, as kiran-kp commented, and also by changing the way the transform field is set. Don't think it's the best way, but maybe it will help someone.

(defcstruct (%model :class model-type)
  "Model type"
  (transform (:struct %matrix))
  (mesh-count :int)
  (material-count :int)
  (meshes (:pointer (:struct %mesh)))
  (materials (:pointer (:struct %material)))
  (mesh-material (:pointer :int))
  (bone-count :int)
  (bones (:pointer (:struct %bone-info)))
  (bind-pose (:pointer (:struct %transform))))

(define-conversion-into-foreign-memory (object (type model-type) pointer)
    (with-foreign-slots ((transform mesh-count material-count meshes materials mesh-material bone-count bones bind-pose) pointer (:struct %model))
      (setf transform (cffi:convert-to-foreign (nth 0 object) '(:struct %matrix)))
      (setf mesh-count (nth 1 object))
      (setf material-count (nth 2 object))
      (setf meshes (nth 3 object))
      (setf materials (nth 4 object))
      (setf mesh-material (nth 5 object))
      (setf bone-count (nth 6 object))
      (setf bones (nth 7 object))
      (setf bind-pose (nth 8 object))))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants