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/GL/matrix.h"
#include "CCMath.h"
/**
Position of the FPS
@ -156,6 +157,7 @@ bool Director::init(void)
//init TextureCache
initTextureCache();
initMatrixStack();
_renderer = new Renderer;
@ -280,20 +282,22 @@ void Director::drawScene()
kmGLPushMatrix();
// global identity matrix is needed... come on kazmath!
kmMat4 identity;
kmMat4Identity(&identity);
Matrix identity = Matrix::identity();
kmMat4 identity2;
kmMat4Fill(&identity2, identity.m);
// draw the scene
if (_runningScene)
{
_runningScene->visit(_renderer, identity, false);
_runningScene->visit(_renderer, identity2, false);
_eventDispatcher->dispatchEvent(_eventAfterVisit);
}
// draw the notifications node
if (_notificationNode)
{
_notificationNode->visit(_renderer, identity, false);
_notificationNode->visit(_renderer, identity2, false);
}
if (_displayStats)
@ -426,6 +430,117 @@ void Director::setNextDeltaTimeZero(bool 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)
{

View File

@ -37,7 +37,7 @@ THE SOFTWARE.
#include "CCGL.h"
#include "CCLabelAtlas.h"
#include "kazmath/mat4.h"
#include <stack>
NS_CC_BEGIN
@ -86,6 +86,25 @@ and when to execute the Scenes.
*/
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:
static const char *EVENT_PROJECTION_CHANGED;
static const char* EVENT_AFTER_UPDATE;