axmol/cocos/2d/CCGrid.cpp

689 lines
18 KiB
C++
Raw Normal View History

2010-12-24 15:53:51 +08:00
/****************************************************************************
Copyright (c) 2009 On-Core
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (C) 2013-2014 Chukong Technologies Inc.
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.
****************************************************************************/
2014-04-30 08:37:36 +08:00
#include "base/ccMacros.h"
Squashed commit of the following: commit c16dcfaaea0922039aad05bce1f4efed18e04871 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:05:18 2014 -0700 more linux fixes commit 1553795976c9090a1b46deb53d12910fe0676008 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:04:04 2014 -0700 more linux fixes commit 1e43a8cabff33cbf25aa5eb5412f53a878222d83 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:02:07 2014 -0700 fixes linux isuses commit 723a445dd6411f91846da2b801248ad8298174f1 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:58:50 2014 -0700 more linux fixes commit 533c8025e794fc76cef02f396b3a93b3d7f4cfc8 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:57:33 2014 -0700 more linux fixes commit 4ba1e84959670bcbf044f18d1c0d4b3cb3be4090 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:53:43 2014 -0700 more linux fixes commit 1f8e011f306a47ed4134224e5e349929201f0539 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:49:28 2014 -0700 more linux fixes commit 3e2033100822ff6d532a1b4f012337491dc11920 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:47:43 2014 -0700 more linux fixes commit 2e708863c75fd032f1b2396dfdf1d31f7a62b713 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:46:00 2014 -0700 more linux fixes commit 861b5b92a6efd4de7b926c20d636ce9d749b293f Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:43:15 2014 -0700 more linux fixes commit 2a43365a0c1755e9b9cada53301be1a20adb31cf Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:36:06 2014 -0700 more fixes for linux commit 7d332bf911892f87c7824d2a5da7bf73ce7ec411 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:35:29 2014 -0700 more fixes for linux commit f1becc17d3316dfe3678c23c9dcedb7a447d9235 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:34:44 2014 -0700 more fixes for linux commit d2e5959bb0dde921dd5e73be1d8acc3b3f50e51d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:33:45 2014 -0700 fixes for linux commit ad9b633c352107cf0e8b060a0e23d6e6a3f5e80f Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:16:48 2014 -0700 compiles on Windows commit 4425ee8e5de8f42a2d6050e4470109600dce8b5d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:07:20 2014 -0700 fix builder
2014-05-01 10:09:13 +08:00
#include "2d/CCGrid.h"
2014-04-30 08:37:36 +08:00
#include "base/CCDirector.h"
#include "CCGrabber.h"
Squashed commit of the following: commit c16dcfaaea0922039aad05bce1f4efed18e04871 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:05:18 2014 -0700 more linux fixes commit 1553795976c9090a1b46deb53d12910fe0676008 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:04:04 2014 -0700 more linux fixes commit 1e43a8cabff33cbf25aa5eb5412f53a878222d83 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:02:07 2014 -0700 fixes linux isuses commit 723a445dd6411f91846da2b801248ad8298174f1 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:58:50 2014 -0700 more linux fixes commit 533c8025e794fc76cef02f396b3a93b3d7f4cfc8 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:57:33 2014 -0700 more linux fixes commit 4ba1e84959670bcbf044f18d1c0d4b3cb3be4090 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:53:43 2014 -0700 more linux fixes commit 1f8e011f306a47ed4134224e5e349929201f0539 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:49:28 2014 -0700 more linux fixes commit 3e2033100822ff6d532a1b4f012337491dc11920 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:47:43 2014 -0700 more linux fixes commit 2e708863c75fd032f1b2396dfdf1d31f7a62b713 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:46:00 2014 -0700 more linux fixes commit 861b5b92a6efd4de7b926c20d636ce9d749b293f Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:43:15 2014 -0700 more linux fixes commit 2a43365a0c1755e9b9cada53301be1a20adb31cf Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:36:06 2014 -0700 more fixes for linux commit 7d332bf911892f87c7824d2a5da7bf73ce7ec411 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:35:29 2014 -0700 more fixes for linux commit f1becc17d3316dfe3678c23c9dcedb7a447d9235 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:34:44 2014 -0700 more fixes for linux commit d2e5959bb0dde921dd5e73be1d8acc3b3f50e51d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:33:45 2014 -0700 fixes for linux commit ad9b633c352107cf0e8b060a0e23d6e6a3f5e80f Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:16:48 2014 -0700 compiles on Windows commit 4425ee8e5de8f42a2d6050e4470109600dce8b5d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:07:20 2014 -0700 fix builder
2014-05-01 10:09:13 +08:00
#include "2d/ccUtils.h"
#include "2d/CCGLProgram.h"
Squashed commit of the following: commit c16dcfaaea0922039aad05bce1f4efed18e04871 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:05:18 2014 -0700 more linux fixes commit 1553795976c9090a1b46deb53d12910fe0676008 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:04:04 2014 -0700 more linux fixes commit 1e43a8cabff33cbf25aa5eb5412f53a878222d83 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:02:07 2014 -0700 fixes linux isuses commit 723a445dd6411f91846da2b801248ad8298174f1 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:58:50 2014 -0700 more linux fixes commit 533c8025e794fc76cef02f396b3a93b3d7f4cfc8 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:57:33 2014 -0700 more linux fixes commit 4ba1e84959670bcbf044f18d1c0d4b3cb3be4090 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:53:43 2014 -0700 more linux fixes commit 1f8e011f306a47ed4134224e5e349929201f0539 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:49:28 2014 -0700 more linux fixes commit 3e2033100822ff6d532a1b4f012337491dc11920 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:47:43 2014 -0700 more linux fixes commit 2e708863c75fd032f1b2396dfdf1d31f7a62b713 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:46:00 2014 -0700 more linux fixes commit 861b5b92a6efd4de7b926c20d636ce9d749b293f Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:43:15 2014 -0700 more linux fixes commit 2a43365a0c1755e9b9cada53301be1a20adb31cf Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:36:06 2014 -0700 more fixes for linux commit 7d332bf911892f87c7824d2a5da7bf73ce7ec411 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:35:29 2014 -0700 more fixes for linux commit f1becc17d3316dfe3678c23c9dcedb7a447d9235 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:34:44 2014 -0700 more fixes for linux commit d2e5959bb0dde921dd5e73be1d8acc3b3f50e51d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:33:45 2014 -0700 fixes for linux commit ad9b633c352107cf0e8b060a0e23d6e6a3f5e80f Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:16:48 2014 -0700 compiles on Windows commit 4425ee8e5de8f42a2d6050e4470109600dce8b5d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:07:20 2014 -0700 fix builder
2014-05-01 10:09:13 +08:00
#include "2d/CCShaderCache.h"
2014-04-27 01:35:57 +08:00
#include "2d/ccGLStateCache.h"
2010-12-24 15:53:51 +08:00
#include "CCGL.h"
2014-04-30 08:37:36 +08:00
#include "renderer/CCRenderer.h"
Squashed commit of the following: commit c16dcfaaea0922039aad05bce1f4efed18e04871 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:05:18 2014 -0700 more linux fixes commit 1553795976c9090a1b46deb53d12910fe0676008 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:04:04 2014 -0700 more linux fixes commit 1e43a8cabff33cbf25aa5eb5412f53a878222d83 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 19:02:07 2014 -0700 fixes linux isuses commit 723a445dd6411f91846da2b801248ad8298174f1 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:58:50 2014 -0700 more linux fixes commit 533c8025e794fc76cef02f396b3a93b3d7f4cfc8 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:57:33 2014 -0700 more linux fixes commit 4ba1e84959670bcbf044f18d1c0d4b3cb3be4090 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:53:43 2014 -0700 more linux fixes commit 1f8e011f306a47ed4134224e5e349929201f0539 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:49:28 2014 -0700 more linux fixes commit 3e2033100822ff6d532a1b4f012337491dc11920 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:47:43 2014 -0700 more linux fixes commit 2e708863c75fd032f1b2396dfdf1d31f7a62b713 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:46:00 2014 -0700 more linux fixes commit 861b5b92a6efd4de7b926c20d636ce9d749b293f Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:43:15 2014 -0700 more linux fixes commit 2a43365a0c1755e9b9cada53301be1a20adb31cf Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:36:06 2014 -0700 more fixes for linux commit 7d332bf911892f87c7824d2a5da7bf73ce7ec411 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:35:29 2014 -0700 more fixes for linux commit f1becc17d3316dfe3678c23c9dcedb7a447d9235 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:34:44 2014 -0700 more fixes for linux commit d2e5959bb0dde921dd5e73be1d8acc3b3f50e51d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:33:45 2014 -0700 fixes for linux commit ad9b633c352107cf0e8b060a0e23d6e6a3f5e80f Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:16:48 2014 -0700 compiles on Windows commit 4425ee8e5de8f42a2d6050e4470109600dce8b5d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed Apr 30 18:07:20 2014 -0700 fix builder
2014-05-01 10:09:13 +08:00
#include "math/TransformUtils.h"
2012-04-18 18:43:45 +08:00
NS_CC_BEGIN
// implementation of GridBase
2012-04-18 18:43:45 +08:00
GridBase* GridBase::create(const Size& gridSize)
2010-12-24 15:53:51 +08:00
{
GridBase *pGridBase = new GridBase();
2010-12-24 15:53:51 +08:00
if (pGridBase)
{
2012-04-18 18:43:45 +08:00
if (pGridBase->initWithSize(gridSize))
{
pGridBase->autorelease();
}
else
{
CC_SAFE_RELEASE_NULL(pGridBase);
}
}
2010-12-24 15:53:51 +08:00
return pGridBase;
2012-04-18 18:43:45 +08:00
}
2010-12-24 15:53:51 +08:00
GridBase* GridBase::create(const Size& gridSize, Texture2D *texture, bool flipped)
2012-04-18 18:43:45 +08:00
{
GridBase *pGridBase = new GridBase();
2010-12-24 15:53:51 +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
return pGridBase;
2012-04-18 18:43:45 +08:00
}
2010-12-24 15:53:51 +08:00
bool GridBase::initWithSize(const Size& gridSize, Texture2D *texture, bool flipped)
2012-04-18 18:43:45 +08:00
{
bool ret = true;
2010-12-24 15:53:51 +08:00
_active = false;
_reuseGrid = 0;
_gridSize = gridSize;
2010-12-24 15:53:51 +08:00
_texture = texture;
CC_SAFE_RETAIN(_texture);
_isTextureFlipped = flipped;
2010-12-24 15:53:51 +08:00
Size texSize = _texture->getContentSize();
_step.x = texSize.width / _gridSize.width;
_step.y = texSize.height / _gridSize.height;
2010-12-24 15:53:51 +08:00
_grabber = new Grabber();
if (_grabber)
{
_grabber->grab(_texture);
}
else
{
ret = false;
}
_shaderProgram = ShaderCache::getInstance()->getProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE);
calculateVertexPoints();
2012-04-18 18:43:45 +08:00
return ret;
2012-04-18 18:43:45 +08:00
}
2010-12-24 15:53:51 +08:00
bool GridBase::initWithSize(const Size& gridSize)
2012-04-18 18:43:45 +08:00
{
Director *director = Director::getInstance();
Size s = director->getWinSizeInPixels();
2013-12-05 17:19:01 +08:00
auto POTWide = ccNextPOT((unsigned int)s.width);
auto POTHigh = ccNextPOT((unsigned int)s.height);
2010-12-24 15:53:51 +08:00
// we only use rgba8888
Texture2D::PixelFormat format = Texture2D::PixelFormat::RGBA8888;
2010-12-24 15:53:51 +08:00
2013-12-05 17:19:01 +08:00
auto dataLen = POTWide * POTHigh * 4;
void *data = calloc(dataLen, 1);
if (! data)
{
CCLOG("cocos2d: Grid: not enough memory.");
this->release();
return false;
}
2010-12-24 15:53:51 +08:00
Texture2D *texture = new Texture2D();
texture->initWithData(data, dataLen, format, POTWide, POTHigh, s);
2012-04-18 18:43:45 +08:00
free(data);
2012-04-18 18:43:45 +08:00
if (! texture)
{
CCLOG("cocos2d: Grid: error creating texture");
return false;
}
2012-04-18 18:43:45 +08:00
initWithSize(gridSize, texture, false);
2012-04-18 18:43:45 +08:00
texture->release();
2012-04-18 18:43:45 +08:00
return true;
2012-04-18 18:43:45 +08:00
}
GridBase::~GridBase(void)
2012-04-18 18:43:45 +08:00
{
CCLOGINFO("deallocing GridBase: %p", this);
2010-12-24 15:53:51 +08:00
//TODO: ? why 2.0 comments this line setActive(false);
CC_SAFE_RELEASE(_texture);
CC_SAFE_RELEASE(_grabber);
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 GridBase::setActive(bool active)
2012-04-18 18:43:45 +08:00
{
_active = active;
if (! active)
{
Director *pDirector = Director::getInstance();
Director::Projection proj = pDirector->getProjection();
pDirector->setProjection(proj);
}
2012-04-18 18:43:45 +08:00
}
2010-12-24 15:53:51 +08:00
void GridBase::setTextureFlipped(bool flipped)
2012-04-18 18:43:45 +08:00
{
if (_isTextureFlipped != flipped)
{
_isTextureFlipped = flipped;
calculateVertexPoints();
}
2012-04-18 18:43:45 +08:00
}
2010-12-24 15:53:51 +08:00
void GridBase::set2DProjection()
2012-04-18 18:43:45 +08:00
{
Director *director = Director::getInstance();
Size size = director->getWinSizeInPixels();
glViewport(0, 0, (GLsizei)(size.width), (GLsizei)(size.height) );
director->loadIdentityMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
Matrix orthoMatrix;
Matrix::createOrthographicOffCenter(0, size.width, 0, size.height, -1, 1, &orthoMatrix);
director->multiplyMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION, orthoMatrix);
director->loadIdentityMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
GL::setProjectionMatrixDirty();
2012-04-18 18:43:45 +08:00
}
2010-12-24 15:53:51 +08:00
void GridBase::beforeDraw(void)
2012-04-18 18:43:45 +08:00
{
// save projection
Director *director = Director::getInstance();
_directorProjection = director->getProjection();
// 2d projection
// [director setProjection:Director::Projection::_2D];
set2DProjection();
_grabber->beforeRender(_texture);
2012-04-18 18:43:45 +08:00
}
2010-12-24 15:53:51 +08:00
void GridBase::afterDraw(cocos2d::Node *target)
2012-04-18 18:43:45 +08:00
{
_grabber->afterRender(_texture);
2010-12-24 15:53:51 +08:00
// restore projection
Director *director = Director::getInstance();
director->setProjection(_directorProjection);
2010-12-24 15:53:51 +08:00
2013-12-21 08:33:31 +08:00
// if (target->getCamera()->isDirty())
// {
// Vector2 offset = target->getAnchorPointInPoints();
2013-12-21 08:33:31 +08:00
//
// //
// // XXX: Camera should be applied in the AnchorPoint
// //
// kmGLTranslatef(offset.x, offset.y, 0);
// target->getCamera()->locate();
// kmGLTranslatef(-offset.x, -offset.y, 0);
// }
2010-12-24 15:53:51 +08:00
GL::bindTexture2D(_texture->getName());
2012-04-18 18:43:45 +08:00
// restore projection for default FBO .fixed bug #543 #544
//TODO: Director::getInstance()->setProjection(Director::getInstance()->getProjection());
//TODO: Director::getInstance()->applyOrientation();
blit();
2012-04-18 18:43:45 +08:00
}
2010-12-24 15:53:51 +08:00
void GridBase::blit(void)
2012-04-18 18:43:45 +08:00
{
CCASSERT(0, "");
2012-04-18 18:43:45 +08:00
}
2010-12-24 15:53:51 +08:00
void GridBase::reuse(void)
2012-04-18 18:43:45 +08:00
{
CCASSERT(0, "");
2012-04-18 18:43:45 +08:00
}
2010-12-24 15:53:51 +08:00
void GridBase::calculateVertexPoints(void)
2012-04-18 18:43:45 +08:00
{
CCASSERT(0, "");
2012-04-18 18:43:45 +08:00
}
2010-12-24 15:53:51 +08:00
// implementation of Grid3D
Grid3D* Grid3D::create(const Size& gridSize, Texture2D *texture, bool flipped)
2012-04-18 18:43:45 +08:00
{
Grid3D *ret= new Grid3D();
2010-12-24 15:53:51 +08:00
if (ret)
{
if (ret->initWithSize(gridSize, texture, flipped))
{
ret->autorelease();
}
else
{
delete ret;
ret = nullptr;
}
}
2010-12-24 15:53:51 +08:00
return ret;
2012-04-18 18:43:45 +08:00
}
Grid3D* Grid3D::create(const Size& gridSize)
2012-04-18 18:43:45 +08:00
{
Grid3D *ret= new Grid3D();
2010-12-24 15:53:51 +08:00
if (ret)
{
if (ret->initWithSize(gridSize))
{
ret->autorelease();
}
else
{
delete ret;
ret = nullptr;
}
}
2010-12-24 15:53:51 +08:00
return ret;
2012-04-18 18:43:45 +08:00
}
Grid3D::Grid3D()
: _texCoordinates(nullptr)
, _vertices(nullptr)
, _originalVertices(nullptr)
, _indices(nullptr)
2012-04-18 18:43:45 +08:00
{
2012-04-18 18:43:45 +08:00
}
2010-12-24 15:53:51 +08:00
Grid3D::~Grid3D(void)
2012-04-18 18:43:45 +08:00
{
CC_SAFE_FREE(_texCoordinates);
CC_SAFE_FREE(_vertices);
CC_SAFE_FREE(_indices);
CC_SAFE_FREE(_originalVertices);
2012-04-18 18:43:45 +08:00
}
void Grid3D::blit(void)
2012-04-18 18:43:45 +08:00
{
int n = _gridSize.width * _gridSize.height;
GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION | GL::VERTEX_ATTRIB_FLAG_TEX_COORDS );
_shaderProgram->use();
_shaderProgram->setUniformsForBuiltins();;
//
// Attributes
//
2013-04-09 08:51:30 +08:00
#ifdef EMSCRIPTEN
// Size calculations from calculateVertexPoints().
unsigned int numOfPoints = (_gridSize.width+1) * (_gridSize.height+1);
2013-04-09 08:51:30 +08:00
// position
2014-04-02 15:06:16 +08:00
setGLBufferData(_vertices, numOfPoints * sizeof(Vector3), 0);
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, 0, 0);
2013-04-09 08:51:30 +08:00
// texCoords
2014-04-02 15:25:37 +08:00
setGLBufferData(_texCoordinates, numOfPoints * sizeof(Vector2), 1);
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, 0, 0);
setGLIndexData(_indices, n * 12, 0);
2013-04-09 08:51:30 +08:00
glDrawElements(GL_TRIANGLES, (GLsizei) n*6, GL_UNSIGNED_SHORT, 0);
#else
// position
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, 0, _vertices);
// texCoords
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, 0, _texCoordinates);
glDrawElements(GL_TRIANGLES, (GLsizei) n*6, GL_UNSIGNED_SHORT, _indices);
2013-04-09 08:51:30 +08:00
#endif // EMSCRIPTEN
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,n*6);
2012-04-18 18:43:45 +08:00
}
2010-12-24 15:53:51 +08:00
void Grid3D::calculateVertexPoints(void)
2012-04-18 18:43:45 +08:00
{
float width = (float)_texture->getPixelsWide();
float height = (float)_texture->getPixelsHigh();
float imageH = _texture->getContentSizeInPixels().height;
int x, y, i;
CC_SAFE_FREE(_vertices);
CC_SAFE_FREE(_originalVertices);
CC_SAFE_FREE(_texCoordinates);
CC_SAFE_FREE(_indices);
unsigned int numOfPoints = (_gridSize.width+1) * (_gridSize.height+1);
2014-04-02 15:06:16 +08:00
_vertices = malloc(numOfPoints * sizeof(Vector3));
_originalVertices = malloc(numOfPoints * sizeof(Vector3));
2014-04-02 15:25:37 +08:00
_texCoordinates = malloc(numOfPoints * sizeof(Vector2));
_indices = (GLushort*)malloc(_gridSize.width * _gridSize.height * sizeof(GLushort) * 6);
GLfloat *vertArray = (GLfloat*)_vertices;
GLfloat *texArray = (GLfloat*)_texCoordinates;
GLushort *idxArray = _indices;
for (x = 0; x < _gridSize.width; ++x)
{
for (y = 0; y < _gridSize.height; ++y)
{
int idx = (y * _gridSize.width) + x;
GLfloat x1 = x * _step.x;
GLfloat x2 = x1 + _step.x;
GLfloat y1 = y * _step.y;
GLfloat y2= y1 + _step.y;
GLushort a = (GLushort)(x * (_gridSize.height + 1) + y);
GLushort b = (GLushort)((x + 1) * (_gridSize.height + 1) + y);
GLushort c = (GLushort)((x + 1) * (_gridSize.height + 1) + (y + 1));
GLushort d = (GLushort)(x * (_gridSize.height + 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};
2014-04-02 15:06:16 +08:00
Vector3 e(x1, y1, 0);
Vector3 f(x2, y1, 0);
Vector3 g(x2, y2, 0);
Vector3 h(x1, y2, 0);
2014-04-02 15:06:16 +08:00
Vector3 l2[4] = {e, f, g, h};
int tex1[4] = {a*2, b*2, c*2, d*2};
Vector2 Tex2F[4] = {Vector2(x1, y1), Vector2(x2, y1), Vector2(x2, y2), Vector2(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]] = Tex2F[i].x / width;
if (_isTextureFlipped)
{
texArray[tex1[i] + 1] = (imageH - Tex2F[i].y) / height;
}
else
{
texArray[tex1[i] + 1] = Tex2F[i].y / height;
}
}
}
}
2014-04-02 15:06:16 +08:00
memcpy(_originalVertices, _vertices, (_gridSize.width+1) * (_gridSize.height+1) * sizeof(Vector3));
2012-04-18 18:43:45 +08:00
}
2010-12-24 15:53:51 +08:00
Vector3 Grid3D::getVertex(const Vector2& pos) const
{
CCASSERT( pos.x == (unsigned int)pos.x && pos.y == (unsigned int) pos.y , "Numbers must be integers");
2013-02-27 09:38:30 +08:00
int index = (pos.x * (_gridSize.height+1) + pos.y) * 3;
float *vertArray = (float*)_vertices;
2010-12-24 15:53:51 +08:00
2014-04-02 15:06:16 +08:00
Vector3 vert(vertArray[index], vertArray[index+1], vertArray[index+2]);
2010-12-24 15:53:51 +08:00
return vert;
2012-04-18 18:43:45 +08:00
}
2010-12-24 15:53:51 +08:00
Vector3 Grid3D::getOriginalVertex(const Vector2& pos) const
2012-04-18 18:43:45 +08:00
{
CCASSERT( pos.x == (unsigned int)pos.x && pos.y == (unsigned int) pos.y , "Numbers must be integers");
2013-02-27 09:38:30 +08:00
int index = (pos.x * (_gridSize.height+1) + pos.y) * 3;
float *vertArray = (float*)_originalVertices;
2010-12-24 15:53:51 +08:00
2014-04-02 15:06:16 +08:00
Vector3 vert(vertArray[index], vertArray[index+1], vertArray[index+2]);
2010-12-24 15:53:51 +08:00
return vert;
2012-04-18 18:43:45 +08:00
}
2010-12-24 15:53:51 +08:00
void Grid3D::setVertex(const Vector2& pos, const Vector3& vertex)
2012-04-18 18:43:45 +08:00
{
CCASSERT( pos.x == (unsigned int)pos.x && pos.y == (unsigned int) pos.y , "Numbers must be integers");
int index = (pos.x * (_gridSize.height + 1) + pos.y) * 3;
float *vertArray = (float*)_vertices;
vertArray[index] = vertex.x;
vertArray[index+1] = vertex.y;
vertArray[index+2] = vertex.z;
2012-04-18 18:43:45 +08:00
}
void Grid3D::reuse(void)
2012-04-18 18:43:45 +08:00
{
if (_reuseGrid > 0)
{
2014-04-02 15:06:16 +08:00
memcpy(_originalVertices, _vertices, (_gridSize.width+1) * (_gridSize.height+1) * sizeof(Vector3));
--_reuseGrid;
}
2012-04-18 18:43:45 +08:00
}
2010-12-24 15:53:51 +08:00
// implementation of TiledGrid3D
2010-12-24 15:53:51 +08:00
TiledGrid3D::TiledGrid3D()
: _texCoordinates(nullptr)
, _vertices(nullptr)
, _originalVertices(nullptr)
, _indices(nullptr)
2012-04-18 18:43:45 +08:00
{
2012-04-18 18:43:45 +08:00
}
TiledGrid3D::~TiledGrid3D(void)
2012-04-18 18:43:45 +08:00
{
CC_SAFE_FREE(_texCoordinates);
CC_SAFE_FREE(_vertices);
CC_SAFE_FREE(_originalVertices);
CC_SAFE_FREE(_indices);
2012-04-18 18:43:45 +08:00
}
2010-12-24 15:53:51 +08:00
TiledGrid3D* TiledGrid3D::create(const Size& gridSize, Texture2D *texture, bool flipped)
2012-04-18 18:43:45 +08:00
{
TiledGrid3D *ret= new TiledGrid3D();
2010-12-24 15:53:51 +08:00
if (ret)
{
if (ret->initWithSize(gridSize, texture, flipped))
{
ret->autorelease();
}
else
{
delete ret;
ret = nullptr;
}
}
2010-12-24 15:53:51 +08:00
return ret;
2012-04-18 18:43:45 +08:00
}
2010-12-24 15:53:51 +08:00
TiledGrid3D* TiledGrid3D::create(const Size& gridSize)
2012-04-18 18:43:45 +08:00
{
TiledGrid3D *ret= new TiledGrid3D();
2012-04-18 18:43:45 +08:00
if (ret)
{
if (ret->initWithSize(gridSize))
{
ret->autorelease();
}
else
{
delete ret;
ret = nullptr;
}
}
2010-12-24 15:53:51 +08:00
return ret;
2012-04-18 18:43:45 +08:00
}
void TiledGrid3D::blit(void)
2012-04-18 18:43:45 +08:00
{
int n = _gridSize.width * _gridSize.height;
_shaderProgram->use();
_shaderProgram->setUniformsForBuiltins();
2010-12-24 15:53:51 +08:00
//
// Attributes
//
GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION | GL::VERTEX_ATTRIB_FLAG_TEX_COORDS );
2013-04-09 12:08:06 +08:00
#ifdef EMSCRIPTEN
int numQuads = _gridSize.width * _gridSize.height;
2013-04-09 12:08:06 +08:00
// position
2014-04-02 15:06:16 +08:00
setGLBufferData(_vertices, (numQuads*4*sizeof(Vector3)), 0);
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, 0, 0);
2013-04-09 12:08:06 +08:00
// texCoords
2014-04-02 15:25:37 +08:00
setGLBufferData(_texCoordinates, (numQuads*4*sizeof(Vector2)), 1);
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, 0, 0);
2013-04-09 12:08:06 +08:00
setGLIndexData(_indices, n * 12, 0);
2013-04-09 12:08:06 +08:00
glDrawElements(GL_TRIANGLES, (GLsizei) n*6, GL_UNSIGNED_SHORT, 0);
#else
// position
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, 0, _vertices);
2010-12-24 15:53:51 +08:00
// texCoords
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, 0, _texCoordinates);
glDrawElements(GL_TRIANGLES, (GLsizei)n*6, GL_UNSIGNED_SHORT, _indices);
2013-04-09 12:08:06 +08:00
#endif // EMSCRIPTEN
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,n*6);
2012-04-18 18:43:45 +08:00
}
2010-12-24 15:53:51 +08:00
void TiledGrid3D::calculateVertexPoints(void)
2012-04-18 18:43:45 +08:00
{
float width = (float)_texture->getPixelsWide();
float height = (float)_texture->getPixelsHigh();
float imageH = _texture->getContentSizeInPixels().height;
int numQuads = _gridSize.width * _gridSize.height;
CC_SAFE_FREE(_vertices);
CC_SAFE_FREE(_originalVertices);
CC_SAFE_FREE(_texCoordinates);
CC_SAFE_FREE(_indices);
2014-04-02 15:06:16 +08:00
_vertices = malloc(numQuads*4*sizeof(Vector3));
_originalVertices = malloc(numQuads*4*sizeof(Vector3));
2014-04-02 15:25:37 +08:00
_texCoordinates = malloc(numQuads*4*sizeof(Vector2));
_indices = (GLushort*)malloc(numQuads*6*sizeof(GLushort));
GLfloat *vertArray = (GLfloat*)_vertices;
GLfloat *texArray = (GLfloat*)_texCoordinates;
GLushort *idxArray = _indices;
int x, y;
for( x = 0; x < _gridSize.width; x++ )
{
for( y = 0; y < _gridSize.height; y++ )
{
float x1 = x * _step.x;
float x2 = x1 + _step.x;
float y1 = y * _step.y;
float y2 = y1 + _step.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 (_isTextureFlipped)
{
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(_originalVertices, _vertices, numQuads * 12 * sizeof(GLfloat));
2012-04-18 18:43:45 +08:00
}
2010-12-24 15:53:51 +08:00
void TiledGrid3D::setTile(const Vector2& pos, const Quad3& coords)
2012-04-18 18:43:45 +08:00
{
CCASSERT( pos.x == (unsigned int)pos.x && pos.y == (unsigned int) pos.y , "Numbers must be integers");
int idx = (_gridSize.height * pos.x + pos.y) * 4 * 3;
float *vertArray = (float*)_vertices;
memcpy(&vertArray[idx], &coords, sizeof(Quad3));
2012-04-18 18:43:45 +08:00
}
2010-12-24 15:53:51 +08:00
Quad3 TiledGrid3D::getOriginalTile(const Vector2& pos) const
2012-04-18 18:43:45 +08:00
{
CCASSERT( pos.x == (unsigned int)pos.x && pos.y == (unsigned int) pos.y , "Numbers must be integers");
int idx = (_gridSize.height * pos.x + pos.y) * 4 * 3;
float *vertArray = (float*)_originalVertices;
2010-12-24 15:53:51 +08:00
Quad3 ret;
memcpy(&ret, &vertArray[idx], sizeof(Quad3));
2010-12-24 15:53:51 +08:00
return ret;
2012-04-18 18:43:45 +08:00
}
2010-12-24 15:53:51 +08:00
Quad3 TiledGrid3D::getTile(const Vector2& pos) const
2012-04-18 18:43:45 +08:00
{
CCASSERT( pos.x == (unsigned int)pos.x && pos.y == (unsigned int) pos.y , "Numbers must be integers");
int idx = (_gridSize.height * pos.x + pos.y) * 4 * 3;
float *vertArray = (float*)_vertices;
2010-12-24 15:53:51 +08:00
Quad3 ret;
memcpy(&ret, &vertArray[idx], sizeof(Quad3));
2012-04-18 18:43:45 +08:00
return ret;
2012-04-18 18:43:45 +08:00
}
2010-12-24 15:53:51 +08:00
void TiledGrid3D::reuse(void)
2012-04-18 18:43:45 +08:00
{
if (_reuseGrid > 0)
{
int numQuads = _gridSize.width * _gridSize.height;
2010-12-24 15:53:51 +08:00
memcpy(_originalVertices, _vertices, numQuads * 12 * sizeof(GLfloat));
--_reuseGrid;
}
2012-04-18 18:43:45 +08:00
}
NS_CC_END