2010-12-24 15:53:51 +08:00
|
|
|
/****************************************************************************
|
2011-03-19 14:45:51 +08:00
|
|
|
Copyright (c) 2010-2011 cocos2d-x.org
|
|
|
|
Copyright (c) 2009 On-Core
|
2010-12-24 15:53:51 +08:00
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
#include "ccMacros.h"
|
2010-12-30 10:28:13 +08:00
|
|
|
#include "effects/CCGrid.h"
|
2010-12-24 15:53:51 +08:00
|
|
|
#include "CCDirector.h"
|
2010-12-30 10:28:13 +08:00
|
|
|
#include "effects/CCGrabber.h"
|
|
|
|
#include "support/ccUtils.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
#include "CCGLProgram.h"
|
|
|
|
#include "CCShaderCache.h"
|
2012-03-15 10:42:22 +08:00
|
|
|
#include "ccGLStateCache.h"
|
2010-12-24 15:53:51 +08:00
|
|
|
#include "CCGL.h"
|
2011-03-07 17:11:57 +08:00
|
|
|
#include "CCPointExtension.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
#include "support/TransformUtils.h"
|
|
|
|
#include "kazmath/kazmath.h"
|
2012-03-14 14:55:17 +08:00
|
|
|
#include "kazmath/GL/matrix.h"
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
// implementation of CCGridBase
|
|
|
|
|
2012-06-14 17:18:05 +08:00
|
|
|
CCGridBase* CCGridBase::gridWithSize(const ccGridSize& gridSize)
|
|
|
|
{
|
|
|
|
return CCGridBase::create(gridSize);
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
|
|
|
CCGridBase* CCGridBase::create(const ccGridSize& gridSize)
|
2010-12-24 15:53:51 +08:00
|
|
|
{
|
2012-04-18 18:43:45 +08:00
|
|
|
CCGridBase *pGridBase = new CCGridBase();
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if (pGridBase)
|
|
|
|
{
|
2012-04-18 18:43:45 +08:00
|
|
|
if (pGridBase->initWithSize(gridSize))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
pGridBase->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE_NULL(pGridBase);
|
|
|
|
}
|
|
|
|
}
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return pGridBase;
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-06-14 17:18:05 +08:00
|
|
|
CCGridBase* CCGridBase::gridWithSize(const ccGridSize& gridSize, CCTexture2D *texture, bool flipped)
|
|
|
|
{
|
|
|
|
return CCGridBase::create(gridSize, texture, flipped);
|
|
|
|
}
|
2012-06-14 15:13:16 +08:00
|
|
|
|
|
|
|
CCGridBase* CCGridBase::create(const ccGridSize& gridSize, CCTexture2D *texture, bool flipped)
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCGridBase *pGridBase = new CCGridBase();
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if (pGridBase)
|
|
|
|
{
|
|
|
|
if (pGridBase->initWithSize(gridSize, texture, flipped))
|
|
|
|
{
|
|
|
|
pGridBase->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE_NULL(pGridBase);
|
|
|
|
}
|
|
|
|
}
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return pGridBase;
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
bool CCGridBase::initWithSize(const ccGridSize& gridSize, CCTexture2D *pTexture, bool bFlipped)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
bool bRet = true;
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
m_bActive = false;
|
|
|
|
m_nReuseGrid = 0;
|
|
|
|
m_sGridSize = gridSize;
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
m_pTexture = pTexture;
|
|
|
|
CC_SAFE_RETAIN(m_pTexture);
|
|
|
|
m_bIsTextureFlipped = bFlipped;
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-06-11 11:53:17 +08:00
|
|
|
const CCSize& texSize = m_pTexture->getContentSize();
|
2012-04-19 14:35:52 +08:00
|
|
|
m_obStep.x = texSize.width / m_sGridSize.x;
|
|
|
|
m_obStep.y = texSize.height / m_sGridSize.y;
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
m_pGrabber = new CCGrabber();
|
|
|
|
if (m_pGrabber)
|
|
|
|
{
|
|
|
|
m_pGrabber->grab(m_pTexture);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-04-18 18:43:45 +08:00
|
|
|
bRet = false;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
m_pShaderProgram = CCShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionTexture);
|
|
|
|
calculateVertexPoints();
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return bRet;
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
bool CCGridBase::initWithSize(const ccGridSize& gridSize)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCDirector *pDirector = CCDirector::sharedDirector();
|
|
|
|
CCSize s = pDirector->getWinSizeInPixels();
|
|
|
|
|
|
|
|
unsigned long POTWide = ccNextPOT((unsigned int)s.width);
|
|
|
|
unsigned long POTHigh = ccNextPOT((unsigned int)s.height);
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// we only use rgba8888
|
|
|
|
CCTexture2DPixelFormat format = kCCTexture2DPixelFormat_RGBA8888;
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
void *data = calloc((int)(POTWide * POTHigh * 4), 1);
|
|
|
|
if (! data)
|
|
|
|
{
|
|
|
|
CCLOG("cocos2d: CCGrid: not enough memory.");
|
|
|
|
this->release();
|
|
|
|
return false;
|
|
|
|
}
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
CCTexture2D *pTexture = new CCTexture2D();
|
|
|
|
pTexture->initWithData(data, format, POTWide, POTHigh, s);
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
free(data);
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if (! pTexture)
|
|
|
|
{
|
|
|
|
CCLOG("cocos2d: CCGrid: error creating texture");
|
|
|
|
delete this;
|
|
|
|
return false;
|
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
initWithSize(gridSize, pTexture, false);
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
pTexture->release();
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CCGridBase::~CCGridBase(void)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCLOGINFO("cocos2d: deallocing %p", this);
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
//TODO: ? why 2.0 comments this line setActive(false);
|
|
|
|
CC_SAFE_RELEASE(m_pTexture);
|
|
|
|
CC_SAFE_RELEASE(m_pGrabber);
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
// properties
|
|
|
|
void CCGridBase::setActive(bool bActive)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
m_bActive = bActive;
|
|
|
|
if (! bActive)
|
|
|
|
{
|
|
|
|
CCDirector *pDirector = CCDirector::sharedDirector();
|
|
|
|
ccDirectorProjection proj = pDirector->getProjection();
|
|
|
|
pDirector->setProjection(proj);
|
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
void CCGridBase::setIsTextureFlipped(bool bFlipped)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
if (m_bIsTextureFlipped != bFlipped)
|
|
|
|
{
|
|
|
|
m_bIsTextureFlipped = bFlipped;
|
|
|
|
calculateVertexPoints();
|
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
void CCGridBase::set2DProjection()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCDirector *director = CCDirector::sharedDirector();
|
|
|
|
|
|
|
|
CCSize size = director->getWinSizeInPixels();
|
|
|
|
|
2012-04-25 16:18:04 +08:00
|
|
|
glViewport(0, 0, (GLsizei)(size.width * CC_CONTENT_SCALE_FACTOR()), (GLsizei)(size.height * CC_CONTENT_SCALE_FACTOR()) );
|
2012-04-19 14:35:52 +08:00
|
|
|
kmGLMatrixMode(KM_GL_PROJECTION);
|
|
|
|
kmGLLoadIdentity();
|
|
|
|
|
|
|
|
kmMat4 orthoMatrix;
|
|
|
|
kmMat4OrthographicProjection(&orthoMatrix, 0, size.width * CC_CONTENT_SCALE_FACTOR(), 0, size.height * CC_CONTENT_SCALE_FACTOR(), -1, 1);
|
|
|
|
kmGLMultMatrix( &orthoMatrix );
|
|
|
|
|
|
|
|
kmGLMatrixMode(KM_GL_MODELVIEW);
|
|
|
|
kmGLLoadIdentity();
|
|
|
|
|
|
|
|
|
|
|
|
ccSetProjectionMatrixDirty();
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
void CCGridBase::beforeDraw(void)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
// save projection
|
|
|
|
CCDirector *director = CCDirector::sharedDirector();
|
|
|
|
m_directorProjection = director->getProjection();
|
|
|
|
|
|
|
|
// 2d projection
|
|
|
|
// [director setProjection:kCCDirectorProjection2D];
|
|
|
|
set2DProjection();
|
|
|
|
m_pGrabber->beforeRender(m_pTexture);
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
void CCGridBase::afterDraw(cocos2d::CCNode *pTarget)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
m_pGrabber->afterRender(m_pTexture);
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// restore projection
|
|
|
|
CCDirector *director = CCDirector::sharedDirector();
|
|
|
|
director->setProjection(m_directorProjection);
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if (pTarget->getCamera()->getDirty())
|
|
|
|
{
|
|
|
|
const CCPoint& offset = pTarget->getAnchorPointInPoints();
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
//
|
|
|
|
// XXX: Camera should be applied in the AnchorPoint
|
|
|
|
//
|
|
|
|
kmGLTranslatef(offset.x, offset.y, 0);
|
|
|
|
pTarget->getCamera()->locate();
|
|
|
|
kmGLTranslatef(-offset.x, -offset.y, 0);
|
|
|
|
}
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
ccGLBindTexture2D(m_pTexture->getName());
|
2012-04-18 18:43:45 +08:00
|
|
|
|
|
|
|
// restore projection for default FBO .fixed bug #543 #544
|
2012-03-14 14:55:17 +08:00
|
|
|
//TODO: CCDirector::sharedDirector()->setProjection(CCDirector::sharedDirector()->getProjection());
|
|
|
|
//TODO: CCDirector::sharedDirector()->applyOrientation();
|
2012-04-19 14:35:52 +08:00
|
|
|
blit();
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
void CCGridBase::blit(void)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCAssert(0, "");
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
void CCGridBase::reuse(void)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCAssert(0, "");
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
void CCGridBase::calculateVertexPoints(void)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCAssert(0, "");
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
// implementation of CCGrid3D
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
CCGrid3D* CCGrid3D::gridWithSize(const ccGridSize& gridSize, CCTexture2D *pTexture, bool bFlipped)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCGrid3D *pRet= new CCGrid3D();
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if (pRet)
|
|
|
|
{
|
|
|
|
if (pRet->initWithSize(gridSize, pTexture, bFlipped))
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
delete pRet;
|
|
|
|
pRet = NULL;
|
|
|
|
}
|
|
|
|
}
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return pRet;
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CCGrid3D* CCGrid3D::gridWithSize(const ccGridSize& gridSize)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCGrid3D *pRet= new CCGrid3D();
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if (pRet)
|
|
|
|
{
|
|
|
|
if (pRet->initWithSize(gridSize))
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
delete pRet;
|
|
|
|
pRet = NULL;
|
|
|
|
}
|
|
|
|
}
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return pRet;
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2012-03-14 18:11:25 +08:00
|
|
|
|
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
CCGrid3D::CCGrid3D()
|
2012-04-19 14:35:52 +08:00
|
|
|
: m_pTexCoordinates(NULL)
|
|
|
|
, m_pVertices(NULL)
|
|
|
|
, m_pOriginalVertices(NULL)
|
|
|
|
, m_pIndices(NULL)
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2012-03-14 18:11:25 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
CCGrid3D::~CCGrid3D(void)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CC_SAFE_FREE(m_pTexCoordinates);
|
2012-04-18 18:43:45 +08:00
|
|
|
CC_SAFE_FREE(m_pVertices);
|
2012-04-19 14:35:52 +08:00
|
|
|
CC_SAFE_FREE(m_pIndices);
|
|
|
|
CC_SAFE_FREE(m_pOriginalVertices);
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCGrid3D::blit(void)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
int n = m_sGridSize.x * m_sGridSize.y;
|
|
|
|
|
|
|
|
ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position | kCCVertexAttribFlag_TexCoords );
|
|
|
|
m_pShaderProgram->use();
|
|
|
|
m_pShaderProgram->setUniformForModelViewProjectionMatrix();;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Attributes
|
|
|
|
//
|
|
|
|
|
|
|
|
// position
|
|
|
|
glVertexAttribPointer(kCCVertexAttrib_Position, 3, GL_FLOAT, GL_FALSE, 0, m_pVertices);
|
|
|
|
|
|
|
|
// texCoods
|
|
|
|
glVertexAttribPointer(kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, 0, m_pTexCoordinates);
|
|
|
|
|
|
|
|
glDrawElements(GL_TRIANGLES, (GLsizei) n*6, GL_UNSIGNED_SHORT, m_pIndices);
|
|
|
|
CC_INCREMENT_GL_DRAWS(1);
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
void CCGrid3D::calculateVertexPoints(void)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
float width = (float)m_pTexture->getPixelsWide();
|
|
|
|
float height = (float)m_pTexture->getPixelsHigh();
|
|
|
|
float imageH = m_pTexture->getContentSizeInPixels().height;
|
|
|
|
|
|
|
|
int x, y, i;
|
|
|
|
CC_SAFE_FREE(m_pVertices);
|
|
|
|
CC_SAFE_FREE(m_pOriginalVertices);
|
|
|
|
CC_SAFE_FREE(m_pTexCoordinates);
|
|
|
|
CC_SAFE_FREE(m_pIndices);
|
|
|
|
|
|
|
|
unsigned int numOfPoints = (m_sGridSize.x+1) * (m_sGridSize.y+1);
|
|
|
|
|
|
|
|
m_pVertices = malloc(numOfPoints * sizeof(ccVertex3F));
|
|
|
|
m_pOriginalVertices = malloc(numOfPoints * sizeof(ccVertex3F));
|
|
|
|
m_pTexCoordinates = malloc(numOfPoints * sizeof(ccVertex2F));
|
|
|
|
m_pIndices = (GLushort*)malloc(m_sGridSize.x * m_sGridSize.y * sizeof(GLushort) * 6);
|
|
|
|
|
|
|
|
GLfloat *vertArray = (GLfloat*)m_pVertices;
|
|
|
|
GLfloat *texArray = (GLfloat*)m_pTexCoordinates;
|
|
|
|
GLushort *idxArray = m_pIndices;
|
|
|
|
|
|
|
|
for (x = 0; x < m_sGridSize.x; ++x)
|
|
|
|
{
|
|
|
|
for (y = 0; y < m_sGridSize.y; ++y)
|
|
|
|
{
|
|
|
|
int idx = (y * m_sGridSize.x) + x;
|
|
|
|
|
|
|
|
GLfloat x1 = x * m_obStep.x;
|
|
|
|
GLfloat x2 = x1 + m_obStep.x;
|
|
|
|
GLfloat y1 = y * m_obStep.y;
|
|
|
|
GLfloat y2= y1 + m_obStep.y;
|
|
|
|
|
|
|
|
GLushort a = (GLushort)(x * (m_sGridSize.y + 1) + y);
|
|
|
|
GLushort b = (GLushort)((x + 1) * (m_sGridSize.y + 1) + y);
|
|
|
|
GLushort c = (GLushort)((x + 1) * (m_sGridSize.y + 1) + (y + 1));
|
|
|
|
GLushort d = (GLushort)(x * (m_sGridSize.y + 1) + (y + 1));
|
|
|
|
|
|
|
|
GLushort tempidx[6] = {a, b, d, b, c, d};
|
|
|
|
|
|
|
|
memcpy(&idxArray[6*idx], tempidx, 6*sizeof(GLushort));
|
|
|
|
|
|
|
|
int l1[4] = {a*3, b*3, c*3, d*3};
|
|
|
|
ccVertex3F e = {x1, y1, 0};
|
|
|
|
ccVertex3F f = {x2, y1, 0};
|
|
|
|
ccVertex3F g = {x2, y2, 0};
|
|
|
|
ccVertex3F h = {x1, y2, 0};
|
|
|
|
|
|
|
|
ccVertex3F l2[4] = {e, f, g, h};
|
|
|
|
|
|
|
|
int tex1[4] = {a*2, b*2, c*2, d*2};
|
|
|
|
CCPoint tex2[4] = {ccp(x1, y1), ccp(x2, y1), ccp(x2, y2), ccp(x1, y2)};
|
|
|
|
|
|
|
|
for (i = 0; i < 4; ++i)
|
|
|
|
{
|
|
|
|
vertArray[l1[i]] = l2[i].x;
|
|
|
|
vertArray[l1[i] + 1] = l2[i].y;
|
|
|
|
vertArray[l1[i] + 2] = l2[i].z;
|
|
|
|
|
|
|
|
texArray[tex1[i]] = tex2[i].x / width;
|
|
|
|
if (m_bIsTextureFlipped)
|
|
|
|
{
|
|
|
|
texArray[tex1[i] + 1] = (imageH - tex2[i].y) / height;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
texArray[tex1[i] + 1] = tex2[i].y / height;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(m_pOriginalVertices, m_pVertices, (m_sGridSize.x+1) * (m_sGridSize.y+1) * sizeof(ccVertex3F));
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
ccVertex3F CCGrid3D::vertex(const ccGridSize& pos)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
int index = (pos.x * (m_sGridSize.y+1) + pos.y) * 3;
|
|
|
|
float *vertArray = (float*)m_pVertices;
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
ccVertex3F vert = {vertArray[index], vertArray[index+1], vertArray[index+2]};
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return vert;
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
ccVertex3F CCGrid3D::originalVertex(const ccGridSize& pos)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
int index = (pos.x * (m_sGridSize.y+1) + pos.y) * 3;
|
|
|
|
float *vertArray = (float*)m_pOriginalVertices;
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
ccVertex3F vert = {vertArray[index], vertArray[index+1], vertArray[index+2]};
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return vert;
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
void CCGrid3D::setVertex(const ccGridSize& pos, const ccVertex3F& vertex)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
int index = (pos.x * (m_sGridSize.y + 1) + pos.y) * 3;
|
|
|
|
float *vertArray = (float*)m_pVertices;
|
|
|
|
vertArray[index] = vertex.x;
|
|
|
|
vertArray[index+1] = vertex.y;
|
|
|
|
vertArray[index+2] = vertex.z;
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCGrid3D::reuse(void)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
if (m_nReuseGrid > 0)
|
|
|
|
{
|
|
|
|
memcpy(m_pOriginalVertices, m_pVertices, (m_sGridSize.x+1) * (m_sGridSize.y+1) * sizeof(ccVertex3F));
|
|
|
|
--m_nReuseGrid;
|
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
// implementation of CCTiledGrid3D
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
CCTiledGrid3D::CCTiledGrid3D()
|
2012-04-19 14:35:52 +08:00
|
|
|
: m_pTexCoordinates(NULL)
|
|
|
|
, m_pVertices(NULL)
|
|
|
|
, m_pOriginalVertices(NULL)
|
|
|
|
, m_pIndices(NULL)
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2012-03-14 18:11:25 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2012-03-14 18:11:25 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
CCTiledGrid3D::~CCTiledGrid3D(void)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CC_SAFE_FREE(m_pTexCoordinates);
|
|
|
|
CC_SAFE_FREE(m_pVertices);
|
|
|
|
CC_SAFE_FREE(m_pOriginalVertices);
|
|
|
|
CC_SAFE_FREE(m_pIndices);
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
CCTiledGrid3D* CCTiledGrid3D::gridWithSize(const ccGridSize& gridSize, CCTexture2D *pTexture, bool bFlipped)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCTiledGrid3D *pRet= new CCTiledGrid3D();
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if (pRet)
|
|
|
|
{
|
|
|
|
if (pRet->initWithSize(gridSize, pTexture, bFlipped))
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
delete pRet;
|
|
|
|
pRet = NULL;
|
|
|
|
}
|
|
|
|
}
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return pRet;
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
CCTiledGrid3D* CCTiledGrid3D::gridWithSize(const ccGridSize& gridSize)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCTiledGrid3D *pRet= new CCTiledGrid3D();
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if (pRet)
|
|
|
|
{
|
|
|
|
if (pRet->initWithSize(gridSize))
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
delete pRet;
|
|
|
|
pRet = NULL;
|
|
|
|
}
|
|
|
|
}
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return pRet;
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCTiledGrid3D::blit(void)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
int n = m_sGridSize.x * m_sGridSize.y;
|
|
|
|
|
|
|
|
|
|
|
|
m_pShaderProgram->use();
|
|
|
|
m_pShaderProgram->setUniformForModelViewProjectionMatrix();
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
//
|
|
|
|
// Attributes
|
|
|
|
//
|
|
|
|
ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position | kCCVertexAttribFlag_TexCoords );
|
|
|
|
// position
|
|
|
|
glVertexAttribPointer(kCCVertexAttrib_Position, 3, GL_FLOAT, GL_FALSE, 0, m_pVertices);
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// texCoods
|
|
|
|
glVertexAttribPointer(kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, 0, m_pTexCoordinates);
|
2012-03-16 13:42:53 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
glDrawElements(GL_TRIANGLES, (GLsizei)n*6, GL_UNSIGNED_SHORT, m_pIndices);
|
|
|
|
|
|
|
|
CC_INCREMENT_GL_DRAWS(1);
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
void CCTiledGrid3D::calculateVertexPoints(void)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
float width = (float)m_pTexture->getPixelsWide();
|
|
|
|
float height = (float)m_pTexture->getPixelsHigh();
|
|
|
|
float imageH = m_pTexture->getContentSizeInPixels().height;
|
|
|
|
|
|
|
|
int numQuads = m_sGridSize.x * m_sGridSize.y;
|
|
|
|
CC_SAFE_FREE(m_pVertices);
|
|
|
|
CC_SAFE_FREE(m_pOriginalVertices);
|
|
|
|
CC_SAFE_FREE(m_pTexCoordinates);
|
|
|
|
CC_SAFE_FREE(m_pIndices);
|
|
|
|
|
|
|
|
m_pVertices = malloc(numQuads*4*sizeof(ccVertex3F));
|
|
|
|
m_pOriginalVertices = malloc(numQuads*4*sizeof(ccVertex3F));
|
|
|
|
m_pTexCoordinates = malloc(numQuads*4*sizeof(ccVertex2F));
|
|
|
|
m_pIndices = (GLushort*)malloc(numQuads*6*sizeof(GLushort));
|
|
|
|
|
|
|
|
GLfloat *vertArray = (GLfloat*)m_pVertices;
|
|
|
|
GLfloat *texArray = (GLfloat*)m_pTexCoordinates;
|
|
|
|
GLushort *idxArray = m_pIndices;
|
|
|
|
|
|
|
|
int x, y;
|
|
|
|
|
|
|
|
for( x = 0; x < m_sGridSize.x; x++ )
|
|
|
|
{
|
|
|
|
for( y = 0; y < m_sGridSize.y; y++ )
|
|
|
|
{
|
|
|
|
float x1 = x * m_obStep.x;
|
|
|
|
float x2 = x1 + m_obStep.x;
|
|
|
|
float y1 = y * m_obStep.y;
|
|
|
|
float y2 = y1 + m_obStep.y;
|
|
|
|
|
|
|
|
*vertArray++ = x1;
|
|
|
|
*vertArray++ = y1;
|
|
|
|
*vertArray++ = 0;
|
|
|
|
*vertArray++ = x2;
|
|
|
|
*vertArray++ = y1;
|
|
|
|
*vertArray++ = 0;
|
|
|
|
*vertArray++ = x1;
|
|
|
|
*vertArray++ = y2;
|
|
|
|
*vertArray++ = 0;
|
|
|
|
*vertArray++ = x2;
|
|
|
|
*vertArray++ = y2;
|
|
|
|
*vertArray++ = 0;
|
|
|
|
|
|
|
|
float newY1 = y1;
|
|
|
|
float newY2 = y2;
|
|
|
|
|
|
|
|
if (m_bIsTextureFlipped)
|
|
|
|
{
|
|
|
|
newY1 = imageH - y1;
|
|
|
|
newY2 = imageH - y2;
|
|
|
|
}
|
|
|
|
|
|
|
|
*texArray++ = x1 / width;
|
|
|
|
*texArray++ = newY1 / height;
|
|
|
|
*texArray++ = x2 / width;
|
|
|
|
*texArray++ = newY1 / height;
|
|
|
|
*texArray++ = x1 / width;
|
|
|
|
*texArray++ = newY2 / height;
|
|
|
|
*texArray++ = x2 / width;
|
|
|
|
*texArray++ = newY2 / height;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (x = 0; x < numQuads; x++)
|
|
|
|
{
|
|
|
|
idxArray[x*6+0] = (GLushort)(x * 4 + 0);
|
|
|
|
idxArray[x*6+1] = (GLushort)(x * 4 + 1);
|
|
|
|
idxArray[x*6+2] = (GLushort)(x * 4 + 2);
|
|
|
|
|
|
|
|
idxArray[x*6+3] = (GLushort)(x * 4 + 1);
|
|
|
|
idxArray[x*6+4] = (GLushort)(x * 4 + 2);
|
|
|
|
idxArray[x*6+5] = (GLushort)(x * 4 + 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(m_pOriginalVertices, m_pVertices, numQuads * 12 * sizeof(GLfloat));
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
void CCTiledGrid3D::setTile(const ccGridSize& pos, const ccQuad3& coords)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
int idx = (m_sGridSize.y * pos.x + pos.y) * 4 * 3;
|
|
|
|
float *vertArray = (float*)m_pVertices;
|
|
|
|
memcpy(&vertArray[idx], &coords, sizeof(ccQuad3));
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
ccQuad3 CCTiledGrid3D::originalTile(const ccGridSize& pos)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
int idx = (m_sGridSize.y * pos.x + pos.y) * 4 * 3;
|
|
|
|
float *vertArray = (float*)m_pOriginalVertices;
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
ccQuad3 ret;
|
|
|
|
memcpy(&ret, &vertArray[idx], sizeof(ccQuad3));
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return ret;
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
ccQuad3 CCTiledGrid3D::tile(const ccGridSize& pos)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
int idx = (m_sGridSize.y * pos.x + pos.y) * 4 * 3;
|
|
|
|
float *vertArray = (float*)m_pVertices;
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
ccQuad3 ret;
|
|
|
|
memcpy(&ret, &vertArray[idx], sizeof(ccQuad3));
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return ret;
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
void CCTiledGrid3D::reuse(void)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
if (m_nReuseGrid > 0)
|
|
|
|
{
|
|
|
|
int numQuads = m_sGridSize.x * m_sGridSize.y;
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
memcpy(m_pOriginalVertices, m_pVertices, numQuads * 12 * sizeof(GLfloat));
|
|
|
|
--m_nReuseGrid;
|
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|