axmol/core/2d/Grid.h

313 lines
8.9 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.
Copyright (c) 2019-present Axmol Engine contributors (see AUTHORS.md).
2021-12-25 10:04:45 +08:00
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.
****************************************************************************/
#pragma once
#include "base/Ref.h"
#include "base/Types.h"
#include "base/Director.h"
#include "renderer/GroupCommand.h"
#include "renderer/CustomCommand.h"
#include "renderer/CallbackCommand.h"
2019-11-23 20:27:39 +08:00
NS_AX_BEGIN
2019-11-23 20:27:39 +08:00
class Texture2D;
// class Grabber;
// class GLProgram;
class Node;
namespace backend
{
class RenderTarget;
}
2019-11-23 20:27:39 +08:00
/**
* @addtogroup _2d
* @{
*/
/** Base class for Other grid.
2021-12-25 10:04:45 +08:00
*/
2022-07-16 10:43:05 +08:00
class AX_DLL GridBase : public Ref
2019-11-23 20:27:39 +08:00
{
public:
/**
Destructor.
* @js NA
* @lua NA
*/
virtual ~GridBase();
/**@{
Interface for custom action when before or after draw.
@js NA
*/
virtual void beforeBlit() {}
virtual void afterBlit() {}
/**@}*/
/**Interface used to blit the texture with grid to screen.*/
virtual void blit() = 0;
/**Interface, Reuse the grid vertices.*/
virtual void reuse() = 0;
/**Interface, Calculate the vertices used for the blit.*/
virtual void calculateVertexPoints() = 0;
/**@{
Init the Grid base.
@param gridSize the size of the grid.
@param texture The texture used for grab.
@param flipped whether or not the grab texture should be flip by Y or not.
@param rect The effective grid rect.
*/
2021-10-23 23:27:14 +08:00
bool initWithSize(const Vec2& gridSize);
bool initWithSize(const Vec2& gridSize, const Rect& rect);
2021-12-25 10:04:45 +08:00
bool initWithSize(const Vec2& gridSize, Texture2D* texture, bool flipped);
bool initWithSize(const Vec2& gridSize, Texture2D* texture, bool flipped, const Rect& rect);
2019-11-23 20:27:39 +08:00
/**@}*/
/** @{
Getter and setter of the active state of the grid.
*/
bool isActive() const { return _active; }
void setActive(bool active);
/**@}*/
/** Get number of times that the grid will be reused. */
int getReuseGrid() const { return _reuseGrid; }
/** Set number of times that the grid will be reused. */
void setReuseGrid(int reuseGrid) { _reuseGrid = reuseGrid; }
2021-10-24 14:09:59 +08:00
/** The size of the grid. */
2021-10-23 23:27:14 +08:00
const Vec2& getGridSize() const { return _gridSize; }
2019-11-23 20:27:39 +08:00
/**Set the size of the grid.*/
2021-10-23 23:27:14 +08:00
void setGridSize(const Vec2& gridSize) { _gridSize = gridSize; }
2019-11-23 20:27:39 +08:00
/** Pixels between the grids. */
const Vec2& getStep() const { return _step; }
/**Get the pixels between the grids.*/
void setStep(const Vec2& step) { _step = step; }
/** is texture flipped. */
bool isTextureFlipped() const { return _isTextureFlipped; }
/**Set the texture flipped or not.*/
void setTextureFlipped(bool flipped);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**@{
Init and reset the status when render effects by using the grid.
*/
void beforeDraw();
2021-12-25 10:04:45 +08:00
void afterDraw(Node* target);
2019-11-23 20:27:39 +08:00
/**@}*/
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**Change projection to 2D for grabbing.*/
void set2DProjection();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* @brief Set the effect grid rect.
* @param rect The effect grid rect.
*/
void setGridRect(const Rect& rect);
/**
* @brief Get the effect grid rect.
* @return Return the effect grid rect.
*/
const Rect& getGridRect() const { return _gridRect; }
protected:
void updateBlendState();
2021-12-25 10:04:45 +08:00
bool _active = false;
int _reuseGrid = 0;
2021-10-23 23:27:14 +08:00
Vec2 _gridSize;
2021-12-25 10:04:45 +08:00
Texture2D* _texture = nullptr;
2019-11-23 20:27:39 +08:00
Vec2 _step;
2021-12-25 10:04:45 +08:00
bool _isTextureFlipped = false;
2019-11-23 20:27:39 +08:00
Director::Projection _directorProjection = Director::Projection::_2D;
Rect _gridRect;
Color4F _clearColor = {0, 0, 0, 0};
CustomCommand _drawCommand;
2022-06-24 14:18:48 +08:00
//CallbackCommand _beforeDrawCommand;
//CallbackCommand _afterDrawCommand;
//CallbackCommand _beforeBlitCommand;
//CallbackCommand _afterBlitCommand;
// New
backend::RenderTarget* _oldRenderTarget = nullptr;
2021-12-25 10:04:45 +08:00
backend::RenderTarget* _renderTarget = nullptr;
2019-11-23 20:27:39 +08:00
backend::UniformLocation _mvpMatrixLocation;
backend::UniformLocation _textureLocation;
backend::ProgramState* _programState = nullptr;
2021-12-25 10:04:45 +08:00
BlendFunc _blendFunc;
2019-11-23 20:27:39 +08:00
};
/**
Grid3D is a 3D grid implementation. Each vertex has 3 dimensions: x,y,z
*/
2022-07-16 10:43:05 +08:00
class AX_DLL Grid3D : public GridBase
2019-11-23 20:27:39 +08:00
{
public:
/** create one Grid. */
2021-10-23 23:27:14 +08:00
static Grid3D* create(const Vec2& gridSize);
2019-11-23 20:27:39 +08:00
/** create one Grid. */
2021-10-23 23:27:14 +08:00
static Grid3D* create(const Vec2& gridSize, const Rect& rect);
2019-11-23 20:27:39 +08:00
/** create one Grid. */
2021-12-25 10:04:45 +08:00
static Grid3D* create(const Vec2& gridSize, Texture2D* texture, bool flipped);
2019-11-23 20:27:39 +08:00
/** create one Grid. */
2021-12-25 10:04:45 +08:00
static Grid3D* create(const Vec2& gridSize, Texture2D* texture, bool flipped, const Rect& rect);
2019-11-23 20:27:39 +08:00
/**
Constructor.
* @js ctor
*/
Grid3D();
/**
Destructor.
* @js NA
* @lua NA
*/
~Grid3D();
/** Returns the vertex at a given position.
* @js NA
* @lua NA
*/
Vec3 getVertex(const Vec2& pos) const;
/** Returns the original (non-transformed) vertex at a given position.
* @js NA
* @lua NA
*/
Vec3 getOriginalVertex(const Vec2& pos) const;
/** Sets a new vertex at a given position.
* @lua NA
*/
void setVertex(const Vec2& pos, const Vec3& vertex);
/**@{
Implementations for interfaces in base class.
*/
virtual void beforeBlit() override;
virtual void afterBlit() override;
virtual void blit() override;
virtual void reuse() override;
virtual void calculateVertexPoints() override;
/**@}*/
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**@{
Getter and Setter for depth test state when blit.
@js NA
*/
void setNeedDepthTestForBlit(bool neededDepthTest) { _needDepthTestForBlit = neededDepthTest; }
bool getNeedDepthTestForBlit() const { return _needDepthTestForBlit; }
/**@}*/
protected:
void updateVertexBuffer();
void updateVertexAndTexCoordinate();
2021-12-25 10:04:45 +08:00
void* _vertexBuffer = nullptr;
void* _texCoordinates = nullptr;
void* _vertices = nullptr;
void* _originalVertices = nullptr;
unsigned short* _indices = nullptr;
2019-11-23 20:27:39 +08:00
bool _needDepthTestForBlit = false;
2021-12-25 10:04:45 +08:00
bool _oldDepthTest = false;
bool _oldDepthWrite = false;
2019-11-23 20:27:39 +08:00
};
/**
TiledGrid3D is a 3D grid implementation. It differs from Grid3D in that
the tiles can be separated from the grid.
*/
2022-07-16 10:43:05 +08:00
class AX_DLL TiledGrid3D : public GridBase
2019-11-23 20:27:39 +08:00
{
public:
/** Create one Grid. */
2021-10-23 23:27:14 +08:00
static TiledGrid3D* create(const Vec2& gridSize);
2019-11-23 20:27:39 +08:00
/** Create one Grid. */
2021-10-23 23:27:14 +08:00
static TiledGrid3D* create(const Vec2& gridSize, const Rect& rect);
2019-11-23 20:27:39 +08:00
/** Create one Grid. */
2021-12-25 10:04:45 +08:00
static TiledGrid3D* create(const Vec2& gridSize, Texture2D* texture, bool flipped);
2019-11-23 20:27:39 +08:00
/** Create one Grid. */
2021-12-25 10:04:45 +08:00
static TiledGrid3D* create(const Vec2& gridSize, Texture2D* texture, bool flipped, const Rect& rect);
2019-11-23 20:27:39 +08:00
/**@{
Implementations for interfaces in base class.
*/
virtual void blit() override;
virtual void reuse() override;
virtual void calculateVertexPoints() override;
/**@}*/
/** Returns the tile at the given position.
* @js NA
* @lua NA
*/
Quad3 getTile(const Vec2& pos) const;
/** Returns the original tile (untransformed) at the given position.
* @js NA
* @lua NA
*/
Quad3 getOriginalTile(const Vec2& pos) const;
/** Sets a new tile.
* @lua NA
*/
void setTile(const Vec2& pos, const Quad3& coords);
protected:
/**
Constructor.
* @js ctor
*/
TiledGrid3D() = default;
/**
Destructor.
* @js NA
* @lua NA
*/
~TiledGrid3D();
void updateVertexBuffer();
void updateVertexAndTexCoordinate();
2021-12-25 10:04:45 +08:00
void* _texCoordinates = nullptr;
void* _vertices = nullptr;
void* _originalVertices = nullptr;
2019-11-23 20:27:39 +08:00
unsigned short* _indices = nullptr;
2021-12-25 10:04:45 +08:00
void* _vertexBuffer = nullptr;
2019-11-23 20:27:39 +08:00
};
// end of effects group
/// @}
NS_AX_END