tjammer / raylib-ocaml

OCaml bindings for raylib and raygui
https://tjammer.github.io/raylib-ocaml/
MIT License
175 stars 14 forks source link

Creating image from memory #33

Closed linsyking closed 11 months ago

linsyking commented 1 year ago

I want to load an image from memory, but I cannot find a function to create an image.

Equivalent in C:

Image img                          = {0};
img.format                         = PIXELFORMAT_UNCOMPRESSED_R8G8B8;
img.mipmaps                        = 1;
img.width  =1920;
img.height = 1080;
img.data   = malloc(img.width * img.height * 3);
linsyking commented 1 year ago

Or how to create texture with given format and size?

tjammer commented 1 year ago

There's load_image_from_memory, where the image data is provided as an ocaml string. From there, you could use load_texture_from_image. I have never used these functions, so please report back if something is not working.

The texture struct isn't exposed for writing right now. I think the data field isn't exposed at all.

linsyking commented 1 year ago

There's load_image_from_memory, where the image data is provided as an ocaml string. From there, you could use load_texture_from_image. I have never used these functions, so please report back if something is not working.

The texture struct isn't exposed for writing right now. I think the data field isn't exposed at all.

From this issue, if i want to use rlgl then I need a texture constructor.

tjammer commented 1 year ago

From this issue, if i want to use rlgl then I need a texture constructor.

In the linked issue, they want to fill the texture struct directly. The issue they are having is the texture id, which needs access to GL to generate (through rlgl here). The function load_texture_from_image sets it correctly though, see here https://github.com/raysan5/raylib/blob/5e1a81555ca130e2c6544add0e2391a8763e7e2a/src/rtextures.c#L3340. So the way of creating an image and then a texture should work.

The lowlevel rlgl function is also exposed by the bindings (https://tjammer.github.io/raylib-ocaml/raylib/Raylib/Rlgl/index.html#val-load_texture), but as I said, the texture struct isn't writable from the ocaml side directly

linsyking commented 1 year ago

From this issue, if i want to use rlgl then I need a texture constructor.

In the linked issue, they want to fill the texture struct directly. The issue they are having is the texture id, which needs access to GL to generate (through rlgl here). The function load_texture_from_image sets it correctly though, see here https://github.com/raysan5/raylib/blob/5e1a81555ca130e2c6544add0e2391a8763e7e2a/src/rtextures.c#L3340. So the way of creating an image and then a texture should work.

The lowlevel rlgl function is also exposed by the bindings (https://tjammer.github.io/raylib-ocaml/raylib/Raylib/Rlgl/index.html#val-load_texture), but as I said, the texture struct isn't writable from the ocaml side directly

Thanks for your help!

The problem with load_texture_from_image is that I cannot set the image format (e.g. PIXELFORMAT_UNCOMPRESSED_R8G8B8), so it becomes impossible to load some image correctly.

Also the image data is binary data so I think it should be pointer type rather than string.

tjammer commented 1 year ago

Yes, you're right about the image format. I'll expose the missing fields of Texture then and make them writable. That way one can use exactly the same method as one would in C. It'll take a few days because the rlgl bindings are pretty raw. One of those int from load_texture shoulde be the image format, for instance.

Regarding binary data; String in OCaml are just arrays of bytes so they could be used that way. I'd be clearer to have something like Bytes.t instead, I agree

tjammer commented 1 year ago

I've added a create function to the Textures module that you can use like in the raylib issue above. The rlgl function doesn't take a PixelFormat.t right now as the pixel format, but an int. To call it, you need to use Pixelformat.to_int.