Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #version 110
- #define PI 3.141592653589793238462643383279
- #define IMAGE_FRONT 0
- #define IMAGE_BACK 1
- #define IMAGE_LEFT 2
- #define IMAGE_RIGHT 3
- #define IMAGE_BOTTOM 4
- #define IMAGE_TOP 5
- //the six input textures
- uniform sampler2D frontTex;
- uniform sampler2D backTex;
- uniform sampler2D leftTex;
- uniform sampler2D rightTex;
- uniform sampler2D bottomTex;
- uniform sampler2D topTex;
- //whether or not to flip the image
- uniform int flip;
- //the amount of yaw and pitch correction to apply (in radians)
- uniform float yawCorrection;
- uniform float pitchCorrection;
- /**
- * Rotates a cartesian vector around the X and Y axes.
- * @param rotX the amount to rotate around the X axis (resulting in pitch change)
- * @param rotY the amount to rotate around the Y axis (resulting in yaw change)
- */
- vec3 rotateVector(vec3 vector, float rotX, float rotY) {
- //using a rotation matrix to calculate the new vector
- //rotate around the X-axis
- vector.x = vector.x;
- vector.y = vector.y * cos(rotX) - vector.z * sin(rotX);
- vector.z = vector.y * sin(rotX) + vector.z * cos(rotX);
- //rotate around the Y-axis
- vector.x = vector.x * cos(rotY) + vector.z * sin(rotY);
- vector.y = vector.y;
- vector.z = -vector.x * sin(rotY) + vector.z * cos(rotY);
- return vector;
- }
- void main() {
- //normalized texture coordinates (from 0 to 1)
- float u = gl_TexCoord[0].x;
- float v = gl_TexCoord[0].y;
- //vertically flip the image if needed
- if(flip == 1) {
- v = 1.0 - gl_TexCoord[0].y;
- }
- //convert the texture coordinates to polar coordinates
- //which still resolve to points on the 2d face
- float phi = u * 2.0 * PI;
- float theta = v * PI;
- //calculate an unit vector in an cartesian coordinate system
- //that points from the center of a hypothetical sphere to the point on the sphere
- //that is mapped to the current pixel's location on the 2d face.
- float x = sin(phi) * sin(theta) * -1.0;
- float y = cos(theta);
- float z = cos(phi) * sin(theta) * -1.0;
- //rotate the vector to respect pitch and yaw correction
- vec3 rotated = rotateVector(vec3(x, y, z), -pitchCorrection, -yawCorrection);
- x = rotated.x;
- y = rotated.y;
- z = rotated.z;
- //divide the vector directions by the longest direction
- //to get the general direction of the vector,
- //thus finding out which of the cube's faces should be mapped to the point on the sphere.
- float a = max(abs(x), max(abs(y), abs(z)));
- float xa = x/a;
- float ya = y/a;
- float za = z/a;
- //normalized pixel position on target face
- float texX, texY;
- //determine target face
- //int target;
- if(xa == 1.0) {
- //target = IMAGE_RIGHT;
- texX = abs(((za + 1.0) / 2.0) - 1.0);
- texY = abs(((ya + 1.0) / 2.0));
- gl_FragColor = texture2D(rightTex, vec2(texX, texY));
- gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
- } else if(xa == -1.0) {
- //target = IMAGE_LEFT;
- texX = abs(((za + 1.0) / 2.0));
- texY = abs(((ya + 1.0) / 2.0));
- gl_FragColor = texture2D(leftTex, vec2(texX, texY));
- gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);
- } else if(ya == 1.0) {
- //target = IMAGE_TOP;
- texX = abs(((xa + 1.0) / 2.0));
- texY = abs(((za + 1.0) / 2.0) - 1.0);
- gl_FragColor = texture2D(topTex, vec2(texX, texY));
- gl_FragColor = vec4(0.0, 0.0, 1.0, 1.0);
- } else if(ya == -1.0) {
- //target == IMAGE_BOTTOM;
- texX = abs(((xa + 1.0) / 2.0));
- texY = abs(((za + 1.0) / 2.0));
- gl_FragColor = texture2D(bottomTex, vec2(texX, texY));
- gl_FragColor = vec4(1.0, 1.0, 0.0, 1.0);
- } else if(za == 1.0) {
- //target = IMAGE_FRONT;
- texX = abs(((xa + 1.0) / 2.0));
- texY = abs(((ya + 1.0) / 2.0));
- gl_FragColor = texture2D(frontTex, vec2(texX, texY));
- gl_FragColor = vec4(1.0, 0.0, 1.0, 1.0);
- } else if(za == -1.0) {
- //target = IMAGE_BACK;
- texX = abs(((xa + 1.0) / 2.0) - 1.0);
- texY = abs(((ya + 1.0) / 2.0));
- gl_FragColor = texture2D(backTex, vec2(texX, texY));
- gl_FragColor = vec4(0.0, 1.0, 1.0, 1.0);
- } else {
- gl_FragColor = vec4(xa, ya, za, 1.0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment