Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //shader version
- #version 150 core
- uniform mat4 modelViewMatrix;
- //inverse and transpose matrix for normals
- uniform mat4 normalMatrix;
- //projectionMatrix*modelViewMatrix
- uniform mat4 modelViewProjectionMatrix;
- //input vertex: position, normal, texture coordinates
- in vec3 pos;
- in vec3 nor;
- in vec2 tex;
- //output vertex to fragment shader
- out VertexData
- {
- vec3 diffus_color;
- vec3 spec_color;
- vec3 position;
- vec3 normal;
- vec2 texcoord;
- } VertexOut;
- const vec3 light_pos = vec3(0, 5, 8);
- uniform mat4 viewMatrix;
- const vec4 mat_ambient = vec4(0.2, 0.2, 0.2, 1.0);
- const vec4 mat_diffuse = vec4(0.7, 0.7, 0.7, 1.0);
- const vec4 mat_specular = vec4(0.5, 0.5, 0.5, 1.0);
- const float mat_shininess = 5.0;
- const float light_power = 7.0;
- void main()
- {
- gl_Position = modelViewProjectionMatrix*vec4(pos.xyz,1);
- VertexOut.position = vec3(modelViewMatrix*vec4(pos.xyz,1));
- VertexOut.normal = vec3(normalMatrix*vec4(nor.xyz,1));
- VertexOut.texcoord = vec2(tex.xy);
- vec3 normalDirection = normalize(vec3(normalMatrix * vec4(nor.xyz, 1)));
- vec3 viewDirection = normalize(-vec3(modelViewMatrix * vec4(nor.xyz, 1)));
- vec3 vertexToLightSource = vec3(viewMatrix * vec4(light_pos, 1)) - VertexOut.position;
- float distance = length(vertexToLightSource);
- float attenuation = light_power / distance;
- vec3 lightDirection = normalize(vertexToLightSource);
- vec3 ambientLighting = vec3(mat_ambient);
- vec3 diffuseReflection = attenuation * vec3(mat_diffuse) * max(0.0, dot(normalDirection, lightDirection));
- vec3 specularReflection;
- if (dot(normalDirection, lightDirection) < 0.0) {
- specularReflection = vec3(0.0, 0.0, 0.0);
- } else {
- specularReflection = attenuation * vec3(mat_specular)
- * pow(max(0.0, dot(reflect(-lightDirection, normalDirection), viewDirection)),
- mat_shininess);
- }
- VertexOut.diffus_color = ambientLighting + diffuseReflection;
- VertexOut.spec_color = specularReflection;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement