Embedding a GLFW window inside windows forms - c++

I'm making an editor for 3d worlds in opengl using the windows forms UI. I'm developing on visual studio 2012 express and i would like to embed a GLFW window/context inside a windows form. just like done in this tutorial http://www.codeproject.com/Articles/16051/Creating-an-OpenGL-view-on-a-Windows-Form except working. And with GLFW for opengl context.
Is this possible? should i use Qt instead? and how would i got about doing this? I'm not bound to using windows forms i just need a simple good-looking functional UI for my project.

That are three different frameworks (Windows Forms, GLFW, Qt) which can all do the same thing, i.e. creating a window and an OpenGL context in it.
See here for an easy example how to create a window with OpenGL context with GLFW.
Or see here for the Qt example.
So, you have to choose between one of them. GLFW and Qt have the advantages that your code will also work on MacOSX and on Linux; with some work even on iPhone.
If you want to do the window creation and event handling and other stuff with GLFW but the GUI still with Qt, there is some way to do offline drawing (some ref here or here or here) the Qt widgets and then draw it onto some OpenGL texture. Or you might also be able to directly draw them via OpenGL (not exactly sure).

Related

How to embed opencascade V3d_View in gtkmm widget

I'm trying to port the code from https://github.com/eryar/occQt to gtkmm, by creating a custom widget and overriding the Gtk::widget::on_realize() method like
void OccView::on_realize() {
// Create Aspect_DisplayConnection
Handle(Aspect_DisplayConnection) display_connection = new Aspect_DisplayConnection();
// Get graphic driver if it exists, otherwise initialize it.
Handle(Graphic3d_GraphicDriver) graphic_driver;
if (!graphic_driver) {
graphic_driver = new OpenGl_GraphicDriver(display_connection);
}
// Get window handle. This returns something suitable for all platforms.
Window x_window = GDK_SURFACE_XID(get_native()->get_surface()->gobj());
// Create window for platform.
Handle(Xw_Window) xw_window = new Xw_Window(display_connection, x_window);
// Create V3dViewer and V3d_View
mViewer = new V3d_Viewer(graphic_driver, Standard_ExtString("viewer3d"));
mView = mViewer->CreateView();
// Set window for the view
mView->SetWindow(xw_window);
if (!xw_window->IsMapped()) {
xw_window->Map();
}
// Create AISInteractiveContext
mContext = new AIS_InteractiveContext(mViewer);
// Set up lights etc
mViewer->SetDefaultLights();
mViewer->SetLightOn();
mView->SetBackgroundColor(Quantity_NOC_BLACK);
mView->MustBeResized();
mView->TriedronDisplay(Aspect_TOTP_LEFT_LOWER, Quantity_NOC_GOLD, 0.08, V3d_ZBUFFER);
mContext->SetDisplayMode(AIS_Shaded, Standard_True);
// Call base method
Gtk::Widget::on_realize();
}
but the Gtk::Window stays empty after appending the OccView object. What am I doing wrong? Is there a working example on how to integrate the Opencascade V3d_View into a Gtk::Widget, or the gtkmm framework in general?
I haven't used GTK since university, so my experience is pretty basic here.
There are two basic approaches for embedding OpenGL-based viewer into GTK:
Ask OCCT to create OpenGL context for a native window taken from a normal Widget or entire window.
Wrap existing OpenGL context created by GUI library itself, e.g. Gtk::GLArea.
Your current code tries to follow the first approach used by conventional samples for Qt Widgets and MFC coming with OCCT. I guess it should be feasible, but implies some limitations and issues with mixing GTK widgets, as GTK will not be aware of OpenGL usage.
In contrast, Gtk::GLArea looks like a "modern" way for embedding OpenGL renderer designed by GTK developers and expected to work transparently.
Therefore, I've tried implementing a Hello-World sample using Gtk::GLArea (based on a development snapshot of OCCT 7.6.0dev):
https://github.com/gkv311/occt-samples-gtk
I don't bring the whole code of the sample here, as it is quite large in size.
Putting OCCT Viewer into Gtk::GLArea includes some tricky parts like:
Wrapping native Window into Aspect_Window (it could be also Xw_Window like in your sample, more general Aspect_NeutralWindow or another subclass).
Wrapping OpenGL context created by Gtk::GLArea into Aspect_RenderingContext/OpenGl_Context.
Wrapping offscreen buffer (FBO) used by Gtk::GLArea for rendering content into OpenGl_FrameBuffer.
Putting all viewer redraws into dedicated callback for Gtk::GLArea::signal_render().
Redirecting user input to viewer (with help of AIS_ViewController).
It is important to note, that GTK may be run in different context:
X11 server - X Window is created and GLX is used for OpenGL.
This is default OCCT configuration for Linux;
Wayland - native window is not X Window and EGL is used for OpenGL context.
OCCT does support EGL but as a dedicated configuration as alternative to GLX, while GTK handles this in runtime somehow. In addition, OCCT does not (yet) provide any wrapper for a Wayland native window, though it might be not critical for using.
GTK also has an option to use OpenGL ES instead of OpenGL.
Initially I expected Gtk::GLArea to work natively, but instead a very basic sample (without OCCT viewer) displays artifacts to me (widgets randomly blacked) on Xubuntu 18.04, though it works as expected on Ubuntu 21.04 (within Xorg session). I don't know if it is a bug fixed in GTK implementation, or there is something that should be fixed in a sample to workaround problem on older Linux.

Using SDL, OpenGL and Qt together

For a while I've been using SDL to write my 3D engine,and have recently been implementing an editor that can export an optimized format for the type of engine Im building. Right now the editor is fairly simple, objects can just be moved around and their textures and models can be changed. As of right now, I'm using SDL with OpenGL to render everything, but I want to use Qt for the GUI part of the editor, that way it looks native on every platform. I've got it working great so far, I'm running a QApplication inside of the SDL application, so it basically just opens 2 windows, one that uses SDL and OpenGL, and the other using Qt. Doing a bit of research, I've found that you can manually update a QApplication, which totally removes any threading problems, and everything works. Just in case you're having a hard time visualizing this, heres a picture:
What my goal is to merge these windows into one, because on smaller screens (like my laptop's) it makes it really hard to keep track of all the different windows that I would eventually have. I know theres a way to render to Qt with OpenGL, but can this be integrated with SDL? Am I going to need to move away from using an SDL window and use a QT one if the editor is enabled? Just to clarify, when the engine isn't in editor mode, it won't use and Qt, just SDL, so optimally I wouldn't need to do this.
Drop the SDL part. You have Qt, which does everything SDL does as well. Just subclass a QGLWidget and use that.
You can keep your game and editor separate processes and still make them part of the same app.
Just spawn the window where you want the game to run as part of Qt, and at least in windows, you can then pass the window handle to your game, and make sure when your game is setting up, instead of creating the window yourself in SDL and binding the opengl context to it, you can actually bind to the existing handle.
There are some gotchas with this technique to watch out for such as input focus I believe (I tested with directX, but it might be similar with SDL). The problem is that the foreground mode does some dumb checks on the "root" window which for me was not the window that owned the opengl context, so it failed to initialize. However background mode worked. I think that was for a joystick now that I think about it, but anyway, that's how you can merge everything together.

Hook SDL, SFML or GLFW into existing OpenGL application

I am working with an existing OpenGL library which needs to be augmented with other UI functions (better keyboard input, mouse handling, etc). I was hoping to use SDL, SFML or GLFW with the existing OpenGL API to facilitate this. Using any of these frameworks, is it possible to hook UI functions from any of these frameworks into an existing window, rather than a window directly created from these frameworks?
-The existing OpenGL window is already created by the library I'm forced to use.
-I'm aware of the SDL_WINDOW_FOREIGN bit set but am not sure how this is supposed to work.
-Is there a better strategy for simply detecting mouse/keyboard input?
The SDL 2.0 function SDL_CreateWindowFrom() can set up SDL for input and rendering from a given native window.
https://wiki.libsdl.org/SDL_CreateWindowFrom

OpenGL in Qt Quick cross platform application

I'm trying to develop a cross platform (or at least desktop + embedded hardware) application. I would like to use Qt Quick to create a touch friendly GUI. I have been implemented a classical application with a QGLWidget displaying data. It is important that only a part of the window is in OpenGL. Because of this there are problems with EGLFS and LinuxFB. Only X11 (or maybe Wayland) can display the application properly (others generates a couple of errors about missing setParent function and the whole screen is black). Now I'm trying to achieve the same thing in QML. I want to use this OpenGL renderer as part of my QML application and some Qt Quick widgets around it. I found a couple of people asking about the same thing and the answer is always to subclass QDeclarativeItem and call the painter's beginNativePainting() (the others says to export it through QDeclarativeItem, but I cannot figure out how to do this). The problem is that on desktop, Qt 5.11 the native painter is not OpenGL. And in QT5 there is no way to force OpenGL graphics system. So when I try to get the OpenGL context (QGLContext::currentContext()) I always get NULL. Another problem: If I export my widget with qmlRegisterType("Test", 1, 0, "Test"); it becomes only visible when I use QDeclarativeView, but then it doesn't sees Qt Quick. If I use QQuickView it says module "Test" is not installed. How can I implement this properly?
QDeclarativeItem is from Qt Quick 1 and Qt4. With Qt 5 and Qt Quick 2 you should use QQuickItem.
There is at least 1 example of this provided with qt docs, which you can find in Qt Creator in the Welcome tab in the Examples section.

How to create a fullscreen OpenGL-ES renderview in Windows?

I'm using cocos2d-x to develop an iPhone game and then it just came to my head why not release my game for PC too? The only problem is that setting the window to full screen mode is not implemented yet. Now I'm just stuck with how to create a full screen window? There are some window creation functions that are used but I'm not sure which one and how I should change.
There is the eglCreateWindowSurface function that cocos2d is calling to create a window. I'm not sure which option I should change so that it creates a full screen window. It would also be nice if I can implement a function that switches my game to full screen mode and back while running.
On Windows it's a bit more compilicated. Essentially you have to:
Create a proxy OpenGL context to get access to functionality above OpenGL-1.1 through extensions
Load the extensions required to create an OpenGL-ES compatible context
Create the higher version OpenGL context
Again Load the extensions, now for this context
Luckily all this has been wrapped up in several easy to use libraries. I recomment GLFW for Window/Context creation (it deals with all that proxy context stuff, too), and GLee or GLEW to make the OpenGL extensions available to the code.
http://www.glfw.org/
http://elf-stone.com/glee.php
http://glew.sourceforge.net/
Those libraries are cross platform, so your application/game itself does not use OS dependent functions, it will compile not only for Windows but also Linux, BSD and MacOS X.