Beginners guide to GLSL (openGL shaders)

Posted on May 4th, 2012 by emil – Be the first to comment

Livecoder so you can follow along: glsl.heroku.com

GLSL is based on the syntax of C, and those of you that have written shaders in PixelBender will recognize the syntax.

There are two types of shaders that are commonly used, Vertex shaders and Fragment shaders.

Vertex shaders handle geometry, like positioning and rotating a 3d model. Usually with a 4×4 matrix in 3d, and 3×3 matrix in 2d.

Fragment shaders handle the output color. All examples here will handle fragment shaders.

The shader code is uploaded to the GPU, which will be able to render it blazingly fast.

The code we write in a fragment shader will be executed once per pixel. So a canvas of 1000×1000 pixels will execute the code 1 million times. Since the scope you have is only one pixel, it requires us to think a bit differently.

On glsl.heroku.com, you get 3 things you can play around with.

Time, mouse and resolution.

Time is a float that increases in value from the second you load the shader.

Mouse is a vec2 that keeps track of our current mouse position.

Vec2 is a data type that stores two floats. You can access the first value with .x, and the second with .y.

Resolution is another vec2 that stores the current resolution in pixels of the canvas.

So resolution.x would give us the width, and resolution.y the height.

void main( void){
}

This is our main function that will be run per pixel. We can create additional functions but everything originates from here.

gl_FragCoord (Fragment Coordinate) is the current pixel being rendered. Let us store this in a variable with a shorter name, for faster typing.

void main( void){
vec2 pos = gl_FragCoord.xy;
}

This could also be written:

void main( void){
vec2 pos = vec2( gl_FragCoord.x, gl_FragCoord.y);
}

gl_FragColor is the output color of the pixel that will be rendered to the screen. It’s a vec4, meaning it stores 4 floats.

it can be used like this:

gl_FragColor = vec4(red, green, blue, alpha);

The color and alpha values range from 0.0 - 1.0.

gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);//this will output black
gl_FragColor = vec4(1.0,1.0, 1.0, 1.0);//this will output white

We can stores these values in variables for easier manipulation.

vec2 pos = gl_FragCoord.xy;</p>
float red = 1.0;
float green = 1.0;
float blue = 1.0;
float alpha = 1.0;
gl_FragColor = vec4(red, green, blue, alpha);

Paste this in to the main function and the background should turn white.

Now let’s create the UV. The UV is basically just a value that ranges from 0.0-1.0, keeping track of the current pixels position in percent.

So if the resolution is 1000 pixels in width and the current current pixel is at pixel 500, the uv.x would be 0.5;

UV is handy to work with since colors values range from 0.0 - 1.0.

vec2 uv = pos.xy/resolution.xy;

So if we type uv.x, we get the current pixels position on the screen in percent. This is handy since the color values are in percent as well.

float red = uv.x;

The shader should update and now the background is a gradient.

By adding the values of the mouse to the mix, we can create some interactivity. The mouse variable is already in percent, so it’s easy to add.

float green = mouse.x;

Moving the mouse should now change and the green output color, and therefore the gradient.

In order to to animate something we need to use the time variable.

An easy way to get a value between 0.0 - 1.0 is to use sin();

sin() will output a value between -1.0 - 1.0 no matter what value you put in. So it’s perfect for our colors.

float blue = sin( time );

But we do not want to get -1.0.

float blue = (sin( time ) + 1.0) / 2.0;

To change the speed of the animation we simply multiply time.

float blue = (sin( time * 10.0 ) + 1.0) / 2.0;

Ok, too epileptic. Lets change to 2. And create a variable for speed.

float speed = 2.0;
float blue = (sin( time * speed ) + 1.0) / 2.0;

That’s it for the basics.

Uppercase your textfields!

Posted on January 13th, 2011 by Pål – Be the first to comment

Ever used the PSD import with “all case” text field that ended up lower case in Flash? Salvation is here with this command! Just select your textfields and run this command to uppercase them back.

Download the file here or check out the tiny code:

// ToUpperCase by Pål @ Apt 2011
var dc = fl.getDocumentDOM();
var sel_array = dc.selection; //Get selected items
 
for (var i=0;i<sel_array.length;i++) {
	s = sel_array[i];
	if (s.elementType == "text") { //Check if it is a textfield
		s.setTextString(s.getTextString().toUpperCase());
	}
}

HTML5 canvas particle experiment

Posted on August 20th, 2010 by Daniel – Be the first to comment

canvas-particles1

One of my first experiments with HTML5 canvas. Particles with gravity, moving against each other depending on the mass / size of each circle. Added some controls, so you can mess around with it. I did not have time to make controls for number of particles on screen, but maybe i’ll fix it later. No framework used, except from jQuery for the controls. The experiment is optimized for webkit, and there are some known bugs :)

Check it!

Multi-touch html demo/experiment (for iOS)

Posted on August 18th, 2010 by Pål – Be the first to comment

Screenshot from the appOn HP’s little challenge to show HTML5/Canvas stuff (beatiful vid btw.), here we go :-)

I had some time in June to experiment with iPhone web-apps and its proprietary Safari html. As a flash developer it was a fun to get my hands dirty and work with a little in the non-strict world of Javscript.

The idea of the web-app was to demonstrate multi-touch in a web-app and also for it to be a little playful drawing tool. The user can draw series of ribbons by swiping two or more fingers across the screen, creating a trail after the movements. Have a go at it on your iPhone or iPad, but remember to use two or more fingers.

Use your iPhone/iPad/iPhone touch and press here

Adding it to the home screen will also create a nice icon and it will run in fullscreen.

In this demo I’ve been using:

  • CSS3 (webkit-*) for 3D perspective
  • Canvas for drawing
  • Safari multi-touch events for user input
  • Icon for the home screen (apple-touch-icon)
  • Picture for the splash screen (apple-touch-startup-image)

For docs I mostly used developer.apple.com and the odd Google search. You are welcome to take a peek at the (single file) code if you are interested in how it works. Feedback is also welcome, ie. I dont think it works optimally on the Ipad. I havent got one yet :-)

PS. I have already got reports that this doesn’t work on Android (surprise!). But I dunno if that was a multi-touch enabled one.

Pål - twitter.com/paalsa

Show and Tell: OpenFrameworks || Cinder.

Posted on August 18th, 2010 by hp. – Be the first to comment

I would love to see what you people are doing out there with cinder and openFrameworks.

Here’s my first stab. CinderFlow code hosted on github.

Oh, all right, show your HTML5/Canvas stuff as well!

5 minute recipe for simple’n'easy export from symfony to Excel

Posted on June 15th, 2010 by Erland – Be the first to comment

So your client wants her data in Excel format? Not to worry. After reading this, you will be able to deliver data to her in a matter of minutes. Having said that, please bear in mind that it is not a generic solution for delivering dynamic Excel spreadsheets, merely a quick-fix solution.

But, you knew that already, didn’t you? Please read on for the good stuff…

read more »

Oldschool dissolve function

Posted on April 26th, 2010 by Thomas H – Be the first to comment

While doing a recent project I wanted to create a fade effect that worked well with the 8bit style design of the application. I wanted to use the dissolve effect, and lo and behold, flash has that function built in to the BitmapData class ( pixelDissolve ). Only problem was that the dissolve is on a pixel to pixel basis, so it did not look really authentic to a 8bit based graphics. All that was needed to do was to mimic the pixelDissolve function but apply them to squares of pixels.

read more »

Keep your flags in order.

Posted on April 21st, 2010 by hp. – 2 Comments

use a go style iota operator:

package rendering {
	public class RenderFlags {
 
		private static var _i:uint;
		private static function get iota():uint { return _i++; }
 
		public static const GENERIC:uint		= 0x1<<iota;
		public static const DIMENSIONS:uint 		= 0x1<<iota;
		public static const POSITION:uint 		= 0x1<<iota;
		public static const SCALE:uint 			= 0x1<<iota;
		public static const CHILDREN:uint 		= 0x1<<iota;
 
 
		public static const BOUNDS:uint = DIMENSIONS | POSITION;
		public static const ALL:uint = ~ 0x0;		
 
	}
}

“HTML5″ versus Flash: Animation Benchmarking

Posted on April 14th, 2010 by Pål – Be the first to comment

Check out this test, benchmarking a particle system with “HTML/Browser” against Flash. Seems like Flash still has the upper hand in pure animation power:
http://www.themaninblue.com/writing/perspective/2010/03/22

Is the only hope IE9? HW/GPU is needed to kick some ass here…

Also check out this test, done on the Google Nexus One.
http://vimeo.com/10553088

iPhone apps built with with Flash CS5

Posted on February 2nd, 2010 by Pål – 1 Comment

In the middle of all the “Flash on the iPad” controversy, it can be a little soothing to watch this video demoing apps built with upcoming Flash CS5.

http://theflashblog.com/?p=1737