vec3 cubeCoord(vec3 R) { float x, y, z, texIndex; float L = sqrt(2.0) / 2.0; // create the normals to check against vec3 N1 = vec3(L, L, 0.0); vec3 N2 = vec3(-L, L, 0.0); vec3 N3 = vec3(0.0, L, L); vec3 N4 = vec3(0.0, L, -L); vec3 N5 = vec3(L, 0.0, L); vec3 N6 = vec3(-L, 0.0, L); R = normalize(R); bool check1 = bool(ceil(dot(N1, R))); bool check2 = bool(ceil(dot(N2, R))); bool check3 = bool(ceil(dot(N3, R))); bool check4 = bool(ceil(dot(N4, R))); bool check5 = bool(ceil(dot(N5, R))); bool check6 = bool(ceil(dot(N6, R))); // positive y if (check1 && check2 && check3 && check4) { // do nothing x = -1.0*R.x; y = 1.0*R.y; z = -1.0*R.z; texIndex = 0.0; } // negative y else if (!check1 && !check2 && !check3 && !check4) { // rotate around x, 180 degrees x = 1.0*R.x; y = 1.0*R.y; z = -1.0*R.z; texIndex = 1.0; } // positive z else if (check5 && check6 && check3 && !check4) { // rotate around x, 90 degrees x = -1.0*R.x; y = 1.0*R.z; z = 1.0*R.y; texIndex = 2.0; } // negative z else if (!check5 && !check6 && !check3 && check4) { // rotate around x, -90 degrees x = R.x; y = -1.0*R.z; z = R.y; texIndex = 4.0; } // negative x else if (!check5 && check6 && !check1 && check2) { // rotate around z, -90 degrees x = -1.0*R.z; y = -1.0*R.x; z = 1.0*R.y; texIndex = 3.0; } // positive x else if (check5 && !check6 && check1 && !check2) { // rotate around z, 90 degrees x = 1.0*R.z; y = R.x; z = 1.0*R.y; texIndex = 5.0; } else { // do nothing x = 0.0; y = 0.0; z = 0.0; texIndex = 6.0; } float S1 = atan((x / y),sqrt(3.4)); float T1 = atan((z / y),sqrt(3.4)); float S = 0.5 - S1; float T = 0.5 - T1; return vec3(S, T, texIndex); } vec3 cubemapSlice(vec3 vec) { float index; float dx,dy; vec2 texpos; index = floor(vec.z); dx = fract(index/3.0); dy = floor(index/3.0)/2.0; texpos.x = dx+(vec.x/3.0); texpos.y = dy+(vec.y/2.0); return vec3(texpos,0.0); } ...with 6 separate samplers void main() { vec3 vVec = normalize(view); vec3 nVec = normalize(normal); vec3 R = reflect(vVec, nVec); R = cubeCoord(R); //getting current coordinate and texture index vec3 col = vec3(0.0); if (R.z < 2.0 && R.z > 0.0) col = texture2D(rightSampler,R.xy).rgb; if (R.z < 1.0 ) col = texture2D(leftSampler,R.xy).rgb; if (R.z < 3.0 && R.z > 1.0) col = texture2D(upSampler,R.xy).rgb; if (R.z < 5.0 && R.z > 3.0) col = texture2D(downSampler,R.xy).rgb; if (R.z < 4.0 && R.z > 2.0) col = texture2D(backSampler,R.xy).rgb; if (R.z < 6.0 && R.z > 4.0) col = texture2D(frontSampler,R.xy).rgb; gl_FragColor.rgb = col; gl_FragColor.a = 1.0; } ...or one Blender style cube map void main() { vec3 vVec = normalize(view); vec3 nVec = normalize(normal); vec3 R = reflect(vVec, nVec); R = cubeCoord(R); //getting current coordinate and texture index R = cubemapSlice(R); //getting the right slice of the current cube coordinate vec3 col = vec3(0.0); col = texture2D(cubeMap,R.xy).rgb; gl_FragColor.rgb = col; gl_FragColor.a = 1.0; }