Advertisement
Boltzmann

vertex shader GLSL

Aug 9th, 2012
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.50 KB | None | 0 0
  1. uniform vec3 fvLightPosition;
  2. uniform vec3 fvEyePosition;
  3.  
  4. uniform float amplitude;
  5. uniform float frequency;
  6. uniform float cubify;
  7. uniform vec3 planetPos;
  8. uniform vec4 planetRot;
  9. uniform float planetRad;
  10. uniform int LOD;
  11. uniform vec3 translation;
  12.  
  13. //varying vec2 Texcoord;
  14. varying vec3 ViewDirection;
  15. varying vec3 LightDirection;
  16. varying vec3 Normal;
  17.  
  18. // TODO: change this to texture
  19. int p[512] = int[512](151,160,137,91,90,15,
  20. 131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23,
  21. 190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33,
  22. 88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166,
  23. 77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244,
  24. 102,143,54, 65,25,63,161, 1,216,80,73,209,76,132,187,208, 89,18,169,200,196,
  25. 135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226,250,124,123,
  26. 5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42,
  27. 223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167, 43,172,9,
  28. 129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,97,228,
  29. 251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107,
  30. 49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254,
  31. 138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180,
  32. 151,160,137,91,90,15,
  33. 131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23,
  34. 190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33,
  35. 88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166,
  36. 77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244,
  37. 102,143,54, 65,25,63,161, 1,216,80,73,209,76,132,187,208, 89,18,169,200,196,
  38. 135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226,250,124,123,
  39. 5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42,
  40. 223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167, 43,172,9,
  41. 129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,97,228,
  42. 251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107,
  43. 49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254,
  44. 138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180
  45. );
  46.  
  47. float fade(float t)
  48. {
  49. return t*t*t*(t*(t*6-15)+10);
  50. }
  51.  
  52. float lerp(float t, float a, float b)
  53. {
  54. return a + t * (b - a);
  55. }
  56.  
  57. float grad(int hash, float x, float y, float z)
  58. {
  59. int h = hash & 15;
  60. float u = h<8 ? x : y;
  61. float v = h<4 ? y : h==12||h==14 ? x : z;
  62. return ((h&1) == 0 ? u : -u) + ((h&2) == 0 ? v : -v);
  63. }
  64.  
  65. float noise(float x, float y, float z)
  66. {
  67. int mx = int(x) & 255;
  68. int my = int(y) & 255;
  69. int mz = int(z) & 255;
  70.  
  71. x -= int(x);
  72. y -= int(y);
  73. z -= int(z);
  74.  
  75. if(x<0) { x+=1; mx = (mx+255) & 255; }
  76. if(y<0) { y+=1; my = (my+255) & 255; }
  77. if(z<0) { z+=1; mz = (mz+255) & 255; }
  78.  
  79. float u = fade(x);
  80. float v = fade(y);
  81. float w = fade(z);
  82.  
  83. int A = p[mx ]+my;
  84. int AA = p[A ]+mz;
  85. int AB = p[A +1]+mz;
  86. int B = p[mx+1]+my;
  87. int BA = p[B ]+mz;
  88. int BB = p[B +1]+mz;
  89.  
  90. return lerp(w, lerp(v, lerp(u,grad(p[AA ],x ,y ,z ),
  91. grad(p[BA ],x-1,y ,z )),
  92. lerp(u,grad(p[AB ],x ,y-1,z ),
  93. grad(p[BB ],x-1,y-1,z ))),
  94. lerp(v, lerp(u,grad(p[AA+1],x ,y ,z-1),
  95. grad(p[BA+1],x-1,y ,z-1)),
  96. lerp(u,grad(p[AB+1],x ,y-1,z-1),
  97. grad(p[BB+1],x-1,y-1,z-1))));
  98. }
  99.  
  100. void main( void )
  101. {
  102. vec3 tmpPos = gl_Vertex.xyz * pow(0.5,LOD) + translation;
  103.  
  104. float xx = tmpPos.x * tmpPos.x;
  105. float yy = tmpPos.y * tmpPos.y;
  106. float zz = tmpPos.z * tmpPos.z;
  107.  
  108. // calculate sphere vertices
  109. float x = tmpPos.x * sqrt(1.0 - (yy/2.0) - (zz/2.0) + (yy*zz/3.0));
  110. float y = tmpPos.y * sqrt(1.0 - (xx/2.0) - (zz/2.0) + (xx*zz/3.0));
  111. float z = tmpPos.z * sqrt(1.0 - (yy/2.0) - (xx/2.0) + (yy*xx/3.0));
  112.  
  113. // apply sphere mapping quantity
  114. x = tmpPos.x*cubify + x*(1.0-cubify);
  115. y = tmpPos.y*cubify + y*(1.0-cubify);
  116. z = tmpPos.z*cubify + z*(1.0-cubify);
  117.  
  118. // apply sphere mapping and fix normals
  119. vec4 newPos = vec4(x,y,z,gl_Vertex.w);
  120. vec3 newNormal = normalize(vec3(x,y,z));
  121.  
  122. // calculate longitude and latitude
  123. //float lon = atan(newPos.z/newPos.x);
  124. float lon = atan(x,z);
  125. float lat = asin(y/planetRad);
  126.  
  127. if(abs(y) > planetRad-0.000001)
  128. lon = 0.0;
  129.  
  130. // calculate increment value
  131. float inc = radians(90.0/(16.0*pow(2,LOD)));
  132. float newLon = lon+inc;
  133. float newLat = lat-inc;
  134. if(lat > radians(87.0))
  135. newLat = lat+inc;
  136.  
  137. // calculate neighbor points
  138. float ny = sin(newLat)*planetRad;
  139. float L = cos(newLat)*planetRad;
  140. float nx = sin(lon)*L;
  141. float nz = cos(lon)*L;
  142. vec3 posPlusLat = vec3(nx,ny,nz);
  143.  
  144. if(abs(y) > planetRad-0.000001)
  145. {
  146. newLon = radians(90.0);
  147. }
  148. else
  149. {
  150. ny = sin(lat)*planetRad;
  151. L = cos(lat)*planetRad;
  152. }
  153. nx = sin(newLon)*L;
  154. nz = cos(newLon)*L;
  155.  
  156. vec3 posPlusLong = vec3(nx,ny,nz);
  157.  
  158.  
  159.  
  160. // apply height displacement
  161. newPos.xyz = newPos.xyz + newNormal * amplitude * noise(newPos.x*frequency,newPos.y*frequency,newPos.z*frequency);
  162.  
  163. // displace neighbors and calculate normal
  164. posPlusLat = posPlusLat + normalize(posPlusLat) * amplitude * noise(posPlusLat.x*frequency,posPlusLat.y*frequency,posPlusLat.z*frequency);
  165. posPlusLong = posPlusLong + normalize(posPlusLong) * amplitude * noise(posPlusLong.x*frequency,posPlusLong.y*frequency,posPlusLong.z*frequency);
  166. vec3 bumpNormal = cross(normalize(posPlusLat-newPos.xyz),normalize(posPlusLong-newPos.xyz));
  167. bumpNormal = normalize(bumpNormal);
  168. if(dot(bumpNormal,newNormal) < 0)
  169. bumpNormal = -bumpNormal;
  170.  
  171. gl_Position = gl_ModelViewProjectionMatrix * newPos;
  172. //Texcoord = gl_MultiTexCoord0.xy;
  173.  
  174.  
  175.  
  176. vec4 fvObjectPosition = gl_ModelViewMatrix * newPos;
  177.  
  178. // TODO: change position and rotation
  179.  
  180. ViewDirection = fvEyePosition - fvObjectPosition.xyz;
  181. LightDirection = (gl_ModelViewMatrix * vec4(fvLightPosition - fvObjectPosition.xyz,0.0)).xyz;
  182. //Normal = gl_NormalMatrix * (gl_Normal*cubify + (1.0-cubify)*bumpNormal);
  183. Normal = gl_NormalMatrix * bumpNormal;
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement