|
|
|
|
Reply From: |
Zylann |
You load the image just the same way as you did in GDScript, the syntax is just a bit different.
Ref<Image> image; // do this once and store it in a member variable
image.instance();
image->load("res://image.png");
image->lock(); // this where you need access to pixels
Color c = image->get_pixel(42, 42);
image->unlock();
You can also read the pixel data directly by using get_data()
Image — Godot Engine (stable) documentation in English
Then use a Read
struct to access by pointer/ But you will have to deal with the internal layout of the pixel format, because that gives you a raw array of bytes.
tbh, I would actually implement only the CPU-intensive part in GDNative, not the whole thing. So for example, you’d load the image or handle the GUI in GDScript and only do rendering in C++, because GDNative can be rough on the edges, while GDScript allows you to make mistakes without crashing too much. But that’s my opinion.
Also depending on how much draw calls you do, GDNative won’t shave the overhead of Godot’s draw functions.
Even better, for a rendering problem like you have, shaders sound like a much better candidate (although, it may require a much different approach than the ones shown in VoxelSpace I suppose).
So I am rewriting this since I just accidentally canceled my Comment earlier (Oops).
Also this time I’ll keep it shorter:
First:
Thank You for the quick and to the Point response!
Second:
Since I still didn’t have success I’m assuming I am doing something wrong, and currently I’m trying to pinpoint possible errors. Meanwhile I would like to ask you how you can store the Ref image in a member variable? Images presumably have to have their own type, correct? Like var in GDScript? Is there an equivalent to that in C++/GDNative?
Third:
I am actually considering to truly switch lanes and go through the Shader approach it really seems to be much more appropriate to my needs. That being said I know less about Shaders than about GDNative… Nonetheless I’ll give it a go. Thank you for that Tip aswell!
Lastly:
I wish you a great Day!
Lord_Kaltenberg | 2020-03-29 13:50
how you can store the Ref image in a member variable? Images presumably have to have their own type, correct? Like var in GDScript? Is there an equivalent to that in C++/GDNative?
It looks like you are not experienced in C++? If so, it is a bit risky to go on this kind of project without learning the language first. Also you should have followed this to see how it works with GDNative: GDExtension — Godot Engine (stable) documentation in English
In C++ there is no var
for members, everything is typed. Godot class instances are all heap-allocated, so they must be stored by pointer (T*). Image
is a reference-counted type (it inherits Reference
), but if you keep it a pointer it won’t automatically manage its reference count. So it is common to wrap it inside a Ref<T>
template container, to automate this (like GDScript does behind the scenes).
An Image
member variable then looks like this:
class MyClass : public BaseType {
GODOT_CLASS(MyClass, BaseType)
private:
Ref<Image> _image;
public:
void set_image(Ref<Image> image) {
_image = image;
}
//...
};
Zylann | 2020-03-29 17:42
Okay now I figured it out and it actually works!
But the performance is still abysmal… So I’ll just move on to Shaders.
It looks like you are not experienced in C++? If so, it is a bit risky to go on this kind of project without learning the language first.
Well I am new to C++. The only things I’ve done so far with it was like a pong game once, and that was years ago.
Lord_Kaltenberg | 2020-03-30 15:35