How to prototype graphical animation using C++ on Linux?

I’m starting my initial research for a VCV Rack module I want to create. It will simulate the physics of a simple system to produce sounds.

In order to make sure my C++ simulation code is behaving the way I expect, I want to animate it on my screen using graphics. I don’t need to do this animation in VCV Rack; I would prefer to write some throwaway code to verify correct dynamics before turning my simulator class into a sound generator.

All I need is the ability to draw 2D lines in different colors. Hopefully it can run with a reasonable frame rate and with a frame buffer to eliminate flicker.

Here is an example simulation I created in JavaScript using an HTML canvas that shows the kind of capability I want using C++ on Linux.

So my question is, what is a good way to quickly prototype a 2D animation of line segments like this using C++ on a Linux desktop? I have read stuff about OpenGL, GTK+, and some other graphics libraries, but it’s hard to tell which one to devote time to studying.

Thanks in advance for any advice!

My suggestion will be ditch all the giant GUI frameworks (Qt, GTK…) and find a simple game library that just opens a window and you can directly call something like draw_line() in it.

For example, here is raylib drawing some lines:

Raw graphics APIs (OpenGL, Vulkan) are not recommended for your use case too, they need you to do a lot of setup and deal with shaders, pipelines, weird state machine (OpenGL), etc.

To be honest, in my opinion it would be easier to prototype this inside VCV rack, rather than setting up a separate app since it already has all the rendering framework you need. As a bonus you could include that as feature on your VCO. If that is something you want to do I would look at the code used by the Fundamental Wave Tables. The code is in the WTDisplay struct in this file WaveTable.cpp. You can also find a wide range of draw methods in nanovg.h.

4 Likes

As @Patheros suggested, nanoVG is great at drawing 2D lines. Is your data 3D? If so, you will need to do your calculations in 3D and then project onto the screen/display context .

OpenFrameworks is good - https://openframeworks.cc/

nanoVG has a number of “transform” functions, but they appear to all be 2D. You would need to create your own 3D to 2D projection matrix (typically orthographic or perspective) and do your own matrix multiplication to transform from 3D to 2D.

I could have overlooked something in nanoVG.

I might add that for my own 3D visualization work I use the Unity game engine which I script in C#. That can get as complicated as you want, even to the point of doing HLSL GPU parallel computing.

While this is a total overkill for what you probably want to do, here is work I did last year in 10D string theory. I do the modelling in 6D for the Fermat surface parametric Calabi-Yau manifolds and then do cross sections and projections and then render in 2D inside of Unity. As you can see, the Calabi-Yau manifolds have some similarities to what you showed as an javascript example. All of my calculations are done on the GPU in HLSL massively parallel manner with typically 2 million points, from which I dynamically triangle tesselate on the GPU. I display both a point cloud and a triangle mesh version of the data side-by side.

Of course this cannot run inside of VCV and is thus just an example of using Unity as a multidimensional data visualization engine.

Thanks to @Patheros and @k-chaffin about the info about how to do arbitrary graphics inside VCV Rack. It is possible that the module I’m talking about will eventually include a visualizer of some kind. I certainly will want to do graphics in VCV Rack at some point. So I’m filing away this info in my ever-growing VCV Rack development notes.

However, there is a reason why I want an animation outside of VCV Rack. The system I am modeling will be something like a guitar string or a drum head vibrating. It will involve possibly hundreds of finite elements that need to be updated at the audio sampling rate.

Before I generate audio, I want to run the simulation at a very slow rate (e.g. 1/1000 speed) so I can see the waves moving gradually along the structure, and make sure the physics looks right. I anticipate this will involve lots of tweaking and adjusting. If I try to visualize this structure in real time while generating audio, it will animate so fast all I see is a blur. That would defeat any ability I have to judge the realism of the physics.

@Letheward, raylib looks like a great option! It looks like a low-friction way to quickly prototype what I’m trying to do, so I can focus on the physics without tangential distractions. As @k-chaffin mentioned, this will be a 3D animation, but it is simple to project onto 2D graphics using a rotation matrix and dilating by the z-coordinate for perspective.

There is no need for ray-tracing, shaders, etc. Learning Unity or something like that sounds like a blast, and it’s on my bucket list, but right now it will just slow me down for this specific project.

Thanks again everyone for taking the time to respond with useful info!

4 Likes