Example code for using FL_Image to load data for OpenGL3 texture with Cmake on MacOSX?

67 views
Skip to first unread message

duncan

unread,
Aug 15, 2021, 4:51:40 AM8/15/21
to fltk.general

Hi guys,

I'm back for my bi-annual attempt to get something working with OpenGL
and have been working my way through https://learnopengl.com and
watching the TheCherno's OpenGL and Game Engine series of videos on

They both recommend using stb_image.h from https://github.com/nothings/stb
but I've been struggling to get it to load the file, and thought I could try using an
Fl_Image class instead, but it's not exactly clear to me that I'm doing it right.

and then try:

Fl_JPEG_Image image("earth_2k.jpg");
printf("Image: data: %p w: %d h: %d d: %d c: %d ld: %d\n",
image.data(), image.data_w(), image.data_h(), image.d(), image.count(), image.ld());

it shows:

Image: data: 0x7ffee3cd2c00 w: 0 h: 0 d: 0 c: 1 ld: -3
Data JPEG file "earth_2k.jpg" is too large or contains errors!


I've spent hours staring at the stb_image version, I've used Mac/Preview to
convert the file to PNG and tried that instead, so I must be missing the obvious.

Could anyone point me in the right direction, or at a working example on github,
other than mcleary's pbr which is waaay too complicated for me at this stage,
or even show how to create an OpenGL3 texture using the Fl_Image interface?

Cheers
Duncan

Daniel G.

unread,
Aug 15, 2021, 6:38:16 AM8/15/21
to fltk.general
Hey,

maybe this helps you out: https://projects.nwrk.biz/projects/clanbomber-fltk/repository/revisions/master/entry/src/GameWindow.cxx. There png's are drawn in an FLTK OpenGL window.

BR,
Giri

Philip Rose

unread,
Aug 15, 2021, 9:00:39 AM8/15/21
to fltkg...@googlegroups.com

Duncan,

 

Have you tested image.fail() to see whether there was an error accessing the file, and what sort of error it was?

 

Phil.

 

Sent from Mail for Windows

--
You received this message because you are subscribed to the Google Groups "fltk.general" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fltkgeneral...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/fltkgeneral/b39d7665-fca3-4041-b5ce-16fba6581470n%40googlegroups.com.

 

duncan

unread,
Aug 15, 2021, 10:18:44 AM8/15/21
to fltk.general
I've spent hours staring at the stb_image version, I've used Mac/Preview to
convert the file to PNG and tried that instead, so I must be missing the obvious.

Could anyone point me in the right direction, or at a working example on github,
other than mcleary's pbr which is waaay too complicated for me at this stage,
or even show how to create an OpenGL3 texture using the Fl_Image interface?

OK, I've solved the problem and thanks to those who replied so far:

@Daniel.G: the GameWindow example is basically what I was trying to
do and the CPU-side texture setup is the same for OpenGL "classic"  and
OpenGL3 with shaders [and as covered by https://learnopengl.com]

@Philip.R: I was struggling with stb_image so I didn't even consider trying
Fl_Image::fail() until after I posted my question and read the Fl_Image docs

The reasons for the problem were a combination of:

1. There's a problem between the chair and keyboard. I was just being thick
     and had been staring at the code for so long I couldn't see it any more.

2. The directory where CLion/CMake runs the executable is not the current
     source directory, so neither stb_load nor Fl_*_Image could find the file

3. By the time I decided to use the /full/path/to/file, I'd managed to corrupt
     it, and had to download a new copy

4. I must have had some inconsistent headers/libraries somewhere, and
     had to rebuild fltk-1.4.x using CMake. In the meantime I had already
     re-run "brew install" for both libpng and libjpeg and built with both
     OPTION_USE_SYSTEM_LIBPNG and OPTION_USE_SYSTEM_LIBJPEG
     set to ON and then back to OFF again. Somewhere along the way, I
     must have cleaned out old versions of something.

So, thanks for being the rubber duck and providing hints to get me going.
D.

lifeatt...@gmail.com

unread,
Aug 15, 2021, 10:58:13 AM8/15/21
to fltk.general
>2. The directory where CLion/CMake runs the executable is not the current
>     source directory, so neither stb_load nor Fl_*_Image could find the file

This one keeps biting me too !!!

Kevin

duncan

unread,
Aug 15, 2021, 11:24:46 AM8/15/21
to fltk.general
>2. The directory where CLion/CMake runs the executable is not the current
>     source directory, so neither stb_load nor Fl_*_Image could find the file

This one keeps biting me too !!!

OK, I should give a big round of applause to Albrecht and Manolo for making
major progress on the documentation and configuration of CMake for MacOSX.
Following the 1.4.x README.CMake.txt gives a much simpler config than before.

The official CMake docs have been updated to include a BUNDLE example
so that you can define which files are resources or assets that need to be
copied into the cmake-build hierarchy as shown in the example below. You
no longer have copy files explicitly [as shown in the commented out lines]
So now I can give a relative path when loading the file to Fl_JPEG_Image.

Not sure what I have to do for the final install to run outside of CLion/CMake
on Linux and Windows will work yet. I'll cross that bridge when I get there.

Cheers
D.

cmake_minimum_required(VERSION 3.17)
project(glearth)

set(CMAKE_CXX_STANDARD 14)
set(FLTK_DIR /path/to/fltk-14x/build)
find_package(FLTK REQUIRED NO_MODULE)

include_directories(glm)
include_directories(stb)

set (SOURCES main.cpp stb_image.cpp)
set (RESOURCES earth_2k.jpg)

add_executable(glearth WIN32 MACOSX_BUNDLE ${SOURCES} ${RESOURCES})

if (APPLE)
  target_link_libraries(glearth "-framework cocoa")
endif (APPLE)

target_include_directories(glearth PUBLIC ${FLTK_INCLUDE_DIRS} glm stb)
target_link_libraries(glearth fltk fltk_gl fltk_images)

set_target_properties(glearth PROPERTIES MACOSX_BUNDLE TRUE RESOURCE "${RESOURCES}")

# set(RESOURCE_PATH ${CMAKE_CURRENT_BINARY_DIR})
# file (MAKE_DIRECTORY ${RESOURCE_PATH})
# file (COPY ${CMAKE_CURRENT_SOURCE_DIR}/earth_2k.jpg DESTINATION ${RESOURCE_PATH})

Ian MacArthur

unread,
Aug 15, 2021, 12:26:07 PM8/15/21
to Fltk General
On 15 Aug 2021, at 16:24, duncan wrote:
>
> Not sure what I have to do for the final install to run outside of CLion/CMake
> on Linux and Windows will work yet. I'll cross that bridge when I get there.

FWIW, when that time comes, I have a code fragment I use (#ifdef’d for cross-platofm operation) that finds the directory in which the exe is installed, and then I use that as the root for relative paths to the resources I need to load, like images etc.

Generally, this is not too tricky on Windows - most times Windows exe’s start with cwd set to the directory the exe resides in, but the various different Linux WM have many strange and interesting ideas about what cwd should be set to, so that tends to be trickier (hence the code I wrote, that so far has done a decent job of finding the “right” path across many different systems...)






Bill Spitzak

unread,
Aug 16, 2021, 1:39:04 PM8/16/21
to fltkg...@googlegroups.com
You can figure out a Unix executable from argv[0] and $PATH.

However on Linux itself there is a really nice shortcut, in /proc/self/exe is a symbolic link to the actual executable. Use readlink() to convert it. You need to run readlink() recursively if your purpose is to find the directory the executable is in.

--
You received this message because you are subscribed to the Google Groups "fltk.general" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fltkgeneral...@googlegroups.com.

Greg Ercolano

unread,
Aug 16, 2021, 3:26:55 PM8/16/21
to fltkg...@googlegroups.com
On 8/16/21 10:38 AM, Bill Spitzak wrote:
You can figure out a Unix executable from argv[0] and $PATH.
However on Linux itself there is a really nice shortcut, in /proc/self/exe is a symbolic link to the actual executable. Use readlink() to convert it. You need to run readlink() recursively if your purpose is to find the directory the executable is in.

Pretty sure on Linux and MacOS you can use realpath(3) on argv[0] to get the
absolute path to the running binary. e.g.

#include <stdlib.h> // realpath()
#include <stdio.h>
int main(int argc, char *argv[]) {

    // Get absolute path to self
    {

        const char *self = realpath(argv[0], NULL);
        printf("realpath(%s) is %s\n", argv[0], self);
        free((void*)self);   // free string realpath() returned
    }

    return 0;
}

So for instance if I build that in /var/tmp and run it with ./foo, I get:

LINUX: realpath(./foo) is /var/tmp/foo
MacOS: realpath(./foo) is /private/var/tmp/foo

Which shows the real path to the binary, with symlinks followed and expanded,
and prevents any . and .. nonsense.

Read the manpage carefully for this POSIX function, esp. any caveats or bugs.
(Had to once go very deep down the realpath(3) rabbit hole once before researching STR #3441)

duncan

unread,
Aug 17, 2021, 6:02:21 AM8/17/21
to fltk.general
>2. The directory where CLion/CMake runs the executable is not the current
>     source directory, so neither stb_load nor Fl_*_Image could find the file
 
The official CMake docs have been updated to include a BUNDLE example
so that you can define which files are resources or assets that need to be
copied into the cmake-build hierarchy as shown in the example below.
[ . . .]
Not sure what I have to do for the final install to run outside of CLion/CMake
on Linux and Windows will work yet. I'll cross that bridge when I get there.

Thanks for all the suggestions on how to find the executable's location on
different systems, but my final comment was more to do with finding out
how to work with CMake/CPack about where to put additional assets and
how to tell the final installed executable where to find them.

But I'm not likely to reach that point for quite a while yet . . .

Cheers
D.

Reply all
Reply to author
Forward
0 new messages