add Frustum culling to new Sprite

This commit is contained in:
Huabing.Xu 2013-11-27 10:35:12 +08:00
parent 80da1dac3f
commit 3da196ea43
5 changed files with 67 additions and 5 deletions

View File

@ -1 +1 @@
89e13afae7209dd4e3bc13d195b83c0be4cf85ae
897a831f2ce0489f68be53deb82150c0c7d86ad9

View File

@ -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();

View File

@ -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;

View File

@ -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

View File

@ -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:
};