OpenGL Drawing

197 views
Skip to first unread message

Ben Edwards

unread,
Apr 1, 2011, 7:46:58 PM4/1/11
to networkx...@googlegroups.com
All,

Here is the openGL drawing I mentioned in the google summer of code thread. I've tested it out some but would love more feedback. You need PyOpenGL installed, with a working OpenGL, glu and glut dev install. Doc strings should let you know how things work. The gl_windows.py file contains a gl_window class that sets up some sane defaults, and has some keyboard and mouse input. Mouse click and drags rotate and translate based on which mouse button used, and scrolling zooms in and out. 'r' resets the view, 'l' turns lighting on and off, 's' saves the current view to a file, and ESCAPE and 'q' quit the window. the nx_opengl contains some input parsing functions and the drawing functions, with a lengthy docstring telling you how things work. Currently I use lists for all the attributes, assuming the indices correspond to the indices in nodelist an edgelist. I am on the verge of converting these all to dictionaries, but this is how it was done in the matplotlib function. Let me know what you think is best. In the end I think I'll make it accept single values, lists, and dicts as input. It supports variable node size, and edge labels!

Caveats:
No node shapes. This can likely be accomplished with some clever sprites, but I am not there yet.
Node text isn't quite centered in the node, open gl doesn't have good centering capabilities, but I think I can make this happen with some cleverness
Arrows are drawn correctly initially, but as you zoom in and out and rotate, they slowly drift up and down the edge. I've puzzled over this a fair amount, so hopefully someone will have some ideas
Because of the way the openGL blocks and exits, the class actually spawns a new process for itself to run in, so after it is spawned you don't really have access to it. This is unlike matplotlib, where you can send multiple draw commands to the same canvas. I think all the same effects can be captured as in the matplotlib function, but it might take a bit of code refactoring. I've refactored the atlas gallery example and include it here.
Still uses matplotlib colors to draw. There color parsing library and colormap is just so tempting to use...
Uses PIL or Image class to save files. I can't tell if Image is part of python or not?

You can see some tests in the __main__ function. 

Any testing you could provide would be great! Also let me know how hard it was to get PyOpenGL running. It seemed too easy to me, just downloaded the library and did python setup.py install. 

Ben
atlas.py
nx_opengl.py
gl_windows.py

Stephan Gerhard

unread,
Apr 6, 2011, 8:31:14 AM4/6/11
to networkx...@googlegroups.com
Hi Ben,

Thanks for providing your example code! I tested them, they all seem to work (PyOpenGL was easy to install with
Ubuntu, package python-opengl). However, if I increase the number of nodes, the framerate drops quite a bit.

Is there the plan to extend NetworkX to incorporate such OpenGL drawing methods?

I work on some very similar code to draw networks using opengl. I was also heavily inspired by the NetworkX API.
It would be really nice if we could agree on some standard API, i.e. of what you can provide to a graph drawing methods,
so that you can "replace" the backend for actual drawing easily.

The project is called fos (see http://fos.me/ ) - and uses pyglet under the hood for windowing, and customized actors. the
network actor is here: https://github.com/Garyfallidis/Fos/blob/master/fos/actor/network.py
there are examples of how to use the code in the examples folder.

Let me know what you think, and whether you see some overlaps!

Stephan

--
You received this message because you are subscribed to the Google Groups "networkx-discuss" group.
To post to this group, send email to networkx...@googlegroups.com.
To unsubscribe from this group, send email to networkx-discu...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/networkx-discuss?hl=en.

Ben Edwards

unread,
Apr 6, 2011, 8:10:13 PM4/6/11
to networkx-discuss
Thanks for the testing. It is possible that your framerate is
dropping, either because the graph is directed (arrows are a little
expensive to draw), or it's because I have turned anti-aliasing up
pretty high. There are some function calls in glinit, that might do
with some commenting out to make things faster. Currently borders are
drawn as extra vertices too, so turning them off might be a good
option to have.

After messing around with it some more, and finding some bugs I am not
sure this is the best thing for NetworkX. There are a lot of
visualization tools out there and I am not sure we need another poorly
written openGL one in NetworkX. I'm working on a proposal for Google
Summer of Code, that I think the main thrust of will be interfacing
with other systems (like Gephi), perhaps making a standard way of
doing so. The idea would be to make a separate compatible module, so
that NetworkX could continue to focus on algorithms. In that case fos
might be interesting to interface.

Ben.

On Apr 6, 6:31 am, Stephan Gerhard <enthou...@unidesign.ch> wrote:
> Hi Ben,
>
> Thanks for providing your example code! I tested them, they all seem to work
> (PyOpenGL was easy to install with
> Ubuntu, package python-opengl). However, if I increase the number of nodes,
> the framerate drops quite a bit.
>
> Is there the plan to extend NetworkX to incorporate such OpenGL drawing
> methods?
>
> I work on some very similar code to draw networks using opengl. I was also
> heavily inspired by the NetworkX API.
> It would be really nice if we could agree on some standard API, i.e. of what
> you can provide to a graph drawing methods,
> so that you can "replace" the backend for actual drawing easily.
>
> The project is called fos (seehttp://fos.me/) - and uses pyglet under the

satyam

unread,
Apr 22, 2011, 3:25:47 PM4/22/11
to networkx-discuss
Hi I was running the code atlas.py and it gave me the following error:

graph has 779 nodes with 1073 edges
(137, 'connected components')
Traceback (most recent call last):
File "atlas.py", line 96, in <module>
dim=2)
File "/Users/mukherjee/Downloads/nx_opengl.py", line 265, in
draw_opengl
edge_label_color))
File "/Users/mukherjee/Downloads/gl_windows.py", line 132, in
__init__
GLUT.glutCloseFunc(self.on_exit)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/
lib/python2.6/site-packages/OpenGL/GLUT/special.py", line 138, in
__call__
self.wrappedOperation( cCallback, *args )
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.6/
lib/python2.6/site-packages/OpenGL/GLUT/special.py", line 106, in
failFunction
cheme-54:Downloads mukherjee$ typeName, 'glut%sFunc'%(typeName),
OpenGL.error.NullFunctionError: Undefined GLUT callback function
Close, check for bool(glutCloseFunc) before calling

Ben Edwards

unread,
Apr 22, 2011, 4:10:51 PM4/22/11
to networkx-discuss
I am not sure, but this may be a problem with the GLUT version or your
PyOpenGL version. Can you tell me what versions you are running?

Ben

franck kalala

unread,
Apr 22, 2011, 6:36:45 PM4/22/11
to networkx...@googlegroups.com
I am having the same error message,
The code altlas.py was running well
in my revious version of networkx
am under version 1.3
Franck


----- Message d'origine ----
De : satyam <dira...@gmail.com>
À : networkx-discuss <networkx...@googlegroups.com>
Envoyé le : Ven 22 avril 2011, 20h 25min 47s
Objet : [networkx-discuss] Re: OpenGL Drawing

Ben Edwards

unread,
Apr 25, 2011, 1:03:35 PM4/25/11
to networkx-discuss
Do you know what version of GLUT you have installed? The window close
callback was added fairly recently, and since PyOpenGL is just a
wrapper around the existing OpenGL, that could be the problem. Also
are either of you running windows? It seems there are some funny
things with this callback on windows.

Ben

On Apr 22, 4:36 pm, franck kalala <franckkal...@yahoo.fr> wrote:
> I am having the same error message,
> The code altlas.py was running well
> in my revious version of networkx
> am under version 1.3
> Franck
>
> ----- Message d'origine ----
> De : satyam <dirac....@gmail.com>

satyam

unread,
May 5, 2011, 12:22:40 PM5/5/11
to networkx-discuss
There is some problem with Mac I think, but works fine in Ubuntu.
However I would like to know how to save the network figure. Earlier I
was displaying the network in pyplot.show() and then saving it (with
various options like .svg, .eps, .jpg, .pdf...etc. But in openGL such
things don't show up.
Thanks

Ben Edwards

unread,
May 5, 2011, 12:55:25 PM5/5/11
to networkx-discuss
While you have focus on the window, hit the 's' key, it will save
exactly whatever you have displayed. You can include a save_file
parameter, and a save_file_type, which will determine the file name
and type, Something like

#Not tested
>>> nx_opengl.draw_opengl(G,save_file="my_graph",save_file_type='jpg')

should save the file as my_graph0.jpg, with mulitple saves
incrementing the number at the end. The file name can include a full
path as well.

Ben

satyam mukherjee

unread,
May 6, 2011, 4:14:55 PM5/6/11
to networkx...@googlegroups.com
Well hitting 's' key saves the file only as png even if I specify the type as 'jpg' or 'svg'....Moreover the if I display png file I dont see any network structure. Its just some hazy lines all over the figure.
-------------------------------------------
WHEN LIFE GIVES U HUNDRED REASONS TO CRY,SHOW LIFE THAT U HAVE THOUSAND REASONS TO SMILE :-)

satyam mukherjee
224-436-3672 (Mob)
847-491-7238 (Off)

Ben Edwards

unread,
May 6, 2011, 5:52:14 PM5/6/11
to networkx-discuss
The filetype bug is fixed. Try to repull from my git repo, it's all
contained in a single git-repo with some other things. The change is
made to gl_windows.py

http://cs.unm.edu/~bedwards/python_lib.html

The hazy lines I am less sure about. I had a similar problem during
development, and believed it to be fixed. Try the new version. Do you
know what version of PIL and OpenGL you have?

Ben
> ...
>
> read more »

satyam mukherjee

unread,
May 8, 2011, 12:18:57 PM5/8/11
to networkx...@googlegroups.com
I am not sure about the version of openGL or PIL. Also I am not able to pull from the git repo. Could you post it here as attachment.
Thanks.

satyam mukherjee

unread,
May 10, 2011, 5:27:44 PM5/10/11
to networkx...@googlegroups.com
Hi Ben, I have figured out the issue with the hazy lines. The PIL version is PIL-1.1.7 and for the PyOpenGL its PyOpenGL-3.0.0. However I can save the image in png format, not in svg format.
Thanks
Satyam
Reply all
Reply to author
Forward
0 new messages