Skip to content

Commit e177da0

Browse files
committed
gui: OpenGL testing
1 parent 7ad102b commit e177da0

5 files changed

Lines changed: 169 additions & 1 deletion

File tree

src/dmt/gui/display/DisfluxDisplay.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
#include "dsp/data/FifoAudioBuffer.h"
3333
#include "gui/display/HelloWorldDisplay.h"
34+
#include "gui/display/ImpulseResponseDisplay.h"
3435
#include "gui/display/MultiDisplay.h"
3536
#include "gui/display/OscilloscopeDisplay.h"
3637
#include <JuceHeader.h>
@@ -62,6 +63,8 @@ class alignas(64) DisfluxDisplay : public dmt::gui::display::MultiDisplay
6263
std::vector<std::unique_ptr<AbstractDisplay>> displays;
6364
displays.push_back(
6465
std::make_unique<HelloWorldDisplay>());
66+
displays.push_back(
67+
std::make_unique<ImpulseResponseDisplay>());
6568
displays.push_back(
6669
std::make_unique<OscilloscopeDisplay<float>>(_fifoBuffer,
6770
_apvts,
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
#pragma once
2+
3+
//==============================================================================
4+
5+
#include "gui/display/AbstractDisplay.h"
6+
#include "gui/widget/Label.h"
7+
// #include "utility/Settings.h"
8+
#include "utility/Fonts.h"
9+
#include <JuceHeader.h>
10+
11+
//==============================================================================
12+
13+
namespace dmt {
14+
namespace gui {
15+
namespace display {
16+
17+
//==============================================================================
18+
19+
/**
20+
* @class ImpulseResponseDisplay
21+
* @brief Specialized display component inheriting from
22+
* AbstractDisplay.
23+
*/
24+
class ImpulseResponseDisplay
25+
: public dmt::gui::display::AbstractDisplay
26+
, public juce::OpenGLRenderer
27+
{
28+
using Label = dmt::gui::widget::Label;
29+
using Settings = dmt::Settings;
30+
using Fonts = dmt::utility::Fonts;
31+
using Colour = juce::Colour;
32+
33+
public:
34+
explicit ImpulseResponseDisplay()
35+
: AbstractDisplay()
36+
, label("Impulse Response", fonts.bold, rawFontSize, juce::Colours::white)
37+
{
38+
// addAndMakeVisible(label);
39+
openGLContext.setRenderer(this);
40+
openGLContext.attachTo(*this);
41+
openGLContext.setContinuousRepainting(true);
42+
openGLContext.setComponentPaintingEnabled(true);
43+
}
44+
45+
~ImpulseResponseDisplay() override { openGLContext.detach(); }
46+
47+
void newOpenGLContextCreated() override
48+
{
49+
shaderProgram.reset(new juce::OpenGLShaderProgram(openGLContext));
50+
51+
if (!shaderProgram->addVertexShader(vertexShaderSource) ||
52+
!shaderProgram->addFragmentShader(fragmentShaderSource) ||
53+
!shaderProgram->link()) {
54+
jassertfalse; // shader compile/link failed
55+
}
56+
57+
positionAttributeID = openGLContext.extensions.glGetAttribLocation(
58+
shaderProgram->getProgramID(), "aPos");
59+
jassert(positionAttributeID >= 0);
60+
createTriangle();
61+
}
62+
63+
void renderOpenGL() override
64+
{
65+
juce::OpenGLHelpers::clear(juce::Colours::black);
66+
67+
juce::OpenGLContext::getCurrentContext()->extensions.glUseProgram(
68+
shaderProgram->getProgramID());
69+
70+
auto& gl = openGLContext.extensions;
71+
72+
gl.glEnableVertexAttribArray(positionAttributeID);
73+
74+
gl.glBindBuffer(juce::gl::GL_ARRAY_BUFFER, vbo);
75+
76+
gl.glVertexAttribPointer(positionAttributeID,
77+
2,
78+
juce::gl::GL_FLOAT,
79+
juce::gl::GL_FALSE,
80+
sizeof(float) * 2,
81+
nullptr);
82+
83+
juce::gl::glDrawArrays(juce::gl::GL_TRIANGLES, 0, 3);
84+
85+
gl.glDisableVertexAttribArray(positionAttributeID);
86+
}
87+
88+
void resized() noexcept override { label.setBounds(getLocalBounds()); }
89+
90+
void paint(juce::Graphics& _g) noexcept override
91+
{
92+
if (Settings::debugBounds) {
93+
_g.setColour(juce::Colours::cyan);
94+
_g.drawRect(getLocalBounds(), 1);
95+
}
96+
_g.fillAll(juce::Colours::cyan);
97+
}
98+
99+
void openGLContextClosing() override
100+
{
101+
auto& gl = openGLContext.extensions;
102+
shaderProgram.reset();
103+
gl.glDeleteBuffers(1, &vbo);
104+
}
105+
106+
private:
107+
juce::OpenGLContext openGLContext;
108+
std::unique_ptr<juce::OpenGLShaderProgram> shaderProgram;
109+
int positionAttributeID = -1;
110+
GLuint vbo = 0;
111+
void createTriangle()
112+
{
113+
float vertices[] = { -0.5f, -0.5f, 0.5f, -0.5f, 0.0f, 0.5f };
114+
auto& gl = openGLContext.extensions;
115+
gl.glGenBuffers(1, &vbo);
116+
gl.glBindBuffer(juce::gl::GL_ARRAY_BUFFER, vbo);
117+
gl.glBufferData(juce::gl::GL_ARRAY_BUFFER,
118+
sizeof(vertices),
119+
vertices,
120+
juce::gl::GL_STATIC_DRAW);
121+
}
122+
const char* vertexShaderSource = R"(
123+
#version 330 core
124+
layout(location = 0) in vec2 aPos;
125+
126+
void main()
127+
{
128+
gl_Position = vec4(aPos, 0.0, 1.0);
129+
}
130+
)";
131+
132+
const char* fragmentShaderSource = R"(
133+
#version 330 core
134+
out vec4 FragColor;
135+
136+
void main()
137+
{
138+
FragColor = vec4(1.0, 0.6, 0.2, 1.0);
139+
}
140+
)";
141+
142+
Label label;
143+
Fonts fonts;
144+
float rawFontSize = 24.0f;
145+
146+
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ImpulseResponseDisplay)
147+
};
148+
} // namespace display
149+
} // namespace gui
150+
} // namespace dmt

src/dmt/gui/widget/SimpleButton.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ class SimpleButton
100100
g.drawRect(outerBounds, borderThickness);
101101
}
102102

103-
g.fillAll(juce::Colours::red);
103+
// g.fillAll(juce::Colours::red);
104104
}
105105

106106
//==============================================================================

src/dmt/shaders/basic_frag.glsl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#version 330 core
2+
out vec4 FragColor;
3+
4+
void main()
5+
{
6+
FragColor = vec4(1.0, 0.6, 0.2, 1.0);
7+
}

src/dmt/shaders/basic_vertex.glsl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#version 330 core
2+
3+
layout(location = 0) in vec2 aPos;
4+
5+
void main()
6+
{
7+
gl_Position = vec4(aPos, 0.0, 1.0);
8+
}

0 commit comments

Comments
 (0)