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.

260 lines
8.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE examples.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. The code included in this file is provided under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  7. To use, copy, modify, and/or distribute this software for any purpose with or
  8. without fee is hereby granted provided that the above copyright notice and
  9. this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES,
  11. WHETHER EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR
  12. PURPOSE, ARE DISCLAIMED.
  13. ==============================================================================
  14. */
  15. /*******************************************************************************
  16. The block below describes the properties of this PIP. A PIP is a short snippet
  17. of code that can be read by the Projucer and used to generate a JUCE project.
  18. BEGIN_JUCE_PIP_METADATA
  19. name: OpenGLDemo2D
  20. version: 1.0.0
  21. vendor: JUCE
  22. website: http://juce.com
  23. description: Simple 2D OpenGL application.
  24. dependencies: juce_core, juce_data_structures, juce_events, juce_graphics,
  25. juce_gui_basics, juce_gui_extra, juce_opengl
  26. exporters: xcode_mac, vs2017, linux_make, androidstudio, xcode_iphone
  27. type: Component
  28. mainClass: OpenGLDemo2D
  29. useLocalCopy: 1
  30. END_JUCE_PIP_METADATA
  31. *******************************************************************************/
  32. #pragma once
  33. #include "../Assets/DemoUtilities.h"
  34. //==============================================================================
  35. class OpenGLDemo2D : public Component,
  36. private CodeDocument::Listener,
  37. private Timer
  38. {
  39. public:
  40. OpenGLDemo2D()
  41. {
  42. setOpaque (true);
  43. if (auto* peer = getPeer())
  44. peer->setCurrentRenderingEngine (0);
  45. openGLContext.attachTo (*getTopLevelComponent());
  46. addAndMakeVisible (statusLabel);
  47. statusLabel.setJustificationType (Justification::topLeft);
  48. statusLabel.setFont (Font (14.0f));
  49. auto presets = getPresets();
  50. for (int i = 0; i < presets.size(); ++i)
  51. presetBox.addItem (presets[i].name, i + 1);
  52. addAndMakeVisible (presetLabel);
  53. presetLabel.attachToComponent (&presetBox, true);
  54. addAndMakeVisible (presetBox);
  55. presetBox.onChange = [this] { selectPreset (presetBox.getSelectedItemIndex()); };
  56. fragmentEditorComp.setOpaque (false);
  57. fragmentDocument.addListener (this);
  58. addAndMakeVisible (fragmentEditorComp);
  59. presetBox.setSelectedItemIndex (0);
  60. setSize (500, 500);
  61. }
  62. ~OpenGLDemo2D()
  63. {
  64. openGLContext.detach();
  65. shader.reset();
  66. }
  67. void paint (Graphics& g) override
  68. {
  69. g.fillCheckerBoard (getLocalBounds().toFloat(), 48.0f, 48.0f, Colours::lightgrey, Colours::white);
  70. if (shader.get() == nullptr || shader->getFragmentShaderCode() != fragmentCode)
  71. {
  72. shader.reset();
  73. if (fragmentCode.isNotEmpty())
  74. {
  75. shader.reset (new OpenGLGraphicsContextCustomShader (fragmentCode));
  76. auto result = shader->checkCompilation (g.getInternalContext());
  77. if (result.failed())
  78. {
  79. statusLabel.setText (result.getErrorMessage(), dontSendNotification);
  80. shader.reset();
  81. }
  82. }
  83. }
  84. if (shader.get() != nullptr)
  85. {
  86. statusLabel.setText ({}, dontSendNotification);
  87. shader->fillRect (g.getInternalContext(), getLocalBounds());
  88. }
  89. }
  90. void resized() override
  91. {
  92. auto area = getLocalBounds().reduced (4);
  93. statusLabel.setBounds (area.removeFromTop (75));
  94. area.removeFromTop (area.getHeight() / 2);
  95. auto presets = area.removeFromTop (25);
  96. presets.removeFromLeft (100);
  97. presetBox.setBounds (presets.removeFromLeft (150));
  98. area.removeFromTop (4);
  99. fragmentEditorComp.setBounds (area);
  100. }
  101. void selectPreset (int preset)
  102. {
  103. fragmentDocument.replaceAllContent (getPresets()[preset].fragmentShader);
  104. startTimer (1);
  105. }
  106. std::unique_ptr<OpenGLGraphicsContextCustomShader> shader;
  107. Label statusLabel, presetLabel { {}, "Shader Preset:" };
  108. ComboBox presetBox;
  109. CodeDocument fragmentDocument;
  110. CodeEditorComponent fragmentEditorComp { fragmentDocument, nullptr };
  111. String fragmentCode;
  112. private:
  113. OpenGLContext openGLContext;
  114. enum { shaderLinkDelay = 500 };
  115. void codeDocumentTextInserted (const String& /*newText*/, int /*insertIndex*/) override
  116. {
  117. startTimer (shaderLinkDelay);
  118. }
  119. void codeDocumentTextDeleted (int /*startIndex*/, int /*endIndex*/) override
  120. {
  121. startTimer (shaderLinkDelay);
  122. }
  123. void timerCallback() override
  124. {
  125. stopTimer();
  126. fragmentCode = fragmentDocument.getAllContent();
  127. repaint();
  128. }
  129. struct ShaderPreset
  130. {
  131. const char* name;
  132. const char* fragmentShader;
  133. };
  134. static Array<ShaderPreset> getPresets()
  135. {
  136. #define SHADER_2DDEMO_HEADER \
  137. "/* This demo shows the use of the OpenGLGraphicsContextCustomShader,\n" \
  138. " which allows a 2D area to be filled using a GL shader program.\n" \
  139. "\n" \
  140. " Edit the shader program below and it will be \n" \
  141. " recompiled in real-time!\n" \
  142. "*/\n\n"
  143. ShaderPreset presets[] =
  144. {
  145. {
  146. "Simple Gradient",
  147. SHADER_2DDEMO_HEADER
  148. "void main()\n"
  149. "{\n"
  150. " " JUCE_MEDIUMP " vec4 colour1 = vec4 (1.0, 0.4, 0.6, 1.0);\n"
  151. " " JUCE_MEDIUMP " vec4 colour2 = vec4 (0.0, 0.8, 0.6, 1.0);\n"
  152. " " JUCE_MEDIUMP " float alpha = pixelPos.x / 1000.0;\n"
  153. " gl_FragColor = pixelAlpha * mix (colour1, colour2, alpha);\n"
  154. "}\n"
  155. },
  156. {
  157. "Circular Gradient",
  158. SHADER_2DDEMO_HEADER
  159. "void main()\n"
  160. "{\n"
  161. " " JUCE_MEDIUMP " vec4 colour1 = vec4 (1.0, 0.4, 0.6, 1.0);\n"
  162. " " JUCE_MEDIUMP " vec4 colour2 = vec4 (0.3, 0.4, 0.4, 1.0);\n"
  163. " " JUCE_MEDIUMP " float alpha = distance (pixelPos, vec2 (600.0, 500.0)) / 400.0;\n"
  164. " gl_FragColor = pixelAlpha * mix (colour1, colour2, alpha);\n"
  165. "}\n"
  166. },
  167. {
  168. "Circle",
  169. SHADER_2DDEMO_HEADER
  170. "void main()\n"
  171. "{\n"
  172. " " JUCE_MEDIUMP " vec4 colour1 = vec4 (0.1, 0.1, 0.9, 1.0);\n"
  173. " " JUCE_MEDIUMP " vec4 colour2 = vec4 (0.0, 0.8, 0.6, 1.0);\n"
  174. " " JUCE_MEDIUMP " float distance = distance (pixelPos, vec2 (600.0, 500.0));\n"
  175. "\n"
  176. " " JUCE_MEDIUMP " float innerRadius = 200.0;\n"
  177. " " JUCE_MEDIUMP " float outerRadius = 210.0;\n"
  178. "\n"
  179. " if (distance < innerRadius)\n"
  180. " gl_FragColor = colour1;\n"
  181. " else if (distance > outerRadius)\n"
  182. " gl_FragColor = colour2;\n"
  183. " else\n"
  184. " gl_FragColor = mix (colour1, colour2, (distance - innerRadius) / (outerRadius - innerRadius));\n"
  185. "\n"
  186. " gl_FragColor *= pixelAlpha;\n"
  187. "}\n"
  188. },
  189. {
  190. "Solid Colour",
  191. SHADER_2DDEMO_HEADER
  192. "void main()\n"
  193. "{\n"
  194. " gl_FragColor = vec4 (1.0, 0.6, 0.1, pixelAlpha);\n"
  195. "}\n"
  196. }
  197. };
  198. return Array<ShaderPreset> (presets, numElementsInArray (presets));
  199. }
  200. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenGLDemo2D)
  201. };