diff --git a/build/cocos2d_libs.xcodeproj/project.pbxproj.REMOVED.git-id b/build/cocos2d_libs.xcodeproj/project.pbxproj.REMOVED.git-id index bc19be45cc..b50334c51a 100644 --- a/build/cocos2d_libs.xcodeproj/project.pbxproj.REMOVED.git-id +++ b/build/cocos2d_libs.xcodeproj/project.pbxproj.REMOVED.git-id @@ -1 +1 @@ -89e13afae7209dd4e3bc13d195b83c0be4cf85ae \ No newline at end of file +897a831f2ce0489f68be53deb82150c0c7d86ad9 \ No newline at end of file diff --git a/cocos/2d/CCDirector.cpp b/cocos/2d/CCDirector.cpp index 535ccbb1a0..c586407c46 100644 --- a/cocos/2d/CCDirector.cpp +++ b/cocos/2d/CCDirector.cpp @@ -62,7 +62,7 @@ THE SOFTWARE. #include "CCEventDispatcher.h" #include "CCFontFreeType.h" #include "Renderer.h" - +#include "renderer/Frustum.h" /** Position of the FPS @@ -136,7 +136,9 @@ bool Director::init(void) _winSizeInPoints = Size::ZERO; _openGLView = nullptr; - + + _cullingFrustum = new Frustum(); + _contentScaleFactor = 1.0f; // scheduler @@ -260,7 +262,17 @@ void Director::drawScene() } kmGLPushMatrix(); - + + //construct the frustum + { + kmMat4 view; + kmMat4 projection; + kmGLGetMatrix(KM_GL_PROJECTION, &projection); + kmGLGetMatrix(KM_GL_MODELVIEW, &view); + + _cullingFrustum->setupFromMatrix(view, projection); + } + // draw the scene if (_runningScene) { @@ -713,6 +725,7 @@ void Director::purgeDirector() CC_SAFE_RELEASE_NULL(_FPSLabel); CC_SAFE_RELEASE_NULL(_SPFLabel); CC_SAFE_RELEASE_NULL(_drawsLabel); + CC_SAFE_DELETE(_cullingFrustum); // purge bitmap cache LabelBMFont::purgeCachedData(); diff --git a/cocos/2d/CCDirector.h b/cocos/2d/CCDirector.h index 880117a2a3..89ea8197cc 100644 --- a/cocos/2d/CCDirector.h +++ b/cocos/2d/CCDirector.h @@ -55,6 +55,7 @@ class Scheduler; class ActionManager; class EventDispatcher; class TextureCache; +class Frustum; /** @brief Class that creates and handles the main Window and manages how @@ -330,6 +331,12 @@ public: */ void setContentScaleFactor(float scaleFactor); float getContentScaleFactor() const; + + /** + Get the Culling Frustum + */ + + Frustum* getFrustum() const { return _cullingFrustum; } public: /** Gets the Scheduler associated with this director @@ -434,6 +441,8 @@ protected: unsigned int _totalFrames; unsigned int _frames; float _secondsPerFrame; + + Frustum* _cullingFrustum; /* The running scene */ Scene *_runningScene; diff --git a/cocos/2d/CCNewSprite.cpp b/cocos/2d/CCNewSprite.cpp index f9c201464d..9d5570076e 100644 --- a/cocos/2d/CCNewSprite.cpp +++ b/cocos/2d/CCNewSprite.cpp @@ -11,6 +11,8 @@ #include "Renderer.h" #include "QuadCommand.h" #include "CCMenuItem.h" +#include "Frustum.h" +#include "CCDirector.h" NS_CC_BEGIN @@ -132,10 +134,46 @@ void NewSprite::updateQuadVertices() void NewSprite::draw(void) { updateQuadVertices(); + if(false == culling()) + { + static int count =0; + CCLOG("culling Sprite New to not visible %d ",++count); + return; + } + //TODO implement z order QuadCommand* renderCommand = new QuadCommand(0, _vertexZ, _texture->getName(), _shaderProgram, _blendFunc, &_quad, 1); Renderer::getInstance()->addCommand(renderCommand); } +bool NewSprite::culling() const +{ + Frustum* frustum = Director::getInstance()->getFrustum(); + AffineTransform worldTM = getNodeToWorldTransform(); + //generate aabb + Point lowLeft(0,0); + Point topRight = lowLeft + Point(getContentSize()); + Point lowRight(topRight.x,0); + Point topLeft(0, topRight.y); + + lowLeft = PointApplyAffineTransform(lowLeft,worldTM); + lowRight = PointApplyAffineTransform(lowRight,worldTM); + topRight = PointApplyAffineTransform(topRight,worldTM); + topLeft = PointApplyAffineTransform(topLeft,worldTM); + + kmVec3 point = {lowLeft.x, lowLeft.y, _vertexZ}; + + AABB aabb(point,point); + point = {lowRight.x, lowRight.y, _vertexZ}; + aabb.expand(point); + point = {topLeft.x, topLeft.y, _vertexZ}; + aabb.expand(point); + point = {topRight.x, topRight.y, _vertexZ}; + aabb.expand(point); + + return Frustum::IntersectResult::OUTSIDE !=frustum->intersectAABB(aabb); +} + + NS_CC_END \ No newline at end of file diff --git a/cocos/2d/CCNewSprite.h b/cocos/2d/CCNewSprite.h index 752d4973f4..8d02b556e4 100644 --- a/cocos/2d/CCNewSprite.h +++ b/cocos/2d/CCNewSprite.h @@ -25,10 +25,12 @@ public: ~NewSprite(); virtual bool initWithTexture(Texture2D *texture, const Rect& rect, bool rotated); - + virtual void updateQuadVertices(); virtual void draw(void) override; + bool culling() const; + protected: };