Browse Source

Made FBO helper to work on OSX when using GL2

shared-context
Mikko Mononen 11 years ago
parent
commit
83c876b9a8
2 changed files with 40 additions and 3 deletions
  1. +26
    -0
      example/example_gl2.c
  2. +14
    -3
      src/nanovg_gl_utils.h

+ 26
- 0
example/example_gl2.c View File

@@ -59,6 +59,7 @@ int main()
struct NVGcontext* vg = NULL; struct NVGcontext* vg = NULL;
struct PerfGraph fps; struct PerfGraph fps;
double prevt = 0; double prevt = 0;
struct NVGLUframebuffer* fb = NULL;


if (!glfwInit()) { if (!glfwInit()) {
printf("Failed to init GLFW."); printf("Failed to init GLFW.");
@@ -110,6 +111,8 @@ int main()
glfwSetTime(0); glfwSetTime(0);
prevt = glfwGetTime(); prevt = glfwGetTime();


fb = nvgluCreateFramebuffer(vg, 600, 600);

while (!glfwWindowShouldClose(window)) while (!glfwWindowShouldClose(window))
{ {
double mx, my, t, dt; double mx, my, t, dt;
@@ -129,6 +132,20 @@ int main()
// Calculate pixel ration for hi-dpi devices. // Calculate pixel ration for hi-dpi devices.
pxRatio = (float)fbWidth / (float)winWidth; 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 // Update and render
glViewport(0, 0, fbWidth, fbHeight); glViewport(0, 0, fbWidth, fbHeight);
if (premult) if (premult)
@@ -142,6 +159,15 @@ int main()
renderDemo(vg, mx,my, winWidth,winHeight, t, blowup, &data); renderDemo(vg, mx,my, winWidth,winHeight, t, blowup, &data);
renderGraph(vg, 5,5, &fps); 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); nvgEndFrame(vg);


if (screenshot) { if (screenshot) {


+ 14
- 3
src/nanovg_gl_utils.h View File

@@ -34,9 +34,20 @@ void nvgluDeleteFramebuffer(struct NVGcontext* ctx, struct NVGLUframebuffer* fb)


#ifdef NANOVG_GL_IMPLEMENTATION #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 <OpenGL/glext.h>
# define NANOVG_FBO_VALID 1
# endif
#endif

struct NVGLUframebuffer* nvgluCreateFramebuffer(struct NVGcontext* ctx, int w, int h) struct NVGLUframebuffer* nvgluCreateFramebuffer(struct NVGcontext* ctx, int w, int h)
{ {
#ifdef NANOVG_GL3
#ifdef NANOVG_FBO_VALID
struct NVGLUframebuffer* fb = NULL; struct NVGLUframebuffer* fb = NULL;
fb = (struct NVGLUframebuffer*)malloc(sizeof(struct NVGLUframebuffer)); fb = (struct NVGLUframebuffer*)malloc(sizeof(struct NVGLUframebuffer));
if (fb == NULL) goto error; if (fb == NULL) goto error;
@@ -75,7 +86,7 @@ error:


void nvgluBindFramebuffer(struct NVGLUframebuffer* fb) void nvgluBindFramebuffer(struct NVGLUframebuffer* fb)
{ {
#ifdef NANOVG_GL3
#ifdef NANOVG_FBO_VALID
glBindFramebuffer(GL_FRAMEBUFFER, fb != NULL ? fb->fbo : 0); glBindFramebuffer(GL_FRAMEBUFFER, fb != NULL ? fb->fbo : 0);
#else #else
NVG_NOTUSED(fb); NVG_NOTUSED(fb);
@@ -84,7 +95,7 @@ void nvgluBindFramebuffer(struct NVGLUframebuffer* fb)


void nvgluDeleteFramebuffer(struct NVGcontext* ctx, struct NVGLUframebuffer* fb) void nvgluDeleteFramebuffer(struct NVGcontext* ctx, struct NVGLUframebuffer* fb)
{ {
#ifdef NANOVG_GL3
#ifdef NANOVG_FBO_VALID
if (fb == NULL) return; if (fb == NULL) return;
if (fb->fbo != 0) if (fb->fbo != 0)
glDeleteFramebuffers(1, &fb->fbo); glDeleteFramebuffers(1, &fb->fbo);


Loading…
Cancel
Save