framebuffer.d 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. import std.stdio;
  11. //==============================================================================
  12. ///
  13. enum FramebufferTarget : GLenum {
  14. Write = GL_DRAW_FRAMEBUFFER,
  15. Read = GL_READ_FRAMEBUFFER
  16. }
  17. //==============================================================================
  18. ///
  19. enum FramebufferStatus : GLenum {
  20. Complete = GL_FRAMEBUFFER_COMPLETE,
  21. Error = 0,
  22. Undefines = GL_FRAMEBUFFER_UNDEFINED,
  23. IncompleteAttachment = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT,
  24. IncompleteMissingAttachment = GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT,
  25. IncompleteDrawBuffer = GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER,
  26. IncompleteReadBuffer = GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER,
  27. Unsupported = GL_FRAMEBUFFER_UNSUPPORTED,
  28. IncompleteMultisample = GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE,
  29. IncompleteLayerTargets = GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS
  30. }
  31. //==============================================================================
  32. ///
  33. enum FramebufferAttachment {
  34. Color = GL_COLOR_ATTACHMENT0,
  35. Depth = GL_DEPTH_ATTACHMENT,
  36. Stencil = GL_STENCIL_ATTACHMENT,
  37. DepthStencil = GL_DEPTH_STENCIL_ATTACHMENT
  38. }
  39. //==============================================================================
  40. ///
  41. final class Framebuffer {
  42. private:
  43. GLuint _id;
  44. public:
  45. ///
  46. this() {
  47. check!glGenFramebuffers(1, &this._id);
  48. writeln("Framebuffer created: ", this._id);
  49. }
  50. ///
  51. ~this() {
  52. check!glDeleteFramebuffers(1, &this._id);
  53. writeln("Framebuffer destroyed: ", this._id);
  54. }
  55. public:
  56. ///
  57. void bind(FramebufferTarget target) {
  58. assert(this.isValid);
  59. check!glBindFramebuffer(target, this._id);
  60. }
  61. ///
  62. static void unbind(FramebufferTarget target) {
  63. check!glBindFramebuffer(target, 0);
  64. }
  65. //~ version(OpenGL4) {
  66. //~ void defaultWidth(Target target, uint width) { check!glFramebufferParameteri(target, GL_FRAMEBUFFER_DEFAULT_WIDTH, width); }
  67. //~ uint defaultWidth(Target target) { GLint width; check!glGetFramebufferParameteriv(target, GL_FRAMEBUFFER_DEFAULT_WIDTH, &width); }
  68. //~ void defaultHeight(Target target, uint height) { check!glFramebufferParameteri(target, GL_FRAMEBUFFER_DEFAULT_HEIGHT, height); }
  69. //~ uint defaultWidth(Target target) { GLint height; check!glGetFramebufferParameteriv(target, GL_FRAMEBUFFER_DEFAULT_HEIGHT, &height); }
  70. //~ void defaultLayers(Target target, uint layers) { check!glFramebufferParameteri(target, GL_FRAMEBUFFER_DEFAULT_LAYERS, layers); }
  71. //~ uint defaultWidth(Target target) { GLint layers; check!glGetFramebufferParameteriv(target, GL_FRAMEBUFFER_DEFAULT_LAYERS, &layers); }
  72. //~ void defaultSamles(Target target, uint samples) { check!glFramebufferParameteri(target, GL_FRAMEBUFFER_DEFAULT_SAMPLES, samples); }
  73. //~ uint defaultWidth(Target target) { GLint samples; check!glGetFramebufferParameteriv(target, GL_FRAMEBUFFER_DEFAULT_SAMPLES, &samples); }
  74. //~ void defaultFixedSampleLocations(Target target, uint fixedSampleLocations) { check!glFramebufferParameteri(target, GL_FRAMEBUFFER_DEFAULT_FIXED_SAMPLE_LOCATIONS, fixedSampleLocations); }
  75. //~ uint defaultFixedSampleLocations(Target target) { GLint fixedSampleLocations; check!glGetFramebufferParameteriv(target, GL_FRAMEBUFFER_DEFAULT_FIXED_SAMPLE_LOCATIONS, &fixedSampleLocations); }
  76. //~ }
  77. //~ void attach(Target target, Attachment attachment, Texture texture, uint location) {
  78. //~ debug{assert(this.db_isBound(target), "framebuffer not bound");}
  79. //~ switch(attachment) {
  80. //~ case Attachment.Color:
  81. //~ check!glFramebufferTexture2D(target, attachment + location, GL_TEXTURE_2D, texture.id, 0);
  82. //~ break;
  83. //~ default:
  84. //~ check!glFramebufferTexture2D(target, attachment, GL_TEXTURE_2D, texture.id, 0);
  85. //~ }
  86. //~ }
  87. public:
  88. ///
  89. @property bool isValid() const @safe nothrow {
  90. return (this._id > 0);
  91. }
  92. ///
  93. static FramebufferStatus status(FramebufferTarget target) {
  94. return cast(FramebufferStatus) check!glCheckFramebufferStatus(target);
  95. }
  96. }