From 04460750b894354c047eabdc8038cc95999cc626 Mon Sep 17 00:00:00 2001 From: Ricardo Quesada Date: Sun, 23 Feb 2014 01:09:52 -0800 Subject: [PATCH] culling working for both 2d and 3d projections --- CHANGELOG | 17 ++++++++++++----- cocos/2d/CCDirector.cpp | 7 ++++--- cocos/2d/CCNode.h | 34 ++++++++++++++++++++++++++++++---- cocos/2d/CCSprite.cpp | 38 +++++++++++++++++++++++--------------- cocos/2d/CCSprite.h | 6 +++--- 5 files changed, 72 insertions(+), 30 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 9b8ad49206..85319da06c 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,12 +1,19 @@ cocos2d-x-3.0rc0 Feb.?? 2014 [All] - [NEW] Adds more console commands: director resume|pause|stopanimation|startanimation. - [NEW] Adds Dutch Language support. + [NEW] Action: RotateBy supports 3D rotations + [NEW] Bindings: Using python to automatically generate script bindings + [NEW] Bindings: Added JS bindings support for Linux [NEW] Console: Added 'resolution', 'projection' commands. Improved API + [NEW] Console: Added more commands: director resume|pause|stopanimation|startanimation. [NEW] Director: Displays 'Vertices drawn' in the stats. Useful to measure performance. - [NEW] Using python to automatically generate script bindings codes. - [NEW] Linux javascript bindings support. - + [NEW] Node: Added set/get Position3D() and set/get Rotation3D() + Node: Calculates rotation X and Y correctly. + Node: set/get VertexZ() -> set/get PositionZ() + Node: setRotationX() -> setRotationSkewX() + Node: setRotationY() -> setRotationSkewY() + [NEW] Language: Added Dutch support. + [NEW] Sprite: Added auto-culling support + [FIX] Character would not be aligned on the baseline when label using distance field. [FIX] Adds a macro to disable inserting script binding relevant codes. [FIX] Removing child from TMXLayer may cause crash. diff --git a/cocos/2d/CCDirector.cpp b/cocos/2d/CCDirector.cpp index 1261663340..d5af6ac15b 100644 --- a/cocos/2d/CCDirector.cpp +++ b/cocos/2d/CCDirector.cpp @@ -448,18 +448,19 @@ void Director::setProjection(Projection projection) // issue #1334 kmMat4PerspectiveProjection(&matrixPerspective, 60, (GLfloat)size.width/size.height, 10, zeye+size.height/2); - // kmMat4PerspectiveProjection( &matrixPerspective, 60, (GLfloat)size.width/size.height, 0.1f, 1500); +// kmMat4PerspectiveProjection( &matrixPerspective, 60, (GLfloat)size.width/size.height, 0.1f, 1500); kmGLMultMatrix(&matrixPerspective); - kmGLMatrixMode(KM_GL_MODELVIEW); - kmGLLoadIdentity(); kmVec3 eye, center, up; kmVec3Fill(&eye, size.width/2, size.height/2, zeye); kmVec3Fill(¢er, size.width/2, size.height/2, 0.0f); kmVec3Fill(&up, 0.0f, 1.0f, 0.0f); kmMat4LookAt(&matrixLookup, &eye, ¢er, &up); kmGLMultMatrix(&matrixLookup); + + kmGLMatrixMode(KM_GL_MODELVIEW); + kmGLLoadIdentity(); break; } diff --git a/cocos/2d/CCNode.h b/cocos/2d/CCNode.h index 55df576f1c..baa9591fdc 100644 --- a/cocos/2d/CCNode.h +++ b/cocos/2d/CCNode.h @@ -352,8 +352,15 @@ public: virtual void setPositionY(float y); virtual float getPositionY(void) const; + /** + * Sets the X, Y, and Z axis position + */ virtual void setPosition3D(const Vertex3F& position); + /** + * returns the X, Y and Z axis position + */ virtual Vertex3F getPosition3D() const; + /** * Sets the 'z' axis in the position. It is the OpenGL Z vertex value. * @@ -391,13 +398,16 @@ public: /** * Changes the X skew angle of the node in degrees. * + * The difference between `setRotationalSkew()` and `setSkew()` is that the first one simulate Flash's skew functionality + * while the second one uses the real skew funciton. + * * This angle describes the shear distortion in the X direction. * Thus, it is the angle between the Y axis and the left edge of the shape * The default skewX angle is 0. Positive values distort the node in a CW direction. * - * @param fSkewX The X skew angle of the node in degrees. + * @param skewX The X skew angle of the node in degrees. */ - virtual void setSkewX(float fSkewX); + virtual void setSkewX(float skewX); /** * Returns the X skew angle of the node in degrees. * @@ -411,13 +421,16 @@ public: /** * Changes the Y skew angle of the node in degrees. * + * The difference between `setRotationalSkew()` and `setSkew()` is that the first one simulate Flash's skew functionality + * while the second one uses the real skew funciton. + * * This angle describes the shear distortion in the Y direction. * Thus, it is the angle between the X axis and the bottom edge of the shape * The default skewY angle is 0. Positive values distort the node in a CCW direction. * - * @param fSkewY The Y skew angle of the node in degrees. + * @param skewY The Y skew angle of the node in degrees. */ - virtual void setSkewY(float fSkewY); + virtual void setSkewY(float skewY); /** * Returns the Y skew angle of the node in degrees. * @@ -514,12 +527,22 @@ public: */ virtual float getRotation() const; + /** + * Sets the X, Y and Z axis rotation + * Useful for 3d rotations + */ virtual void setRotation3D(const Vertex3F& rotation); + /** + * returns the X, Y and Z axis rotation + */ virtual Vertex3F getRotation3D() const; /** * Sets the X rotation (angle) of the node in degrees which performs a horizontal rotational skew. * + * The difference between setRotationalSkew() and setSkew() is that the first one simulate Flash's skew functionality + * while the second one uses the real skew funciton. + * * 0 is the default rotation angle. * Positive values rotate node clockwise, and negative values for anti-clockwise. * @@ -541,6 +564,9 @@ public: /** * Sets the Y rotation (angle) of the node in degrees which performs a vertical rotational skew. * + * The difference between setRotationalSkew() and setSkew() is that the first one simulate Flash's skew functionality + * while the second one uses the real skew funciton. + * * 0 is the default rotation angle. * Positive values rotate node clockwise, and negative values for anti-clockwise. * diff --git a/cocos/2d/CCSprite.cpp b/cocos/2d/CCSprite.cpp index 7cc7c47d11..181a60b0ca 100644 --- a/cocos/2d/CCSprite.cpp +++ b/cocos/2d/CCSprite.cpp @@ -599,7 +599,7 @@ void Sprite::updateTransform(void) Point( _quad.tr.vertices.x, _quad.tr.vertices.y ), Point( _quad.tl.vertices.x, _quad.tl.vertices.y ), }; - ccDrawPoly(vertices, 4, true); + DrawPrimitives::drawPoly(vertices, 4, true); #endif // CC_SPRITE_DEBUG_DRAW } @@ -618,19 +618,27 @@ void Sprite::draw(void) bool Sprite::culling() const { Size s = Director::getInstance()->getWinSize(); - kmVec3 v3 = {_contentSize.width*0.5f, _contentSize.height*0.5f, 0}; - kmVec3Transform(&v3, &v3, &_modelViewTransform); + float hcsx = _contentSize.width * 0.5f; + float hcsy = _contentSize.height * 0.5f; - float cshw = _contentSize.width * fmaxf(fabsf(_modelViewTransform.mat[0] + _modelViewTransform.mat[4]), fabsf(_modelViewTransform.mat[0] - _modelViewTransform.mat[4])); - float cshh = _contentSize.height * fmaxf(fabsf(_modelViewTransform.mat[1] + _modelViewTransform.mat[5]), fabsf(_modelViewTransform.mat[1] - _modelViewTransform.mat[5])); + // convert to world coordinates + float x = hcsx * _modelViewTransform.mat[0] + hcsy * _modelViewTransform.mat[4] + _modelViewTransform.mat[12]; + float y = hcsx * _modelViewTransform.mat[1] + hcsy * _modelViewTransform.mat[5] + _modelViewTransform.mat[13]; - return ( (fabsf(v3.x)-cshw) < s.width/2 && (fabsf(v3.y)-cshh) < s.height/2); + // center of screen is (0,0) + x -= s.width/2; + y -= s.height/2; + + // convert content size to world coordinates + float wchw = hcsx * fmaxf(fabsf(_modelViewTransform.mat[0] + _modelViewTransform.mat[4]), fabsf(_modelViewTransform.mat[0] - _modelViewTransform.mat[4])); + float wchh = hcsy * fmaxf(fabsf(_modelViewTransform.mat[1] + _modelViewTransform.mat[5]), fabsf(_modelViewTransform.mat[1] - _modelViewTransform.mat[5])); + + float tmpx = (fabsf(x)-wchw); + float tmpy = (fabsf(y)-wchh); + return (tmpx < s.width/2 && tmpy < s.height/2); } - - // Node overrides - void Sprite::addChild(Node *child, int zOrder, int tag) { CCASSERT(child != nullptr, "Argument must be non-nullptr"); @@ -796,15 +804,15 @@ void Sprite::setRotation(float rotation) SET_DIRTY_RECURSIVELY(); } -void Sprite::setRotationX(float fRotationX) +void Sprite::setRotationSkewX(float fRotationX) { - Node::setRotationX(fRotationX); + Node::setRotationSkewX(fRotationX); SET_DIRTY_RECURSIVELY(); } -void Sprite::setRotationY(float fRotationY) +void Sprite::setRotationSkewY(float fRotationY) { - Node::setRotationY(fRotationY); + Node::setRotationSkewY(fRotationY); SET_DIRTY_RECURSIVELY(); } @@ -844,9 +852,9 @@ void Sprite::setScale(float scaleX, float scaleY) SET_DIRTY_RECURSIVELY(); } -void Sprite::setVertexZ(float fVertexZ) +void Sprite::setPositionZ(float fVertexZ) { - Node::setVertexZ(fVertexZ); + Node::setPositionZ(fVertexZ); SET_DIRTY_RECURSIVELY(); } diff --git a/cocos/2d/CCSprite.h b/cocos/2d/CCSprite.h index 5510a00d46..bb77d131dc 100644 --- a/cocos/2d/CCSprite.h +++ b/cocos/2d/CCSprite.h @@ -408,8 +408,8 @@ public: virtual void setPosition(const Point& pos) override; virtual void setPosition(float x, float y) override; virtual void setRotation(float rotation) override; - virtual void setRotationX(float rotationX) override; - virtual void setRotationY(float rotationY) override; + virtual void setRotationSkewX(float rotationX) override; + virtual void setRotationSkewY(float rotationY) override; virtual void setSkewX(float sx) override; virtual void setSkewY(float sy) override; virtual void removeChild(Node* child, bool cleanup) override; @@ -419,7 +419,7 @@ public: virtual void addChild(Node *child, int zOrder, int tag) override; virtual void sortAllChildren() override; virtual void setScale(float scale) override; - virtual void setVertexZ(float vertexZ) override; + virtual void setPositionZ(float positionZ) override; virtual void setAnchorPoint(const Point& anchor) override; virtual void ignoreAnchorPointForPosition(bool value) override; virtual void setVisible(bool bVisible) override;