11
Feb
08

Vertex Noise GLSL Sourcecode

As promised, here’s some sourcecode for the Vertex Noise effect.

This is the source for the effect described in the glsl discard, z-fighting, supersampling post from the 2nd Feb. This shader produces lots of aliasing ‘jaggies’, hence the need for supersampling, which is done in QC by rendering the shader at twice the output resolution using a Render In Image macro patch, then resizing it back down again with an Image Resize patch.

The Vertex Shader code is unchanged from the VertexNoise.qtz example that ships with the Leopard Developer Tools. I’ve added some very simple code to the Fragment Shader to create the stripes.


Vertex Shader

// vertexnoise.vert: Vertex shader for warping the geometry with noise.
// author: Philip Rideout
// Copyright (c) 2005-2006: 3Dlabs, Inc.
// See 3Dlabs-License.txt for license information

uniform vec3 LightPosition;
uniform vec4 surfaceColor;
uniform vec3 offset;
uniform float scaleIn;
uniform float scaleOut;
varying vec4 Color;

void main(void)
{
vec3 normal = gl_Normal;
vec3 vertex = gl_Vertex.xyz + noise3(offset + gl_Vertex.xyz * scaleIn) * scaleOut;

normal = normalize(gl_NormalMatrix * normal);
vec3 position = vec3(gl_ModelViewMatrix * vec4(vertex,1.0));
vec3 lightVec   = normalize(LightPosition - position);
float diffuse   = max(dot(lightVec, normal), 0.0);
if (diffuse < 0.125)
diffuse = 0.125;
Color = vec4(surfaceColor.rgb * diffuse, 1.0);
gl_Position = gl_ModelViewProjectionMatrix * vec4(vertex,1.0);
gl_FrontColor = gl_Color;
gl_TexCoord&#91;0&#93; = gl_TextureMatrix&#91;0&#93; * gl_MultiTexCoord0;
}
&#91;/code&#93;

<font color="#00ccff">Fragment Shader</font>
// Based on vertexnoise.frag: Fragment shader for warping the geometry with noise.
// author: Philip Rideout
// Copyright (c) 2005-2006: 3Dlabs, Inc.
// See 3Dlabs-License.txt for license information

varying vec4 Color;
uniform sampler2D Texture; // (Unused)
uniform float Freq, Width, StripeShift;

void main (void)
{
// Fragment coords
vec2 xy = gl_TexCoord[0].xy;

// Waveform phase
float phase = fract((xy.y + StripeShift) / Freq);

// Discard fragments
if (phase > Width)
discard;

// Output
gl_FragColor = Color;
}

As well as QC handling supersampling, I've also made extensive use of the Smooth patch, to give the controls a nice organic quality, so the above code alone can't reproduce the effect completely, but should give you an idea of what's going on.

Advertisements

0 Responses to “Vertex Noise GLSL Sourcecode”



  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Twitter

February 2008
M T W T F S S
« Jan   Mar »
 123
45678910
11121314151617
18192021222324
2526272829  

Links

Blog Stats

  • 473,930 hits

%d bloggers like this: