CrushedPixel

Untitled

Sep 19th, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.13 KB | None | 0 0
  1. #version 110
  2.  
  3. #define PI 3.141592653589793238462643383279
  4.  
  5. #define IMAGE_FRONT 0
  6. #define IMAGE_BACK 1
  7. #define IMAGE_LEFT 2
  8. #define IMAGE_RIGHT 3
  9. #define IMAGE_BOTTOM 4
  10. #define IMAGE_TOP 5
  11.  
  12. //the six input textures
  13. uniform sampler2D frontTex;
  14. uniform sampler2D backTex;
  15. uniform sampler2D leftTex;
  16. uniform sampler2D rightTex;
  17. uniform sampler2D bottomTex;
  18. uniform sampler2D topTex;
  19.  
  20. //whether or not to flip the image
  21. uniform int flip;
  22.  
  23. //the amount of yaw and pitch correction to apply (in radians)
  24. uniform float yawCorrection;
  25. uniform float pitchCorrection;
  26.  
  27. /**
  28.  * Rotates a cartesian vector around the X and Y axes.
  29.  * @param rotX the amount to rotate around the X axis (resulting in pitch change)
  30.  * @param rotY the amount to rotate around the Y axis (resulting in yaw change)
  31.  */
  32. vec3 rotateVector(vec3 vector, float rotX, float rotY) {
  33.     //using a rotation matrix to calculate the new vector
  34.  
  35.     //rotate around the X-axis
  36.     vector.x = vector.x;
  37.     vector.y = vector.y * cos(rotX) - vector.z * sin(rotX);
  38.     vector.z = vector.y * sin(rotX) + vector.z * cos(rotX);
  39.  
  40.     //rotate around the Y-axis
  41.     vector.x = vector.x * cos(rotY) + vector.z * sin(rotY);
  42.     vector.y = vector.y;
  43.     vector.z = -vector.x * sin(rotY) + vector.z * cos(rotY);
  44.  
  45.     return vector;
  46. }
  47.  
  48. void main() {  
  49.     //normalized texture coordinates (from 0 to 1)
  50.     float u = gl_TexCoord[0].x;
  51.     float v = gl_TexCoord[0].y;
  52.  
  53.     //vertically flip the image if needed
  54.     if(flip == 1) {
  55.         v = 1.0 - gl_TexCoord[0].y;
  56.     }
  57.  
  58.     //convert the texture coordinates to polar coordinates
  59.     //which still resolve to points on the 2d face
  60.     float phi = u * 2.0 * PI;
  61.     float theta = v * PI;
  62.  
  63.     //calculate an unit vector in an cartesian coordinate system
  64.     //that points from the center of a hypothetical sphere to the point on the sphere
  65.     //that is mapped to the current pixel's location on the 2d face.
  66.     float x = sin(phi) * sin(theta) * -1.0;
  67.     float y = cos(theta);
  68.     float z = cos(phi) * sin(theta) * -1.0;
  69.  
  70.     //rotate the vector to respect pitch and yaw correction
  71.     vec3 rotated = rotateVector(vec3(x, y, z), -pitchCorrection, -yawCorrection);
  72.     x = rotated.x;
  73.     y = rotated.y;
  74.     z = rotated.z;
  75.  
  76.     //divide the vector directions by the longest direction
  77.     //to get the general direction of the vector,
  78.     //thus finding out which of the cube's faces should be mapped to the point on the sphere.
  79.     float a = max(abs(x), max(abs(y), abs(z)));
  80.  
  81.     float xa = x/a;
  82.     float ya = y/a;
  83.     float za = z/a;
  84.  
  85.     //normalized pixel position on target face
  86.     float texX, texY;
  87.  
  88.     //determine target face
  89.     //int target;
  90.     if(xa == 1.0) {
  91.         //target = IMAGE_RIGHT;
  92.         texX = abs(((za + 1.0) / 2.0) - 1.0);
  93.         texY = abs(((ya + 1.0) / 2.0));
  94.         gl_FragColor = texture2D(rightTex, vec2(texX, texY));
  95.         gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
  96.     } else if(xa == -1.0) {
  97.         //target = IMAGE_LEFT;
  98.         texX = abs(((za + 1.0) / 2.0));
  99.         texY = abs(((ya + 1.0) / 2.0));
  100.         gl_FragColor = texture2D(leftTex, vec2(texX, texY));
  101.         gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);
  102.     } else if(ya == 1.0) {
  103.         //target = IMAGE_TOP;
  104.         texX = abs(((xa + 1.0) / 2.0));
  105.         texY = abs(((za + 1.0) / 2.0) - 1.0);
  106.         gl_FragColor = texture2D(topTex, vec2(texX, texY));
  107.         gl_FragColor = vec4(0.0, 0.0, 1.0, 1.0);
  108.     } else if(ya == -1.0) {
  109.         //target == IMAGE_BOTTOM;
  110.         texX = abs(((xa + 1.0) / 2.0));
  111.         texY = abs(((za + 1.0) / 2.0));
  112.         gl_FragColor = texture2D(bottomTex, vec2(texX, texY));
  113.         gl_FragColor = vec4(1.0, 1.0, 0.0, 1.0);
  114.     } else if(za == 1.0) {
  115.         //target = IMAGE_FRONT;
  116.         texX = abs(((xa + 1.0) / 2.0));
  117.         texY = abs(((ya + 1.0) / 2.0));
  118.         gl_FragColor = texture2D(frontTex, vec2(texX, texY));
  119.         gl_FragColor = vec4(1.0, 0.0, 1.0, 1.0);
  120.     } else if(za == -1.0) {
  121.         //target = IMAGE_BACK;
  122.         texX = abs(((xa + 1.0) / 2.0) - 1.0);
  123.         texY = abs(((ya + 1.0) / 2.0));
  124.         gl_FragColor = texture2D(backTex, vec2(texX, texY));
  125.         gl_FragColor = vec4(0.0, 1.0, 1.0, 1.0);
  126.     } else {
  127.         gl_FragColor = vec4(xa, ya, za, 1.0);
  128.     }
  129.    
  130. }
Advertisement
Add Comment
Please, Sign In to add comment