Thursday, 24 March 2016

First code actually works!

Exciting times!  While waiting for half the lighting electronics to turn up (still waiting for the MOSFETs and a board to solder them into) I thought I'd turn my attention to the coding side of things.  I've had to pull out my extremely rusty novice C++ coding knowledge and get up to speed.

It was initially really difficult - finding something that suited my use case and platform.  The Raspberry Pi has libraries for a subset of OpenGL, called OpenGL ES (the ES is for Embedded Systems, see https://en.wikipedia.org/wiki/OpenGL_ES ). This means that a lot of the full-fat OpenGL examples have to be adapted. and most of the ES ones are to do with Android and iOS.

Inevitably this has meant a lot of cobbling stuff together, trying to write out other libraries where possible, and lots of looking at black (and white) screens, and scratching my head as to why nothing was happening!

As the week drew on, I've made more sense of exactly what the examples do, and have adjusted them to suit my needs. Until eventually, it worked!




So, my program basically works like this:

Initialise and compile an OpenGL shader program, consisting of:
  • a vertex shader which draws triangles based on points we feed it
  • a fragment shader which runs the Julia code, and picks a colour for each pixel on the triangles, with the C vector and scale/offset being set externally
Then, 
Set some initial defaults for C and scale

Loop
  Check the keyboard for keypresses and quit when there is one
  Read the mouse and see if it's moved
  Pass new values to the shaders if necessary
  Clear the screen
  Tell the shader program to draw 2 triangles that fill the screen
  (the shader draws the 2 triangles in the Vertex shader, and applies the Julia fragment shader to them)
Repeat

Computing the Julia set in the GPU is great because it's fast at floating point maths, and also means the main CPU can concentrate on all the other things I'll be asking the Pi to do!

The code is available on GitHub here: https://github.com/simonpeterscce/julia

No comments:

Post a Comment