Browse Source

Use another nanovg context for rendering to the framebuffer

tags/v0.5.0
Andrew Belt 7 years ago
parent
commit
f5e8ba0369
6 changed files with 36 additions and 32 deletions
  1. +1
    -0
      include/gui.hpp
  2. +0
    -1
      include/widgets.hpp
  3. +1
    -1
      src/app/RackScene.cpp
  4. +3
    -0
      src/app/RackWidget.cpp
  5. +8
    -3
      src/gui.cpp
  6. +23
    -27
      src/widgets/FramebufferWidget.cpp

+ 1
- 0
include/gui.hpp View File

@@ -17,6 +17,7 @@ namespace rack {

extern GLFWwindow *gWindow;
extern NVGcontext *gVg;
extern NVGcontext *gFramebufferVg;
extern std::shared_ptr<Font> gGuiFont;
extern float gPixelRatio;
extern bool gAllowCursorLock;


+ 0
- 1
include/widgets.hpp View File

@@ -228,7 +228,6 @@ struct FramebufferWidget : virtual Widget {

FramebufferWidget();
~FramebufferWidget();
void step() override;
void draw(NVGcontext *vg) override;
int getImageHandle();
};


+ 1
- 1
src/app/RackScene.cpp View File

@@ -31,7 +31,7 @@ RackScene::RackScene() {
scrollWidget = new RackScrollWidget();
{
zoomWidget = new ZoomWidget();
zoomWidget->zoom = 1.0;
zoomWidget->zoom = 0.5;
{
assert(!gRackWidget);
gRackWidget = new RackWidget();


+ 3
- 0
src/app/RackWidget.cpp View File

@@ -404,6 +404,9 @@ struct AddManufacturerMenuItem : MenuItem {
for (Model *model : models) {
AddModuleMenuItem *item = new AddModuleMenuItem();
item->text = model->name;
// item->rightText = model->plugin->slug;
// if (!model->plugin->version.empty())
// item->rightText += " v" + model->plugin->version;
item->model = model;
item->modulePos = modulePos;
menu->pushChild(item);


+ 8
- 3
src/gui.cpp View File

@@ -29,6 +29,7 @@ namespace rack {

GLFWwindow *gWindow = NULL;
NVGcontext *gVg = NULL;
NVGcontext *gFramebufferVg = NULL;
std::shared_ptr<Font> gGuiFont;
float gPixelRatio = 0.0;
bool gAllowCursorLock = true;
@@ -220,15 +221,15 @@ void renderGui() {
gPixelRatio = (float)width / windowWidth;

// Update and render
glViewport(0, 0, width, height);
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
nvgBeginFrame(gVg, width, height, gPixelRatio);

nvgReset(gVg);
nvgScale(gVg, gPixelRatio, gPixelRatio);
gScene->draw(gVg);

glViewport(0, 0, width, height);
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
nvgEndFrame(gVg);
glfwSwapBuffers(gWindow);
}
@@ -288,6 +289,9 @@ void guiInit() {
// gVg = nvgCreateGL3(NVG_ANTIALIAS);
assert(gVg);

gFramebufferVg = nvgCreateGL2(NVG_ANTIALIAS);
assert(gFramebufferVg);

// Set up Blendish
gGuiFont = Font::load(assetGlobal("res/DejaVuSans.ttf"));
bndSetFont(gGuiFont->handle);
@@ -298,6 +302,7 @@ void guiDestroy() {
gGuiFont.reset();
nvgDeleteGL2(gVg);
// nvgDeleteGL3(gVg);
nvgDeleteGL2(gFramebufferVg);
glfwDestroyWindow(gWindow);
glfwTerminate();
}


+ 23
- 27
src/widgets/FramebufferWidget.cpp View File

@@ -32,11 +32,7 @@ FramebufferWidget::~FramebufferWidget() {
delete internal;
}

void FramebufferWidget::step() {
// Step children before rendering
Widget::step();

// Render the scene to the framebuffer if dirty
void FramebufferWidget::draw(NVGcontext *vg) {
if (dirty) {
internal->box.pos = Vec(0, 0);
internal->box.size = box.size;
@@ -50,6 +46,7 @@ void FramebufferWidget::step() {

// Delete old one first to free up GPU memory
internal->setFramebuffer(NULL);
// Create a framebuffer from the main nanovg context. We will draw to this in the secondary nanovg context.
NVGLUframebuffer *fb = nvgluCreateFramebuffer(gVg, fbSize.x, fbSize.y, NVG_IMAGE_REPEATX | NVG_IMAGE_REPEATY);
if (!fb)
return;
@@ -59,36 +56,19 @@ void FramebufferWidget::step() {
glViewport(0.0, 0.0, fbSize.x, fbSize.y);
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
nvgBeginFrame(gVg, fbSize.x, fbSize.y, gPixelRatio * oversample);

nvgScale(gVg, gPixelRatio * oversample, gPixelRatio * oversample);
Widget::draw(gVg);
nvgBeginFrame(gFramebufferVg, fbSize.x, fbSize.y, gPixelRatio * oversample);

nvgEndFrame(gVg);
nvgScale(gFramebufferVg, gPixelRatio * oversample, gPixelRatio * oversample);
Widget::draw(gFramebufferVg);

nvgEndFrame(gFramebufferVg);
nvgluBindFramebuffer(NULL);

dirty = false;
}
}

void FramebufferWidget::draw(NVGcontext *vg) {
// {
// float xform[6];
// nvgCurrentTransform(vg, xform);
// printf("%f %f %f %f %f %f\n", xform[0], xform[1], xform[2], xform[3], xform[4], xform[5]);
// nvgSave(vg);
// nvgResetTransform(vg);
// nvgTranslate(vg, xform[5], xform[6]);
// nvgBeginPath(vg);
// nvgRect(vg, 0, 0, 50, 50);
// nvgFillColor(vg, nvgRGBf(1.0, 0.0, 0.0));
// nvgFill(vg);
// nvgRestore(vg);
// }

if (!internal->fb) {
// Bypass framebuffer cache entirely
// Widget::draw(vg);
return;
}

@@ -102,6 +82,22 @@ void FramebufferWidget::draw(NVGcontext *vg) {
// For debugging bounding box of framebuffer image
// nvgFillColor(vg, nvgRGBA(255, 0, 0, 64));
// nvgFill(vg);

{
float xform[6];
nvgCurrentTransform(vg, xform);
// printf("%f %f %f %f; %f %f\n", xform[0], xform[1], xform[2], xform[3], xform[4], xform[5]);
nvgSave(vg);
nvgResetTransform(vg);
nvgTranslate(vg, xform[4], xform[5]);
nvgScale(vg, xform[0], xform[3]);
nvgBeginPath(vg);
nvgRect(vg, 0, 0, internal->box.size.x, internal->box.size.y);
nvgStrokeWidth(vg, 2.0);
nvgStrokeColor(vg, nvgRGBf(1.0, 0.0, 0.0));
nvgStroke(vg);
nvgRestore(vg);
}
}

int FramebufferWidget::getImageHandle() {


Loading…
Cancel
Save