Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tkinter? #43

Closed
jonwright opened this issue Oct 9, 2019 · 15 comments
Closed

Tkinter? #43

jonwright opened this issue Oct 9, 2019 · 15 comments
Labels
enhancement New feature or request

Comments

@jonwright
Copy link

I have some (rough) code at https://github.com/jonwright/pyopengltk which makes an OpenGL context for tkinter. You can summarise it as creating empty Tk.frame, and then using the window information (winfo_id/winfo_screen) for some ctypes calls.

Would you be interested to try to get a Tk backend working here?

@einarf
Copy link
Member

einarf commented Oct 9, 2019

Definitely want to give that a try at least. I'm not entirely sure how to cover mouse, keyboard and window events, but I'm guessing tkinter should be able to provide most of those? Worst case missing events can be emulated somehow.

I guess starting with a simple Tk window with a frame covering the entire window is a good idea. Then later make a widget that can be mixed with other UI elements (making one for PyQt5 and PySide2 as well).

It would probably require a bit of investment in your pyopengltk project. I see darwin is missing and I'm sure there will be other things to tweak as well to make this work. If it can benefit both pyopengl and moderngl users.. even better. I'm not sure if you had other things in mind?

One very important thing to resolve is setting a package license on github and in setup.py (making license visible on PyPI). I'm not sure what that would be, considering this is derived work from projects using CPOL (pytkogl), zlib (opengl-tcltk) and BSD (pyopengl).

There is a 1000% percent chance we will get questions about this if not resolved 😄
Worst case contact the maintainers of pytkogl + opengl-tcltk to be sure. All these licenses are fairly permissive and similar, so I'm sure it's not too complicated to resolve.

@einarf
Copy link
Member

einarf commented Oct 9, 2019

I manage to get moderngl context and basic rendering working on windows at least. There's of course a lot more work needed to make this into a proper window, but a good sign this idea is doable.

Capture

moderngl_window.context.base.window - INFO - Context Version:
moderngl_window.context.base.window - INFO - ModernGL: 5.5.3
moderngl_window.context.base.window - INFO - vendor: NVIDIA Corporation
moderngl_window.context.base.window - INFO - renderer: GeForce GTX 1070/PCIe/SSE2
moderngl_window.context.base.window - INFO - version: 4.6.0 NVIDIA 436.48
moderngl_window.context.base.window - INFO - python: 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit (AMD64)] 
moderngl_window.context.base.window - INFO - platform: win32
moderngl_window.context.base.window - INFO - code: 460

@jonwright
Copy link
Author

That was fast! I had been thinking to eliminate all the ctypes code and just use the winfo_id and winfo_screen to hook up the window with one of your contexts. It sounds like you already got it working, so I am happy I asked. I don't have access to mac but would be happy to do some testing on windows and linux (although it might take me some time to get up to speed).

@einarf
Copy link
Member

einarf commented Oct 9, 2019

The tkinter window is located here:
https://github.com/moderngl/moderngl_window/tree/master/moderngl_window/context/tk

You can test the window by running:

python.exe .\examples\moderngl_logo.py --window tk

.. or any other of the examples.

Currently window resizing, minimize and maximize works. The only way to close the window currently is clicking the window close button. I'll probably see if I can add some more event support later today.

I have access to win10, linux and OS X.

@einarf
Copy link
Member

einarf commented Oct 9, 2019

@jonwright This might be done pretty fast as an experimental window. See if you can fix the license issue because I don't want to deal with emails about this 😄

@einarf
Copy link
Member

einarf commented Oct 9, 2019

The way moderngl works in 5.x : create_context() relies on an existing opengl context and a default framebuffer. The alternative is create_standalone_context() for headless rendering. This is why pyopengltk still would be better off doing the window creation and default framebuffer creation.

We do have an experimental glcontext package : https://github.com/moderngl/glcontext

.. but I'm not sure if the plan is to support context creation and default framebuffer creation (that is not headless) in that one. It's a very odd use case.

@einarf
Copy link
Member

einarf commented Oct 9, 2019

I think the first version of the tk window is actually done now. Currently only tested on windows. I'll test it out tomorrow in linux.

What has been implemented:

  • A working tk.Window backend (in windows) properly detecting the context and default framebuffer. We also bypass tkinter's event loop.
  • Support for hiding mouse cursor
  • Support for fullscreen mode
  • Support for enable/disable window resize
  • Window resize event automatically calculating new viewport
  • Key press events
  • Key release events
  • Key pressed state map
  • Unicode character pressed event
  • Mouse position events
  • Mouse button events
  • Mouse button state map
  • Mouse drag events (when mouse is moving and any button is pressed)
  • Mouse wheel events (positive/negative y)
  • .. probably more

Missing features:

  • vsync control (Depends on pyopengltk)
  • Multisample anti-aliasing support for the default framebuffer (Depends on pyopengltk)

I created these issues in : https://github.com/jonwright/pyopengltk

@einarf
Copy link
Member

einarf commented Oct 9, 2019

Just a more complex test rendering the sponza scene. Ran this for about 15 minutes and the cpu is stable around 5%, gpu also around 5%. Memory also stable at 88.5MB. ~480 fps without vsync judging from the redering time. Looking good!

Capture

@einarf
Copy link
Member

einarf commented Oct 9, 2019

moderngl-window 1.5.0 is now on PyPI with the new experimental tkinter window.

For the optional dependencies for tk window:

pip install moderngl-window[tk]

.. and you can run examples using the tk window like this (pyglet window will be the default if --window is not specified)

# The basic animated logo example
python examples/moderngl_logo.py --window tk
# Other parameters ..
python examples/moderngl_logo.py --window tk --cursor no --resizable no
# more fun .. (mouse + AWSD / QE)
python examples/cube_model.py --window tk --fullscreen

We'll see how that works out on linux... and possibly OS X in the future. Don't think it should be that hard to do some minor tweaks in pyopengltk to conver the missing stuff. The pyglet code base can also be a great reference for this. They do everything with ctypes.

https://github.com/pyglet/pyglet

I can look into darwin support some time in the near future.

@einarf einarf added the enhancement New feature or request label Oct 9, 2019
@einarf
Copy link
Member

einarf commented Oct 10, 2019

Released moderngl-window 1.5.1 using pyopengltk==0.0.2 (from 0.0.1)

I'm not sure what the difference between these versions are, but at least it works. I assume some bug was fixed.

@einarf
Copy link
Member

einarf commented Oct 14, 2019

Works on Ubuntu Eoan Ermine (AMD Vega) ⭐

GLX version: 1.4
Screen is  0
Number of FBconfigs 336
Got a matching visual: index 144 33 xid 0x21
Is Direct?:  1
Done making a first context
base.window - INFO - Context Version:
base.window - INFO - ModernGL: 5.5.3
base.window - INFO - vendor: X.Org
base.window - INFO - renderer: AMD RAVEN (DRM 3.32.0, 5.2.0-15-generic, LLVM 9.0.0)
base.window - INFO - version: 4.5 (Compatibility Profile) Mesa 19.2.1
base.window - INFO - python: 3.7.4 (default, Sep 11 2019, 16:12:59) 
[GCC 9.2.1 20190909]
base.window - INFO - platform: linux
base.window - INFO - code: 450

Screenshot from 2019-10-14 14-56-00

Missing darwin support. Currently working on that. (Half way done)

@einarf
Copy link
Member

einarf commented Oct 18, 2019

Most of this is now resolved. We are still missing OS X support in pyopengltk.

More info here : jonwright/pyopengltk#4

@jonwright
Copy link
Author

Looks good to me. I managed to see some moderngl test windows. Eventually there may be enhancements needed for the context types etc, but those issues are in pyopengltk, I think. You could close this issue I guess?

@einarf
Copy link
Member

einarf commented Oct 18, 2019

Yup. I guess we can close this. moderngl-window provides a single interface to multiple window libraries so user can just switch the window type when on OS X. (Configuration option or enviroment variable).

@einarf einarf closed this as completed Oct 18, 2019
@einarf
Copy link
Member

einarf commented Oct 18, 2019

@jonwright Also, thanks a lot for suggesting a window for tikinter. I think a lot of people will be happy.

Adding separate widgets is the next step (existing task #42 ) so users can easily combine these with UI elements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants