#version 420
// Input vertex data, different for all executions of this shader.
layout(location = 0) in vec3 vertexPosition_modelspace;
layout(location = 1) in vec3 vertexNormal_modelspace;
layout(location = 2) in vec2 vertexUV;
layout(location = 3) in vec3 vertexTangent_modelspace;
layout(location = 4) in vec3 vertexBitangent_modelspace;
// Output data ; will be interpolated for each fragment.
out vec2 UV;
out vec3 Position_worldspace;
out vec3 EyeDirection_cameraspace;
out vec3 EyeDirection_tangentspace;
out vec3 VertexNormalCameraspace;
out vec3 VertexPositionCameraspace;
// Values that stay constant for the whole mesh.
uniform mat4 P;
uniform mat4 V;
uniform mat4 M;
struct LightInfo {
vec4 Position;
vec4 View;
vec3 Color;
float Intensity;
float Bias;
};
uniform LightInfo Light[1];
out vec3 LightDirection_tangentspace[1];
uniform int ActiveShadowMaps;
uniform mat4 shadowmat[1]; // combination of projection * view matrices of the light source
out vec4 sc[1]; // output "shadow coords" used for sampling the depth-map texture
//convert clip coords to texture coords
const mat4 bias = mat4(0.5, 0.0, 0.0, 0.0,
0.0, 0.5, 0.0, 0.0,
0.0, 0.0, 0.5, 0.0,
0.5, 0.5, 0.5, 1.0);
uniform float MaterialTextureTileFactor;
void main(){
// Position of the vertex, in worldspace : M * position
Position_worldspace = (M * vec4(vertexPosition_modelspace,1)).xyz;
// Vector that goes from the vertex to the camera, in camera space.
// In camera space, the camera is at the origin (0,0,0).
VertexPositionCameraspace = ( V * M * vec4(vertexPosition_modelspace,1)).xyz;
EyeDirection_cameraspace = vec3(0,0,0) - VertexPositionCameraspace;
// UV of the vertex. No special space for this one.
UV = vertexUV * MaterialTextureTileFactor;
vec3 vertexTangent_cameraspace;
vec3 vertexBitangent_cameraspace;
// model to camera = ModelView
mat3 MV3x3 = V * M;
vertexTangent_cameraspace = MV3x3 * vertexTangent_modelspace;
vertexBitangent_cameraspace = MV3x3 * vertexBitangent_modelspace;
VertexNormalCameraspace = MV3x3 * vertexNormal_modelspace;
mat3 TBN = transpose(mat3(
vertexTangent_cameraspace,
vertexBitangent_cameraspace,
VertexNormalCameraspace
)); // You can use dot products instead of building this matrix and transposing it. See References for details.
// Fail! for (int i = 0; i < ActiveShadowMaps; i++) {
// Works!
for (int i = 0; i < 1; i++) {
// Vector that goes from the vertex to the light, in camera space. M is ommited because it's identity.
vec3 LightPosition_cameraspace = (V * vec4(Light[i].Position)).xyz;
sc[i] = bias * shadowmat[i] * vec4(Position_worldspace, 1);
LightDirection_tangentspace[i] = TBN * (LightPosition_cameraspace + EyeDirection_cameraspace);
}
EyeDirection_tangentspace = TBN * EyeDirection_cameraspace;
// Output position of the vertex, in clip space : MVP * position
gl_Position = (P * V * M) * vec4(vertexPosition_modelspace,1);
}