axmol/core/2d/Grid.cpp

798 lines
25 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2009 On-Core
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2021-10-11 12:15:41 +08:00
Copyright (c) 2021 Bytedance Inc.
2022-10-01 16:24:52 +08:00
https://axmolengine.github.io/
2019-11-23 20:27:39 +08:00
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 "2d/Grid.h"
2019-11-23 20:27:39 +08:00
#include "base/Macros.h"
#include "base/Utils.h"
#include "2d/Node.h"
#include "renderer/Renderer.h"
#include "renderer/Texture2D.h"
#include "renderer/Shaders.h"
2019-11-23 20:27:39 +08:00
#include "renderer/backend/ProgramState.h"
#include "renderer/backend/Device.h"
#include "renderer/backend/RenderTarget.h"
#include "2d/Camera.h"
2019-11-23 20:27:39 +08:00
NS_AX_BEGIN
2019-11-23 20:27:39 +08:00
// implementation of GridBase
2021-10-23 23:27:14 +08:00
bool GridBase::initWithSize(const Vec2& gridSize)
2019-11-23 20:27:39 +08:00
{
return initWithSize(gridSize, Rect::ZERO);
}
2022-08-08 18:02:17 +08:00
bool GridBase::initWithSize(const Vec2& gridSize, const ax::Rect& rect)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
Director* director = Director::getInstance();
Vec2 s = director->getWinSizeInPixels();
2019-11-23 20:27:39 +08:00
auto POTWide = ccNextPOT((unsigned int)s.width);
auto POTHigh = ccNextPOT((unsigned int)s.height);
2021-12-25 10:04:45 +08:00
Texture2D* texture = new Texture2D();
2019-11-23 20:27:39 +08:00
backend::TextureDescriptor descriptor;
2021-12-25 10:04:45 +08:00
descriptor.width = POTWide;
descriptor.height = POTHigh;
descriptor.textureUsage = backend::TextureUsage::RENDER_TARGET;
descriptor.textureFormat = backend::PixelFormat::RGBA8;
texture->updateTextureDescriptor(descriptor);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
initWithSize(gridSize, texture, false, rect);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
texture->release();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return true;
}
2021-12-25 10:04:45 +08:00
bool GridBase::initWithSize(const Vec2& gridSize, Texture2D* texture, bool flipped)
2019-11-23 20:27:39 +08:00
{
return initWithSize(gridSize, texture, flipped, Rect::ZERO);
}
2021-12-25 10:04:45 +08:00
bool GridBase::initWithSize(const Vec2& gridSize, Texture2D* texture, bool flipped, const Rect& rect)
2019-11-23 20:27:39 +08:00
{
bool ret = true;
2021-12-25 10:04:45 +08:00
_active = false;
2019-11-23 20:27:39 +08:00
_reuseGrid = 0;
2021-12-25 10:04:45 +08:00
_gridSize = gridSize;
2019-11-23 20:27:39 +08:00
_texture = texture;
2022-07-16 10:43:05 +08:00
AX_SAFE_RETAIN(_texture);
2019-11-23 20:27:39 +08:00
_isTextureFlipped = flipped;
2022-07-16 10:43:05 +08:00
#ifdef AX_USE_METAL
2019-11-23 20:27:39 +08:00
_isTextureFlipped = !flipped;
#endif
2021-12-25 10:04:45 +08:00
if (rect.equals(Rect::ZERO))
{
2019-11-23 20:27:39 +08:00
auto size = _texture->getContentSize();
_gridRect.setRect(0, 0, size.width, size.height);
}
2021-12-25 10:04:45 +08:00
else
{
2019-11-23 20:27:39 +08:00
_gridRect = rect;
}
2021-12-25 10:04:45 +08:00
_step.x = _gridRect.size.width / _gridSize.width;
_step.y = _gridRect.size.height / _gridSize.height;
2019-11-23 20:27:39 +08:00
auto& pipelineDescriptor = _drawCommand.getPipelineDescriptor();
2022-07-16 10:43:05 +08:00
AX_SAFE_RELEASE(_programState);
2021-12-25 10:04:45 +08:00
auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_TEXTURE);
_programState = new backend::ProgramState(program);
2019-11-23 20:27:39 +08:00
pipelineDescriptor.programState = _programState;
2021-12-25 10:04:45 +08:00
_mvpMatrixLocation = pipelineDescriptor.programState->getUniformLocation("u_MVPMatrix");
_textureLocation = pipelineDescriptor.programState->getUniformLocation("u_tex0");
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
#define VERTEX_POSITION_SIZE 3
#define VERTEX_TEXCOORD_SIZE 2
2021-12-25 10:04:45 +08:00
uint32_t texcoordOffset = (VERTEX_POSITION_SIZE) * sizeof(float);
uint32_t totalSize = (VERTEX_POSITION_SIZE + VERTEX_TEXCOORD_SIZE) * sizeof(float);
2019-11-23 20:27:39 +08:00
const auto& attributeInfo = _programState->getProgram()->getActiveAttributes();
2021-12-25 10:04:45 +08:00
auto iter = attributeInfo.find("a_position");
if (iter != attributeInfo.end())
2019-11-23 20:27:39 +08:00
{
2023-08-10 18:36:45 +08:00
_programState->setVertexAttrib("a_position", iter->second.location, backend::VertexFormat::FLOAT3, 0, false);
2019-11-23 20:27:39 +08:00
}
iter = attributeInfo.find("a_texCoord");
2021-12-25 10:04:45 +08:00
if (iter != attributeInfo.end())
2019-11-23 20:27:39 +08:00
{
2023-08-10 18:36:45 +08:00
_programState->setVertexAttrib("a_texCoord", iter->second.location, backend::VertexFormat::FLOAT2, texcoordOffset,
2021-12-25 10:04:45 +08:00
false);
2019-11-23 20:27:39 +08:00
}
2023-08-10 18:36:45 +08:00
_programState->setVertexStride(totalSize);
2019-11-23 20:27:39 +08:00
calculateVertexPoints();
updateBlendState();
return ret;
}
void GridBase::updateBlendState()
{
2021-12-25 10:04:45 +08:00
if (!_texture || !_texture->hasPremultipliedAlpha())
2019-11-23 20:27:39 +08:00
{
_blendFunc = BlendFunc::ALPHA_NON_PREMULTIPLIED;
}
else
{
_blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
}
}
GridBase::~GridBase()
{
2022-07-16 10:43:05 +08:00
AXLOGINFO("deallocing GridBase: %p", this);
2019-11-23 20:27:39 +08:00
2022-07-16 10:43:05 +08:00
AX_SAFE_RELEASE(_renderTarget);
2021-12-25 10:04:45 +08:00
// TODO: ? why 2.0 comments this line: setActive(false);
2022-07-16 10:43:05 +08:00
AX_SAFE_RELEASE(_texture);
2021-12-25 10:04:45 +08:00
2022-07-16 10:43:05 +08:00
AX_SAFE_RELEASE(_programState);
2019-11-23 20:27:39 +08:00
}
// properties
void GridBase::setActive(bool active)
{
_active = active;
2021-12-25 10:04:45 +08:00
if (!active)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
Director* pDirector = Director::getInstance();
2019-11-23 20:27:39 +08:00
Director::Projection proj = pDirector->getProjection();
pDirector->setProjection(proj);
}
}
void GridBase::setTextureFlipped(bool flipped)
{
if (_isTextureFlipped != flipped)
{
_isTextureFlipped = flipped;
calculateVertexPoints();
}
}
void GridBase::set2DProjection()
{
2021-12-25 10:04:45 +08:00
Director* director = Director::getInstance();
Vec2 size = director->getWinSizeInPixels();
2019-11-23 20:27:39 +08:00
director->loadIdentityMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
Mat4 orthoMatrix;
Mat4::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);
}
2022-08-08 18:02:17 +08:00
void GridBase::setGridRect(const ax::Rect& rect)
2019-11-23 20:27:39 +08:00
{
_gridRect = rect;
}
void GridBase::beforeDraw()
{
// save projection
2021-12-25 10:04:45 +08:00
Director* director = Director::getInstance();
2019-11-23 20:27:39 +08:00
auto* renderer = director->getRenderer();
auto* groupCommand = renderer->getNextGroupCommand();
renderer->addCommand(groupCommand);
renderer->pushGroup(groupCommand->getRenderQueueID());
2019-11-23 20:27:39 +08:00
2023-03-08 08:34:17 +08:00
auto beforeDrawCommandFunc = [director, renderer, this]() -> void {
2019-11-23 20:27:39 +08:00
_directorProjection = director->getProjection();
set2DProjection();
2021-12-25 10:04:45 +08:00
Vec2 size = director->getWinSizeInPixels();
2019-11-23 20:27:39 +08:00
renderer->setViewPort(0, 0, (unsigned int)size.width, (unsigned int)size.height);
_oldRenderTarget = renderer->getRenderTarget();
2022-07-16 10:43:05 +08:00
AX_SAFE_RELEASE(_renderTarget);
2021-12-25 10:04:45 +08:00
_renderTarget =
backend::Device::getInstance()->newRenderTarget(TargetBufferFlags::COLOR, _texture->getBackendTexture());
renderer->setRenderTarget(_renderTarget);
2019-11-23 20:27:39 +08:00
};
2022-06-24 14:18:48 +08:00
renderer->addCallbackCommand(beforeDrawCommandFunc);
// since we use clearCommand, should call at here to avoid iterator damage
renderer->clear(TargetBufferFlags::COLOR, _clearColor, 1, 0, 0.0);
2019-11-23 20:27:39 +08:00
}
2022-08-08 18:02:17 +08:00
void GridBase::afterDraw(ax::Node* /*target*/)
2019-11-23 20:27:39 +08:00
{
// restore projection
2021-12-25 10:04:45 +08:00
Director* director = Director::getInstance();
auto renderer = director->getRenderer();
2019-11-23 20:27:39 +08:00
2022-06-24 14:18:48 +08:00
//_afterDrawCommand.func = [=]() -> void {
// director->setProjection(_directorProjection);
// const auto& vp = Camera::getDefaultViewport();
// renderer->setViewPort(vp.x, vp.y, vp.w, vp.h);
// renderer->setRenderTarget(_oldRenderTarget);
//};
2023-03-08 08:34:17 +08:00
renderer->addCallbackCommand([director, renderer, this]() -> void {
2019-11-23 20:27:39 +08:00
director->setProjection(_directorProjection);
const auto& vp = Camera::getDefaultViewport();
2023-08-10 18:36:45 +08:00
renderer->setViewPort(vp.x, vp.y, vp.w, vp.h);
renderer->setRenderTarget(_oldRenderTarget);
2022-06-24 14:18:48 +08:00
});
2019-11-23 20:27:39 +08:00
renderer->popGroup();
2021-12-25 10:04:45 +08:00
// if (target->getCamera()->isDirty())
// {
// Vec2 offset = target->getAnchorPointInPoints();
//
// //
// // FIXME: Camera should be applied in the AnchorPoint
// //
// kmGLTranslatef(offset.x, offset.y, 0);
// target->getCamera()->locate();
// kmGLTranslatef(-offset.x, -offset.y, 0);
// }
2019-11-23 20:27:39 +08:00
// restore projection for default FBO .fixed bug #543 #544
2021-12-25 10:04:45 +08:00
// TODO: Director::getInstance()->setProjection(Director::getInstance()->getProjection());
// TODO: Director::getInstance()->applyOrientation();
2022-06-24 14:18:48 +08:00
//_beforeBlitCommand.func = [=]() -> void { beforeBlit(); };
2023-03-08 08:34:17 +08:00
renderer->addCallbackCommand([this]() -> void { beforeBlit(); });
2019-11-23 20:27:39 +08:00
blit();
2022-06-24 14:18:48 +08:00
//_afterBlitCommand.func = [=]() -> void { afterBlit(); };
2023-03-08 08:34:17 +08:00
renderer->addCallbackCommand([this]() -> void { afterBlit(); });
2019-11-23 20:27:39 +08:00
}
// implementation of Grid3D
2021-10-23 23:27:14 +08:00
Grid3D* Grid3D::create(const Vec2& gridSize)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
Grid3D* ret = new Grid3D();
2021-12-08 00:11:53 +08:00
if (ret->initWithSize(gridSize))
2019-11-23 20:27:39 +08:00
{
2021-12-08 00:11:53 +08:00
ret->autorelease();
}
else
{
delete ret;
ret = nullptr;
2019-11-23 20:27:39 +08:00
}
return ret;
}
2021-10-23 23:27:14 +08:00
Grid3D* Grid3D::create(const Vec2& gridSize, const Rect& rect)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
Grid3D* ret = new Grid3D();
2021-12-08 00:11:53 +08:00
if (ret->initWithSize(gridSize, rect))
2019-11-23 20:27:39 +08:00
{
2021-12-08 00:11:53 +08:00
ret->autorelease();
}
else
{
delete ret;
ret = nullptr;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return ret;
}
2021-12-25 10:04:45 +08:00
Grid3D* Grid3D::create(const Vec2& gridSize, Texture2D* texture, bool flipped)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
Grid3D* ret = new Grid3D();
2021-12-08 00:11:53 +08:00
if (ret->initWithSize(gridSize, texture, flipped))
2019-11-23 20:27:39 +08:00
{
2021-12-08 00:11:53 +08:00
ret->autorelease();
}
else
{
delete ret;
ret = nullptr;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return ret;
}
2021-12-25 10:04:45 +08:00
Grid3D* Grid3D::create(const Vec2& gridSize, Texture2D* texture, bool flipped, const Rect& rect)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
Grid3D* ret = new Grid3D();
2021-12-08 00:11:53 +08:00
if (ret->initWithSize(gridSize, texture, flipped, rect))
2019-11-23 20:27:39 +08:00
{
2021-12-08 00:11:53 +08:00
ret->autorelease();
}
else
{
delete ret;
ret = nullptr;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return ret;
}
2021-12-25 10:04:45 +08:00
Grid3D::Grid3D() {}
2019-11-23 20:27:39 +08:00
Grid3D::~Grid3D()
{
2022-07-16 10:43:05 +08:00
AX_SAFE_FREE(_texCoordinates);
AX_SAFE_FREE(_vertices);
AX_SAFE_FREE(_indices);
AX_SAFE_FREE(_originalVertices);
AX_SAFE_FREE(_vertexBuffer);
2019-11-23 20:27:39 +08:00
}
void Grid3D::beforeBlit()
{
auto renderer = Director::getInstance()->getRenderer();
2021-12-25 10:04:45 +08:00
if (_needDepthTestForBlit)
2019-11-23 20:27:39 +08:00
{
_oldDepthTest = renderer->getDepthTest();
renderer->setDepthTest(true);
_oldDepthWrite = renderer->getDepthWrite();
renderer->setDepthWrite(true);
}
}
void Grid3D::afterBlit()
{
auto renderer = Director::getInstance()->getRenderer();
2021-12-25 10:04:45 +08:00
if (_needDepthTestForBlit)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
renderer->setDepthTest(_oldDepthTest);
renderer->setDepthWrite(_oldDepthWrite);
2019-11-23 20:27:39 +08:00
}
}
void Grid3D::blit()
{
updateVertexBuffer();
_drawCommand.init(0, _blendFunc);
Director::getInstance()->getRenderer()->addCommand(&_drawCommand);
2022-08-08 18:02:17 +08:00
ax::Mat4 projectionMat = Director::getInstance()->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
2021-12-25 10:04:45 +08:00
auto programState = _drawCommand.getPipelineDescriptor().programState;
2019-11-23 20:27:39 +08:00
programState->setUniform(_mvpMatrixLocation, projectionMat.m, sizeof(projectionMat.m));
programState->setTexture(_textureLocation, 0, _texture->getBackendTexture());
}
void Grid3D::calculateVertexPoints()
{
2021-12-25 10:04:45 +08:00
float width = (float)_texture->getPixelsWide();
2019-11-23 20:27:39 +08:00
float height = (float)_texture->getPixelsHigh();
float imageH = _texture->getContentSizeInPixels().height;
2022-07-16 10:43:05 +08:00
AX_SAFE_FREE(_vertices);
AX_SAFE_FREE(_originalVertices);
AX_SAFE_FREE(_texCoordinates);
AX_SAFE_FREE(_vertexBuffer);
AX_SAFE_FREE(_indices);
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
size_t numOfPoints = static_cast<size_t>((_gridSize.width + 1) * (_gridSize.height + 1));
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
_vertices = malloc(numOfPoints * sizeof(Vec3));
2019-11-23 20:27:39 +08:00
_originalVertices = malloc(numOfPoints * sizeof(Vec3));
2021-12-25 10:04:45 +08:00
_texCoordinates = malloc(numOfPoints * sizeof(Vec2));
_vertexBuffer = malloc(numOfPoints * (sizeof(Vec3) + sizeof(Vec2)));
_indices =
(unsigned short*)malloc(static_cast<size_t>(_gridSize.width * _gridSize.height * sizeof(unsigned short) * 6));
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
float* vertArray = (float*)_vertices;
float* texArray = (float*)_texCoordinates;
unsigned short* idxArray = _indices;
2019-11-23 20:27:39 +08:00
2021-10-11 12:15:41 +08:00
for (int x = 0; x < _gridSize.width; ++x)
2019-11-23 20:27:39 +08:00
{
2021-10-11 12:15:41 +08:00
for (int y = 0; y < _gridSize.height; ++y)
2019-11-23 20:27:39 +08:00
{
int idx = (int)(y * _gridSize.width) + x;
float x1 = x * _step.x + _gridRect.origin.x;
float x2 = x1 + _step.x;
float y1 = y * _step.y + _gridRect.origin.y;
2021-12-25 10:04:45 +08:00
float y2 = y1 + _step.y;
2019-11-23 20:27:39 +08:00
unsigned short a = (unsigned short)(x * (_gridSize.height + 1) + y);
unsigned short b = (unsigned short)((x + 1) * (_gridSize.height + 1) + y);
unsigned short c = (unsigned short)((x + 1) * (_gridSize.height + 1) + (y + 1));
unsigned short d = (unsigned short)(x * (_gridSize.height + 1) + (y + 1));
unsigned short tempidx[6] = {a, b, d, b, c, d};
2021-12-25 10:04:45 +08:00
memcpy(&idxArray[6 * idx], tempidx, 6 * sizeof(unsigned short));
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
int l1[4] = {a * 3, b * 3, c * 3, d * 3};
2019-11-23 20:27:39 +08:00
Vec3 e(x1, y1, 0);
Vec3 f(x2, y1, 0);
Vec3 g(x2, y2, 0);
Vec3 h(x1, y2, 0);
Vec3 l2[4] = {e, f, g, h};
2021-12-25 10:04:45 +08:00
int tex1[4] = {a * 2, b * 2, c * 2, d * 2};
2019-11-23 20:27:39 +08:00
Vec2 Tex2F[4] = {Vec2(x1, y1), Vec2(x2, y1), Vec2(x2, y2), Vec2(x1, y2)};
2021-10-11 12:15:41 +08:00
for (int i = 0; i < 4; ++i)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
vertArray[l1[i]] = l2[i].x;
2019-11-23 20:27:39 +08:00
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;
}
}
}
}
updateVertexAndTexCoordinate();
2021-12-25 10:04:45 +08:00
memcpy(_originalVertices, _vertices,
static_cast<size_t>((_gridSize.width + 1) * (_gridSize.height + 1) * sizeof(Vec3)));
2019-11-23 20:27:39 +08:00
}
Vec3 Grid3D::getVertex(const Vec2& pos) const
{
2022-07-16 10:43:05 +08:00
AXASSERT(pos.x == (unsigned int)pos.x && pos.y == (unsigned int)pos.y, "Numbers must be integers");
2021-12-25 10:04:45 +08:00
int index = (int)(pos.x * (_gridSize.height + 1) + pos.y) * 3;
float* vertArray = (float*)_vertices;
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
Vec3 vert(vertArray[index], vertArray[index + 1], vertArray[index + 2]);
2019-11-23 20:27:39 +08:00
return vert;
}
Vec3 Grid3D::getOriginalVertex(const Vec2& pos) const
{
2022-07-16 10:43:05 +08:00
AXASSERT(pos.x == (unsigned int)pos.x && pos.y == (unsigned int)pos.y, "Numbers must be integers");
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
int index = (int)(pos.x * (_gridSize.height + 1) + pos.y) * 3;
float* vertArray = (float*)_originalVertices;
Vec3 vert(vertArray[index], vertArray[index + 1], vertArray[index + 2]);
2019-11-23 20:27:39 +08:00
return vert;
}
void Grid3D::setVertex(const Vec2& pos, const Vec3& vertex)
{
2022-07-16 10:43:05 +08:00
AXASSERT(pos.x == (unsigned int)pos.x && pos.y == (unsigned int)pos.y, "Numbers must be integers");
2021-12-25 10:04:45 +08:00
int index = (int)(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;
2019-11-23 20:27:39 +08:00
}
void Grid3D::reuse()
{
if (_reuseGrid > 0)
{
2021-12-25 10:04:45 +08:00
memcpy(_originalVertices, _vertices,
static_cast<size_t>((_gridSize.width + 1) * (_gridSize.height + 1) * sizeof(Vec3)));
2019-11-23 20:27:39 +08:00
--_reuseGrid;
}
}
void Grid3D::updateVertexBuffer()
{
2021-12-25 10:04:45 +08:00
size_t numOfPoints = static_cast<size_t>((_gridSize.width + 1) * (_gridSize.height + 1));
2019-11-23 20:27:39 +08:00
auto tempVecPointer = (Vec3*)_vertices;
for (size_t i = 0; i < numOfPoints; ++i)
{
auto offset = i * (sizeof(Vec3) + sizeof(Vec2));
memcpy((char*)_vertexBuffer + offset, &tempVecPointer[i], sizeof(Vec3));
}
2021-12-25 10:04:45 +08:00
_drawCommand.updateVertexBuffer(_vertexBuffer,
(unsigned int)(numOfPoints * sizeof(Vec3) + numOfPoints * sizeof(Vec2)));
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
_drawCommand.updateIndexBuffer(
_indices, static_cast<unsigned int>(_gridSize.width * _gridSize.height * 6 * sizeof(unsigned short)));
2019-11-23 20:27:39 +08:00
}
void Grid3D::updateVertexAndTexCoordinate()
{
2021-12-25 10:04:45 +08:00
unsigned int numOfPoints = static_cast<unsigned int>((_gridSize.width + 1) * (_gridSize.height + 1));
auto tempVecPointer = (Vec3*)_vertices;
auto tempTexPointer = (Vec2*)_texCoordinates;
2019-11-23 20:27:39 +08:00
for (unsigned int i = 0; i < numOfPoints; ++i)
{
auto offset = i * (sizeof(Vec3) + sizeof(Vec2));
memcpy((char*)_vertexBuffer + offset, &tempVecPointer[i], sizeof(Vec3));
memcpy((char*)_vertexBuffer + offset + sizeof(Vec3), &tempTexPointer[i], sizeof(Vec2));
}
2021-12-25 10:04:45 +08:00
_drawCommand.createVertexBuffer((unsigned int)(sizeof(Vec3) + sizeof(Vec2)), numOfPoints,
CustomCommand::BufferUsage::DYNAMIC);
2019-11-23 20:27:39 +08:00
_drawCommand.updateVertexBuffer(_vertexBuffer, numOfPoints * sizeof(Vec3) + numOfPoints * sizeof(Vec2));
unsigned int capacity = (unsigned int)(_gridSize.width * _gridSize.height) * 6;
_drawCommand.createIndexBuffer(CustomCommand::IndexFormat::U_SHORT, capacity, CustomCommand::BufferUsage::DYNAMIC);
_drawCommand.updateIndexBuffer(_indices, capacity * sizeof(unsigned short));
}
// implementation of TiledGrid3D
TiledGrid3D::~TiledGrid3D()
{
2022-07-16 10:43:05 +08:00
AX_SAFE_FREE(_texCoordinates);
AX_SAFE_FREE(_vertices);
AX_SAFE_FREE(_originalVertices);
AX_SAFE_FREE(_indices);
2019-11-23 20:27:39 +08:00
}
2021-10-23 23:27:14 +08:00
TiledGrid3D* TiledGrid3D::create(const Vec2& gridSize)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
TiledGrid3D* ret = new TiledGrid3D();
2019-11-23 20:27:39 +08:00
2021-12-08 00:11:53 +08:00
if (ret->initWithSize(gridSize))
2019-11-23 20:27:39 +08:00
{
2021-12-08 00:11:53 +08:00
ret->autorelease();
}
else
{
delete ret;
ret = nullptr;
2019-11-23 20:27:39 +08:00
}
return ret;
}
2021-10-23 23:27:14 +08:00
TiledGrid3D* TiledGrid3D::create(const Vec2& gridSize, const Rect& rect)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
TiledGrid3D* ret = new TiledGrid3D();
2021-12-08 00:11:53 +08:00
if (ret->initWithSize(gridSize, rect))
2019-11-23 20:27:39 +08:00
{
2021-12-08 00:11:53 +08:00
ret->autorelease();
}
else
{
delete ret;
ret = nullptr;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return ret;
}
2021-12-25 10:04:45 +08:00
TiledGrid3D* TiledGrid3D::create(const Vec2& gridSize, Texture2D* texture, bool flipped, const Rect& rect)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
TiledGrid3D* ret = new TiledGrid3D();
2021-12-08 00:11:53 +08:00
if (ret->initWithSize(gridSize, texture, flipped, rect))
2019-11-23 20:27:39 +08:00
{
2021-12-08 00:11:53 +08:00
ret->autorelease();
}
else
{
delete ret;
ret = nullptr;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return ret;
}
2021-12-25 10:04:45 +08:00
TiledGrid3D* TiledGrid3D::create(const Vec2& gridSize, Texture2D* texture, bool flipped)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
TiledGrid3D* ret = new TiledGrid3D();
2021-12-08 00:11:53 +08:00
if (ret->initWithSize(gridSize, texture, flipped))
2019-11-23 20:27:39 +08:00
{
2021-12-08 00:11:53 +08:00
ret->autorelease();
}
else
{
delete ret;
ret = nullptr;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return ret;
}
void TiledGrid3D::blit()
{
updateVertexBuffer();
Director::getInstance()->getRenderer()->addCommand(&_drawCommand);
2022-08-08 18:02:17 +08:00
ax::Mat4 projectionMat = Director::getInstance()->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
2021-12-25 10:04:45 +08:00
auto programState = _drawCommand.getPipelineDescriptor().programState;
2019-11-23 20:27:39 +08:00
programState->setUniform(_mvpMatrixLocation, projectionMat.m, sizeof(projectionMat.m));
programState->setTexture(_textureLocation, 0, _texture->getBackendTexture());
}
void TiledGrid3D::calculateVertexPoints()
{
2021-12-25 10:04:45 +08:00
float width = (float)_texture->getPixelsWide();
2019-11-23 20:27:39 +08:00
float height = (float)_texture->getPixelsHigh();
float imageH = _texture->getContentSizeInPixels().height;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
int numQuads = (int)(_gridSize.width * _gridSize.height);
2022-07-16 10:43:05 +08:00
AX_SAFE_FREE(_vertices);
AX_SAFE_FREE(_originalVertices);
AX_SAFE_FREE(_texCoordinates);
AX_SAFE_FREE(_indices);
AX_SAFE_FREE(_vertexBuffer);
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
_vertices = malloc(numQuads * 4 * sizeof(Vec3));
_originalVertices = malloc(numQuads * 4 * sizeof(Vec3));
_texCoordinates = malloc(numQuads * 4 * sizeof(Vec2));
_indices = (unsigned short*)malloc(numQuads * 6 * sizeof(unsigned short));
_vertexBuffer = malloc(numQuads * 4 * (sizeof(Vec3) + sizeof(Vec2)));
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
float* vertArray = (float*)_vertices;
float* texArray = (float*)_texCoordinates;
unsigned short* idxArray = _indices;
2019-11-23 20:27:39 +08:00
for (int x = 0; x < _gridSize.width; ++x)
{
for (int y = 0; y < _gridSize.height; ++y)
{
float x1 = x * _step.x + _gridRect.origin.x;
float x2 = x1 + _step.x;
float y1 = y * _step.y + _gridRect.origin.y;
float y2 = y1 + _step.y;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +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;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
float newY1 = y1;
float newY2 = y2;
2021-12-25 10:04:45 +08:00
if (_isTextureFlipped)
2019-11-23 20:27:39 +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;
}
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
for (int x = 0; x < numQuads; x++)
{
2021-12-25 10:04:45 +08:00
idxArray[x * 6 + 0] = (unsigned short)(x * 4 + 0);
idxArray[x * 6 + 1] = (unsigned short)(x * 4 + 1);
idxArray[x * 6 + 2] = (unsigned short)(x * 4 + 2);
idxArray[x * 6 + 3] = (unsigned short)(x * 4 + 1);
idxArray[x * 6 + 4] = (unsigned short)(x * 4 + 2);
idxArray[x * 6 + 5] = (unsigned short)(x * 4 + 3);
2019-11-23 20:27:39 +08:00
}
updateVertexAndTexCoordinate();
memcpy(_originalVertices, _vertices, numQuads * 12 * sizeof(float));
}
void TiledGrid3D::setTile(const Vec2& pos, const Quad3& coords)
{
2022-07-16 10:43:05 +08:00
AXASSERT(pos.x == (unsigned int)pos.x && pos.y == (unsigned int)pos.y, "Numbers must be integers");
2021-12-25 10:04:45 +08:00
int idx = (int)(_gridSize.height * pos.x + pos.y) * 4 * 3;
float* vertArray = (float*)_vertices;
2019-11-23 20:27:39 +08:00
memcpy(&vertArray[idx], &coords, sizeof(Quad3));
}
Quad3 TiledGrid3D::getOriginalTile(const Vec2& pos) const
{
2022-07-16 10:43:05 +08:00
AXASSERT(pos.x == (unsigned int)pos.x && pos.y == (unsigned int)pos.y, "Numbers must be integers");
2021-12-25 10:04:45 +08:00
int idx = (int)(_gridSize.height * pos.x + pos.y) * 4 * 3;
float* vertArray = (float*)_originalVertices;
2019-11-23 20:27:39 +08:00
Quad3 ret;
memcpy(&ret, &vertArray[idx], sizeof(Quad3));
return ret;
}
Quad3 TiledGrid3D::getTile(const Vec2& pos) const
{
2022-07-16 10:43:05 +08:00
AXASSERT(pos.x == (unsigned int)pos.x && pos.y == (unsigned int)pos.y, "Numbers must be integers");
2021-12-25 10:04:45 +08:00
int idx = (int)(_gridSize.height * pos.x + pos.y) * 4 * 3;
float* vertArray = (float*)_vertices;
2019-11-23 20:27:39 +08:00
Quad3 ret;
memcpy(&ret, &vertArray[idx], sizeof(Quad3));
return ret;
}
void TiledGrid3D::reuse()
{
if (_reuseGrid > 0)
{
int numQuads = (int)(_gridSize.width * _gridSize.height);
memcpy(_originalVertices, _vertices, numQuads * 12 * sizeof(float));
--_reuseGrid;
}
}
void TiledGrid3D::updateVertexBuffer()
{
2021-12-25 10:04:45 +08:00
size_t gradSize = static_cast<size_t>(_gridSize.width * _gridSize.height);
size_t numOfPoints = gradSize * 4;
2019-11-23 20:27:39 +08:00
auto tempVecPointer = (Vec3*)_vertices;
for (size_t i = 0; i < numOfPoints; ++i)
{
auto offset = i * (sizeof(Vec3) + sizeof(Vec2));
memcpy((char*)_vertexBuffer + offset, &tempVecPointer[i], sizeof(Vec3));
}
2021-12-25 10:04:45 +08:00
_drawCommand.updateVertexBuffer(_vertexBuffer,
(unsigned int)(numOfPoints * sizeof(Vec3) + numOfPoints * sizeof(Vec2)));
2019-11-23 20:27:39 +08:00
_drawCommand.updateIndexBuffer(_indices, gradSize * 6 * sizeof(unsigned short));
}
void TiledGrid3D::updateVertexAndTexCoordinate()
{
2021-12-25 10:04:45 +08:00
size_t gradSize = static_cast<size_t>(_gridSize.width * _gridSize.height);
auto numOfPoints = gradSize * 4;
2019-11-23 20:27:39 +08:00
auto tempVecPointer = (Vec3*)_vertices;
auto tempTexPointer = (Vec2*)_texCoordinates;
for (size_t i = 0; i < numOfPoints; ++i)
{
auto offset = i * (sizeof(Vec3) + sizeof(Vec2));
memcpy((char*)_vertexBuffer + offset, &tempVecPointer[i], sizeof(Vec3));
memcpy((char*)_vertexBuffer + offset + sizeof(Vec3), &tempTexPointer[i], sizeof(Vec2));
}
2021-12-25 10:04:45 +08:00
_drawCommand.createVertexBuffer((sizeof(Vec3) + sizeof(Vec2)), numOfPoints, CustomCommand::BufferUsage::DYNAMIC);
2019-11-23 20:27:39 +08:00
_drawCommand.updateVertexBuffer(_vertexBuffer, numOfPoints * sizeof(Vec3) + numOfPoints * sizeof(Vec2));
2021-12-25 10:04:45 +08:00
_drawCommand.createIndexBuffer(CustomCommand::IndexFormat::U_SHORT, gradSize * 6,
CustomCommand::BufferUsage::DYNAMIC);
2019-11-23 20:27:39 +08:00
_drawCommand.updateIndexBuffer(_indices, gradSize * 6 * sizeof(unsigned short));
}
NS_AX_END