#version 120 #define USE_LIGHTMAP #define MAX_LIGHTS_PER_OBJECT 4 varying vec3 WorldNormal; varying vec4 WorldPosition; varying vec4 ReflectionMapSamplingPos; varying vec4 CausticTexCoords; varying vec4 ShadowMapSamplingPos; varying vec4 ShadowMapSamplingPos2; varying vec4 ShadowMapSamplingPos3; varying vec4 MVar; varying float ClipDistance; // **************************************************************************** // Shadowmap Parameters // **************************************************************************** uniform mat4 mWorld; uniform mat4 mWorldViewProj; uniform mat4 mWorldViewProj2; uniform mat4 mShadowWorldViewProj2; uniform mat4 mShadowWorldViewProj3; uniform float mMaxD; uniform float mMAPRES; // Sunlight Parameters struct Sunlight { vec3 mSunPosition; vec3 mSunDirection; vec3 mSunColor; float mUseSun; }; uniform Sunlight sun; // Reflection Parameters uniform mat4 mReflectionView; uniform mat4 mCausticMatrix; uniform vec2 mTextureRotate; uniform vec2 mTextureScale; uniform vec2 mTextureTranslation; uniform vec4 mClipPlane; // **************************************************************************** // Skinning Parameters // **************************************************************************** #if defined USE_SKINNING || defined USE_INSTANCING #define MaxBones 75 // Max bones shader can support #define RowCount 3 uniform vec4 mJointTransform[MaxBones * RowCount]; // Bone matrices #endif // **************************************************************************** // VERTEX SHADER // **************************************************************************** void main() { // ************************************************************************* // Non-skinned Objects // ************************************************************************* // Calculate mWorld normal, mWorld position, and object position vec4 Tangent = vec4(gl_MultiTexCoord1.xyz, 0.0); vec4 Normal = vec4(gl_Normal, 0.0); // Calculate view direction from the eye to the surface, unnormalized //vec3 eyePosition = (gl_ModelViewMatrixTranspose * -view[3]).xyz; vec4 MeshPos = vec4(gl_Vertex.xyz, 1.0); #ifndef USE_SKINNING #ifdef USE_INSTANCING int index = int(Tangent.x) * RowCount; mat4 matTran; matTran[0] = mJointTransform[index]; matTran[1] = mJointTransform[index+1]; matTran[2] = mJointTransform[index+2]; matTran[3] = vec4(0, 0, 0, 1); MeshPos = MeshPos * matTran; WorldPosition = MeshPos; gl_Position = mWorldViewProj * MeshPos; WorldNormal = normalize(Normal * matTran).rgb; #else gl_Position = mWorldViewProj * gl_Vertex; WorldPosition = mWorld * gl_Vertex; // Build tangent space to mWorld space matrix WorldNormal = normalize(mWorld * Normal).rgb; #endif #else int verCol = (int(gl_Color.r * 255.9) - 1) * RowCount; mat4 matTran; matTran[0] = mJointTransform[verCol]; matTran[1] = mJointTransform[verCol+1]; matTran[2] = mJointTransform[verCol+2]; matTran[3] = vec4(0, 0, 0, 1); matTran *= gl_MultiTexCoord2.x; mat4 temp; verCol = (int(gl_Color.g * 255.9)); if(verCol > 0) { verCol = (verCol - 1) * RowCount; temp[0] = mJointTransform[verCol]; temp[1] = mJointTransform[verCol+1]; temp[2] = mJointTransform[verCol+2]; temp[3] = vec4(0, 0, 0, 1); matTran += temp * gl_MultiTexCoord2.y; } verCol = (int(gl_Color.b * 255.9)); if(verCol > 0) { verCol = (verCol - 1) * RowCount; temp[0] = mJointTransform[verCol]; temp[1] = mJointTransform[verCol+1]; temp[2] = mJointTransform[verCol+2]; temp[3] = vec4(0, 0, 0, 1); matTran += temp * gl_MultiTexCoord2.z; } verCol = (int(gl_Color.a * 255.9)); if(verCol > 0) { verCol = (verCol - 1) * RowCount; temp[0] = mJointTransform[verCol]; temp[1] = mJointTransform[verCol+1]; temp[2] = mJointTransform[verCol+2]; temp[3] = vec4(0, 0, 0, 1); matTran += temp * gl_MultiTexCoord3.x; } // Apply final transformation. MeshPos = MeshPos * matTran; gl_Position = mWorldViewProj * MeshPos; WorldPosition = mWorld * MeshPos; WorldNormal = normalize(mWorld * (Normal * matTran)).rgb; #endif // Texture coordinates get interpolated for pixel shader //gl_TexCoord[0] = gl_MultiTexCoord0; gl_TexCoord[0].xy = (vec2( gl_MultiTexCoord0.x*mTextureRotate.x - gl_MultiTexCoord0.y*mTextureRotate.y, gl_MultiTexCoord0.x*mTextureRotate.y + gl_MultiTexCoord0.y*mTextureRotate.x) + mTextureTranslation) * mTextureScale; #ifdef USE_LIGHTMAP gl_TexCoord[1] = gl_MultiTexCoord1; #endif #ifdef USE_REFLECTION vec4 pos = mReflectionView * gl_Vertex; ReflectionMapSamplingPos.x = 0.5 * (pos.w + pos.x); ReflectionMapSamplingPos.y = 0.5 * (pos.w + pos.y); ReflectionMapSamplingPos.z = pos.w; ReflectionMapSamplingPos.w = 1.0; #endif #ifdef USE_CAUSTICS //CausticTexCoords = (mCausticMatrix * gl_Position * 0.1) + 0.5f; // ? und korrektur der position des projizierten bildes //CausticTexCoords.xy = (mWorld * vec4(CausticTexCoords.x, CausticTexCoords.y, 0, 1)).xy; #endif // ************************************************************************* // Shadow Calculation // ************************************************************************* vec4 SMPos = mWorldViewProj2 * vec4(MeshPos.xyz, 1.0); SMPos.xy *= 0.5; ShadowMapSamplingPos = SMPos; vec3 LightDir = normalize(sun.mSunPosition - WorldPosition.xyz); MVar = vec4(gl_Position.z, clamp(dot(WorldNormal, LightDir), 0.0, 1.0), mMaxD, mMAPRES); SMPos = mShadowWorldViewProj2 * vec4(gl_Vertex.xyz, 1.0); SMPos.xy *= 0.5; ShadowMapSamplingPos2 = SMPos; SMPos = mShadowWorldViewProj3 * vec4(gl_Vertex.xyz, 1.0); SMPos.xy *= 0.5; ShadowMapSamplingPos3 = SMPos; if (mClipPlane != vec4(0.0)) ClipDistance = dot(mClipPlane, vec4(gl_Position));//gl_Position;//mView * WorldPosition; else ClipDistance = 1.0; //gl_ClipDistance[0] = dot(mClipPlane, vec4(gl_Position)); }