Demo

I spent a little time today playing with the TexGen code today and ended up adding two new texture mutators - Twist and Warp. As you might imagine, Twist will cause the image to twist around a given point on the plane, accomplishing this by rotating requested image positions around the center point by an amount that is dependent on the distance from that center point. The other mutator, Warp, will cause a localized expansion or contraction of the image, accomplishing this by scaling requested image positions by an amount that is dependent on the distance from the requested point of the warp.

Demo

Inspired by Craig Reynolds' work on the subject I spent some time this weekend playing around with procedural texture generation, putting together a webpage where you can type in a small Javascript program that when run will generate a 512x512 image in a canvas element on the page. The image above is the output from the default sample program that is loaded up when you visit the page.

A bit of explanation from the help text. The page presents an interface for the creation and execution of small texture synthesis programs. Fundamentally, the program you type in needs to return some primitive function taking as input a point in R2 and returning an RGB floating point triple. There are several functions provided to aid in this process: generator functions which simply take some set of inputs and themselves return an appropriate primitive function, and mutator functions which will take some set of parameters as input as well as one or more primitive functions and return a new primitive function as a result. So, for example, the program typed in could simply be:

return TexGen.SoftSpot([0.3,0.6]);

SoftSpot is an example of a generator function - in this case it will return a primitive function that takes a point as input and returns white if its distance is less than 0.3 from the origin, black if its distance is more than 0.6 from the origin and a linear interpolation between.

There are a number of built-in functions provided to aid the creation of interesting images and I will be expanding it in the future. For now though you can go to the page, read more of the help text to get a list of the available functions and have some fun playing around with it. As always - feedback is appreciated.

This image took a little over 3 hours to generate on my laptop. For some reason I thought it would be fun to write a compile time raytracer - that is write a program using C++ template metaprogramming that would raytrace a scene during the compilation process. Really it was fun, but it sure is slow. What is the point? Well... there isn't much of one other than trying to bend my mind around the sometimes obscure machinery that is C++ templates. In the end the metaprogramming wasn't all that complicated - mostly just lots of time spent waiting on the compiler and getting distracted by Uncharted 3 (fun game).

I might work on a longer post later explaining the workings of the code but for now I have just uploaded it to github should anyone want to take a look.

Going with the whole programmatic music thing that has been going around, I went ahead put together a little project: Check it out.

The inspiration to do this started after I saw this video shared on YouTube:

The idea is simple: find very short programs that generate interesting sounding audio. For me at least, there is a certain beauty in discovering the complexity that can emerge from such simple programs.

Thus inspired I went about making my own little web app for making these things. I ended up taking the opportunity to have some fun playing with a few different bits of web tech. To start with I ended up playing with a JavaScript parser generator to support the parsing and compilation of these simple programs. Next, I decided I wanted people to be able to submit these programs for sharing and to be able to vote on the shared programs - so I ended up putting together a simple server that interfaces with Redis in order to store the programs and push submissions to the client. In the meantime, I have seen a couple other cool sites that serve much the same purpose, namely: this one and this one.

Usage should, I hope, be fairly straight forward. In the program box just type in your code and hit play. More specifically, you need to enter one or more functions of the form "let foo = (i rate) -> { ... }" in to the box and then call the function listen, passing in a function you wish to listen to as an argument. In this case, i is the current sample index to be returned and rate is the sampling rate in hz. The language itself is fairly simple, supporting a set of basic binary operators (+,-,*,/,&,|,^,<<,>>), the unary operators ! and ~, functions and immutable arrays ([0 1 2 3]). I might add support for conditionals at some point as well. Once you have a program you like, you can enter your name and the name of the program then click share and it should show up under the "Latest" tab at the bottom. Once submitted, anyone can hit Play on the shared item to listen to it and see/modify the program. If you like a shared program then you can hit the +1 button - the most highly rated programs should show up under the "Top" tab.

I've seeded the database with a few different example programs that don't really sound like much but should hopefully give people the idea. The Arrays program does actually sound like something fairly cool though and is a modification of this program that I have attributed to "Unknown" since, well, I don't know who originally made it.

So, please, have fun and make something cool. If you encounter issues with the functionality or interface don't be shy - go ahead and contact me.

I started working on a little project over the weekend that would involve needing to execute small user supplied programs on a webpage. As these programs would be fairly trivial (essentially just small bits of math) and as I was thinking it might be fun to provide a way to share them around, it made sense to look in to something a little safer and more constrained than just letting the user type some JavaScript and then evaling it.

Looking around for something to use for this I stumbled upon PEG.js, a JavaScript parser generator project, and I quickly got sidetracked with playing around with that. I ended up deciding to get my hands a little dirty and have some fun implementing a simple toy language. Having fairly little experience with parser generators and language implementation in general it took a little bit of time to get up and working with PEG.js, but after a while I started to grok it and found the whole thing fairly easy to work with.

As an example of how this works, consider the following simple grammar:

start = ((op:binop "\n"*) { return op })+
binop = left:[0-9]+ op:[+-/*] right:[0-9]+ {
	return {
		operator: op,
		args: [parseInt(left.join("")), parseInt(right.join(""))]
	}
}

Running this through PEG.js results in a parser that can parse a series of binary operations between integers separated by newline characters. So, for example, the parser could then be fed this as input:

22+3 2+3

The parser that was generated will take this input and transform it in to a JSON abstract syntax tree that looks like this:

[
	{
		"operator": "+",
		"args": [
			22,
			3
		]
	},
	{
		"operator": "+",
		"args": [
			2,
			3
		]
	}
]

Pretty simply stuff and the PEG.js website has some good documentation on it as well as a convenient page where you can test out grammars - just type in your grammar, let it generate a parser, feed the parser with some input and it will spit out the resulting JSON AST right there on the page. Very convenient for testing things out.

After about a day of poking around with PEG.js I had put together a simple grammar and paired the generated parser with a simple interpreter. You can try out the working result here - it's very basic, so I don't think it needs a whole lot of explanation to use. I should warn though that I have put in little to no error reporting at this point.

All in all it was a fun distraction and I was happy to spend some time playing with something outside of my normal programming adventures. Next up, I'll be returning to the project that started me off on this tangent in the first place.