Collection of DPF-based plugins for packaging
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

235 lines
6.6KB

  1. /*
  2. * DISTRHO glBars Plugin based on XMMS/XBMC "GL Bars"
  3. * Copyright (C) 1998-2000 Peter Alm, Mikael Alm, Olle Hallnas, Thomas Nilsson and 4Front Technologies
  4. * Copyright (C) 2000 Christian Zander <phoenix@minion.de>
  5. * Copyright (C) 2015 Nedko Arnaudov
  6. * Copyright (C) 2016 Filipe Coelho <falktx@falktx.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Lesser General Public License for more details.
  16. *
  17. * For a full copy of the license see the LICENSE file.
  18. */
  19. #ifndef GLBARS_STATE_HPP_INCLUDED
  20. #define GLBARS_STATE_HPP_INCLUDED
  21. #include "Base.hpp"
  22. static inline
  23. void draw_rectangle(GLfloat x1, GLfloat y1, GLfloat z1, GLfloat x2, GLfloat y2, GLfloat z2)
  24. {
  25. if (y1 == y2)
  26. {
  27. glVertex3f(x1, y1, z1);
  28. glVertex3f(x2, y1, z1);
  29. glVertex3f(x2, y2, z2);
  30. glVertex3f(x2, y2, z2);
  31. glVertex3f(x1, y2, z2);
  32. glVertex3f(x1, y1, z1);
  33. }
  34. else
  35. {
  36. glVertex3f(x1, y1, z1);
  37. glVertex3f(x2, y1, z2);
  38. glVertex3f(x2, y2, z2);
  39. glVertex3f(x2, y2, z2);
  40. glVertex3f(x1, y2, z1);
  41. glVertex3f(x1, y1, z1);
  42. }
  43. }
  44. static inline
  45. void draw_bar(GLenum mode, GLfloat x_offset, GLfloat z_offset, GLfloat height, GLfloat red, GLfloat green, GLfloat blue)
  46. {
  47. const GLfloat width = 0.1;
  48. if (mode == GL_POINT)
  49. glColor3f(0.2, 1.0, 0.2);
  50. if (mode != GL_POINT)
  51. {
  52. glColor3f(red,green,blue);
  53. draw_rectangle(x_offset, height, z_offset, x_offset + width, height, z_offset + 0.1);
  54. }
  55. draw_rectangle(x_offset, 0, z_offset, x_offset + width, 0, z_offset + 0.1);
  56. if (mode != GL_POINT)
  57. {
  58. glColor3f(0.5 * red, 0.5 * green, 0.5 * blue);
  59. draw_rectangle(x_offset, 0.0, z_offset + 0.1, x_offset + width, height, z_offset + 0.1);
  60. }
  61. draw_rectangle(x_offset, 0.0, z_offset, x_offset + width, height, z_offset );
  62. if (mode != GL_POINT)
  63. {
  64. glColor3f(0.25 * red, 0.25 * green, 0.25 * blue);
  65. draw_rectangle(x_offset, 0.0, z_offset , x_offset, height, z_offset + 0.1);
  66. }
  67. draw_rectangle(x_offset + width, 0.0, z_offset , x_offset + width, height, z_offset + 0.1);
  68. }
  69. struct glBarsState {
  70. GLenum g_mode;
  71. GLfloat x_angle, x_speed;
  72. GLfloat y_angle, y_speed;
  73. GLfloat z_angle, z_speed;
  74. GLfloat heights[16][16], cHeights[16][16], scale;
  75. GLfloat hSpeed;
  76. glBarsState()
  77. {
  78. g_mode = GL_FILL;
  79. x_angle = 20.0;
  80. x_speed = 0.0;
  81. y_angle = 15.0; // was 45
  82. y_speed = 0.5;
  83. z_angle = 0.0;
  84. z_speed = 0.0;
  85. // Set "Bar Height"
  86. scale = 1.f / log(256.f); // "Default" / standard
  87. //scale = 2.f / log(256.f); // "Big"
  88. //scale = 3.f / log(256.f); // "Very Big" / real big
  89. //scale = 0.33f / log(256.f); // unused
  90. //scale = 0.5f / log(256.f); // "Small"
  91. // Set "Speed"
  92. //hSpeed = 0.025f; // "Slow"
  93. hSpeed = 0.0125f; // "Default"
  94. //hSpeed = 0.1f; // "Fast"
  95. //hSpeed = 0.2f; // "Very Fast"
  96. //hSpeed = 0.05f; // "Very Slow"
  97. for (int x = 0; x < 16; x++)
  98. {
  99. for (int y = 0; y < 16; y++)
  100. cHeights[y][x] = heights[y][x] = 0;
  101. }
  102. }
  103. void drawBars()
  104. {
  105. GLfloat x_offset, z_offset, r_base, b_base;
  106. glClear(GL_DEPTH_BUFFER_BIT);
  107. glPushMatrix();
  108. glTranslatef(0.0,-0.5,-5.0);
  109. glRotatef(x_angle,1.0,0.0,0.0);
  110. glRotatef(y_angle,0.0,1.0,0.0);
  111. glRotatef(z_angle,0.0,0.0,1.0);
  112. glPolygonMode(GL_FRONT_AND_BACK, g_mode);
  113. glBegin(GL_TRIANGLES);
  114. for (int y = 0; y < 16; y++)
  115. {
  116. z_offset = -1.6 + ((15 - y) * 0.2);
  117. b_base = y * (1.0 / 15);
  118. r_base = 1.0 - b_base;
  119. for (int x = 0; x < 16; x++)
  120. {
  121. x_offset = -1.6 + ((float)x * 0.2);
  122. if (::fabs(cHeights[y][x]-heights[y][x])>hSpeed)
  123. {
  124. if (cHeights[y][x]<heights[y][x])
  125. cHeights[y][x] += hSpeed;
  126. else
  127. cHeights[y][x] -= hSpeed;
  128. }
  129. draw_bar(g_mode, x_offset, z_offset,
  130. cHeights[y][x], r_base - (float(x) * (r_base / 15.0)),
  131. (float)x * (1.0 / 15), b_base /*, 16*y+x*/);
  132. }
  133. }
  134. glEnd();
  135. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  136. glPopMatrix();
  137. }
  138. void Render()
  139. {
  140. glDisable(GL_BLEND);
  141. glMatrixMode(GL_PROJECTION);
  142. glPushMatrix();
  143. glLoadIdentity();
  144. glFrustum(-1, 1, -1, 1, 1.5, 10);
  145. glMatrixMode(GL_MODELVIEW);
  146. glPushMatrix();
  147. glLoadIdentity();
  148. glEnable(GL_DEPTH_TEST);
  149. glDepthFunc(GL_LESS);
  150. glPolygonMode(GL_FRONT, GL_FILL);
  151. //glPolygonMode(GL_BACK, GL_FILL);
  152. x_angle += x_speed;
  153. if (x_angle >= 360.0)
  154. x_angle -= 360.0;
  155. y_angle += y_speed;
  156. if (y_angle >= 360.0)
  157. y_angle -= 360.0;
  158. z_angle += z_speed;
  159. if (z_angle >= 360.0)
  160. z_angle -= 360.0;
  161. drawBars();
  162. glPopMatrix();
  163. glMatrixMode(GL_PROJECTION);
  164. glPopMatrix();
  165. glDisable(GL_DEPTH_TEST);
  166. glEnable(GL_BLEND);
  167. }
  168. void AudioData(const float* pAudioData, int iAudioDataLength)
  169. {
  170. const int xscale[] = {0, 1, 2, 3, 5, 7, 10, 14, 20, 28, 40, 54, 74, 101, 137, 187, 255};
  171. GLfloat val;
  172. for (int y = 15; y > 0; y--)
  173. {
  174. for (int i = 0; i < 16; i++)
  175. heights[y][i] = heights[y - 1][i];
  176. }
  177. for (int i = 0; i < 16; i++)
  178. {
  179. int y = 0;
  180. for (int c = xscale[i]; c < xscale[i + 1]; c++)
  181. {
  182. if (c<iAudioDataLength)
  183. {
  184. if ((int)(pAudioData[c] * (INT16_MAX)) > y)
  185. y = (int)(pAudioData[c] * (INT16_MAX));
  186. }
  187. else
  188. {
  189. continue;
  190. }
  191. }
  192. y >>= 7;
  193. if (y > 0)
  194. val = (logf(y) * scale);
  195. else
  196. val = 0;
  197. heights[0][i] = val;
  198. }
  199. }
  200. };
  201. #endif // GLBARS_STATE_HPP_INCLUDED