Kaleidoscope shader

The images above are created by rendering some graphics to the back buffer, capturing this as a texture and then using that as input for a kaleidoscope shader. The kaleidoscope shader is trivially. I'm assuming you have drawn your scene and captured it to texture (see previous articles).

First the vertex shader.

varying vec4 vertexcoord;
void main(){
vertexcoord = gl_Vertex;
gl_Position = gl_Vertex;

First line 2. It sets the position to be the input vertex (not the more usual ftransform()). This ensures whatever object we draw ignores the transformation matrix. It just makes it easy to draw an object covering the screen. The important line is : vertexcoord = gl_Vertex; Vertexcoord will be used as texture coordinates in the fragment shader.

Now the fragment shader...

uniform sampler2D tex;
varying vec4 vertexcoord;
void main() {
gl_FragColor = texture2D ( tex, abs(vertexcoord.xy) );
So an input image is drawn on the screen using the vertexcoord set in the vertex shader. Theres a trick here: abs. This is the kaleidoscope effect - this simple! Now in the main code we just draw a rect...

A Kaleidoscope shader in very few bytes.

No comments:

Post a Comment