-
Notifications
You must be signed in to change notification settings - Fork 139
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
Suggestion: add cgltf_load_buffers_memory, and rewrite cgltf_load_buffers to use it #109
Comments
I am also just realizing, if you moved the library away from the notion that data buffers ever need to be set manually (which right now appears to be an unavoidably required thing to do for loading from memory, see above) then cgltf could also apply sparse-to-non-spare conversion automatically in the Because if cgltf doesn't automatically apply non-sparse conversion, isn't it reasonable to assume a lot of users just won't implement it even though the functionality is right there in cgltf? I also just don't see the use in making everyone do it manually when it could be a trivial load time conversion that pretty much everyone would prefer to support anyway. (Or who wouldn't want to support sparse data, intentionally? Maybe I'm missing a use case) |
Did you see the file options in |
@jkuhlmann ah nice! Examples would help btw to not miss such things, I just didn't notice that was a possibility. However that still leaves some cases out in the open, mainly: 1. custom ways of file URI resolution (e.g. my engine has custom guessing that tries multiple ways to resolve relative paths relative to model, relative to game dir, ... to compensate for when the model stores it weirdly or the user moves files around) 2. supporting HTTP/HTTPS and other ways of retrieving resources. It also seems to me like Ideally, I think cgltf should at the very least offer a custom callback to let me resolve any URI to a full absolute file path (not a relative one that it somehow appends) as an alternative to whatever it does. Because my URI resolver is somewhat elaborate and does support a |
Alright, your points all make sense. I've already got examples on my list. And, yes, it looks like cgltf should resolve reserved URI character encodings. However, about resolving URIs, doesn't the code pretty much leave the URI intact if you just pass in an empty string for the |
Hm but now imagine Now if I could pass in a custom URI-to-file-path-or-whatever-my-custom-fread-wants callback I could decide on my own what I need to escape or unescape, while the default could still deal with reserved URI character encodings properly and make more paths work for So that is why I think an URI resolution callback to set in the options would really be the best way to solve all of this. |
I just checked out more in-depth in how to load up cgltf from memory / a VFS library (like PhysFS).
It appears to me for loading files with cgltf with all external resources there are two choices:
Use
cgltf_load_buffers
(which requires file access)Duplicate the entire content of
cgltf_load_buffers
, especifically also the loop recognizing & handlingdata:
URIs, and dealing with thedata->buffers
detail members which doesn't seem particularly future-proof, and adjust it to load from memory or wherever elseThe second approach seems unnecessarily more complicated, especially since I assume most more versatile game engines and frameworks will need this. It also seems counter-intuitive to me that reading from memory isn't the regular case with file reading being a specialization case, rather than the other way around.
Therefore I suggest the following:
There could be an implementation alike to this:
which does what
cgltf_load_buffers
does now, except it calls thedata_callback
to retrieve the file contents instead of disk access. The data_callback shall set*buffer_size = 0
on failure to obtain the data, and shall otherwise point*buffer_data = ...
to an existing data buffer. It shall be up to the cgltf user, who supplied data_callback, to dispose of whatever the data_callback allocated during its use after the entire run ofcglt_load_buffers_from_memory
has returned, and all data was safely copied into the cgltf structures.Once step 1 is implemented, possibly reimplement
cgltf_load_buffers
on top by just using a callback to read the files supplied to the newcgltf_load_buffers_memory
to avoid code duplication(Very optional and nasty to do, I know. But just as an idea:) possibly fix the whole naming scheme with some deprecation plan. It's
cgltf_parse
andcgltf_parse_file
right now, so shouldn'tcgltf_load_buffers
actually becgltf_load_buffers_file
? With possibly this newcgltf_load_buffers_memory
function eventually becomingcgltf_load_buffers
as consistently also the memory-based variant (or alternatively, maybe prefix all the memory-based loading functions with something like_memory
, e.g.cgltf_parse_memory
)The text was updated successfully, but these errors were encountered: