dmuratshin 8 years ago
parent
commit
62c8b9f0f8

+ 2 - 2
examples/Demo/src/TestColorFont.h

@@ -24,9 +24,9 @@ public:
         return a.uniformBlack == b.uniformBlack;
     }
 
-    void apply() override
+    void xapply() override
     {
-        MaterialTX<STDMatData>::apply();
+        MaterialTX<STDMatData>::xapply();
         IVideoDriver::instance->setUniform("_black", uniformBlack);
     }
 };

+ 21 - 15
oxygine/src/oxygine/MaterialX.cpp

@@ -46,6 +46,11 @@ namespace oxygine
         r->setBlendMode(_blend);
     }
 
+    void STDMatData::flush()
+    {
+        STDRenderer::getCurrent()->drawBatch();
+    }
+
     MaterialX::MaterialX(const MaterialX& other)
     {
         _hash = other._hash;
@@ -64,29 +69,30 @@ namespace oxygine
 
     void MaterialX::render(const AffineTransform &tr, const Color& c, const RectF& src, const RectF& dest)
     {
-        /*
-        if (current.get() != this)
-        {
-            STDRenderer::getCurrent()->flush();
-            apply();
-            current = this;
-        }
-        */
-        STDRenderer::getCurrent()->draw(this, tr, c, src, dest);
+        STDRenderer::getCurrent()->setTransform(tr);
+        STDRenderer::getCurrent()->draw(c, src, dest);
     }
 
     void MaterialX::render(const Color& c, const RectF& src, const RectF& dest)
     {
-        /*
-        if (current.get() != this)
+        STDRenderer::getCurrent()->draw(c, src, dest);
+    }
+
+    void MaterialX::apply()
+    {
+        if (current != this)
         {
-            STDRenderer::getCurrent()->flush();
-            apply();
+            if (current)
+                current->flush();
+            xapply();
             current = this;
         }
-        */
+    }
+
 
-        STDRenderer::getCurrent()->draw(this, c, src, dest);
+    void MaterialX::flush()
+    {
+        xflush();
     }
 
     oxygine::MaterialX& MaterialX::operator=(const MaterialX& r)

+ 30 - 31
oxygine/src/oxygine/MaterialX.h

@@ -44,7 +44,10 @@ namespace oxygine
         compare _compare;
 
         virtual void init() {}
-        virtual void apply() = 0;
+        
+        virtual void xapply() {}
+        virtual void xflush() {}
+
         virtual MaterialX* clone() const = 0;
         virtual void copyFrom(const MaterialX& r) = 0;
         virtual void update(size_t& hash, compare&) const = 0;
@@ -52,39 +55,13 @@ namespace oxygine
 
         virtual void render(const AffineTransform &tr, const Color& c, const RectF& src, const RectF& dest);
         virtual void render(const Color& c, const RectF& src, const RectF& dest);
-    };
-
-    typedef intrusive_ptr<MaterialX> spMaterialX;
-
-
-    /*
-    class STDMaterialX : public MaterialX
-    {
-    public:
-        spNativeTexture _base;
-        spNativeTexture _alpha;
-        blend_mode      _blend;
-        int             _flags;
 
-        void init(size_t& hash) override
-        {
-            hash_combine(hash, _base.get());
-            hash_combine(hash, _alpha.get());
-            hash_combine(hash, (int)_blend);
-            hash_combine(hash, _flags);
-        }
+        void apply();
+        void flush();
 
-        void apply() override
-        {
-            STDRenderer* r = STDRenderer::getCurrent();
-            r->setShaderFlags(_flags);
-            r->setTextureNew(UberShaderProgram::SAMPLER_BASE, _base);
-            r->setTextureNew(UberShaderProgram::SAMPLER_ALPHA, _alpha);
-            r->setBlendMode(_blend);
-        }
     };
-    */
 
+    typedef intrusive_ptr<MaterialX> spMaterialX;
 
     template<class T>
     class MaterialTX : public MaterialX
@@ -112,11 +89,32 @@ namespace oxygine
             return a.data.cmp(b.data);
         }
 
-        void apply() override
+        void xapply() override
         {
             data.apply();
         }
+
+        void xflush() override
+        {
+            data.flush();
+        }
+
+    };
+
+    /*
+    class STDMaterialX: public MaterialX
+    {
+    public:
+        MATX(STDMaterialX);
+
+        spNativeTexture    _base;
+        spNativeTexture    _alpha;
+        blend_mode         _blend;
+        UberShaderProgram* _uberShader;
+        int                _flags;
+
     };
+    */
 
 
 
@@ -143,6 +141,7 @@ namespace oxygine
 
         void init(size_t& hash) const;
         void apply();
+        void flush();
         bool cmp(const STDMatData& b) const;
     };
 

+ 4 - 1
oxygine/src/oxygine/Polygon.cpp

@@ -77,7 +77,10 @@ namespace oxygine
         if (!_verticesSize)
             return;
 
-        Material::setCurrent(rs.material);
+        _mat->apply();
+
+
+        //Material::setCurrent(rs.material);
 
         const Diffuse& df = _frame.getDiffuse();
 

+ 2 - 0
oxygine/src/oxygine/STDMaterial.cpp

@@ -141,6 +141,8 @@ namespace oxygine
 
         Color color = rs.getFinalColor(sprite->getColor());
 
+        sprite->_mat->apply();
+
         sprite->_mat->render(rs.transform, color, sprite->getAnimFrame().getSrcRect(), sprite->getDestRect());
     }
 

+ 22 - 25
oxygine/src/oxygine/STDRenderer.cpp

@@ -312,7 +312,7 @@ namespace oxygine
         _transform.identity();
         for (int i = 0; i < MAX_TEXTURES; ++i)
             _textures[i] = 0;
-        _currentMaterial = 0;
+        MaterialX::current = 0;
         resetSettings();
 
         xbegin();
@@ -604,21 +604,6 @@ namespace oxygine
         addVertices(v, sizeof(v));
     }
 
-    void STDRenderer::draw(MaterialX* mat, const Color& color, const RectF& srcRect, const RectF& destRect)
-    {
-        draw(mat, _transform, color, srcRect, destRect);
-    }
-
-    void STDRenderer::draw(MaterialX* mat, const AffineTransform& tr, const Color& color_, const RectF& srcRect, const RectF& destRect)
-    {
-        Color color = color_.premultiplied();
-
-        vertexPCT2 v[4];
-        fillQuadT(v, srcRect, destRect, tr, color.rgba());
-        draw(mat, v);
-    }
-
-
     void STDRenderer::setTextureNew(int sampler, const spNativeTexture& t)
     {
         if (_textures[sampler] == t)
@@ -637,6 +622,23 @@ namespace oxygine
             _program = sp;
         }
     }
+    /*
+
+
+    void STDRenderer::draw(MaterialX* mat, const Color& color, const RectF& srcRect, const RectF& destRect)
+    {
+    draw(mat, _transform, color, srcRect, destRect);
+    }
+
+    void STDRenderer::draw(MaterialX* mat, const AffineTransform& tr, const Color& color_, const RectF& srcRect, const RectF& destRect)
+    {
+    Color color = color_.premultiplied();
+
+    vertexPCT2 v[4];
+    fillQuadT(v, srcRect, destRect, tr, color.rgba());
+    draw(mat, v);
+    }
+
 
     void STDRenderer::draw(MaterialX* mat, vertexPCT2 vert[4])
     {
@@ -649,12 +651,6 @@ namespace oxygine
             b.bbox.unite(Vector2(vert[i].x, vert[i].y));
         }
 #else
-        if (_currentMaterial != mat)
-        {
-            flush();
-            mat->apply();
-            _currentMaterial = mat;
-        }
         _vertices.insert(_vertices.end(), (unsigned char*)vert, (unsigned char*)(vert + 4));
 
 #endif
@@ -713,6 +709,7 @@ namespace oxygine
         }
     }
 #endif
+*/
     void STDRenderer::flush()
     {
 #ifdef EXP_SORT
@@ -861,16 +858,16 @@ namespace oxygine
         return a.outlineColor == b.outlineColor && a.outlineParams == b.outlineParams;
     }
 
-    void SDFMaterial::apply()
+    void SDFMaterial::xapply()
     {
-        STDMaterialX::apply();
+        STDMaterialX::xapply();
         IVideoDriver::instance->setUniform("sdf_outline_color", outlineColor);
         IVideoDriver::instance->setUniform("sdf_params", outlineParams);
     }
 
     void SDFMaterial::render(const Color& c, const RectF& src, const RectF& dest)
     {
-        STDRenderer::getCurrent()->draw(this, c, src, dest);
+        //STDRenderer::getCurrent()->draw(this, c, src, dest);
     }
 
 }

+ 4 - 2
oxygine/src/oxygine/STDRenderer.h

@@ -20,7 +20,7 @@ namespace oxygine
 
         static bool cmp(const SDFMaterial& a, const SDFMaterial& b);
 
-        void apply();
+        void xapply();
         void render(const Color& c, const RectF& src, const RectF& dest) override;
     };
 
@@ -87,9 +87,12 @@ namespace oxygine
         void applySimpleMode(bool basePremultiplied);
         /**used in pair with applySimpleMode/applySDF, fast, don't have excess checks*/
         void draw(const spNativeTexture& texture, unsigned int color, const RectF& src, const RectF& dest) override;
+
+        /*
         void draw(MaterialX* mat, const Color& color, const RectF& src, const RectF& dest) override;
         void draw(MaterialX* mat, const AffineTransform& transform, const Color& color, const RectF& src, const RectF& dest);
         void draw(MaterialX* mat, vertexPCT2 vert[4]);
+        */
 
         /**Begins rendering into RenderTexture or into primary framebuffer if rt is null*/
         void begin();
@@ -137,7 +140,6 @@ namespace oxygine
         int _baseShaderFlags;
         Transform _transform;
 
-        spMaterialX _currentMaterial;
         STDRenderer* _previous;
         void setShader(ShaderProgram* prog);
 

+ 5 - 6
oxygine/src/oxygine/Sprite.cpp

@@ -224,13 +224,12 @@ namespace oxygine
 
 
 
-        STDMaterialX mat;
-        mat.data = _mat->data;
-        mat.data._base = _frame.getDiffuse().base;
-        mat.data._alpha = _frame.getDiffuse().alpha;
-        mat.data._flags = _frame.getDiffuse().premultiplied ? 0 : UberShaderProgram::ALPHA_PREMULTIPLY;
+        spSTDMaterialX mat = _mat->clone();
+        mat->data._base = _frame.getDiffuse().base;
+        mat->data._alpha = _frame.getDiffuse().alpha;
+        mat->data._flags = _frame.getDiffuse().premultiplied ? 0 : UberShaderProgram::ALPHA_PREMULTIPLY;
 
-        _mat = mc().cache(mat);
+        _mat = mc().cache(*mat.get());
 
         animFrameChanged(_frame);
     }

+ 4 - 0
oxygine/src/oxygine/Stage.cpp

@@ -170,6 +170,10 @@ namespace oxygine
         Material::setCurrent(0);
 
         STDRenderer::getCurrent()->flush();
+
+        MaterialX::current->flush();
+
+        MaterialX::current = 0;
     }
 
     void Stage::render(const Color& clearColor, const Rect& viewport)

+ 1 - 1
oxygine/src/oxygine/core/Renderer.h

@@ -222,7 +222,7 @@ namespace oxygine
     public:
         virtual ~IElementRenderer() {}
         virtual void draw(const spNativeTexture& texture, unsigned int color, const RectF& src, const RectF& dest) = 0;
-        virtual void draw(MaterialX* mat, const Color& color, const RectF& src, const RectF& dest) = 0;
+        //virtual void draw(MaterialX* mat, const Color& color, const RectF& src, const RectF& dest) = 0;
     };
 
     /**Returns View matrix where Left Top corner is (0,0), and right bottom is (w,h)*/

+ 1 - 0
oxygine/src/oxygine/text_utils/Node.cpp

@@ -156,6 +156,7 @@ namespace oxygine
                 const Symbol& s = _data[i];
                 if (!s.mat)
                     continue;
+                s.mat->apply();
                 s.mat->render(dc.color, s.gl.src, s.destRect);
             }