Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

nanovg_gl_utils.h 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. //
  2. // Copyright (c) 2009-2013 Mikko Mononen memon@inside.org
  3. //
  4. // This software is provided 'as-is', without any express or implied
  5. // warranty. In no event will the authors be held liable for any damages
  6. // arising from the use of this software.
  7. // Permission is granted to anyone to use this software for any purpose,
  8. // including commercial applications, and to alter it and redistribute it
  9. // freely, subject to the following restrictions:
  10. // 1. The origin of this software must not be misrepresented; you must not
  11. // claim that you wrote the original software. If you use this software
  12. // in a product, an acknowledgment in the product documentation would be
  13. // appreciated but is not required.
  14. // 2. Altered source versions must be plainly marked as such, and must not be
  15. // misrepresented as being the original software.
  16. // 3. This notice may not be removed or altered from any source distribution.
  17. //
  18. #ifndef NANOVG_GL_UTILS_H
  19. #define NANOVG_GL_UTILS_H
  20. struct NVGLUframebuffer {
  21. NVGcontext* ctx;
  22. GLuint fbo;
  23. GLuint rbo;
  24. GLuint texture;
  25. int image;
  26. };
  27. typedef struct NVGLUframebuffer NVGLUframebuffer;
  28. // Helper function to create GL frame buffer to render to.
  29. void nvgluBindFramebuffer(NVGLUframebuffer* fb);
  30. NVGLUframebuffer* nvgluCreateFramebuffer(NVGcontext* ctx, int w, int h, int imageFlags);
  31. void nvgluDeleteFramebuffer(NVGLUframebuffer* fb);
  32. #endif // NANOVG_GL_UTILS_H
  33. #ifdef NANOVG_GL_IMPLEMENTATION
  34. #if defined(NANOVG_GL3) || defined(NANOVG_GLES2) || defined(NANOVG_GLES3)
  35. // FBO is core in OpenGL 3>.
  36. # define NANOVG_FBO_VALID 1
  37. #elif defined(NANOVG_GL2)
  38. // On OS X including glext defines FBO on GL2 too.
  39. # ifdef __APPLE__
  40. # include <OpenGL/glext.h>
  41. # define NANOVG_FBO_VALID 1
  42. # endif
  43. #endif
  44. static GLint defaultFBO = -1;
  45. NVGLUframebuffer* nvgluCreateFramebuffer(NVGcontext* ctx, int w, int h, int imageFlags)
  46. {
  47. #ifdef NANOVG_FBO_VALID
  48. GLint defaultFBO;
  49. GLint defaultRBO;
  50. NVGLUframebuffer* fb = NULL;
  51. glGetIntegerv(GL_FRAMEBUFFER_BINDING, &defaultFBO);
  52. glGetIntegerv(GL_RENDERBUFFER_BINDING, &defaultRBO);
  53. fb = (NVGLUframebuffer*)malloc(sizeof(NVGLUframebuffer));
  54. if (fb == NULL) goto error;
  55. memset(fb, 0, sizeof(NVGLUframebuffer));
  56. fb->image = nvgCreateImageRGBA(ctx, w, h, imageFlags | NVG_IMAGE_FLIPY | NVG_IMAGE_PREMULTIPLIED, NULL);
  57. #if defined NANOVG_GL2
  58. fb->texture = nvglImageHandleGL2(ctx, fb->image);
  59. #elif defined NANOVG_GL3
  60. fb->texture = nvglImageHandleGL3(ctx, fb->image);
  61. #elif defined NANOVG_GLES2
  62. fb->texture = nvglImageHandleGLES2(ctx, fb->image);
  63. #elif defined NANOVG_GLES3
  64. fb->texture = nvglImageHandleGLES3(ctx, fb->image);
  65. #endif
  66. fb->ctx = ctx;
  67. // frame buffer object
  68. glGenFramebuffers(1, &fb->fbo);
  69. glBindFramebuffer(GL_FRAMEBUFFER, fb->fbo);
  70. // render buffer object
  71. glGenRenderbuffers(1, &fb->rbo);
  72. glBindRenderbuffer(GL_RENDERBUFFER, fb->rbo);
  73. glRenderbufferStorage(GL_RENDERBUFFER, GL_STENCIL_INDEX8, w, h);
  74. // combine all
  75. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fb->texture, 0);
  76. glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, fb->rbo);
  77. if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) goto error;
  78. glBindFramebuffer(GL_FRAMEBUFFER, defaultFBO);
  79. glBindRenderbuffer(GL_RENDERBUFFER, defaultRBO);
  80. return fb;
  81. error:
  82. glBindFramebuffer(GL_FRAMEBUFFER, defaultFBO);
  83. glBindRenderbuffer(GL_RENDERBUFFER, defaultRBO);
  84. nvgluDeleteFramebuffer(fb);
  85. return NULL;
  86. #else
  87. NVG_NOTUSED(ctx);
  88. NVG_NOTUSED(w);
  89. NVG_NOTUSED(h);
  90. NVG_NOTUSED(imageFlags);
  91. return NULL;
  92. #endif
  93. }
  94. void nvgluBindFramebuffer(NVGLUframebuffer* fb)
  95. {
  96. #ifdef NANOVG_FBO_VALID
  97. if (defaultFBO == -1) glGetIntegerv(GL_FRAMEBUFFER_BINDING, &defaultFBO);
  98. glBindFramebuffer(GL_FRAMEBUFFER, fb != NULL ? fb->fbo : defaultFBO);
  99. #else
  100. NVG_NOTUSED(fb);
  101. #endif
  102. }
  103. void nvgluDeleteFramebuffer(NVGLUframebuffer* fb)
  104. {
  105. #ifdef NANOVG_FBO_VALID
  106. if (fb == NULL) return;
  107. if (fb->fbo != 0)
  108. glDeleteFramebuffers(1, &fb->fbo);
  109. if (fb->rbo != 0)
  110. glDeleteRenderbuffers(1, &fb->rbo);
  111. if (fb->image >= 0)
  112. nvgDeleteImage(fb->ctx, fb->image);
  113. fb->ctx = NULL;
  114. fb->fbo = 0;
  115. fb->rbo = 0;
  116. fb->texture = 0;
  117. fb->image = -1;
  118. free(fb);
  119. #else
  120. NVG_NOTUSED(fb);
  121. #endif
  122. }
  123. #endif // NANOVG_GL_IMPLEMENTATION