honeycombGeometryShader.glsl 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /*
  3. * This file is part of the LibreOffice project.
  4. *
  5. * This Source Code Form is subject to the terms of the Mozilla Public
  6. * License, v. 2.0. If a copy of the MPL was not distributed with this
  7. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  8. */
  9. #version 150
  10. layout(triangles) in;
  11. layout(triangle_strip, max_vertices=27) out;
  12. in mat4 projectionMatrix[];
  13. in mat4 modelViewMatrix[];
  14. in mat4 shadowMatrix[];
  15. uniform float hexagonSize;
  16. uniform sampler2D permTexture;
  17. out vec2 texturePosition;
  18. out float fuzz;
  19. out vec2 v_center;
  20. out vec3 normal;
  21. out vec4 shadowCoordinate;
  22. const float expandFactor = 0.0318;
  23. float snoise(vec2 p)
  24. {
  25. return texture(permTexture, p).r;
  26. }
  27. mat4 identityMatrix(void)
  28. {
  29. return mat4(1.0, 0.0, 0.0, 0.0,
  30. 0.0, 1.0, 0.0, 0.0,
  31. 0.0, 0.0, 1.0, 0.0,
  32. 0.0, 0.0, 0.0, 1.0);
  33. }
  34. mat4 scaleMatrix(vec3 axis)
  35. {
  36. mat4 matrix = identityMatrix();
  37. matrix[0][0] = axis.x;
  38. matrix[1][1] = axis.y;
  39. matrix[2][2] = axis.z;
  40. return matrix;
  41. }
  42. mat4 translationMatrix(vec3 axis)
  43. {
  44. mat4 matrix = identityMatrix();
  45. matrix[3] = vec4(axis, 1.0);
  46. return matrix;
  47. }
  48. void emitHexagonVertex(vec3 center, vec2 translation)
  49. {
  50. vec4 pos = vec4(center + hexagonSize * expandFactor * vec3(translation, 0.0), 1.0);
  51. gl_Position = projectionMatrix[0] * modelViewMatrix[0] * pos;
  52. shadowCoordinate = translationMatrix(vec3(0.5, 0.5, 0.5)) * scaleMatrix(vec3(0.5, 0.5, 0.5)) * shadowMatrix[0] * modelViewMatrix[0] * pos;
  53. texturePosition = vec2((pos.x + 1), (1 - pos.y)) / 2;
  54. EmitVertex();
  55. }
  56. void main()
  57. {
  58. const vec2 translateVectors[6] = vec2[](
  59. vec2(-3, -2),
  60. vec2(0, -4),
  61. vec2(3, -2),
  62. vec2(3, 2),
  63. vec2(0, 4),
  64. vec2(-3, 2)
  65. );
  66. vec3 center = gl_in[0].gl_Position.xyz;
  67. v_center = (1 + center.xy) / 2;
  68. fuzz = snoise(center.xy);
  69. // Draw “walls” to the hexagons.
  70. if (hexagonSize < 1.0) {
  71. vec3 rearCenter = vec3(center.xy, -0.3);
  72. normal = vec3(0.0, 0.0, 0.3);
  73. emitHexagonVertex(center, translateVectors[5]);
  74. emitHexagonVertex(rearCenter, translateVectors[5]);
  75. for (int i = 0; i < 6; ++i) {
  76. emitHexagonVertex(center, translateVectors[i]);
  77. emitHexagonVertex(rearCenter, translateVectors[i]);
  78. }
  79. EndPrimitive();
  80. }
  81. // Draw the main hexagon part.
  82. normal = vec3(0.0, 0.0, 1.0);
  83. emitHexagonVertex(center, translateVectors[5]);
  84. for (int i = 0; i < 6; ++i) {
  85. emitHexagonVertex(center, translateVectors[i]);
  86. emitHexagonVertex(center, vec2(0.0, 0.0));
  87. }
  88. EndPrimitive();
  89. }
  90. /* vim:set shiftwidth=4 softtabstop=4 expandtab: */