Skip to content

Instantly share code, notes, and snippets.

@FineCutBodies FineCutBodies/PluginEditor.cpp Secret
Created Aug 28, 2018

Embed
What would you like to do?
OpenGL framerate
#include "PluginEditor.h"
//==============================================================================
OpenGlTestAudioProcessorEditor::OpenGlTestAudioProcessorEditor (OpenGlTestAudioProcessor& p)
: AudioProcessorEditor(&p), processor (p)
{
//=========================
// setup some dummy vertexdata
//=========================
m_cacheSize = 10;
m_vertexes = new GLfloat[2 * m_cacheSize];
float sign = 0.25f;
for (int i = 0; i < m_cacheSize*2; i += 2) {
m_vertexes[i + 0] = float(i) / (2 * (m_cacheSize - 1));
m_vertexes[i + 1] = 0.5f + (sign*=-1.0f);
}
//=========================
// init OpenGL
//=========================
if (auto* peer = getPeer()) peer->setCurrentRenderingEngine(0);
// Antialiasing
OpenGLPixelFormat pixelFormat;
pixelFormat.multisamplingLevel = 8;
m_glContext.setPixelFormat(pixelFormat);
m_glContext.setMultisamplingEnabled(true);
// attach
m_glContext.setRenderer(this);
m_glContext.attachTo(*getTopLevelComponent());
//=========================
// Make sure that before the constructor has finished, you've set the editor's size to whatever you need it to be.
//=========================
setSize (600, 300);
}
OpenGlTestAudioProcessorEditor::~OpenGlTestAudioProcessorEditor()
{
m_glContext.detach();
delete[] m_vertexes;
m_vertexes = nullptr;
}
//==============================================================================
void OpenGlTestAudioProcessorEditor::paint (Graphics& g) {}
void OpenGlTestAudioProcessorEditor::resized() {}
void OpenGlTestAudioProcessorEditor::newOpenGLContextCreated()
{
// start the rendering loop
m_glContext.setContinuousRepainting(true);
// V-sync
m_glContext.setSwapInterval(1);
m_shaderProgram = createShaders();
jassert(m_shaderProgram != nullptr);
m_glContext.extensions.glGenBuffers(1, &m_vertexBufferID);
m_glContext.extensions.glBindBuffer(GL_ARRAY_BUFFER, m_vertexBufferID);
m_glContext.extensions.glBufferData(GL_ARRAY_BUFFER, m_cacheSize * 2 * sizeof(GLfloat), nullptr, GL_DYNAMIC_DRAW);
}
void OpenGlTestAudioProcessorEditor::renderOpenGL()
{
//=========================
// FPS calculation (this will mostly show 60fps for me, so this function is called nicely)
//=========================
if (lastTick == 0) lastTick = Time::getHighResolutionTicks();
else {
auto fps = Time::getHighResolutionTicksPerSecond() / (Time::getHighResolutionTicks() - lastTick);
sum += fps;
if (frames < 100) frames++;
else sum -= sum / frames;
DBG("FPS: "+String(sum/frames));
lastTick = Time::getHighResolutionTicks();
}
//=========================
// dummy sawtooth drawing
//=========================
glViewport(0, 0, getWidth(), getHeight());
OpenGLHelpers::clear(Colours::grey);
m_shaderProgram->use();
OpenGLShaderProgram::Uniform color(*m_shaderProgram, "drawColor");
color.set(0.15f, 0.32f, 0.37f, 1.0f);
m_glContext.extensions.glBindBuffer(GL_ARRAY_BUFFER, m_vertexBufferID);
m_glContext.extensions.glBufferSubData(GL_ARRAY_BUFFER, 0, m_cacheSize * 2 * sizeof(GLfloat), m_vertexes);
const auto posLoc = m_glContext.extensions.glGetAttribLocation(m_shaderProgram->getProgramID(), "position");
m_glContext.extensions.glEnableVertexAttribArray(posLoc);
m_glContext.extensions.glVertexAttribPointer(posLoc, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
glDrawArrays(GL_LINE_STRIP, 0, m_cacheSize);
m_glContext.extensions.glBindBuffer(GL_ARRAY_BUFFER, 0);
}
#pragma once
#include "../JuceLibraryCode/JuceHeader.h"
#include "PluginProcessor.h"
class OpenGlTestAudioProcessorEditor : public AudioProcessorEditor, public OpenGLRenderer
{
public:
OpenGlTestAudioProcessorEditor (OpenGlTestAudioProcessor&);
~OpenGlTestAudioProcessorEditor();
//==============================================================================
void paint (Graphics&) override;
void resized() override;
void newOpenGLContextCreated() override;
void renderOpenGL() override;
void openGLContextClosing() override {}
OpenGLContext m_glContext;
ScopedPointer<OpenGLShaderProgram> m_shaderProgram;
int m_cacheSize = 0;
GLfloat* m_vertexes = nullptr;
GLuint m_vertexBufferID = 0;
GLuint m_vertexArrayID = 0;
int64 lastTick = 0;
int64 frames = 1;
int64 sum = 0;
// openGL related members
OpenGLShaderProgram* createShaders() { return createShaders(m_tempVertexShaderCode, m_tempFragmentShaderCode); }
OpenGLShaderProgram* createShaders(String const& vertexShaderCode, String const& fragmentShaderCode) {
auto* shaderProgram = new OpenGLShaderProgram(m_glContext);
if (!shaderProgram->addVertexShader(OpenGLHelpers::translateVertexShaderToV3(vertexShaderCode))
|| !shaderProgram->addFragmentShader(OpenGLHelpers::translateFragmentShaderToV3(fragmentShaderCode))
|| !shaderProgram->link()) {
DBG(shaderProgram->getLastError());
delete shaderProgram;
shaderProgram = nullptr;
}
return shaderProgram;
}
// using openGL v3.0
const char* m_tempVertexShaderCode =
"#version 130\n"
"in vec4 position;\n"
"void main () {\n"
" gl_Position = vec4(2.0 * position.xyz - 1.0,position.w);\n"
"}\n";
const char* m_tempFragmentShaderCode =
"#version 130\n"
"uniform vec4 drawColor = vec4(1.0, 1.0, 1.0, 1.0);\n"
"out vec4 out_color;\n"
"void main () {\n"
" out_color = drawColor;\n"
"}\n";
private:
OpenGlTestAudioProcessor& processor;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenGlTestAudioProcessorEditor)
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.