The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

250 lines
8.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. #include "../JuceDemoHeader.h"
  20. #if JUCE_OPENGL
  21. //==============================================================================
  22. class OpenGL2DShaderDemo : public Component,
  23. private CodeDocument::Listener,
  24. private ComboBox::Listener,
  25. private Timer
  26. {
  27. public:
  28. OpenGL2DShaderDemo()
  29. : fragmentEditorComp (fragmentDocument, nullptr)
  30. {
  31. setOpaque (true);
  32. if (MainAppWindow* mw = MainAppWindow::getMainAppWindow())
  33. mw->setOpenGLRenderingEngine();
  34. addAndMakeVisible (statusLabel);
  35. statusLabel.setJustificationType (Justification::topLeft);
  36. statusLabel.setFont (Font (14.0f));
  37. Array<ShaderPreset> presets (getPresets());
  38. StringArray presetNames;
  39. for (int i = 0; i < presets.size(); ++i)
  40. presetBox.addItem (presets[i].name, i + 1);
  41. addAndMakeVisible (presetLabel);
  42. presetLabel.setText ("Shader Preset:", dontSendNotification);
  43. presetLabel.attachToComponent (&presetBox, true);
  44. addAndMakeVisible (presetBox);
  45. presetBox.addListener (this);
  46. fragmentEditorComp.setOpaque (false);
  47. fragmentDocument.addListener (this);
  48. addAndMakeVisible (fragmentEditorComp);
  49. presetBox.setSelectedItemIndex (0);
  50. }
  51. ~OpenGL2DShaderDemo()
  52. {
  53. shader = nullptr;
  54. }
  55. void paint (Graphics& g) override
  56. {
  57. g.fillCheckerBoard (getLocalBounds(), 48, 48, Colours::lightgrey, Colours::white);
  58. if (shader == nullptr || shader->getFragmentShaderCode() != fragmentCode)
  59. {
  60. shader = nullptr;
  61. if (fragmentCode.isNotEmpty())
  62. {
  63. shader = new OpenGLGraphicsContextCustomShader (fragmentCode);
  64. Result result (shader->checkCompilation (g.getInternalContext()));
  65. if (result.failed())
  66. {
  67. statusLabel.setText (result.getErrorMessage(), dontSendNotification);
  68. shader = nullptr;
  69. }
  70. }
  71. }
  72. if (shader != nullptr)
  73. {
  74. statusLabel.setText (String(), dontSendNotification);
  75. shader->fillRect (g.getInternalContext(), getLocalBounds());
  76. }
  77. }
  78. void resized() override
  79. {
  80. Rectangle<int> area (getLocalBounds().reduced (4));
  81. statusLabel.setBounds (area.removeFromTop (75));
  82. area.removeFromTop (area.getHeight() / 2);
  83. Rectangle<int> presets (area.removeFromTop (25));
  84. presets.removeFromLeft (100);
  85. presetBox.setBounds (presets.removeFromLeft (150));
  86. area.removeFromTop (4);
  87. fragmentEditorComp.setBounds (area);
  88. }
  89. void selectPreset (int preset)
  90. {
  91. fragmentDocument.replaceAllContent (getPresets()[preset].fragmentShader);
  92. startTimer (1);
  93. }
  94. ScopedPointer<OpenGLGraphicsContextCustomShader> shader;
  95. Label statusLabel, presetLabel;
  96. ComboBox presetBox;
  97. CodeDocument fragmentDocument;
  98. CodeEditorComponent fragmentEditorComp;
  99. String fragmentCode;
  100. private:
  101. enum { shaderLinkDelay = 500 };
  102. void codeDocumentTextInserted (const String& /*newText*/, int /*insertIndex*/) override
  103. {
  104. startTimer (shaderLinkDelay);
  105. }
  106. void codeDocumentTextDeleted (int /*startIndex*/, int /*endIndex*/) override
  107. {
  108. startTimer (shaderLinkDelay);
  109. }
  110. void timerCallback() override
  111. {
  112. stopTimer();
  113. fragmentCode = fragmentDocument.getAllContent();
  114. repaint();
  115. }
  116. void comboBoxChanged (ComboBox*) override
  117. {
  118. selectPreset (presetBox.getSelectedItemIndex());
  119. }
  120. struct ShaderPreset
  121. {
  122. const char* name;
  123. const char* fragmentShader;
  124. };
  125. static Array<ShaderPreset> getPresets()
  126. {
  127. #define SHADER_DEMO_HEADER \
  128. "/* This demo shows the use of the OpenGLGraphicsContextCustomShader,\n" \
  129. " which allows a 2D area to be filled using a GL shader program.\n" \
  130. "\n" \
  131. " Edit the shader program below and it will be \n" \
  132. " recompiled in real-time!\n" \
  133. "*/\n\n"
  134. ShaderPreset presets[] =
  135. {
  136. {
  137. "Simple Gradient",
  138. SHADER_DEMO_HEADER
  139. "void main()\n"
  140. "{\n"
  141. " " JUCE_MEDIUMP " vec4 colour1 = vec4 (1.0, 0.4, 0.6, 1.0);\n"
  142. " " JUCE_MEDIUMP " vec4 colour2 = vec4 (0.0, 0.8, 0.6, 1.0);\n"
  143. " " JUCE_MEDIUMP " float alpha = pixelPos.x / 1000.0;\n"
  144. " gl_FragColor = pixelAlpha * mix (colour1, colour2, alpha);\n"
  145. "}\n"
  146. },
  147. {
  148. "Circular Gradient",
  149. SHADER_DEMO_HEADER
  150. "void main()\n"
  151. "{\n"
  152. " " JUCE_MEDIUMP " vec4 colour1 = vec4 (1.0, 0.4, 0.6, 1.0);\n"
  153. " " JUCE_MEDIUMP " vec4 colour2 = vec4 (0.3, 0.4, 0.4, 1.0);\n"
  154. " " JUCE_MEDIUMP " float alpha = distance (pixelPos, vec2 (600.0, 500.0)) / 400.0;\n"
  155. " gl_FragColor = pixelAlpha * mix (colour1, colour2, alpha);\n"
  156. "}\n"
  157. },
  158. {
  159. "Circle",
  160. SHADER_DEMO_HEADER
  161. "void main()\n"
  162. "{\n"
  163. " " JUCE_MEDIUMP " vec4 colour1 = vec4 (0.1, 0.1, 0.9, 1.0);\n"
  164. " " JUCE_MEDIUMP " vec4 colour2 = vec4 (0.0, 0.8, 0.6, 1.0);\n"
  165. " " JUCE_MEDIUMP " float distance = distance (pixelPos, vec2 (600.0, 500.0));\n"
  166. "\n"
  167. " " JUCE_MEDIUMP " float innerRadius = 200.0;\n"
  168. " " JUCE_MEDIUMP " float outerRadius = 210.0;\n"
  169. "\n"
  170. " if (distance < innerRadius)\n"
  171. " gl_FragColor = colour1;\n"
  172. " else if (distance > outerRadius)\n"
  173. " gl_FragColor = colour2;\n"
  174. " else\n"
  175. " gl_FragColor = mix (colour1, colour2, (distance - innerRadius) / (outerRadius - innerRadius));\n"
  176. "\n"
  177. " gl_FragColor *= pixelAlpha;\n"
  178. "}\n"
  179. },
  180. {
  181. "Solid Colour",
  182. SHADER_DEMO_HEADER
  183. "void main()\n"
  184. "{\n"
  185. " gl_FragColor = vec4 (1.0, 0.6, 0.1, pixelAlpha);\n"
  186. "}\n"
  187. }
  188. };
  189. return Array<ShaderPreset> (presets, numElementsInArray (presets));
  190. }
  191. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenGL2DShaderDemo)
  192. };
  193. //==============================================================================
  194. // This static object will register this demo type in a global list of demos..
  195. static JuceDemoType<OpenGL2DShaderDemo> demo ("20 Graphics: OpenGL 2D");
  196. #endif