Skip to content

Commit

Permalink
Window type refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Meakk committed Sep 30, 2024
1 parent 7778405 commit 5a5a940
Show file tree
Hide file tree
Showing 51 changed files with 249 additions and 120 deletions.
2 changes: 1 addition & 1 deletion application/F3DOptionsTools.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ static inline const std::array<CLIGroup, 8> CLIOptions = {{
{ "config", "", "Specify the configuration file to use. absolute/relative path or filename/filestem to search in configuration file locations", "<filePath/filename/fileStem>", "" },
{ "dry-run", "", "Do not read the configuration file", "<bool>", "1" },
{ "no-render", "", "Do not read the configuration file", "<bool>", "1" },
{ "offscreen-backend", "", "Backend to use when rendering offscreen (native|egl|osmesa)", "<string>", "native" },
{ "offscreen-backend", "", "Backend to use when rendering offscreen (auto|glx|wgl|egl|osmesa)", "<string>", "native" },
{ "max-size", "", "Maximum size in Mib of a file to load, negative value means unlimited", "<size in Mib>", "" },
{ "watch", "", "Watch current file and automatically reload it whenever it is modified on disk", "<bool>", "1" },
{ "load-plugins", "", "List of plugins to load separated with a comma", "<paths or names>", "" },
Expand Down
2 changes: 1 addition & 1 deletion application/F3DOptionsTools.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ static inline const OptionsDict DefaultAppOptions = {
{ "config", "" },
{ "dry-run", "false" },
{ "no-render", "false" },
{ "offscreen-backend", "native" },
{ "offscreen-backend", "auto" },
{ "max-size", "-1.0" },
{ "watch", "false" },
{ "load-plugins", "" },
Expand Down
18 changes: 13 additions & 5 deletions application/F3DStarter.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -713,25 +713,33 @@ int F3DStarter::Start(int argc, char** argv)

if (!offscreen)
{
this->Internals->Engine = std::make_unique<f3d::engine>(f3d::engine::createNative(offscreen));
this->Internals->Engine = std::make_unique<f3d::engine>(f3d::engine::create(offscreen));
}
else
{
if (this->Internals->AppOptions.OffscreenBackend == "egl")
{
this->Internals->Engine = std::make_unique<f3d::engine>(f3d::engine::createEGL());
this->Internals->Engine = std::make_unique<f3d::engine>(f3d::engine::createEGL(true));
}
else if (this->Internals->AppOptions.OffscreenBackend == "osmesa")
{
this->Internals->Engine = std::make_unique<f3d::engine>(f3d::engine::createOSMesa());
}
else if (this->Internals->AppOptions.OffscreenBackend == "glx")
{
this->Internals->Engine = std::make_unique<f3d::engine>(f3d::engine::createGLX(true));
}
else if (this->Internals->AppOptions.OffscreenBackend == "wgl")
{
this->Internals->Engine = std::make_unique<f3d::engine>(f3d::engine::createWGL(true));
}
else
{
if (this->Internals->AppOptions.OffscreenBackend != "native")
if (this->Internals->AppOptions.OffscreenBackend != "auto")
{
f3d::log::warn("--offscreen-backend value is invalid, falling back to \"native\"");
f3d::log::warn("--offscreen-backend value is invalid, falling back to \"auto\"");
}
this->Internals->Engine = std::make_unique<f3d::engine>(f3d::engine::createNative(true));
this->Internals->Engine = std::make_unique<f3d::engine>(f3d::engine::create(true));
}
}

Expand Down
10 changes: 5 additions & 5 deletions doc/libf3d/OPTIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ There are three APIs to access the options
The most straightforward and easy to use API, just access it through the structs available in the options instance, eg:

```cpp
f3d::engine eng = f3d::engine::createNative();
f3d::engine eng = f3d::engine::create();
f3d::options& opt = eng.getOptions();
opt.render.show_edges = true;
opt.render.grid.enable = true;
Expand All @@ -130,7 +130,7 @@ The most straightforward and easy to use API, just access it through the structs
Please note that when accessing optional options, special care must be used, eg:

```cpp
f3d::engine eng = f3d::engine::createNative();
f3d::engine eng = f3d::engine::create();
f3d::options& opt = eng.getOptions();
if (opt.render.line_width.has_value())
{
Expand All @@ -147,7 +147,7 @@ The most generic and flexible API, as it rely on parsing and string generation.
The documentation about option parsing is upcoming.
```cpp
f3d::engine eng = f3d::engine::createNative();
f3d::engine eng = f3d::engine::create();
f3d::options& opt = eng.getOptions();
opt.setAsString("render.show_edges", "true");
opt.setAsString("render.grid.enable", "true");
Expand All @@ -158,7 +158,7 @@ The documentation about option parsing is upcoming.
When using this API make sure to catch exceptions has needed, eg:

```cpp
f3d::engine eng = f3d::engine::createNative();
f3d::engine eng = f3d::engine::create();
f3d::options& opt = eng.getOptions();

try
Expand All @@ -180,7 +180,7 @@ When using this API make sure to catch exceptions has needed, eg:
An API that is similar to the F3D 2.0 options API thanks to std::variant.
```cpp
f3d::engine eng = f3d::engine::createNative();
f3d::engine eng = f3d::engine::create();
f3d::options& opt = eng.getOptions();
opt.set("render.show_edges", true);
opt.set("render.grid.enable", true);
Expand Down
4 changes: 2 additions & 2 deletions doc/libf3d/OVERVIEW.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ Manipulating the window directly can be done this way:
// Load VTK native readers
f3d::engine::autoloadPlugins();
// Create a f3d::engine
f3d::engine eng = f3d::engine::createNative(true);
// Create a f3d::engine with a hidden window
f3d::engine eng = f3d::engine::create(true);
// Load a geometry
eng.getLoader().loadGeometry("path/to/file.ext");
Expand Down
2 changes: 1 addition & 1 deletion examples/libf3d/cpp/multi-geom/main.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ int main(int argc, char** argv)
f3d::log::setVerboseLevel(f3d::log::VerboseLevel::DEBUG);

// Create a native window engine
f3d::engine eng = f3d::engine::createNative();
f3d::engine eng = f3d::engine::create();

// Load all files from provided directory as geometries
f3d::loader& load = eng.getLoader();
Expand Down
2 changes: 1 addition & 1 deletion examples/libf3d/cpp/render-image/main.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ int main(int argc, char** argv)
f3d::engine::autoloadPlugins();

// Create a offscreen window engine
f3d::engine eng = f3d::engine::createNative(true);
f3d::engine eng = f3d::engine::create(true);

// Load a model
eng.getLoader().loadGeometry(std::string(argv[1]));
Expand Down
2 changes: 1 addition & 1 deletion examples/libf3d/cpp/render-interact/main.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ int main(int argc, char** argv)
f3d::log::setVerboseLevel(f3d::log::VerboseLevel::DEBUG);

// Create a native window engine
f3d::engine eng = f3d::engine::createNative();
f3d::engine eng = f3d::engine::create();

// Load a model
const f3d::loader& load = eng.getLoader().loadGeometry(std::string(argv[1]));
Expand Down
2 changes: 1 addition & 1 deletion examples/libf3d/cpp/use-options-string/main.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ int main(int argc, char** argv)
f3d::log::setVerboseLevel(f3d::log::VerboseLevel::DEBUG);

// Create a native window engine
f3d::engine eng = f3d::engine::createNative();
f3d::engine eng = f3d::engine::create();

// Modify options use struct API
f3d::options& opt = eng.getOptions();
Expand Down
2 changes: 1 addition & 1 deletion examples/libf3d/cpp/use-options-struct/main.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ int main(int argc, char** argv)
f3d::log::setVerboseLevel(f3d::log::VerboseLevel::DEBUG);

// Create a native window engine
f3d::engine eng = f3d::engine::createNative();
f3d::engine eng = f3d::engine::create();

// Modify options use struct API
f3d::options& opt = eng.getOptions();
Expand Down
2 changes: 1 addition & 1 deletion examples/libf3d/cpp/use-options-variant/main.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ int main(int argc, char** argv)
f3d::log::setVerboseLevel(f3d::log::VerboseLevel::DEBUG);

// Create a native window engine
f3d::engine eng = f3d::engine::createNative();
f3d::engine eng = f3d::engine::create();

// Modify options
f3d::options& opt = eng.getOptions();
Expand Down
2 changes: 1 addition & 1 deletion examples/libf3d/python/multi-geom/multi_geom.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
f3d.Engine.autoload_plugins()

# Create a native window engine
eng = f3d.Engine.create_native()
eng = f3d.Engine.create()

# Load all files from provided directory as geometries
files = [f for f in Path(sys.argv[1]).iterdir() if f.is_file()]
Expand Down
2 changes: 1 addition & 1 deletion examples/libf3d/python/render-image/render_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
try:
f3d.Engine.autoload_plugins()

eng = f3d.Engine.create_native(True)
eng = f3d.Engine.create(True)
eng.loader.load_geometry(sys.argv[1])

eng.window.size = 300, 300
Expand Down
2 changes: 1 addition & 1 deletion examples/libf3d/python/render-interact/render_interact.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
f3d.Engine.autoload_plugins()

# Create a native window engine
eng = f3d.Engine.create_native()
eng = f3d.Engine.create()

# Load a model
try:
Expand Down
2 changes: 1 addition & 1 deletion examples/libf3d/python/render-terminal/render_terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def main():
rows, cols = 40, 20

# setup engine
engine = f3d.Engine.create_native(True)
engine = f3d.Engine.create(True)
engine.options.update(options)
engine.loader.load_geometry(str(model_path))
engine.window.size = rows, cols * 2
Expand Down
2 changes: 1 addition & 1 deletion java/F3DJavaBindings.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ extern "C"
JNIEXPORT jlong JAVA_BIND(Engine, construct)(JNIEnv*, jobject)
{
f3d::log::setVerboseLevel(f3d::log::VerboseLevel::DEBUG);
return reinterpret_cast<jlong>(new f3d::engine(f3d::engine::createNative()));
return reinterpret_cast<jlong>(new f3d::engine(f3d::engine::create()));
}

JNIEXPORT void JAVA_BIND(Engine, destroy)(JNIEnv*, jobject, jlong ptr)
Expand Down
3 changes: 2 additions & 1 deletion library/private/window_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class window_impl : public window
* Create the internal vtkRenderWindow using the offscreen param
* and store option ref for later usage
*/
window_impl(const options& options, Type type, const context::function& getProcAddress);
window_impl(const options& options, Type type, bool offscreen, const context::function& getProcAddress);
/**
* Default destructor
*/
Expand All @@ -42,6 +42,7 @@ class window_impl : public window
* Documented public API
*/
Type getType() override;
bool isOffscreen() override;
camera& getCamera() override;
bool render() override;
image renderToImage(bool noBackground = false) override;
Expand Down
6 changes: 3 additions & 3 deletions library/public/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ class F3D_EXPORT context
using function = std::function<fptr(const char*)>;

/**
* Create a native context function.
* Looks for GLX on Linux, WGL on Windows, and Cocoa on macOS
* Create a GLX context function.
* Only supported on Linux.
*/
static function native();
static function glx();

/**
* Create a EGL context function.
Expand Down
51 changes: 30 additions & 21 deletions library/public/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace f3d
* Example usage for a default scene file:
*
* \code{.cpp}
* f3d::engine eng = f3d::engine::createNative();
* f3d::engine eng = f3d::engine::create();
* f3d::loader& load = eng.getLoader();
* load.loadGeometry("path/to/file").loadGeometry("path/to/another/file");
* f3d::interactor& inter = eng.getInteractor();
Expand All @@ -36,7 +36,7 @@ namespace f3d
* Example usage for a full scene file:
*
* \code{.cpp}
* f3d::engine eng = f3d::engine::createNative();
* f3d::engine eng = f3d::engine::create();
* f3d::loader& load = eng.getLoader();
* load.loadScene("path/to/file");
* f3d::interactor& inter = eng.getInteractor();
Expand All @@ -48,25 +48,40 @@ class F3D_EXPORT engine
{
public:
/**
* Create an engine with a native window.
* Linux: Use GLX
* Windows: Use Win32
* macOS: Use Cocoa
* Create an engine with an automatic window.
* Linux: Try GLX, then EGL, then OSMesa
* Windows: Try Win32, then EGL
* macOS: Always use Cocoa
* Optionally, the window can be hidden by setting offscreen to true.
*/
static engine createNative(bool offscreen = false);
static engine create(bool offscreen = false);

/**
* Create an engine with no window.
*/
static engine createNone();

/**
* Create an engine with a GLX window.
* Works on Linux only.
* Optionally, the window can be hidden by setting offscreen to true.
*/
static engine createGLX(bool offscreen = false);

/**
* Create an engine with a WGL window.
* Works on Windows only.
* Optionally, the window can be hidden by setting offscreen to true.
*/
static engine createWGL(bool offscreen = false);

/**
* Create an engine with an EGL window.
* If several GPU are available, the environment variable
* `VTK_DEFAULT_EGL_DEVICE_INDEX` allows its selection.
* Optionally, the window can be hidden by setting offscreen to true.
*/
static engine createEGL();
static engine createEGL(bool offscreen = false);

/**
* Create an engine with an OSMesa window.
Expand All @@ -76,6 +91,7 @@ class F3D_EXPORT engine
/**
* Create an engine with an external window.
* A context to retrieve OpenGL symbols is required.
* The context can be nullptr for an external WGL or Cocoa window.
* Here's an example if a GLFW window is used:
* \code{.cpp}
* f3d::engine eng = f3d::engine::createExternal(glfwGetProcAddress);
Expand All @@ -84,11 +100,11 @@ class F3D_EXPORT engine
static engine createExternal(const context::function& getProcAddress);

/**
* Create an engine with an external GLX/WGL/Cocoa context.
* Equivalent to createExternal(f3d::context::native());
* Create an engine with an external GLX.
* Equivalent to createExternal(f3d::context::glx());
* Throws context::loading_exception if GLX library is not found or if not running on Linux.
*/
static engine createExternalNative();
static engine createExternalGLX();

/**
* Create an engine with an external EGL context.
Expand Down Expand Up @@ -266,17 +282,10 @@ class F3D_EXPORT engine
internals* Internals;

/**
* Engine constructor, choose the window type using the enum.
* see window.h for details about the window.
* When using window::Type::NONE, window and interactor will not be provided by the engine.
* When using window::Type::EXTERNAL, interactor will not be provided by the engine.
* All objects instances will be created on construction.
* Default is window::Type::NATIVE.
* Throw a no_window_exception when using a Using window::Type::EXTERNAL without the right cmake
* option.
* This is a private method. The user must rely on factories to create the engine instance.
* Engine constructor. This is a private method.
* The user must rely on factories to create the engine instance.
*/
engine(window::Type windowType = window::Type::NATIVE, const context::function& loader = nullptr);
engine(window::Type windowType, bool offscreen, const context::function& loader = nullptr);
};
}

Expand Down
2 changes: 1 addition & 1 deletion library/public/loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace f3d
* Example usage:
* \code{.cpp}
* std::string path = ...
* f3d::engine eng = f3d::engine::createNative();
* f3d::engine eng = f3d::engine::create();
* f3d::loader& load = eng.getLoader();
*
* if (load.hasSceneReader(path)
Expand Down
15 changes: 11 additions & 4 deletions library/public/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class F3D_EXPORT window
* =====================================
* NONE: A mock window without rendering capabilities.
* NATIVE: A window using the native graphical stack.
* NATIVE_OFFSCREEN: A native window rendering to an offscreen buffer, not visible on screen.
* EXTERNAL: An external window that assume the OpenGL context would have been created by another
* framework.
* EGL: An offscreen window using hardware acceleration that can run headless.
Expand All @@ -33,18 +32,26 @@ class F3D_EXPORT window
enum class Type : unsigned char
{
NONE,
NATIVE,
NATIVE_OFFSCREEN,
AUTO,
EXTERNAL,
GLX,
WGL,
COCOA,
EGL,
OSMESA
OSMESA,
UNKNOWN
};

/**
* Get the type of the window.
*/
virtual Type getType() = 0;

/**
* Is the window offscreen.
*/
virtual bool isOffscreen() = 0;

/**
* Get the camera provided by the window.
*/
Expand Down
Loading

0 comments on commit 5a5a940

Please sign in to comment.