TweenOutline.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include "TweenOutline.h"
  2. #include "STDMaterial.h"
  3. #include "Actor.h"
  4. #include "RenderState.h"
  5. #include "core/gl/VertexDeclarationGL.h"
  6. namespace oxygine
  7. {
  8. class TweenOutlineImpl : public TweenPostProcess
  9. {
  10. public:
  11. Color _color;
  12. int _downsample;
  13. TweenOutlineImpl(const Color& c, const PostProcessOptions& opt) : TweenPostProcess(opt), _color(c), _downsample(1) {}
  14. void render(Actor* actor, const RenderState& rs) OVERRIDE
  15. {
  16. STDMaterial* mat = STDMaterial::instance;
  17. STDRenderer* renderer = mat->getRenderer();
  18. RectF src(0, 0,
  19. _pp._screen.getWidth() / (float)_pp._rt->getWidth() / _downsample,
  20. _pp._screen.getHeight() / (float)_pp._rt->getHeight() / _downsample);
  21. RectF dest = _pp._screen.cast<RectF>();
  22. renderer->setBlendMode(blend_premultiplied_alpha);
  23. AffineTransform tr = _pp._transform * _actor->computeGlobalTransform();
  24. renderer->setTransform(tr);
  25. renderer->beginElementRendering(true);
  26. Color color = Color(Color::White).withAlpha(255).premultiplied();
  27. renderer->drawElement(_pp._rt, color.rgba(), src, dest);
  28. renderer->drawBatch();
  29. RenderState r = rs;
  30. r.material = mat;
  31. actor->setMaterial(_prevMaterial);
  32. actor->render(r);
  33. actor->setMaterial(this);
  34. }
  35. void _renderPP() OVERRIDE
  36. {
  37. PostProcess::initShaders();
  38. int w = _pp._screen.size.x;
  39. int h = _pp._screen.size.y;
  40. if (w < 0 || h < 0)
  41. return;
  42. IVideoDriver* driver = IVideoDriver::instance;
  43. const VertexDeclarationGL* decl = static_cast<const VertexDeclarationGL*>(IVideoDriver::instance->getVertexDeclaration(vertexPCT2::FORMAT));
  44. _downsample = 1;
  45. spNativeTexture rt = _pp._rt;
  46. spNativeTexture rt2 = getRTManager().get(0, w, h, _pp._format);
  47. #if 0
  48. driver->setShaderProgram(PostProcess::shaderBlit);
  49. pass(rt, Rect(0, 0, w, h), rt2, Rect(0, 0, w / 2, h / 2));
  50. w /= 2;
  51. h /= 2;
  52. _downsample *= 2;
  53. #endif
  54. #if 0
  55. rt = getRTManager().get(w / 2, h / 2, _pp._format);
  56. _pp._rt = rt;
  57. pass(rt2, Rect(0, 0, w, h), rt, Rect(0, 0, w / 2, h / 2));
  58. w /= 2;
  59. h /= 2;
  60. _downsample *= 2;
  61. #endif
  62. Rect rc(0, 0, w, h);
  63. driver->setShaderProgram(PostProcess::shaderBlurH);
  64. driver->setUniform("step", 1.0f / rt->getWidth());
  65. pass(rt, rc, rt2, rc);
  66. int alpha = lerp(0, 255, _progress);
  67. //log::messageln("tween alpha %d", alpha);
  68. Color c;
  69. if (_pp._options._flags & PostProcessOptions::flag_singleR2T)
  70. c = _color;
  71. else
  72. c = _color.withAlpha(alpha).premultiplied();
  73. driver->setShaderProgram(PostProcess::shaderBlurV);
  74. driver->setUniform("step", 1.0f / rt2->getHeight());
  75. pass(rt2, rc, rt, rc, c);
  76. }
  77. };
  78. TweenOutline::TweenOutline(const Color& color, const PostProcessOptions& opt) : TweenProxy(new TweenOutlineImpl(color, opt))
  79. {
  80. }
  81. }