//Cubemap
vec4 pos;
//Extract the scale from the matrix
vec3 scalex=vec3(gl_TextureMatrix[2][0][0],gl_TextureMatrix[2][0][1],gl_TextureMatrix[2][0][2]);
vec3 scaley=vec3(gl_TextureMatrix[2][1][0],gl_TextureMatrix[2][1][1],gl_TextureMatrix[2][1][2]);
vec3 scalez=vec3(gl_TextureMatrix[2][2][0],gl_TextureMatrix[2][2][1],gl_TextureMatrix[2][2][2]);
vec3 scale=vec3(length(scalex),length(scaley),length(scalez));
//Transform vertex to eye space
pos=gl_Vertex;
pos.x=pos.x*scale.x*scale.x;
pos.y=pos.y*scale.y*scale.y;
pos.z=pos.z*scale.z*scale.z;
pos=pos*gl_ModelViewMatrixInverse;
pos.x=pos.x+gl_TextureMatrix[2][3][0];
pos.y=pos.y+gl_TextureMatrix[2][3][1];
pos.z=pos.z+gl_TextureMatrix[2][3][2];
//Transform eye space to world space
pos.x=pos.x-gl_TextureMatrix[3][3][0];
pos.y=pos.y-gl_TextureMatrix[3][3][1];
pos.z=pos.z-gl_TextureMatrix[3][3][2];
pos=pos*gl_TextureMatrix[3];
ReflectDir=vec3(pos.x,pos.y,pos.z)-CameraPosition;