CrushedPixel

Untitled

Sep 19th, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.16 KB | None | 0 0
  1. #version 110
  2.  
  3. #define PI 3.141592653589793238462643383279
  4.  
  5. //the six input textures
  6. uniform sampler2D frontTex;
  7. uniform sampler2D backTex;
  8. uniform sampler2D leftTex;
  9. uniform sampler2D rightTex;
  10. uniform sampler2D bottomTex;
  11. uniform sampler2D topTex;
  12.  
  13. //whether or not to flip the image
  14. uniform int flip;
  15.  
  16. //the amount of yaw and pitch correction to apply (in radians)
  17. uniform float yawCorrection;
  18. uniform float pitchCorrection;
  19. uniform float rollCorrection;
  20.  
  21. //a plane in vector notation
  22. struct CubeFace {
  23.     vec3 point; //a point on the plane
  24.     vec3 norm; //the plane's normal vector
  25.     vec3 corner00; //the corner that corresponds to texture position (0|0)
  26.     vec3 corner01; //the corner that corresponds to texture position (0|1)
  27.     vec3 corner10; //the corner that corresponds to texture position (1|0)
  28. } pTop, pBottom, pLeft, pRight, pFront, pBack;
  29.  
  30. struct Line {
  31.     vec3 point; //a point on the line
  32.     vec3 dir; //the vector
  33. } lineH, lineV;
  34.  
  35. //Rotates a cartesian vector around the X-, Y- and Z-axis.
  36. void rotateVector(inout vec3 vector, float rotX, float rotY, float rotZ) {
  37.     //using a rotation matrix to calculate the new vector
  38.  
  39.     //rotate around the X-axis
  40.     vector.x = vector.x;
  41.     vector.y = vector.y * cos(rotX) - vector.z * sin(rotX);
  42.     vector.z = vector.y * sin(rotX) + vector.z * cos(rotX);
  43.  
  44.     //rotate around the Y-axis
  45.     vector.x = vector.x * cos(rotY) + vector.z * sin(rotY);
  46.     vector.y = vector.y;
  47.     vector.z = -vector.x * sin(rotY) + vector.z * cos(rotY);
  48.  
  49.     //rotate around the Z-axis
  50.     vector.x = vector.x * cos(rotZ) - vector.y * sin(rotZ);
  51.     vector.y = vector.x * sin(rotZ) + vector.y * cos(rotZ);
  52.     vector.z = vector.z;
  53. }
  54.  
  55. //calculates the four corner points of the cube face
  56. void calculateCorners(inout CubeFace plane,
  57.     CubeFace faceLeft, CubeFace faceRight,
  58.     CubeFace faceAbove, CubeFace faceBelow) {
  59.  
  60.     plane.corner00 = plane.point + faceLeft.point + faceAbove.point;
  61.     plane.corner01 = plane.point + faceLeft.point + faceBelow.point;
  62.     plane.corner10 = plane.point + faceRight.point + faceBelow.point;
  63. }
  64.  
  65. //calculate the distance from the center to the point where
  66. //the view ray crosses a plane.
  67. // d = ((p0-l0) * n) / (l * n)
  68. float distance(vec3 ray, CubeFace plane) {
  69.     //check if the plane is parallel to the ray
  70.     if(dot(ray, plane.norm) == 0.0) {
  71.         //as the plane can never cross the center
  72.         //we simply return 0.0 to mark that this plane isn't the correct one.
  73.         return 0.0;
  74.     } else {
  75.         float d = dot(plane.point, plane.norm) / dot(ray, plane.norm);
  76.         return d;
  77.     }
  78. }
  79.  
  80. //calculate the distance from a point to a line.
  81. float distance(vec3 point, Line line) {
  82.     return length((line.point - point) * line.dir) / length(line.dir);
  83. }
  84.  
  85. //calculates the uv coordinates on the cube face where the ray intersects with the plane.
  86. //values not within the range of 0, 1 are outside of the cube face.
  87. vec2 intersection(vec3 ray, CubeFace plane) {
  88.     //first, calculate the distance from the center to the intersection point
  89.     float d = distance(ray, plane);
  90.     //only intersections in the positive direction of the ray are handled
  91.     if(d > 0.0) {
  92.         //calculate the actual point in cartesian space
  93.         vec3 intersection = d * ray;
  94.  
  95.         //translate the point in cartesian space to uv coordinates on the cube face
  96.  
  97.         //calculate the x position by calculating the distance from the intersection
  98.         //to the cube faces y axis
  99.         //constructing the y axis
  100.         lineV.point = plane.corner00;
  101.         lineV.dir = plane.corner01 - plane.corner00;
  102.  
  103.         //calculating the distance from the intersection to the line
  104.         float u = distance(intersection, lineV);
  105.  
  106.         //constructing the x axis
  107.         lineH.point = plane.corner00;
  108.         lineH.dir = plane.corner10 - plane.corner00;
  109.  
  110.         float v = distance(intersection, lineH);
  111.  
  112.         return vec2(u, v);
  113.     } else {
  114.         //return invalid uv values
  115.         return vec2(-1.0, -1.0);
  116.     }
  117. }
  118.  
  119. bool isPointOnCube(vec2 uv) {
  120.     return uv.x >= 0.0 && uv.x <= 1.0 && uv.y >= 0.0 && uv.y <= 1.0;
  121. }
  122.  
  123. void main() {  
  124.     //normalized texture coordinates (from 0 to 1)
  125.     float u = gl_TexCoord[0].x;
  126.     float v = gl_TexCoord[0].y;
  127.  
  128.     //vertically flip the image if needed
  129.     if(flip == 1) {
  130.         v = 1.0 - gl_TexCoord[0].y;
  131.     }
  132.  
  133.     //convert the texture coordinates to polar coordinates
  134.     //which still resolve to points on the 2d face
  135.     float phi = u * 2.0 * PI;
  136.     float theta = v * PI;
  137.  
  138.     //calculate an unit vector in an cartesian coordinate system
  139.     //that points from the center of a hypothetical sphere to the point on the sphere
  140.     //that is mapped to the current pixel's location on the 2d face (the ray)
  141.     float x = sin(phi) * sin(theta) * -1.0;
  142.     float y = cos(theta);
  143.     float z = cos(phi) * sin(theta) * -1.0;
  144.     vec3 ray = vec3(x, y, z);
  145.  
  146.     //construct six planes representing the rotated cube with the six textures
  147.     //create six vectors pointing towards the faces of the cube
  148.     vec3 rightVec = vec3(1.0, 0.0, 0.0);
  149.     vec3 leftVec = vec3(-1.0, 0.0, 0.0);
  150.     vec3 topVec = vec3(0.0, 1.0, 0.0);
  151.     vec3 bottomVec = vec3(0.0, -1.0, 0.0);
  152.     vec3 frontVec = vec3(0.0, 0.0, 1.0);
  153.     vec3 backVec = vec3(0.0, 0.0, -1.0);
  154.  
  155.     //rotate the vectors according to pitch, yaw and roll correction
  156.     rotateVector(rightVec, -pitchCorrection, -yawCorrection, -rollCorrection);
  157.     rotateVector(leftVec, -pitchCorrection, -yawCorrection, -rollCorrection);
  158.     rotateVector(topVec, -pitchCorrection, -yawCorrection, -rollCorrection);
  159.     rotateVector(bottomVec, -pitchCorrection, -yawCorrection, -rollCorrection);
  160.     rotateVector(frontVec, -pitchCorrection, -yawCorrection, -rollCorrection);
  161.     rotateVector(backVec, -pitchCorrection, -yawCorrection, -rollCorrection);
  162.  
  163.     //as the cube is 1*1*1 large, the distance to each of the planes is 0.5
  164.     pRight.point = rightVec * 0.5;
  165.     pRight.norm = rightVec;
  166.  
  167.     pLeft.point = leftVec * 0.5;
  168.     pLeft.norm = leftVec;
  169.  
  170.     pTop.point = topVec * 0.5;
  171.     pTop.norm = topVec;
  172.  
  173.     pBottom.point = bottomVec * 0.5;
  174.     pBottom.norm = bottomVec;
  175.  
  176.     pFront.point = frontVec * 0.5;
  177.     pFront.norm = frontVec;
  178.  
  179.     pBack.point = backVec * 0.5;
  180.     pBack.norm = backVec;
  181.  
  182.     //calculate the corner points of the cube faces
  183.     calculateCorners(pRight, pFront, pBack, pTop, pBottom);
  184.     calculateCorners(pLeft, pBack, pFront, pTop, pBottom);
  185.     calculateCorners(pTop, pLeft, pRight, pFront, pBack);
  186.     calculateCorners(pBottom, pRight, pLeft, pFront, pBack);
  187.     calculateCorners(pFront, pLeft, pRight, pTop, pBottom);
  188.     calculateCorners(pBack, pRight, pLeft, pTop, pBottom);
  189.  
  190.     vec2 texPos;
  191.  
  192.     if(isPointOnCube(texPos = intersection(ray, pRight))) {
  193.         gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
  194.     } else if(isPointOnCube(texPos = intersection(ray, pLeft))) {
  195.         gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);
  196.     } else if(isPointOnCube(texPos = intersection(ray, pTop))) {
  197.         gl_FragColor = vec4(0.0, 0.0, 1.0, 1.0);
  198.     } else if(isPointOnCube(texPos = intersection(ray, pBottom))) {
  199.         gl_FragColor = vec4(1.0, 1.0, 0.0, 1.0);
  200.     } else if(isPointOnCube(texPos = intersection(ray, pFront))) {
  201.         gl_FragColor = vec4(1.0, 0.0, 1.0, 1.0);
  202.     } else if(isPointOnCube(texPos = intersection(ray, pBack))) {
  203.         gl_FragColor = vec4(0.0, 1.0, 1.0, 1.0);
  204.     }
  205.    
  206. }
Advertisement
Add Comment
Please, Sign In to add comment