add matrix stack in director(will replace kmGLfunctions)

This commit is contained in:
Huabing.Xu 2014-04-02 16:36:11 +08:00
parent 45b0ba3871
commit 106a0da87b
2 changed files with 139 additions and 5 deletions

View File

@ -65,6 +65,7 @@ THE SOFTWARE.
#include "kazmath/kazmath.h" #include "kazmath/kazmath.h"
#include "kazmath/GL/matrix.h" #include "kazmath/GL/matrix.h"
#include "CCMath.h"
/** /**
Position of the FPS Position of the FPS
@ -156,6 +157,7 @@ bool Director::init(void)
//init TextureCache //init TextureCache
initTextureCache(); initTextureCache();
initMatrixStack();
_renderer = new Renderer; _renderer = new Renderer;
@ -280,20 +282,22 @@ void Director::drawScene()
kmGLPushMatrix(); kmGLPushMatrix();
// global identity matrix is needed... come on kazmath! // global identity matrix is needed... come on kazmath!
kmMat4 identity; Matrix identity = Matrix::identity();
kmMat4Identity(&identity);
kmMat4 identity2;
kmMat4Fill(&identity2, identity.m);
// draw the scene // draw the scene
if (_runningScene) if (_runningScene)
{ {
_runningScene->visit(_renderer, identity, false); _runningScene->visit(_renderer, identity2, false);
_eventDispatcher->dispatchEvent(_eventAfterVisit); _eventDispatcher->dispatchEvent(_eventAfterVisit);
} }
// draw the notifications node // draw the notifications node
if (_notificationNode) if (_notificationNode)
{ {
_notificationNode->visit(_renderer, identity, false); _notificationNode->visit(_renderer, identity2, false);
} }
if (_displayStats) if (_displayStats)
@ -427,6 +431,117 @@ void Director::setNextDeltaTimeZero(bool bNextDeltaTimeZero)
_nextDeltaTimeZero = bNextDeltaTimeZero; _nextDeltaTimeZero = bNextDeltaTimeZero;
} }
void Director::initMatrixStack()
{
kmMat4 identity;
kmMat4Identity(&identity);
_modelViewMatrixStack.push(identity);
_projectionMatrixStack.push(identity);
_textureMatrixStack.push(identity);
}
void Director::popMatrix(MATRIX_STACK_TYPE type)
{
if(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW == type)
{
_modelViewMatrixStack.pop();
}
else if(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION == type)
{
_projectionMatrixStack.pop();
}
else if(MATRIX_STACK_TYPE::MATRIX_STACK_TEXTURE == type)
{
_textureMatrixStack.pop();
}
else
{
CCASSERT(false, "unknow matrix stack type");
}
}
void Director::loadMatrix(MATRIX_STACK_TYPE type, const kmMat4& mat)
{
if(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW == type)
{
_modelViewMatrixStack.top() = mat;
}
else if(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION == type)
{
_projectionMatrixStack.top() = mat;
}
else if(MATRIX_STACK_TYPE::MATRIX_STACK_TEXTURE == type)
{
_textureMatrixStack.top() = mat;
}
else
{
CCASSERT(false, "unknow matrix stack type");
}
}
void Director::multiplyMatrix(MATRIX_STACK_TYPE type, const kmMat4& mat)
{
if(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW == type)
{
kmMat4Multiply(&_modelViewMatrixStack.top(), &_modelViewMatrixStack.top(), &mat);
}
else if(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION == type)
{
kmMat4Multiply(&_projectionMatrixStack.top(), &_projectionMatrixStack.top(), &mat);
}
else if(MATRIX_STACK_TYPE::MATRIX_STACK_TEXTURE == type)
{
kmMat4Multiply(&_textureMatrixStack.top(), &_textureMatrixStack.top(), &mat);
}
else
{
CCASSERT(false, "unknow matrix stack type");
}
}
void Director::pushMatrix(MATRIX_STACK_TYPE type)
{
if(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW == type)
{
_modelViewMatrixStack.push(_modelViewMatrixStack.top());
}
else if(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION == type)
{
_projectionMatrixStack.push(_projectionMatrixStack.top());
}
else if(MATRIX_STACK_TYPE::MATRIX_STACK_TEXTURE == type)
{
_textureMatrixStack.push(_textureMatrixStack.top());
}
else
{
CCASSERT(false, "unknow matrix stack type");
}
}
kmMat4 Director::getMatrix(MATRIX_STACK_TYPE type)
{
if(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW == type)
{
return _modelViewMatrixStack.top();
}
else if(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION == type)
{
return _projectionMatrixStack.top();
}
else if(MATRIX_STACK_TYPE::MATRIX_STACK_TEXTURE == type)
{
return _textureMatrixStack.top();
}
else
{
CCASSERT(false, "unknow matrix stack type, will return modelview matrix instead");
return _modelViewMatrixStack.top();
}
}
void Director::setProjection(Projection projection) void Director::setProjection(Projection projection)
{ {
Size size = _winSizeInPoints; Size size = _winSizeInPoints;

View File

@ -37,7 +37,7 @@ THE SOFTWARE.
#include "CCGL.h" #include "CCGL.h"
#include "CCLabelAtlas.h" #include "CCLabelAtlas.h"
#include "kazmath/mat4.h" #include "kazmath/mat4.h"
#include <stack>
NS_CC_BEGIN NS_CC_BEGIN
@ -86,6 +86,25 @@ and when to execute the Scenes.
*/ */
class CC_DLL Director : public Ref class CC_DLL Director : public Ref
{ {
public:
enum class MATRIX_STACK_TYPE
{
MATRIX_STACK_MODELVIEW,
MATRIX_STACK_PROJECTION,
MATRIX_STACK_TEXTURE
};
private:
std::stack<kmMat4> _modelViewMatrixStack;
std::stack<kmMat4> _projectionMatrixStack;
std::stack<kmMat4> _textureMatrixStack;
protected:
void initMatrixStack();
public:
void pushMatrix(MATRIX_STACK_TYPE type);
void popMatrix(MATRIX_STACK_TYPE type);
void loadMatrix(MATRIX_STACK_TYPE type, const kmMat4& mat);
void multiplyMatrix(MATRIX_STACK_TYPE type, const kmMat4& mat);
kmMat4 getMatrix(MATRIX_STACK_TYPE type);
public: public:
static const char *EVENT_PROJECTION_CHANGED; static const char *EVENT_PROJECTION_CHANGED;
static const char* EVENT_AFTER_UPDATE; static const char* EVENT_AFTER_UPDATE;