2010-12-24 15:53:51 +08:00
|
|
|
/****************************************************************************
|
2011-03-19 14:45:51 +08:00
|
|
|
Copyright (c) 2009 On-Core
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2016-08-05 09:42:15 +08:00
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
2014-01-07 11:25:07 +08:00
|
|
|
|
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-08-27 13:39:50 +08:00
|
|
|
#include "2d/CCGrid.h"
|
2014-05-10 09:39:25 +08:00
|
|
|
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/ccMacros.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "base/ccUtils.h"
|
2014-08-27 13:39:50 +08:00
|
|
|
#include "2d/CCNode.h"
|
|
|
|
#include "2d/CCGrabber.h"
|
2014-05-09 09:01:48 +08:00
|
|
|
#include "renderer/CCGLProgram.h"
|
2014-05-10 09:39:25 +08:00
|
|
|
#include "renderer/CCGLProgramCache.h"
|
2014-05-09 09:01:48 +08:00
|
|
|
#include "renderer/ccGLStateCache.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "renderer/CCRenderer.h"
|
2015-06-13 06:03:48 +08:00
|
|
|
#include "renderer/CCRenderState.h"
|
2014-08-27 13:39:50 +08:00
|
|
|
#include "renderer/CCTexture2D.h"
|
2014-09-10 07:50:02 +08:00
|
|
|
#include "platform/CCGL.h"
|
2015-06-09 17:24:30 +08:00
|
|
|
#include "2d/CCCamera.h"
|
2014-02-08 11:37:44 +08:00
|
|
|
|
2012-04-18 18:43:45 +08:00
|
|
|
NS_CC_BEGIN
|
2013-06-20 14:13:12 +08:00
|
|
|
// implementation of GridBase
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
GridBase* GridBase::create(const Size& gridSize)
|
2010-12-24 15:53:51 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
GridBase *pGridBase = new (std::nothrow) GridBase();
|
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
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
GridBase* GridBase::create(const Size& gridSize, Texture2D *texture, bool flipped)
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
GridBase *pGridBase = new (std::nothrow) GridBase();
|
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
|
|
|
|
2015-07-08 17:47:04 +08:00
|
|
|
bool GridBase::initWithSize(const Size& gridSize)
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2015-07-08 17:47:04 +08:00
|
|
|
return initWithSize(gridSize, Rect::ZERO);
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2015-07-08 17:26:29 +08:00
|
|
|
bool GridBase::initWithSize(const cocos2d::Size &gridSize, const cocos2d::Rect &rect)
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
Director *director = Director::getInstance();
|
|
|
|
Size s = director->getWinSizeInPixels();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-12-05 17:19:01 +08:00
|
|
|
auto POTWide = ccNextPOT((unsigned int)s.width);
|
|
|
|
auto POTHigh = ccNextPOT((unsigned int)s.height);
|
2015-07-08 17:26:29 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// we only use rgba8888
|
2013-07-26 04:36:19 +08:00
|
|
|
Texture2D::PixelFormat format = Texture2D::PixelFormat::RGBA8888;
|
2015-07-08 17:26:29 +08:00
|
|
|
|
2013-12-05 17:19:01 +08:00
|
|
|
auto dataLen = POTWide * POTHigh * 4;
|
2013-07-25 21:35:00 +08:00
|
|
|
void *data = calloc(dataLen, 1);
|
2012-04-19 14:35:52 +08:00
|
|
|
if (! data)
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
CCLOG("cocos2d: Grid: not enough memory.");
|
2012-04-19 14:35:52 +08:00
|
|
|
this->release();
|
|
|
|
return false;
|
|
|
|
}
|
2015-07-08 17:26:29 +08:00
|
|
|
|
2014-08-28 07:31:57 +08:00
|
|
|
Texture2D *texture = new (std::nothrow) Texture2D();
|
2013-07-27 22:06:30 +08:00
|
|
|
texture->initWithData(data, dataLen, format, POTWide, POTHigh, s);
|
2015-07-08 17:26:29 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
free(data);
|
2015-07-08 17:26:29 +08:00
|
|
|
|
2013-07-26 06:53:24 +08:00
|
|
|
if (! texture)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
CCLOG("cocos2d: Grid: error creating texture");
|
2012-04-19 14:35:52 +08:00
|
|
|
return false;
|
|
|
|
}
|
2015-07-08 17:26:29 +08:00
|
|
|
|
|
|
|
initWithSize(gridSize, texture, false, rect);
|
|
|
|
|
2013-07-26 06:53:24 +08:00
|
|
|
texture->release();
|
2015-07-08 17:26:29 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
|
|
|
|
2015-07-08 17:47:04 +08:00
|
|
|
bool GridBase::initWithSize(const Size& gridSize, Texture2D *texture, bool flipped)
|
2015-07-08 17:26:29 +08:00
|
|
|
{
|
2015-07-08 17:47:04 +08:00
|
|
|
return initWithSize(gridSize, texture, flipped, Rect::ZERO);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GridBase::initWithSize(const Size& gridSize, Texture2D *texture, bool flipped, const Rect& rect)
|
|
|
|
{
|
|
|
|
bool ret = true;
|
|
|
|
|
|
|
|
_active = false;
|
|
|
|
_reuseGrid = 0;
|
|
|
|
_gridSize = gridSize;
|
|
|
|
|
|
|
|
_texture = texture;
|
|
|
|
CC_SAFE_RETAIN(_texture);
|
|
|
|
_isTextureFlipped = flipped;
|
|
|
|
|
|
|
|
if (rect.equals(Rect::ZERO)) {
|
|
|
|
auto size = _texture->getContentSize();
|
|
|
|
_gridRect.setRect(0, 0, size.width, size.height);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
_gridRect = rect;
|
|
|
|
}
|
|
|
|
_step.x = _gridRect.size.width/_gridSize.width;
|
|
|
|
_step.y = _gridRect.size.height/_gridSize.height;
|
|
|
|
|
|
|
|
_grabber = new (std::nothrow) Grabber();
|
|
|
|
if (_grabber)
|
|
|
|
{
|
|
|
|
_grabber->grab(_texture);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
_shaderProgram = GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE);
|
|
|
|
calculateVertexPoints();
|
|
|
|
|
|
|
|
return ret;
|
2015-07-08 17:26:29 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
GridBase::~GridBase(void)
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2013-08-22 11:05:06 +08:00
|
|
|
CCLOGINFO("deallocing GridBase: %p", this);
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2014-08-30 03:54:24 +08:00
|
|
|
//TODO: ? why 2.0 comments this line: setActive(false);
|
2013-06-15 14:03:30 +08:00
|
|
|
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
|
2013-12-18 17:47:20 +08:00
|
|
|
void GridBase::setActive(bool active)
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
_active = active;
|
|
|
|
if (! active)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-12 06:24:23 +08:00
|
|
|
Director *pDirector = Director::getInstance();
|
2013-07-25 14:39:32 +08:00
|
|
|
Director::Projection proj = pDirector->getProjection();
|
2012-04-19 14:35:52 +08:00
|
|
|
pDirector->setProjection(proj);
|
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
void GridBase::setTextureFlipped(bool flipped)
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
if (_isTextureFlipped != flipped)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
_isTextureFlipped = flipped;
|
2012-04-19 14:35:52 +08:00
|
|
|
calculateVertexPoints();
|
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void GridBase::set2DProjection()
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2013-07-12 06:24:23 +08:00
|
|
|
Director *director = Director::getInstance();
|
2013-06-20 14:13:12 +08:00
|
|
|
Size size = director->getWinSizeInPixels();
|
2015-06-09 17:24:30 +08:00
|
|
|
|
2014-04-03 14:32:49 +08:00
|
|
|
director->loadIdentityMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Mat4 orthoMatrix;
|
|
|
|
Mat4::createOrthographicOffCenter(0, size.width, 0, size.height, -1, 1, &orthoMatrix);
|
2014-04-03 14:32:49 +08:00
|
|
|
director->multiplyMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION, orthoMatrix);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-04-03 14:32:49 +08:00
|
|
|
director->loadIdentityMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-26 09:42:53 +08:00
|
|
|
GL::setProjectionMatrixDirty();
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2015-07-08 15:57:27 +08:00
|
|
|
void GridBase::setGridRect(const cocos2d::Rect &rect)
|
|
|
|
{
|
|
|
|
_gridRect = rect;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void GridBase::beforeDraw(void)
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
// save projection
|
2013-07-12 06:24:23 +08:00
|
|
|
Director *director = Director::getInstance();
|
2013-06-15 14:03:30 +08:00
|
|
|
_directorProjection = director->getProjection();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// 2d projection
|
2013-07-26 04:36:19 +08:00
|
|
|
// [director setProjection:Director::Projection::_2D];
|
2012-04-19 14:35:52 +08:00
|
|
|
set2DProjection();
|
2015-06-09 17:24:30 +08:00
|
|
|
|
|
|
|
Size size = director->getWinSizeInPixels();
|
|
|
|
glViewport(0, 0, (GLsizei)(size.width), (GLsizei)(size.height) );
|
2013-06-15 14:03:30 +08:00
|
|
|
_grabber->beforeRender(_texture);
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2013-07-18 07:56:19 +08:00
|
|
|
void GridBase::afterDraw(cocos2d::Node *target)
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_grabber->afterRender(_texture);
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// restore projection
|
2013-07-12 06:24:23 +08:00
|
|
|
Director *director = Director::getInstance();
|
2013-06-15 14:03:30 +08:00
|
|
|
director->setProjection(_directorProjection);
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2015-06-09 17:24:30 +08:00
|
|
|
director->setViewport();
|
|
|
|
const auto& vp = Camera::getDefaultViewport();
|
|
|
|
glViewport(vp._left, vp._bottom, vp._width, vp._height);
|
2013-12-21 08:33:31 +08:00
|
|
|
// if (target->getCamera()->isDirty())
|
|
|
|
// {
|
2014-05-15 01:07:09 +08:00
|
|
|
// Vec2 offset = target->getAnchorPointInPoints();
|
2013-12-21 08:33:31 +08:00
|
|
|
//
|
|
|
|
// //
|
2014-08-30 03:54:24 +08:00
|
|
|
// // FIXME: Camera should be applied in the AnchorPoint
|
2013-12-21 08:33:31 +08:00
|
|
|
// //
|
|
|
|
// kmGLTranslatef(offset.x, offset.y, 0);
|
|
|
|
// target->getCamera()->locate();
|
|
|
|
// kmGLTranslatef(-offset.x, -offset.y, 0);
|
|
|
|
// }
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2013-07-26 09:42:53 +08:00
|
|
|
GL::bindTexture2D(_texture->getName());
|
2012-04-18 18:43:45 +08:00
|
|
|
|
|
|
|
// restore projection for default FBO .fixed bug #543 #544
|
2014-08-30 03:54:24 +08:00
|
|
|
//TODO: Director::getInstance()->setProjection(Director::getInstance()->getProjection());
|
|
|
|
//TODO: Director::getInstance()->applyOrientation();
|
2014-09-17 18:33:18 +08:00
|
|
|
beforeBlit();
|
2012-04-19 14:35:52 +08:00
|
|
|
blit();
|
2014-09-17 18:33:18 +08:00
|
|
|
afterBlit();
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void GridBase::blit(void)
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2015-07-14 15:28:36 +08:00
|
|
|
CCASSERT(0, "Subclass should implement it.");
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void GridBase::reuse(void)
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2015-07-14 15:28:36 +08:00
|
|
|
CCASSERT(0, "Subclass should implement it!");
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void GridBase::calculateVertexPoints(void)
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2015-07-14 15:28:36 +08:00
|
|
|
CCASSERT(0, "Subclass should implement it.");
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// implementation of Grid3D
|
2012-07-23 22:49:11 +08:00
|
|
|
|
2015-07-08 17:47:04 +08:00
|
|
|
Grid3D* Grid3D::create(const Size& gridSize)
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
Grid3D *ret= new (std::nothrow) Grid3D();
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
if (ret)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2015-07-08 17:47:04 +08:00
|
|
|
if (ret->initWithSize(gridSize))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
ret->autorelease();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
delete ret;
|
|
|
|
ret = nullptr;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
return ret;
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
|
|
|
|
2015-07-08 17:47:04 +08:00
|
|
|
Grid3D* Grid3D::create(const Size& gridSize, const Rect& rect)
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
Grid3D *ret= new (std::nothrow) Grid3D();
|
2015-07-08 17:47:04 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
if (ret)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2015-07-08 17:47:04 +08:00
|
|
|
if (ret->initWithSize(gridSize, rect))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
ret->autorelease();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
delete ret;
|
|
|
|
ret = nullptr;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
2015-07-08 17:47:04 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
return ret;
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2015-07-08 17:47:04 +08:00
|
|
|
Grid3D* Grid3D::create(const Size& gridSize, Texture2D *texture, bool flipped)
|
2015-07-08 17:26:29 +08:00
|
|
|
{
|
|
|
|
Grid3D *ret= new (std::nothrow) Grid3D();
|
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
if (ret)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2015-07-08 17:47:04 +08:00
|
|
|
if (ret->initWithSize(gridSize, texture, flipped))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
ret->autorelease();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
delete ret;
|
|
|
|
ret = nullptr;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
2015-07-08 17:26:29 +08:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2015-07-08 17:47:04 +08:00
|
|
|
Grid3D* Grid3D::create(const Size& gridSize, Texture2D *texture, bool flipped, const Rect& rect)
|
2015-07-08 17:26:29 +08:00
|
|
|
{
|
|
|
|
Grid3D *ret= new (std::nothrow) Grid3D();
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
{
|
2015-07-08 17:47:04 +08:00
|
|
|
if (ret->initWithSize(gridSize, texture, flipped, rect))
|
2015-07-08 17:26:29 +08:00
|
|
|
{
|
|
|
|
ret->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
delete ret;
|
|
|
|
ret = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
return ret;
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2012-03-14 18:11:25 +08:00
|
|
|
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Grid3D::Grid3D()
|
2014-09-17 18:33:18 +08:00
|
|
|
: _texCoordinates(nullptr)
|
|
|
|
, _vertices(nullptr)
|
|
|
|
, _originalVertices(nullptr)
|
|
|
|
, _indices(nullptr)
|
|
|
|
, _needDepthTestForBlit(false)
|
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
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Grid3D::~Grid3D(void)
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +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
|
|
|
}
|
|
|
|
|
2014-09-17 18:33:18 +08:00
|
|
|
void Grid3D::beforeBlit()
|
|
|
|
{
|
|
|
|
if(_needDepthTestForBlit)
|
|
|
|
{
|
2015-01-23 09:02:33 +08:00
|
|
|
_oldDepthTestValue = glIsEnabled(GL_DEPTH_TEST) != GL_FALSE;
|
2015-01-16 15:21:23 +08:00
|
|
|
GLboolean depthWriteMask;
|
|
|
|
glGetBooleanv(GL_DEPTH_WRITEMASK, &depthWriteMask);
|
2015-01-23 09:02:33 +08:00
|
|
|
_oldDepthWriteValue = depthWriteMask != GL_FALSE;
|
2015-01-16 15:21:23 +08:00
|
|
|
CHECK_GL_ERROR_DEBUG();
|
2015-06-13 06:03:48 +08:00
|
|
|
|
2014-09-17 18:33:18 +08:00
|
|
|
glEnable(GL_DEPTH_TEST);
|
2015-06-13 06:03:48 +08:00
|
|
|
RenderState::StateBlock::_defaultState->setDepthTest(true);
|
|
|
|
|
2015-01-16 08:41:07 +08:00
|
|
|
glDepthMask(true);
|
2015-06-13 06:03:48 +08:00
|
|
|
RenderState::StateBlock::_defaultState->setDepthWrite(true);
|
2014-09-17 18:33:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Grid3D::afterBlit()
|
|
|
|
{
|
|
|
|
if(_needDepthTestForBlit)
|
|
|
|
{
|
|
|
|
if(_oldDepthTestValue)
|
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
else
|
|
|
|
glDisable(GL_DEPTH_TEST);
|
2015-06-13 06:03:48 +08:00
|
|
|
RenderState::StateBlock::_defaultState->setDepthTest(_oldDepthTestValue);
|
|
|
|
|
2015-01-16 08:41:07 +08:00
|
|
|
glDepthMask(_oldDepthWriteValue);
|
2015-06-13 06:03:48 +08:00
|
|
|
RenderState::StateBlock::_defaultState->setDepthWrite(_oldDepthWriteValue);
|
2014-09-17 18:33:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Grid3D::blit(void)
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
int n = _gridSize.width * _gridSize.height;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-05-13 10:12:56 +08:00
|
|
|
GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION | GL::VERTEX_ATTRIB_FLAG_TEX_COORD );
|
2013-06-15 14:03:30 +08:00
|
|
|
_shaderProgram->use();
|
|
|
|
_shaderProgram->setUniformsForBuiltins();;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
//
|
|
|
|
// Attributes
|
|
|
|
//
|
|
|
|
|
|
|
|
// position
|
2013-07-25 17:48:22 +08:00
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, 0, _vertices);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-09-17 15:02:24 +08:00
|
|
|
// texCoords
|
2014-05-09 03:34:26 +08:00
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, 0, _texCoordinates);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
glDrawElements(GL_TRIANGLES, (GLsizei) n*6, GL_UNSIGNED_SHORT, _indices);
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Grid3D::calculateVertexPoints(void)
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
float width = (float)_texture->getPixelsWide();
|
|
|
|
float height = (float)_texture->getPixelsHigh();
|
|
|
|
float imageH = _texture->getContentSizeInPixels().height;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
int x, y, i;
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_FREE(_vertices);
|
|
|
|
CC_SAFE_FREE(_originalVertices);
|
|
|
|
CC_SAFE_FREE(_texCoordinates);
|
|
|
|
CC_SAFE_FREE(_indices);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
unsigned int numOfPoints = (_gridSize.width+1) * (_gridSize.height+1);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
_vertices = malloc(numOfPoints * sizeof(Vec3));
|
|
|
|
_originalVertices = malloc(numOfPoints * sizeof(Vec3));
|
|
|
|
_texCoordinates = malloc(numOfPoints * sizeof(Vec2));
|
2013-06-15 14:03:30 +08:00
|
|
|
_indices = (GLushort*)malloc(_gridSize.width * _gridSize.height * sizeof(GLushort) * 6);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
GLfloat *vertArray = (GLfloat*)_vertices;
|
|
|
|
GLfloat *texArray = (GLfloat*)_texCoordinates;
|
|
|
|
GLushort *idxArray = _indices;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
for (x = 0; x < _gridSize.width; ++x)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
for (y = 0; y < _gridSize.height; ++y)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
int idx = (y * _gridSize.width) + x;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2015-07-08 15:57:27 +08:00
|
|
|
GLfloat x1 = x * _step.x + _gridRect.origin.x;
|
2013-06-15 14:03:30 +08:00
|
|
|
GLfloat x2 = x1 + _step.x;
|
2015-07-08 15:57:27 +08:00
|
|
|
GLfloat y1 = y * _step.y + _gridRect.origin.y;
|
2013-06-15 14:03:30 +08:00
|
|
|
GLfloat y2= y1 + _step.y;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
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));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
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-05-15 01:07:09 +08:00
|
|
|
Vec3 e(x1, y1, 0);
|
|
|
|
Vec3 f(x2, y1, 0);
|
|
|
|
Vec3 g(x2, y2, 0);
|
|
|
|
Vec3 h(x1, y2, 0);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec3 l2[4] = {e, f, g, h};
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
int tex1[4] = {a*2, b*2, c*2, d*2};
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 Tex2F[4] = {Vec2(x1, y1), Vec2(x2, y1), Vec2(x2, y2), Vec2(x1, y2)};
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
texArray[tex1[i]] = Tex2F[i].x / width;
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_isTextureFlipped)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-05 16:49:22 +08:00
|
|
|
texArray[tex1[i] + 1] = (imageH - Tex2F[i].y) / height;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-07-05 16:49:22 +08:00
|
|
|
texArray[tex1[i] + 1] = Tex2F[i].y / height;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
memcpy(_originalVertices, _vertices, (_gridSize.width+1) * (_gridSize.height+1) * sizeof(Vec3));
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec3 Grid3D::getVertex(const Vec2& pos) const
|
2013-07-07 21:08:14 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +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
|
|
|
|
2013-06-15 14:03: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-05-15 01:07:09 +08:00
|
|
|
Vec3 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
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec3 Grid3D::getOriginalVertex(const Vec2& pos) const
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +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
|
|
|
|
2013-06-15 14:03: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-05-15 01:07:09 +08:00
|
|
|
Vec3 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
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void Grid3D::setVertex(const Vec2& pos, const Vec3& vertex)
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT( pos.x == (unsigned int)pos.x && pos.y == (unsigned int) pos.y , "Numbers must be integers");
|
2013-06-15 14:03:30 +08:00
|
|
|
int index = (pos.x * (_gridSize.height + 1) + pos.y) * 3;
|
|
|
|
float *vertArray = (float*)_vertices;
|
2012-04-19 14:35:52 +08:00
|
|
|
vertArray[index] = vertex.x;
|
|
|
|
vertArray[index+1] = vertex.y;
|
|
|
|
vertArray[index+2] = vertex.z;
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Grid3D::reuse(void)
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_reuseGrid > 0)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
memcpy(_originalVertices, _vertices, (_gridSize.width+1) * (_gridSize.height+1) * sizeof(Vec3));
|
2013-06-15 14:03:30 +08:00
|
|
|
--_reuseGrid;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// implementation of TiledGrid3D
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
TiledGrid3D::TiledGrid3D()
|
2013-12-18 17:47:20 +08:00
|
|
|
: _texCoordinates(nullptr)
|
|
|
|
, _vertices(nullptr)
|
|
|
|
, _originalVertices(nullptr)
|
|
|
|
, _indices(nullptr)
|
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
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
TiledGrid3D::~TiledGrid3D(void)
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +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
|
|
|
|
2015-07-08 17:47:04 +08:00
|
|
|
TiledGrid3D* TiledGrid3D::create(const Size& gridSize)
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
TiledGrid3D *ret= new (std::nothrow) TiledGrid3D();
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
if (ret)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2015-07-08 17:47:04 +08:00
|
|
|
if (ret->initWithSize(gridSize))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
ret->autorelease();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
delete ret;
|
|
|
|
ret = nullptr;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
return ret;
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2015-07-08 17:47:04 +08:00
|
|
|
TiledGrid3D* TiledGrid3D::create(const Size& gridSize, const Rect& rect)
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
TiledGrid3D *ret= new (std::nothrow) TiledGrid3D();
|
2015-07-08 17:47:04 +08:00
|
|
|
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
if (ret->initWithSize(gridSize, rect))
|
|
|
|
{
|
|
|
|
ret->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
delete ret;
|
|
|
|
ret = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2015-07-08 17:47:04 +08:00
|
|
|
TiledGrid3D* TiledGrid3D::create(const Size& gridSize, Texture2D *texture, bool flipped, const Rect& rect)
|
|
|
|
{
|
|
|
|
TiledGrid3D *ret= new (std::nothrow) TiledGrid3D();
|
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
if (ret)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2015-07-08 17:47:04 +08:00
|
|
|
if (ret->initWithSize(gridSize, texture, flipped, rect))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
ret->autorelease();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
delete ret;
|
|
|
|
ret = nullptr;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
2015-07-08 17:47:04 +08:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2015-07-08 17:47:04 +08:00
|
|
|
TiledGrid3D* TiledGrid3D::create(const Size& gridSize, Texture2D *texture, bool flipped)
|
|
|
|
{
|
|
|
|
TiledGrid3D *ret= new (std::nothrow) TiledGrid3D();
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
if (ret->initWithSize(gridSize, texture, flipped))
|
|
|
|
{
|
|
|
|
ret->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
delete ret;
|
|
|
|
ret = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
return ret;
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void TiledGrid3D::blit(void)
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
int n = _gridSize.width * _gridSize.height;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_shaderProgram->use();
|
|
|
|
_shaderProgram->setUniformsForBuiltins();
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
//
|
|
|
|
// Attributes
|
|
|
|
//
|
2014-05-13 10:12:56 +08:00
|
|
|
GL::enableVertexAttribs( GL::VERTEX_ATTRIB_FLAG_POSITION | GL::VERTEX_ATTRIB_FLAG_TEX_COORD );
|
2013-04-09 12:08:06 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// position
|
2013-07-25 17:48:22 +08:00
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, 0, _vertices);
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2012-09-17 15:02:24 +08:00
|
|
|
// texCoords
|
2014-05-09 03:34:26 +08:00
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, 0, _texCoordinates);
|
2012-03-16 13:42:53 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
glDrawElements(GL_TRIANGLES, (GLsizei)n*6, GL_UNSIGNED_SHORT, _indices);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-02-08 11:37:44 +08:00
|
|
|
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
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void TiledGrid3D::calculateVertexPoints(void)
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
float width = (float)_texture->getPixelsWide();
|
|
|
|
float height = (float)_texture->getPixelsHigh();
|
|
|
|
float imageH = _texture->getContentSizeInPixels().height;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
int numQuads = _gridSize.width * _gridSize.height;
|
|
|
|
CC_SAFE_FREE(_vertices);
|
|
|
|
CC_SAFE_FREE(_originalVertices);
|
|
|
|
CC_SAFE_FREE(_texCoordinates);
|
|
|
|
CC_SAFE_FREE(_indices);
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
_vertices = malloc(numQuads*4*sizeof(Vec3));
|
|
|
|
_originalVertices = malloc(numQuads*4*sizeof(Vec3));
|
|
|
|
_texCoordinates = malloc(numQuads*4*sizeof(Vec2));
|
2013-06-15 14:03:30 +08:00
|
|
|
_indices = (GLushort*)malloc(numQuads*6*sizeof(GLushort));
|
|
|
|
|
|
|
|
GLfloat *vertArray = (GLfloat*)_vertices;
|
|
|
|
GLfloat *texArray = (GLfloat*)_texCoordinates;
|
|
|
|
GLushort *idxArray = _indices;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
int x, y;
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
for( x = 0; x < _gridSize.width; x++ )
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
for( y = 0; y < _gridSize.height; y++ )
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2015-07-08 17:47:04 +08:00
|
|
|
float x1 = x * _step.x + _gridRect.origin.x;
|
2013-06-15 14:03:30 +08:00
|
|
|
float x2 = x1 + _step.x;
|
2015-07-08 17:47:04 +08:00
|
|
|
float y1 = y * _step.y + _gridRect.origin.y;
|
2013-06-15 14:03:30 +08:00
|
|
|
float y2 = y1 + _step.y;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
*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;
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_isTextureFlipped)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
memcpy(_originalVertices, _vertices, numQuads * 12 * sizeof(GLfloat));
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void TiledGrid3D::setTile(const Vec2& pos, const Quad3& coords)
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT( pos.x == (unsigned int)pos.x && pos.y == (unsigned int) pos.y , "Numbers must be integers");
|
2013-06-15 14:03:30 +08:00
|
|
|
int idx = (_gridSize.height * pos.x + pos.y) * 4 * 3;
|
|
|
|
float *vertArray = (float*)_vertices;
|
2013-07-05 16:49:22 +08:00
|
|
|
memcpy(&vertArray[idx], &coords, sizeof(Quad3));
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Quad3 TiledGrid3D::getOriginalTile(const Vec2& pos) const
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT( pos.x == (unsigned int)pos.x && pos.y == (unsigned int) pos.y , "Numbers must be integers");
|
2013-06-15 14:03:30 +08:00
|
|
|
int idx = (_gridSize.height * pos.x + pos.y) * 4 * 3;
|
|
|
|
float *vertArray = (float*)_originalVertices;
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
Quad3 ret;
|
|
|
|
memcpy(&ret, &vertArray[idx], sizeof(Quad3));
|
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
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Quad3 TiledGrid3D::getTile(const Vec2& pos) const
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT( pos.x == (unsigned int)pos.x && pos.y == (unsigned int) pos.y , "Numbers must be integers");
|
2013-06-15 14:03:30 +08:00
|
|
|
int idx = (_gridSize.height * pos.x + pos.y) * 4 * 3;
|
|
|
|
float *vertArray = (float*)_vertices;
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
Quad3 ret;
|
|
|
|
memcpy(&ret, &vertArray[idx], sizeof(Quad3));
|
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
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void TiledGrid3D::reuse(void)
|
2012-04-18 18:43:45 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_reuseGrid > 0)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
int numQuads = _gridSize.width * _gridSize.height;
|
2010-12-24 15:53:51 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
memcpy(_originalVertices, _vertices, numQuads * 12 * sizeof(GLfloat));
|
|
|
|
--_reuseGrid;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|