graphics

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

simple.glsl (1209B)


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