From 7ad64ece3647bc73b0e46df53b9c85a4e5272695 Mon Sep 17 00:00:00 2001 From: Nite Luo Date: Tue, 19 Nov 2013 11:06:26 -0800 Subject: [PATCH] Add Transform to DrawNode --- cocos/2d/CCDrawNode.cpp | 4 ++ cocos/2d/CCDrawNode.h | 1 + cocos/2d/CCNewDrawNode.cpp | 51 ++++++++++++++++++- cocos/2d/CCNewDrawNode.h | 1 + cocos/2d/renderer/QuadCommand.h | 1 + .../NewRendererTest/NewRendererTest.cpp | 2 +- 6 files changed, 58 insertions(+), 2 deletions(-) diff --git a/cocos/2d/CCDrawNode.cpp b/cocos/2d/CCDrawNode.cpp index 67ba1075b1..7d63c3f992 100644 --- a/cocos/2d/CCDrawNode.cpp +++ b/cocos/2d/CCDrawNode.cpp @@ -102,6 +102,7 @@ DrawNode::DrawNode() , _bufferCapacity(0) , _bufferCount(0) , _buffer(NULL) +, _finalBuffer(NULL) , _dirty(false) { _blendFunc = BlendFunc::ALPHA_PREMULTIPLIED; @@ -111,6 +112,8 @@ DrawNode::~DrawNode() { free(_buffer); _buffer = NULL; + free(_finalBuffer); + _finalBuffer = NULL; glDeleteBuffers(1, &_vbo); _vbo = 0; @@ -150,6 +153,7 @@ void DrawNode::ensureCapacity(long count) { _bufferCapacity += MAX(_bufferCapacity, count); _buffer = (V2F_C4B_T2F*)realloc(_buffer, _bufferCapacity*sizeof(V2F_C4B_T2F)); + _finalBuffer = (V2F_C4B_T2F*)realloc(_buffer, _bufferCapacity*sizeof(V2F_C4B_T2F)); } } diff --git a/cocos/2d/CCDrawNode.h b/cocos/2d/CCDrawNode.h index fe7312ade8..2355ec55db 100644 --- a/cocos/2d/CCDrawNode.h +++ b/cocos/2d/CCDrawNode.h @@ -108,6 +108,7 @@ protected: long _bufferCapacity; GLsizei _bufferCount; V2F_C4B_T2F *_buffer; + V2F_C4B_T2F *_finalBuffer; BlendFunc _blendFunc; diff --git a/cocos/2d/CCNewDrawNode.cpp b/cocos/2d/CCNewDrawNode.cpp index 164bb5b4b0..6eb6330ab9 100644 --- a/cocos/2d/CCNewDrawNode.cpp +++ b/cocos/2d/CCNewDrawNode.cpp @@ -5,6 +5,7 @@ #include "CCNewDrawNode.h" #include "QuadCommand.h" +#include "Renderer.h" NS_CC_BEGIN @@ -38,11 +39,59 @@ bool NewDrawNode::init() return DrawNode::init(); } +void NewDrawNode::updateTransform() +{ + if(_dirty && _visible) + { + //TODO optimize this transformation, should use parent's transformation instead + AffineTransform _transformToBatch = getNodeToWorldTransform(); + + // + // calculate the Quad based on the Affine Matrix + // + + Size size = _rect.size; + + float x1 = _offsetPosition.x; + float y1 = _offsetPosition.y; + + float x2 = x1 + size.width; + float y2 = y1 + size.height; + float x = _transformToBatch.tx; + float y = _transformToBatch.ty; + + float cr = _transformToBatch.a; + float sr = _transformToBatch.b; + float cr2 = _transformToBatch.d; + float sr2 = -_transformToBatch.c; + float ax = x1 * cr - y1 * sr2 + x; + float ay = x1 * sr + y1 * cr2 + y; + + float bx = x2 * cr - y1 * sr2 + x; + float by = x2 * sr + y1 * cr2 + y; + + float cx = x2 * cr - y2 * sr2 + x; + float cy = x2 * sr + y2 * cr2 + y; + + float dx = x1 * cr - y2 * sr2 + x; + float dy = x1 * sr + y2 * cr2 + y; + + _quad.bl.vertices = Vertex3F( RENDER_IN_SUBPIXEL(ax), RENDER_IN_SUBPIXEL(ay), _vertexZ ); + _quad.br.vertices = Vertex3F( RENDER_IN_SUBPIXEL(bx), RENDER_IN_SUBPIXEL(by), _vertexZ ); + _quad.tl.vertices = Vertex3F( RENDER_IN_SUBPIXEL(dx), RENDER_IN_SUBPIXEL(dy), _vertexZ ); + _quad.tr.vertices = Vertex3F( RENDER_IN_SUBPIXEL(cx), RENDER_IN_SUBPIXEL(cy), _vertexZ ); + } + + _recursiveDirty = false; + setDirty(false); +} + void NewDrawNode::draw() { updateTransform(); -// QuadCommand* quadCommand = new QuadCommand(0, _vertexZ, 0, _shaderProgram, _blendFunc, ) +// QuadCommand* quadCommand = new QuadCommand(0, _vertexZ, CC_NO_TEXTURE, _shaderProgram, _blendFunc, &_quads, _bufferCount); +// Renderer::getInstance()-> } NS_CC_END \ No newline at end of file diff --git a/cocos/2d/CCNewDrawNode.h b/cocos/2d/CCNewDrawNode.h index 6cfbfee8a3..702d465ca7 100644 --- a/cocos/2d/CCNewDrawNode.h +++ b/cocos/2d/CCNewDrawNode.h @@ -21,6 +21,7 @@ public: virtual bool init(); + void updateTransform(); void draw(); protected: diff --git a/cocos/2d/renderer/QuadCommand.h b/cocos/2d/renderer/QuadCommand.h index 9483ee8439..422f00202e 100644 --- a/cocos/2d/renderer/QuadCommand.h +++ b/cocos/2d/renderer/QuadCommand.h @@ -12,6 +12,7 @@ NS_CC_BEGIN +#define CC_NO_TEXTURE 0 class QuadCommand : public RenderCommand { diff --git a/samples/Cpp/TestCpp/Classes/NewRendererTest/NewRendererTest.cpp b/samples/Cpp/TestCpp/Classes/NewRendererTest/NewRendererTest.cpp index 12b8a8077c..ccadcaf1bd 100644 --- a/samples/Cpp/TestCpp/Classes/NewRendererTest/NewRendererTest.cpp +++ b/samples/Cpp/TestCpp/Classes/NewRendererTest/NewRendererTest.cpp @@ -283,9 +283,9 @@ NewClippingNodeTest::NewClippingNodeTest() // stencil->drawPolygon(rectangle, 4, white, 1, white); // clipper->setStencil(stencil); + //Test with alpha Test clipper->setAlphaThreshold(0.05f); auto stencil = NewSprite::create("Images/grossini.png"); - stencil->setScale(2.0f); stencil->setPosition(s.width/2, s.height/2); clipper->setStencil(stencil);