Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //vert
- #version 330 core
- #define VERT_POSITION 0
- #define VERT_TEXCOORD 1
- #define VERT_NORMAL 2
- #define VERT_TANGENT 3
- layout( location = VERT_POSITION ) in vec3 position;
- layout( location = VERT_TEXCOORD ) in vec2 texcoord;
- layout( location = VERT_NORMAL ) in vec3 normal;
- layout( location = VERT_TANGENT ) in vec3 tangent;
- uniform struct Transform
- {
- mat4 model;
- mat4 viewProjection;
- mat3 normal;
- vec3 viewPosition;
- } transform;
- out Vertex
- {
- vec2 texcoord;
- vec3 normal;
- vec3 fragPosition;
- vec3 viewDir;
- } Vert;
- out mat3 TBN_matrix;
- void main(void)
- {
- vec4 vertex = transform.model * vec4( position, 1.0 );
- gl_Position = transform.viewProjection * vertex;
- vec3 t = normalize( ( transform.model * vec4( tangent, 0.0 ) ).xyz );
- vec3 n = normalize( ( transform.model * vec4( normal, 0.0 ) ).xyz );
- vec3 b = normalize( ( transform.model * vec4( cross( n, t ), 0.0 ) ).xyz );
- TBN_matrix = transpose( mat3( t, b, n ) );
- Vert.normal = ( transform.normal * normal );
- Vert.fragPosition = ( transform.model * vec4( position, 1.0 ) ).xyz;
- Vert.texcoord = texcoord;
- Vert.viewDir = transform.viewPosition - Vert.fragPosition;
- }
- //frag
- #version 330 core
- #define FRAG_OUTPUT0 0
- struct LightProperties
- {
- bool isEnabled;
- bool isLocal;
- bool isSpot;
- vec4 position;
- vec4 ambient;
- vec4 diffuse;
- vec4 specular;
- vec3 attenuation;
- vec3 spotDirection;
- float spotCosCutoff;
- float spotExponent;
- };
- struct Material
- {
- vec4 ambient;
- vec4 diffuse; //texture0
- vec4 specular; //texture1
- vec4 emission;
- float shininess;
- };
- uniform LightProperties light;
- uniform Material material;
- uniform sampler2D texture0; //diffuse
- uniform sampler2D texture1; //normal
- in Vertex
- {
- vec2 texcoord;
- vec3 normal;
- vec3 fragPosition;
- vec3 viewDir;
- } Vert;
- in mat3 TBN_matrix;
- layout( location = FRAG_OUTPUT0 ) out vec4 frag_color;
- float CalcReflect( vec3 norm, vec3 lightdir, vec3 viewdir, float shine );
- vec4 CalcLight( LightProperties light, Material material, vec3 normal, vec3 fragpos, vec3 viewdir );
- void main(void)
- {
- vec3 normal = normalize( ( TBN_matrix * ( 2.0 * texture( texture1, Vert.texcoord ).xyz - 1.0 ) ) );
- vec3 viewDir = normalize( Vert.viewDir );
- frag_color = vec4( 0.0 );
- frag_color = texture( texture0, Vert.texcoord ) * CalcLight( light, material, normal, Vert.fragPosition, viewDir );
- }
- vec4 CalcLight( LightProperties light, Material material, vec3 normal, vec3 fragpos, vec3 viewdir )
- {
- vec3 lightdir = ( light.position.xyz - fragpos );
- float distance = length( lightdir );
- lightdir = normalize( lightdir );
- vec4 output = vec4( 0.0 );
- output = material.emission;
- output += material.ambient * light.ambient;
- float diffuse = max( 0.0, dot( normal, lightdir ) );
- if( diffuse > 0.0 )
- {
- output += material.diffuse * light.diffuse * diffuse;
- }
- float specular = CalcReflect( normal, lightdir, viewdir, material.shininess );
- if( specular > 0 )
- {
- output += material.specular * light.specular * specular;
- }
- return output;
- }
- float CalcReflect( vec3 norm, vec3 lightdir, vec3 viewdir, float shine )
- {
- return max( pow( dot( reflect( lightdir, norm ), viewdir ), shine ), 0.0 );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement