Files
crushFX/Source/FilterView.cpp
2026-01-21 20:17:54 +13:00

188 lines
6.4 KiB
C++

/*
==============================================================================
FilterView.cpp
Created: 16 Jan 2026 5:06:31pm
Author: esca
==============================================================================
*/
#include <JuceHeader.h>
#include "FilterView.h"
//==============================================================================
/*
* ==============================================================================
*
* FilterView.cpp
* Created: 16 Jan 2026 5:05:38pm
* Author: esca
*
* ==============================================================================
*/
#include <JuceHeader.h>
#include "FilterView.h"
#include "BinaryData.h"
#include "OpenGLUtils.h"
#include "juce_opengl/juce_opengl.h"
#include "juce_opengl/opengl/juce_gl.h"
//==============================================================================
FilterView::FilterView()
{
float startingColour[4] = {128.0f, 255.0f, 255.0f, 255.0f};
waveform = generateSineWave(CRUSHVIEW_SAMPLECOUNT, 0, startingColour);
distortedWaveform = waveform;
vScale(distortedWaveform, 2, 0.75);
vTransform(distortedWaveform, -1, 0);
for(int i = 0; i<CRUSHVIEW_SAMPLECOUNT; i++){
indices.push_back(i);
}
backgroundVertices.push_back(VertexTexture{{-1, -1}, {255.0f, 255.0f, 255.0f, 255.0f}, {0.0f, 0.0f}});
backgroundVertices.push_back(VertexTexture{{-1, 1}, {255.0f, 255.0f, 255.0f, 255.0f}, {0.0f, 1.0f}});
backgroundVertices.push_back(VertexTexture{{1, -1}, {255.0f, 255.0f, 255.0f, 255.0f}, {1.0f, 0.0f}});
backgroundVertices.push_back(VertexTexture{{1, 1}, {255.0f, 255.0f, 255.0f, 255.0f}, {1.0f, 1.0f}});
backgroundIndices = {0,1,2,1,2,3};
antialiasing.multisamplingLevel = 3;
ctx.setPixelFormat(antialiasing);
setOpaque(true);
ctx.setContinuousRepainting(true);
ctx.setRenderer(this);
ctx.attachTo(*this);
}
FilterView::~FilterView()
{
bgTexture.release();
shaderProgram->release();
shaderProgramBackground->release();
ctx.detach();
}
void FilterView::distortWaveForm(int samplerate){
Vertex lastVertex;
int lastMultiplier = 0;
for (int i = 0; i>waveform.size(); i++){
if(floor((float)i/samplerate)>lastMultiplier){
lastMultiplier = floor((float)i/samplerate);
lastVertex = waveform[i];
}
distortedWaveform[i] = lastVertex;
}
}
void FilterView::paint (juce::Graphics& g)
{
ctx.triggerRepaint();
}
void FilterView::resized()
{
}
//OPENGL STUFF
void FilterView::newOpenGLContextCreated(){
background = juce::ImageCache::getFromMemory(BinaryData::panel_png, BinaryData::panel_pngSize);
bgTexture.loadImage(background);
//GENERATE BUFFERS
ctx.extensions.glGenBuffers(1, &vboBackground);
ctx.extensions.glGenBuffers(1, &iboBackground);
ctx.extensions.glGenBuffers(1, &vbo);
ctx.extensions.glGenBuffers(1, &ibo);
//BIND BACKGROUND BUFFERS
ctx.extensions.glBindBuffer(juce::gl::GL_ARRAY_BUFFER, vboBackground);
ctx.extensions.glBufferData(juce::gl::GL_ARRAY_BUFFER, sizeof(VertexTexture)*backgroundVertices.size(), backgroundVertices.data(), juce::gl::GL_STATIC_DRAW);
ctx.extensions.glBindBuffer(juce::gl::GL_ELEMENT_ARRAY_BUFFER, iboBackground);
ctx.extensions.glBufferData(juce::gl::GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * backgroundIndices.size(), backgroundIndices.data(), juce::gl::GL_STATIC_DRAW);
//BIND FFT BUFFERS
//BIND FILTER BUFFERS
ctx.extensions.glBindBuffer(juce::gl::GL_ARRAY_BUFFER, vbo);
ctx.extensions.glBufferData(juce::gl::GL_ARRAY_BUFFER, sizeof(Vertex)*filterVertices.size(), filterVertices.data(), juce::gl::GL_STATIC_DRAW);
ctx.extensions.glBindBuffer(juce::gl::GL_ELEMENT_ARRAY_BUFFER, ibo);
ctx.extensions.glBufferData(juce::gl::GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * filterIndices.size(), filterIndices.data(), juce::gl::GL_STATIC_DRAW);
//SHADERS
vertexShader = VERTEXSHADER;
fragmentShader = FRAGSHADER;
vertexShaderBackground = VERTEXSHADERBACKGROUND;
fragmentShaderBackground = FRAGSHADERBACKGROUND;
//actually render the shaders now
shaderProgramBackground.reset(new juce::OpenGLShaderProgram(ctx));
shaderProgramBackground->addVertexShader(vertexShaderBackground);
shaderProgramBackground->addFragmentShader(fragmentShaderBackground);
shaderProgramBackground->link();
shaderProgramBackground->use();
shaderProgram.reset(new juce::OpenGLShaderProgram(ctx));
shaderProgram->addVertexShader(vertexShader);
shaderProgram->addFragmentShader(fragmentShader);
shaderProgram->link();
shaderProgram->use();
}
void FilterView::renderOpenGL(){
juce::gl::glLineWidth(2.5f);
juce::OpenGLHelpers::clear(juce::Colours::black);
ctx.extensions.glBindBuffer(juce::gl::GL_ARRAY_BUFFER, vboBackground);
ctx.extensions.glBindBuffer(juce::gl::GL_ELEMENT_ARRAY_BUFFER, iboBackground);
//RENDER BACKGROUND
shaderProgramBackground->use();
bgTexture.bind();
juce::gl::glEnable(juce::gl::GL_BLEND);
juce::gl::glBlendFunc(juce::gl::GL_SRC_ALPHA, juce::gl::GL_ONE_MINUS_SRC_ALPHA);
ctx.extensions.glVertexAttribPointer(0, 2, juce::gl::GL_FLOAT, juce::gl::GL_FALSE, sizeof(VertexTexture), nullptr);
ctx.extensions.glEnableVertexAttribArray(0);
ctx.extensions.glVertexAttribPointer(1, 4, juce::gl::GL_FLOAT, juce::gl::GL_FALSE, sizeof(VertexTexture), (GLvoid*)(sizeof(float)*2));
ctx.extensions.glEnableVertexAttribArray(1);
ctx.extensions.glVertexAttribPointer(2, 2, juce::gl::GL_FLOAT, juce::gl::GL_FALSE, sizeof(VertexTexture), (GLvoid*)(sizeof(float)*6));
ctx.extensions.glEnableVertexAttribArray(2);
juce::gl::glDrawElements(juce::gl::GL_TRIANGLES, backgroundIndices.size(), juce::gl::GL_UNSIGNED_INT, nullptr);
bgTexture.unbind();
//RENDER FFT
//RENDER FILTER
ctx.extensions.glBindBuffer(juce::gl::GL_ARRAY_BUFFER, vbo);
ctx.extensions.glBindBuffer(juce::gl::GL_ELEMENT_ARRAY_BUFFER, ibo);
shaderProgram->use();
ctx.extensions.glVertexAttribPointer(0, 2, juce::gl::GL_FLOAT, juce::gl::GL_FALSE, sizeof(Vertex), nullptr);
ctx.extensions.glEnableVertexAttribArray(2);
ctx.extensions.glVertexAttribPointer(1, 4, juce::gl::GL_FLOAT, juce::gl::GL_FALSE, sizeof(Vertex), (GLvoid*)(sizeof(float)*2));
ctx.extensions.glEnableVertexAttribArray(3);
juce::gl::glDrawElements(juce::gl::GL_LINE_STRIP, filterIndices.size(), juce::gl::GL_UNSIGNED_INT, nullptr);
}
void FilterView::openGLContextClosing(){
}