STDRenderer.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #pragma once
  2. #include "oxygine_include.h"
  3. #include "core/Renderer.h"
  4. namespace oxygine
  5. {
  6. class UberShaderProgram;
  7. DECLARE_SMART(STDMaterial, spSTDMaterial);
  8. class STDRenderer : public IElementRenderer
  9. {
  10. public:
  11. static STDRenderer* instance;
  12. /**Sets default rendering OpenGL options for 2D*/
  13. static void setDefaultSettings();
  14. /**Initializes internal classes. Called automatically from oxygine::init();*/
  15. static void initialize();
  16. /**Clears internal data*/
  17. static void release();
  18. /**for lost context*/
  19. static void reset();
  20. /**is Renderer was restored and ready to be used*/
  21. static bool isReady();
  22. /**restore after lost context*/
  23. static void restore();
  24. /**White 4x4 Texture*/
  25. static spNativeTexture white;
  26. static UberShaderProgram uberShader;
  27. static std::vector<unsigned char> uberShaderBody;
  28. static std::vector<unsigned char> indices8;
  29. static std::vector<unsigned short> indices16;
  30. static size_t maxVertices;
  31. STDRenderer(IVideoDriver* driver = 0);
  32. virtual ~STDRenderer();
  33. const Matrix& getViewProjection() const { return _vp; }
  34. IVideoDriver* getDriver();
  35. void setDriver(IVideoDriver*);
  36. void setViewProjTransform(const Matrix& view, const Matrix& proj);
  37. void setViewProjTransform(const Matrix& viewProj);
  38. void setVertexDeclaration(const VertexDeclaration* decl);
  39. void setUberShaderProgram(UberShaderProgram* pr);
  40. /**Sets blend mode. Default value is blend_premultiplied_alpha*/
  41. void setBlendMode(blend_mode blend);
  42. /**Sets texture. If texture is null White texture would be used.*/
  43. void setTexture(const spNativeTexture& base, const spNativeTexture& alpha, bool basePremultiplied = true);
  44. void setTexture(const spNativeTexture& base, bool basePremultiplied = true);
  45. /**Sets World transformation.*/
  46. void setTransform(const Transform& tr) { _transform = tr; }
  47. void beginElementRendering(bool basePremultiplied);// OVERRIDE;
  48. void drawElement(const spNativeTexture& texture, unsigned int color, const RectF& src, const RectF& dest) OVERRIDE;
  49. void draw(const Color&, const RectF& srcRect, const RectF& destRect);
  50. /**Draws existing batch immediately.*/
  51. void drawBatch();
  52. /**Begins rendering into RenderTexture or into primary framebuffer if rt is null*/
  53. void begin(STDRenderer* prev);
  54. /**Completes started rendering and restores previous Frame Buffer.*/
  55. void end();
  56. /**initializes View + Projection matrices where TopLeft is (0,0) and RightBottom is (width, height). use flipU = true for render to texture*/
  57. void initCoordinateSystem(int width, int height, bool flipU = false);
  58. void resetSettings();
  59. /**Cleans existing accumulated batch.*/
  60. void cleanup();
  61. virtual void addVertices(const void* data, unsigned int size);
  62. //debug utils
  63. #ifdef OXYGINE_DEBUG_T2P
  64. static void showTexel2PixelErrors(bool show);
  65. #endif
  66. protected:
  67. Transform _transform;
  68. STDRenderer* _previous;
  69. void setShader(ShaderProgram* prog);
  70. void _drawBatch();
  71. void _addVertices(const void* data, unsigned int size);
  72. void _checkDrawBatch();
  73. std::vector<unsigned char> _vertices;
  74. const VertexDeclaration* _vdecl;
  75. IVideoDriver* _driver;
  76. ShaderProgram* _program;
  77. Matrix _vp;
  78. virtual void preDrawBatch();
  79. virtual void _cleanup();
  80. virtual void _begin();
  81. virtual void _resetSettings();
  82. //virtual
  83. spNativeTexture _base;
  84. spNativeTexture _alpha;
  85. blend_mode _blend;
  86. UberShaderProgram* _uberShader;
  87. unsigned int _shaderFlags;
  88. };
  89. }