framebuffer.d 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Written in the D programming language.
  2. /**
  3. Copyright: Copyright Felix 'Zoadian' Hufnagel 2014-.
  4. License: $(WEB http://www.gnu.org/licenses/lgpl.html, LGPLv3).
  5. Authors: $(WEB zoadian.de, Felix 'Zoadian' Hufnagel)
  6. */
  7. module three.gl.framebuffer;
  8. import derelict.opengl3.gl3;
  9. import three.gl.util;
  10. //==============================================================================
  11. ///
  12. enum FramebufferTarget : GLenum {
  13. Write = GL_DRAW_FRAMEBUFFER,
  14. Read = GL_READ_FRAMEBUFFER
  15. }
  16. //==============================================================================
  17. ///
  18. enum FramebufferStatus : GLenum {
  19. Complete = GL_FRAMEBUFFER_COMPLETE,
  20. Error = 0,
  21. Undefines = GL_FRAMEBUFFER_UNDEFINED,
  22. IncompleteAttachment = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT,
  23. IncompleteMissingAttachment = GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT,
  24. IncompleteDrawBuffer = GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER,
  25. IncompleteReadBuffer = GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER,
  26. Unsupported = GL_FRAMEBUFFER_UNSUPPORTED,
  27. IncompleteMultisample = GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE,
  28. IncompleteLayerTargets = GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS
  29. }
  30. //==============================================================================
  31. ///
  32. enum FramebufferAttachment {
  33. Color = GL_COLOR_ATTACHMENT0,
  34. Depth = GL_DEPTH_ATTACHMENT,
  35. Stencil = GL_STENCIL_ATTACHMENT,
  36. DepthStencil = GL_DEPTH_STENCIL_ATTACHMENT
  37. }
  38. //==============================================================================
  39. ///
  40. final class Framebuffer {
  41. private:
  42. GLuint _id;
  43. public:
  44. ///
  45. this() {
  46. check!glGenFramebuffers(1, &this._id);
  47. }
  48. ///
  49. ~this() {
  50. check!glDeleteFramebuffers(1, &this._id);
  51. }
  52. public:
  53. ///
  54. void bind(FramebufferTarget target) {
  55. check!glBindFramebuffer(target, this._id);
  56. }
  57. ///
  58. static void unbind(FramebufferTarget target) {
  59. check!glBindFramebuffer(target, 0);
  60. }
  61. //~ version(OpenGL4) {
  62. //~ void defaultWidth(Target target, uint width) { check!glFramebufferParameteri(target, GL_FRAMEBUFFER_DEFAULT_WIDTH, width); }
  63. //~ uint defaultWidth(Target target) { GLint width; check!glGetFramebufferParameteriv(target, GL_FRAMEBUFFER_DEFAULT_WIDTH, &width); }
  64. //~ void defaultHeight(Target target, uint height) { check!glFramebufferParameteri(target, GL_FRAMEBUFFER_DEFAULT_HEIGHT, height); }
  65. //~ uint defaultWidth(Target target) { GLint height; check!glGetFramebufferParameteriv(target, GL_FRAMEBUFFER_DEFAULT_HEIGHT, &height); }
  66. //~ void defaultLayers(Target target, uint layers) { check!glFramebufferParameteri(target, GL_FRAMEBUFFER_DEFAULT_LAYERS, layers); }
  67. //~ uint defaultWidth(Target target) { GLint layers; check!glGetFramebufferParameteriv(target, GL_FRAMEBUFFER_DEFAULT_LAYERS, &layers); }
  68. //~ void defaultSamles(Target target, uint samples) { check!glFramebufferParameteri(target, GL_FRAMEBUFFER_DEFAULT_SAMPLES, samples); }
  69. //~ uint defaultWidth(Target target) { GLint samples; check!glGetFramebufferParameteriv(target, GL_FRAMEBUFFER_DEFAULT_SAMPLES, &samples); }
  70. //~ void defaultFixedSampleLocations(Target target, uint fixedSampleLocations) { check!glFramebufferParameteri(target, GL_FRAMEBUFFER_DEFAULT_FIXED_SAMPLE_LOCATIONS, fixedSampleLocations); }
  71. //~ uint defaultFixedSampleLocations(Target target) { GLint fixedSampleLocations; check!glGetFramebufferParameteriv(target, GL_FRAMEBUFFER_DEFAULT_FIXED_SAMPLE_LOCATIONS, &fixedSampleLocations); }
  72. //~ }
  73. //~ void attach(Target target, Attachment attachment, Texture texture, uint location) {
  74. //~ debug{assert(this.db_isBound(target), "framebuffer not bound");}
  75. //~ switch(attachment) {
  76. //~ case Attachment.Color:
  77. //~ check!glFramebufferTexture2D(target, attachment + location, GL_TEXTURE_2D, texture.id, 0);
  78. //~ break;
  79. //~ default:
  80. //~ check!glFramebufferTexture2D(target, attachment, GL_TEXTURE_2D, texture.id, 0);
  81. //~ }
  82. //~ }
  83. public:
  84. ///
  85. @property bool isValid() const @safe nothrow {
  86. return (this._id > 0);
  87. }
  88. ///
  89. static FramebufferStatus status(FramebufferTarget target) {
  90. return cast(FramebufferStatus) check!glCheckFramebufferStatus(target);
  91. }
  92. }