graphics

experiments with opengl3.2/ogles3.3 on linux and win7
git clone http://frotz.net/git/graphics.git
Log | Files | Refs

simple.glsl (1136B)


      1 #version 140
      2 #extension GL_ARB_explicit_attrib_location : enable
      3 
      4 -- vertex
      5 
      6 layout(location = A_POSITION) in vec4 aPosition;
      7 layout(location = A_NORMAL) in vec3 aNormal;
      8 layout(location = A_TEXCOORD) in vec2 aTexCoord;
      9 
     10 layout(location = 3) in vec4 aOffset;
     11 
     12 out vec2 vTexCoord;
     13 out vec3 vPosition;
     14 out vec3 vNormal;
     15 
     16 void main() {
     17 	vec4 pos = aPosition + aOffset * vec4(127.0, 127.0, 127.0, 0.0);
     18 	vPosition = (uMV * pos).xyz;
     19 	vNormal = normalize(uMV * vec4(aNormal, 0.0)).xyz;
     20 	vTexCoord = aTexCoord;
     21 	gl_Position = uMVP * pos;
     22 }
     23 
     24 -- fragment
     25 
     26 in vec2 vTexCoord;
     27 in vec3 vPosition;
     28 in vec3 vNormal;
     29 
     30 void main() {
     31         vec4 c = uColor;
     32 	vec3 n = normalize(vNormal);
     33 	vec3 s;
     34 	if (uLightPosition.w > 0) {
     35 		/* positional light, compute direction */
     36 		s = normalize(uLightPosition.xyz - vPosition);
     37 	} else {
     38 		/* directional light - light position is actually a vector */
     39 		s = uLightPosition.xyz;
     40 	}
     41 	vec3 v = normalize(-vPosition);
     42 	vec3 h = normalize(v + s);
     43 
     44 	gl_FragColor = uAmbient * c
     45 		+ uDiffuse * c * max(dot(s, n), 0.0) 
     46 #ifdef SPECULAR
     47 		+ uSpecular * uLightColor * pow( max( dot(h,n), 0.0), uShininess)
     48 #endif
     49 		;
     50 
     51 }