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.

262 lines
8.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE examples.
  4. Copyright (c) 2022 - Raw Material Software Limited
  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, vs2022, linux_make, androidstudio, xcode_iphone
  27. moduleFlags: JUCE_STRICT_REFCOUNTEDPOINTER=1
  28. type: Component
  29. mainClass: OpenGLDemo2D
  30. useLocalCopy: 1
  31. END_JUCE_PIP_METADATA
  32. *******************************************************************************/
  33. #pragma once
  34. #include "../Assets/DemoUtilities.h"
  35. //==============================================================================
  36. class OpenGLDemo2D final : public Component,
  37. private CodeDocument::Listener,
  38. private Timer
  39. {
  40. public:
  41. OpenGLDemo2D()
  42. {
  43. setOpaque (true);
  44. if (auto* peer = getPeer())
  45. peer->setCurrentRenderingEngine (0);
  46. openGLContext.attachTo (*getTopLevelComponent());
  47. addAndMakeVisible (statusLabel);
  48. statusLabel.setJustificationType (Justification::topLeft);
  49. statusLabel.setFont (Font (14.0f));
  50. auto presets = getPresets();
  51. for (int i = 0; i < presets.size(); ++i)
  52. presetBox.addItem (presets[i].name, i + 1);
  53. addAndMakeVisible (presetLabel);
  54. presetLabel.attachToComponent (&presetBox, true);
  55. addAndMakeVisible (presetBox);
  56. presetBox.onChange = [this] { selectPreset (presetBox.getSelectedItemIndex()); };
  57. fragmentEditorComp.setOpaque (false);
  58. fragmentDocument.addListener (this);
  59. addAndMakeVisible (fragmentEditorComp);
  60. presetBox.setSelectedItemIndex (0);
  61. setSize (500, 500);
  62. }
  63. ~OpenGLDemo2D() override
  64. {
  65. openGLContext.detach();
  66. shader.reset();
  67. }
  68. void paint (Graphics& g) override
  69. {
  70. g.fillCheckerBoard (getLocalBounds().toFloat(), 48.0f, 48.0f, Colours::lightgrey, Colours::white);
  71. if (shader.get() == nullptr || shader->getFragmentShaderCode() != fragmentCode)
  72. {
  73. shader.reset();
  74. if (fragmentCode.isNotEmpty())
  75. {
  76. shader.reset (new OpenGLGraphicsContextCustomShader (fragmentCode));
  77. auto result = shader->checkCompilation (g.getInternalContext());
  78. if (result.failed())
  79. {
  80. statusLabel.setText (result.getErrorMessage(), dontSendNotification);
  81. shader.reset();
  82. }
  83. }
  84. }
  85. if (shader.get() != nullptr)
  86. {
  87. statusLabel.setText ({}, dontSendNotification);
  88. shader->fillRect (g.getInternalContext(), getLocalBounds());
  89. }
  90. }
  91. void resized() override
  92. {
  93. auto area = getLocalBounds().reduced (4);
  94. statusLabel.setBounds (area.removeFromTop (75));
  95. area.removeFromTop (area.getHeight() / 2);
  96. auto presets = area.removeFromTop (25);
  97. presets.removeFromLeft (100);
  98. presetBox.setBounds (presets.removeFromLeft (150));
  99. area.removeFromTop (4);
  100. fragmentEditorComp.setBounds (area);
  101. }
  102. void selectPreset (int preset)
  103. {
  104. fragmentDocument.replaceAllContent (getPresets()[preset].fragmentShader);
  105. startTimer (1);
  106. }
  107. std::unique_ptr<OpenGLGraphicsContextCustomShader> shader;
  108. Label statusLabel, presetLabel { {}, "Shader Preset:" };
  109. ComboBox presetBox;
  110. CodeDocument fragmentDocument;
  111. CodeEditorComponent fragmentEditorComp { fragmentDocument, nullptr };
  112. String fragmentCode;
  113. private:
  114. OpenGLContext openGLContext;
  115. enum { shaderLinkDelay = 500 };
  116. void codeDocumentTextInserted (const String& /*newText*/, int /*insertIndex*/) override
  117. {
  118. startTimer (shaderLinkDelay);
  119. }
  120. void codeDocumentTextDeleted (int /*startIndex*/, int /*endIndex*/) override
  121. {
  122. startTimer (shaderLinkDelay);
  123. }
  124. void timerCallback() override
  125. {
  126. stopTimer();
  127. fragmentCode = fragmentDocument.getAllContent();
  128. repaint();
  129. }
  130. struct ShaderPreset
  131. {
  132. const char* name;
  133. const char* fragmentShader;
  134. };
  135. static Array<ShaderPreset> getPresets()
  136. {
  137. #define SHADER_2DDEMO_HEADER \
  138. "/* This demo shows the use of the OpenGLGraphicsContextCustomShader,\n" \
  139. " which allows a 2D area to be filled using a GL shader program.\n" \
  140. "\n" \
  141. " Edit the shader program below and it will be \n" \
  142. " recompiled in real-time!\n" \
  143. "*/\n\n"
  144. ShaderPreset presets[] =
  145. {
  146. {
  147. "Simple Gradient",
  148. SHADER_2DDEMO_HEADER
  149. "void main()\n"
  150. "{\n"
  151. " " JUCE_MEDIUMP " vec4 colour1 = vec4 (1.0, 0.4, 0.6, 1.0);\n"
  152. " " JUCE_MEDIUMP " vec4 colour2 = vec4 (0.0, 0.8, 0.6, 1.0);\n"
  153. " " JUCE_MEDIUMP " float alpha = pixelPos.x / 1000.0;\n"
  154. " gl_FragColor = pixelAlpha * mix (colour1, colour2, alpha);\n"
  155. "}\n"
  156. },
  157. {
  158. "Circular Gradient",
  159. SHADER_2DDEMO_HEADER
  160. "void main()\n"
  161. "{\n"
  162. " " JUCE_MEDIUMP " vec4 colour1 = vec4 (1.0, 0.4, 0.6, 1.0);\n"
  163. " " JUCE_MEDIUMP " vec4 colour2 = vec4 (0.3, 0.4, 0.4, 1.0);\n"
  164. " " JUCE_MEDIUMP " float alpha = distance (pixelPos, vec2 (600.0, 500.0)) / 400.0;\n"
  165. " gl_FragColor = pixelAlpha * mix (colour1, colour2, alpha);\n"
  166. "}\n"
  167. },
  168. {
  169. "Circle",
  170. SHADER_2DDEMO_HEADER
  171. "void main()\n"
  172. "{\n"
  173. " " JUCE_MEDIUMP " vec4 colour1 = vec4 (0.1, 0.1, 0.9, 1.0);\n"
  174. " " JUCE_MEDIUMP " vec4 colour2 = vec4 (0.0, 0.8, 0.6, 1.0);\n"
  175. " " JUCE_MEDIUMP " float distance = distance (pixelPos, vec2 (600.0, 500.0));\n"
  176. "\n"
  177. " " JUCE_MEDIUMP " float innerRadius = 200.0;\n"
  178. " " JUCE_MEDIUMP " float outerRadius = 210.0;\n"
  179. "\n"
  180. " if (distance < innerRadius)\n"
  181. " gl_FragColor = colour1;\n"
  182. " else if (distance > outerRadius)\n"
  183. " gl_FragColor = colour2;\n"
  184. " else\n"
  185. " gl_FragColor = mix (colour1, colour2, (distance - innerRadius) / (outerRadius - innerRadius));\n"
  186. "\n"
  187. " gl_FragColor *= pixelAlpha;\n"
  188. "}\n"
  189. },
  190. {
  191. "Solid Colour",
  192. SHADER_2DDEMO_HEADER
  193. "void main()\n"
  194. "{\n"
  195. " gl_FragColor = vec4 (1.0, 0.6, 0.1, pixelAlpha);\n"
  196. "}\n"
  197. }
  198. };
  199. return Array<ShaderPreset> (presets, numElementsInArray (presets));
  200. }
  201. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenGLDemo2D)
  202. };