0 votes

I'm trying to load an image using GDNative for use as a heightmap in terrain generation.
So far I've achieved 2 different results, one being the game crashing and the other resulting in an empty image resource.

The first way is trough loading an image with the resource loader:

Ref<Image> heightMap = (Ref<Image>) ResourceLoader::get_singleton()->load("res://res/heightmaps/desert_heightmap.png")

heightMap->lock();
auto color = heightMap->get_pixel(1, 1);
printf("R: %f\tG: %f\tB: %f\n", color.r, color.g, color.b);

The console output:

CrashHandlerException: Program crashed
Dumping the backtrace. Please include this when reporting the bug on https://github.com/godotengine/godot/issues
[0] PoolVector<unsigned char>::size (E:\Programs\godot\core\pool_vector.h:475)
[1] PoolVector<unsigned char>::size (E:\Programs\godot\core\pool_vector.h:475)
[2] Image::lock (E:\Programs\godot\core\image.cpp:2394)
[3] MethodBind0<Image>::ptrcall (E:\Programs\godot\core\method_bind.gen.inc:157)
[4] godot_method_bind_ptrcall (E:\Programs\godot\modules\gdnative\gdnative\gdnative.cpp:70)
[5] godot::___godot_icall_void (E:\godot-projects\hex-strategy\godot-cpp\include\gen\__icalls.hpp:163)
[6] godot::Image::lock (E:\godot-projects\hex-strategy\godot-cpp\src\gen\Image.cpp:242)
[7] godot::HexGridMap::generateSegmentMesh (E:\godot-projects\hex-strategy\src\HexGridMap.cpp:179)
[8] godot::HexGridMap::_init (E:\godot-projects\hex-strategy\src\HexGridMap.cpp:68)
[9] godot::_godot_class_instance_func<godot::HexGridMap> (E:\godot-projects\godot-cpp\include\core\Godot.hpp:163)
[10] NativeScript::instance_create (E:\Programs\godot\modules\gdnative\nativescript\nativescript.cpp:220)
[11] Object::set_script (E:\Programs\godot\core\object.cpp:1030)
[12] Object::set (E:\Programs\godot\core\object.cpp:433)
[13] SceneState::instance (E:\Programs\godot\scene\resources\packed_scene.cpp:215)
[14] PackedScene::instance (E:\Programs\godot\scene\resources\packed_scene.cpp:1698)
[15] Main::start (E:\Programs\godot\main\main.cpp:1942)
[16] widechar_main (E:\Programs\godot\platform\windows\godot_windows.cpp:160)
[17] _main (E:\Programs\godot\platform\windows\godot_windows.cpp:184)
[18] main (E:\Programs\godot\platform\windows\godot_windows.cpp:196)
[19] __scrt_common_main_seh (d:\A01\_work\6\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288)
[20] BaseThreadInitThunk
-- END OF BACKTRACE --

The second method I've tried is by instancing the resource after it has been loaded like so:

Ref<Image> heightMap = (Ref<Image>) ResourceLoader::get_singleton()->load("res://res/heightmaps/desert_heightmap.png");
heightMap.instance();

The result in this case is this:

ERROR: Image::lock: Condition "data.size() == 0" is true.
   At: core\image.cpp:2394
ERROR: Image must be locked with 'lock()' before using get_pixel().
   At: core\image.cpp:2411
R: 0.000000     G: 0.000000     B: 0.000000

I suppose this makes sense because the reference gets reset to a null state?

Am I overlooking something relating to resources?

Thanks in advance.

in Engine by (69 points)

1 Answer

0 votes

So I've figured out what the problem is. It seems I had the image imported as a StreamTexture (as is default?) thus I couldn't load the resource as an Image. So for anyone having this problem in the future, reimport your image as an Image resource.

I however would still like to learn more about how resource management/loading works on the lower level so if anyone has any resources or a short explanation about this, you have my gratitude.

by (69 points)

Indeed, StreamTexture is default, to change it open project in the editor, select file in the File System tab, open Import tab (next to Scene), change type in Import As: and click Reimport.

Indeed, StreamTexture is default, to change it open project in the editor, select file in the File System tab, open Import tab (next to Scene), change type in Import As: and click Reimport.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.