diff --git a/example/example_gl2.c b/example/example_gl2.c index 0d50dc3..6242690 100644 --- a/example/example_gl2.c +++ b/example/example_gl2.c @@ -59,6 +59,7 @@ int main() struct NVGcontext* vg = NULL; struct PerfGraph fps; double prevt = 0; + struct NVGLUframebuffer* fb = NULL; if (!glfwInit()) { printf("Failed to init GLFW."); @@ -110,6 +111,8 @@ int main() glfwSetTime(0); prevt = glfwGetTime(); + fb = nvgluCreateFramebuffer(vg, 600, 600); + while (!glfwWindowShouldClose(window)) { double mx, my, t, dt; @@ -129,6 +132,20 @@ int main() // Calculate pixel ration for hi-dpi devices. pxRatio = (float)fbWidth / (float)winWidth; + if (fb != NULL) { + int fboWidth, fboHeight; + nvgImageSize(vg, fb->image, &fboWidth, &fboHeight); + // Draw some stull to an FBO as a test + nvgluBindFramebuffer(fb); + glViewport(0, 0, fboWidth, fboHeight); + glClearColor(0, 0, 0, 0); + glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT); + nvgBeginFrame(vg, fboWidth, fboHeight, pxRatio, NVG_PREMULTIPLIED_ALPHA); + renderDemo(vg, mx, my, fboWidth, fboHeight, t, blowup, &data); + nvgEndFrame(vg); + nvgluBindFramebuffer(NULL); + } + // Update and render glViewport(0, 0, fbWidth, fbHeight); if (premult) @@ -142,6 +159,15 @@ int main() renderDemo(vg, mx,my, winWidth,winHeight, t, blowup, &data); renderGraph(vg, 5,5, &fps); + if (fb != NULL) { + struct NVGpaint img = nvgImagePattern(vg, 0, 0, 150, 150, 0, fb->image, NVG_NOREPEAT, 1.0f); + nvgBeginPath(vg); + nvgTranslate(vg, 540, 300); + nvgRect(vg, 0, 0, 150, 150); + nvgFillPaint(vg, img); + nvgFill(vg); + } + nvgEndFrame(vg); if (screenshot) { diff --git a/src/nanovg_gl_utils.h b/src/nanovg_gl_utils.h index 8368d74..d9772ee 100644 --- a/src/nanovg_gl_utils.h +++ b/src/nanovg_gl_utils.h @@ -34,9 +34,20 @@ void nvgluDeleteFramebuffer(struct NVGcontext* ctx, struct NVGLUframebuffer* fb) #ifdef NANOVG_GL_IMPLEMENTATION +#if defined(NANOVG_GL3) +// FBO is core in OpenGL 3>. +# define NANOVG_FBO_VALID 1 +#elif defined(NANOVG_GL2) +// On OS X including glext defines FBO on GL2 too. +# ifdef __APPLE__ +# include +# define NANOVG_FBO_VALID 1 +# endif +#endif + struct NVGLUframebuffer* nvgluCreateFramebuffer(struct NVGcontext* ctx, int w, int h) { -#ifdef NANOVG_GL3 +#ifdef NANOVG_FBO_VALID struct NVGLUframebuffer* fb = NULL; fb = (struct NVGLUframebuffer*)malloc(sizeof(struct NVGLUframebuffer)); if (fb == NULL) goto error; @@ -75,7 +86,7 @@ error: void nvgluBindFramebuffer(struct NVGLUframebuffer* fb) { -#ifdef NANOVG_GL3 +#ifdef NANOVG_FBO_VALID glBindFramebuffer(GL_FRAMEBUFFER, fb != NULL ? fb->fbo : 0); #else NVG_NOTUSED(fb); @@ -84,7 +95,7 @@ void nvgluBindFramebuffer(struct NVGLUframebuffer* fb) void nvgluDeleteFramebuffer(struct NVGcontext* ctx, struct NVGLUframebuffer* fb) { -#ifdef NANOVG_GL3 +#ifdef NANOVG_FBO_VALID if (fb == NULL) return; if (fb->fbo != 0) glDeleteFramebuffers(1, &fb->fbo);