/com/subfty/sub/ogl/OGLRenderToTexture.hx

https://bitbucket.org/subfty/sub
Haxe | 58 lines | 44 code | 13 blank | 1 comment | 4 complexity | 49bb2b6825acd5ca12e3a92c157f4ce4 MD5 | raw file
  1. package com.subfty.sub.ogl;
  2. import flash.geom.Rectangle;
  3. import flash.display.Sprite;
  4. import com.subfty.sub.ogl.OGLAction;
  5. import openfl.gl.GLRenderbuffer;
  6. import openfl.gl.GLTexture;
  7. import openfl.gl.GLFramebuffer;
  8. import openfl.gl.GL;
  9. class OGLRenderToTexture {
  10. private function new(){}
  11. public static function attachCapture( doActionOnFramebuffer : GLTexture -> Void,
  12. target : Sprite,
  13. ?wrapBefore : Sprite = null,
  14. ?wrapAfter : Sprite = null){
  15. if(wrapBefore == null)
  16. wrapBefore = cast target.getChildAt(0);
  17. if(wrapAfter == null)
  18. wrapAfter = cast target.getChildAt(target.numChildren - 1);
  19. var framebuffer : GLFramebuffer = GL.createFramebuffer();
  20. var texture : GLTexture = GL.createTexture();
  21. var renderbuffer : GLRenderbuffer = GL.createRenderbuffer();
  22. var before : OGLAction = new OGLAction(function(rect : Rectangle){
  23. GL.viewport (Std.int (rect.x), Std.int (rect.y), Std.int (rect.width), Std.int (rect.height));
  24. GL.bindFramebuffer(GL.FRAMEBUFFER, framebuffer);
  25. GL.clearColor(0.0, 0.0, 0.0, 0.0);
  26. GL.clear(GL.COLOR_BUFFER_BIT);
  27. //TODO: rozwiaz problem przezroczystosci
  28. GL.enable(GL.BLEND);
  29. GL.blendFunc(GL.SRC_ALPHA, GL.ONE_MINUS_SRC_ALPHA);
  30. });
  31. var after : OGLAction = new OGLAction(function(rect : Rectangle){
  32. GL.bindFramebuffer(GL.FRAMEBUFFER, null);
  33. doActionOnFramebuffer(texture);
  34. });
  35. GL.bindFramebuffer(GL.FRAMEBUFFER, framebuffer);
  36. GL.bindTexture(GL.TEXTURE_2D, texture);
  37. GL.texImage2D(GL.TEXTURE_2D, 0, GL.RGBA, SMain.aspect.screenW, SMain.aspect.screenH, 0, GL.RGBA, GL.UNSIGNED_BYTE, null);
  38. GL.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_MAG_FILTER, GL.NEAREST);
  39. GL.texParameteri(GL.TEXTURE_2D, GL.TEXTURE_MIN_FILTER, GL.NEAREST);
  40. GL.framebufferTexture2D(GL.FRAMEBUFFER, GL.COLOR_ATTACHMENT0, GL.TEXTURE_2D, texture, 0);
  41. GL.bindFramebuffer(GL.FRAMEBUFFER, null);
  42. target.addChildAt(before, target.getChildIndex(wrapBefore));
  43. target.addChildAt(after, target.getChildIndex(wrapAfter));
  44. }
  45. }