Wednesday, February 17, 2010

GLSL Shader Examples Vertex and Fragment

Hi Everyone,

Here is a quick example of a GLSL shader I use in my c++ 3d engine. Just thought I'd share some of the GLSL syntax with the internet community. I will fill in more information soon!

Vertex Shader


varying vec3 normal;

void main() {

normal = normalize(gl_NormalMatrix * gl_Normal);


//gl_Position = gl_ModelViewMatrix * gl_Vertex;

/*vec2 test = vec2((gl_MultiTexCoord0.xy - 0.5) * 2.0);
test.x *= 1.01;
test.y *= 1.01;
gl_Position.xy = test.xy;
gl_Position.z = 0.0;
gl_Position.w = 1.0;*/

gl_Position = ftransform();
gl_TexCoord[0] = gl_MultiTexCoord0;
}


Fragment Shader

uniform sampler2D texture_0;
uniform sampler2D texture_1;
varying vec3 normal;

void main() {
vec4 color = vec4(texture2D(texture_0, gl_TexCoord[0].xy));
float NdotL,NdotHV;
vec3 lDir = vec3(0.0, 0.0, 1.0);

if(color.r + color.g + color.b == 0.0)
color = vec4(1.0, 1.0, 1.0, 1.0);

if(color.r < 0.85)
color.r = 0.0;

color.g = color.r;
color.b = color.r;
//color.a = color.r;

vec3 n = normalize(normal);
NdotL = max(dot(n,lDir),0.0);
color.rgb *= NdotL;
gl_FragColor = color;
}

No comments:

Post a Comment