2019-11-23 20:27:39 +08:00
|
|
|
/* Copyright (c) 2012 Scott Lembcke and Howling Moon Software
|
|
|
|
* Copyright (c) 2012 cocos2d-x.org
|
|
|
|
* Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
* Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2023-12-08 00:13:39 +08:00
|
|
|
* Copyright (c) 2019-present Axmol Engine contributors (see AUTHORS.md).
|
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.
|
|
|
|
*/
|
|
|
|
|
2023-06-11 13:08:08 +08:00
|
|
|
#include "2d/DrawNode.h"
|
2024-09-06 21:31:18 +08:00
|
|
|
#include <stddef.h>
|
2023-06-11 13:08:08 +08:00
|
|
|
#include "base/Types.h"
|
|
|
|
#include "base/EventType.h"
|
|
|
|
#include "base/Configuration.h"
|
|
|
|
#include "renderer/Renderer.h"
|
|
|
|
#include "base/Director.h"
|
|
|
|
#include "base/EventListenerCustom.h"
|
|
|
|
#include "base/EventDispatcher.h"
|
|
|
|
#include "2d/ActionCatmullRom.h"
|
|
|
|
#include "base/Utils.h"
|
|
|
|
#include "renderer/Shaders.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
#include "renderer/backend/ProgramState.h"
|
2023-11-16 12:06:39 +08:00
|
|
|
#include "poly2tri/poly2tri.h"
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2024-08-26 00:25:33 +08:00
|
|
|
namespace ax
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2024-09-06 21:31:18 +08:00
|
|
|
#if defined(_WIN32)
|
|
|
|
# pragma push_macro("TRANSPARENT")
|
|
|
|
# undef TRANSPARENT
|
|
|
|
#endif
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2023-11-16 12:06:39 +08:00
|
|
|
/** Is a polygon convex?
|
|
|
|
* @param verts A pointer to point coordinates.
|
|
|
|
* @param count The number of verts measured in points.
|
|
|
|
*/
|
2024-09-06 21:31:18 +08:00
|
|
|
static bool isConvex(const Vec2* verts, int count)
|
2023-11-16 12:06:39 +08:00
|
|
|
{
|
|
|
|
bool isPositive = false, isNegative = false;
|
2024-09-06 21:31:18 +08:00
|
|
|
for (unsigned int i = 0; i < count; i++)
|
2023-11-16 12:06:39 +08:00
|
|
|
{
|
2024-09-06 21:31:18 +08:00
|
|
|
auto& A = verts[i];
|
|
|
|
auto& B = verts[(i + 1) % count];
|
|
|
|
auto& C = verts[(i + 2) % count];
|
2023-11-16 12:06:39 +08:00
|
|
|
|
|
|
|
double crossProduct = (B.x - A.x) * (C.y - B.y) - (B.y - A.y) * (C.x - B.x);
|
|
|
|
|
|
|
|
if (crossProduct > 0)
|
|
|
|
isPositive = true;
|
|
|
|
else if (crossProduct < 0)
|
|
|
|
isNegative = true;
|
|
|
|
|
|
|
|
if (isPositive && isNegative)
|
2024-09-06 21:31:18 +08:00
|
|
|
return false; // is concave
|
2023-11-16 12:06:39 +08:00
|
|
|
}
|
2024-09-06 21:31:18 +08:00
|
|
|
return true; // is convex
|
2023-11-16 12:06:39 +08:00
|
|
|
}
|
|
|
|
|
2024-09-06 21:31:18 +08:00
|
|
|
DrawNode::DrawNode(float lineWidth) : _lineWidth(lineWidth), _defaultLineWidth(lineWidth)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
|
2024-09-06 21:31:18 +08:00
|
|
|
|
|
|
|
properties.setDefaultValues();
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
#if AX_ENABLE_CACHE_TEXTURE_DATA
|
2021-12-25 10:04:45 +08:00
|
|
|
// TODO new-renderer: interface setupBuffer removal
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
// Need to listen the event only when not use batchnode, because it will use VBO
|
2024-09-06 21:31:18 +08:00
|
|
|
// auto listener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, [this](EventCustom* event){
|
|
|
|
// /** listen the event that renderer was recreated on Android/WP8 */
|
|
|
|
// this->setupBuffer();
|
|
|
|
// });
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2024-09-06 21:31:18 +08:00
|
|
|
// _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
2019-11-23 20:27:39 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
DrawNode::~DrawNode()
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_FREE(_bufferTriangle);
|
2020-10-26 14:49:14 +08:00
|
|
|
freeShaderInternal(_customCommandTriangle);
|
2024-09-06 21:31:18 +08:00
|
|
|
|
|
|
|
AX_SAFE_FREE(_bufferPoint);
|
2020-10-26 14:49:14 +08:00
|
|
|
freeShaderInternal(_customCommandPoint);
|
2024-09-06 21:31:18 +08:00
|
|
|
|
|
|
|
AX_SAFE_FREE(_bufferLine);
|
2020-10-26 14:49:14 +08:00
|
|
|
freeShaderInternal(_customCommandLine);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
DrawNode* DrawNode::create(float defaultLineWidth)
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
DrawNode* ret = new DrawNode(defaultLineWidth);
|
|
|
|
if (ret->init())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
ret->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_DELETE(ret);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2024-09-06 21:31:18 +08:00
|
|
|
void DrawNode::ensureCapacityTriangle(int count)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(count >= 0, "capacity must be >= 0");
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
if (_bufferCountTriangle + count > _bufferCapacityTriangle)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2020-10-26 14:49:14 +08:00
|
|
|
_bufferCapacityTriangle += MAX(_bufferCapacityTriangle, count);
|
|
|
|
_bufferTriangle = (V2F_C4B_T2F*)realloc(_bufferTriangle, _bufferCapacityTriangle * sizeof(V2F_C4B_T2F));
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
_customCommandTriangle.createVertexBuffer(sizeof(V2F_C4B_T2F), _bufferCapacityTriangle,
|
|
|
|
CustomCommand::BufferUsage::STATIC);
|
2020-10-26 14:49:14 +08:00
|
|
|
_customCommandTriangle.updateVertexBuffer(_bufferTriangle, _bufferCapacityTriangle * sizeof(V2F_C4B_T2F));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-06 21:31:18 +08:00
|
|
|
void DrawNode::ensureCapacityPoint(int count)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(count >= 0, "capacity must be >= 0");
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
if (_bufferCountPoint + count > _bufferCapacityPoint)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2020-10-26 14:49:14 +08:00
|
|
|
_bufferCapacityPoint += MAX(_bufferCapacityPoint, count);
|
2021-12-25 10:04:45 +08:00
|
|
|
_bufferPoint = (V2F_C4B_T2F*)realloc(_bufferPoint, _bufferCapacityPoint * sizeof(V2F_C4B_T2F));
|
|
|
|
|
|
|
|
_customCommandPoint.createVertexBuffer(sizeof(V2F_C4B_T2F), _bufferCapacityPoint,
|
|
|
|
CustomCommand::BufferUsage::STATIC);
|
|
|
|
_customCommandPoint.updateVertexBuffer(_bufferPoint, _bufferCapacityPoint * sizeof(V2F_C4B_T2F));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-06 21:31:18 +08:00
|
|
|
void DrawNode::ensureCapacityLine(int count)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(count >= 0, "capacity must be >= 0");
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
if (_bufferCountLine + count > _bufferCapacityLine)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2020-10-26 14:49:14 +08:00
|
|
|
_bufferCapacityLine += MAX(_bufferCapacityLine, count);
|
2021-12-25 10:04:45 +08:00
|
|
|
_bufferLine = (V2F_C4B_T2F*)realloc(_bufferLine, _bufferCapacityLine * sizeof(V2F_C4B_T2F));
|
|
|
|
|
|
|
|
_customCommandLine.createVertexBuffer(sizeof(V2F_C4B_T2F), _bufferCapacityLine,
|
|
|
|
CustomCommand::BufferUsage::STATIC);
|
|
|
|
_customCommandLine.updateVertexBuffer(_bufferLine, _bufferCapacityLine * sizeof(V2F_C4B_T2F));
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DrawNode::init()
|
|
|
|
{
|
|
|
|
_blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
|
|
|
|
updateShader();
|
2024-09-06 21:31:18 +08:00
|
|
|
ensureCapacityTriangle(512);
|
2020-10-26 14:49:14 +08:00
|
|
|
_dirtyTriangle = true;
|
2024-09-06 21:31:18 +08:00
|
|
|
|
|
|
|
ensureCapacityPoint(64);
|
|
|
|
ensureCapacityLine(256);
|
|
|
|
_dirtyLine = true;
|
|
|
|
_dirtyPoint = true;
|
2020-10-26 14:49:14 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DrawNode::updateShader()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
updateShaderInternal(_customCommandTriangle, backend::ProgramType::POSITION_COLOR_LENGTH_TEXTURE,
|
|
|
|
CustomCommand::DrawType::ARRAY, CustomCommand::PrimitiveType::TRIANGLE);
|
2020-10-26 14:49:14 +08:00
|
|
|
|
|
|
|
updateShaderInternal(_customCommandPoint, backend::ProgramType::POSITION_COLOR_TEXTURE_AS_POINTSIZE,
|
2021-12-25 10:04:45 +08:00
|
|
|
CustomCommand::DrawType::ARRAY, CustomCommand::PrimitiveType::POINT);
|
2020-10-26 14:49:14 +08:00
|
|
|
|
|
|
|
updateShaderInternal(_customCommandLine, backend::ProgramType::POSITION_COLOR_LENGTH_TEXTURE,
|
2021-12-25 10:04:45 +08:00
|
|
|
CustomCommand::DrawType::ARRAY, CustomCommand::PrimitiveType::LINE);
|
2020-10-26 14:49:14 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void DrawNode::updateShaderInternal(CustomCommand& cmd,
|
|
|
|
uint32_t programType,
|
|
|
|
CustomCommand::DrawType drawType,
|
|
|
|
CustomCommand::PrimitiveType primitiveType)
|
2020-10-26 14:49:14 +08:00
|
|
|
{
|
|
|
|
auto& pipelinePS = cmd.getPipelineDescriptor().programState;
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_RELEASE(pipelinePS);
|
2020-10-26 14:49:14 +08:00
|
|
|
|
|
|
|
auto program = backend::Program::getBuiltinProgram(programType);
|
2021-12-25 10:04:45 +08:00
|
|
|
pipelinePS = new backend::ProgramState(program);
|
2022-10-20 20:22:28 +08:00
|
|
|
setVertexLayout(cmd);
|
2020-10-26 14:49:14 +08:00
|
|
|
cmd.setPrimitiveType(primitiveType);
|
|
|
|
cmd.setDrawType(drawType);
|
|
|
|
}
|
|
|
|
|
2022-10-20 20:22:28 +08:00
|
|
|
void DrawNode::setVertexLayout(CustomCommand& cmd)
|
|
|
|
{
|
|
|
|
auto* programState = cmd.getPipelineDescriptor().programState;
|
2023-09-02 19:56:50 +08:00
|
|
|
programState->validateSharedVertexLayout(backend::VertexLayoutType::DrawNode);
|
2022-10-20 20:22:28 +08:00
|
|
|
}
|
|
|
|
|
2020-10-26 14:49:14 +08:00
|
|
|
void DrawNode::freeShaderInternal(CustomCommand& cmd)
|
2024-08-15 12:14:02 +08:00
|
|
|
{
|
2020-10-26 14:49:14 +08:00
|
|
|
auto& pipelinePS = cmd.getPipelineDescriptor().programState;
|
2022-07-16 10:43:05 +08:00
|
|
|
AX_SAFE_RELEASE_NULL(pipelinePS);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void DrawNode::updateBlendState(CustomCommand& cmd)
|
|
|
|
{
|
|
|
|
backend::BlendDescriptor& blendDescriptor = cmd.getPipelineDescriptor().blendDescriptor;
|
2021-12-25 10:04:45 +08:00
|
|
|
blendDescriptor.blendEnabled = true;
|
2019-11-23 20:27:39 +08:00
|
|
|
if (_blendFunc == BlendFunc::ALPHA_NON_PREMULTIPLIED)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
blendDescriptor.sourceRGBBlendFactor = backend::BlendFactor::SRC_ALPHA;
|
|
|
|
blendDescriptor.destinationRGBBlendFactor = backend::BlendFactor::ONE_MINUS_SRC_ALPHA;
|
|
|
|
blendDescriptor.sourceAlphaBlendFactor = backend::BlendFactor::SRC_ALPHA;
|
2019-11-23 20:27:39 +08:00
|
|
|
blendDescriptor.destinationAlphaBlendFactor = backend::BlendFactor::ONE_MINUS_SRC_ALPHA;
|
|
|
|
setOpacityModifyRGB(false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
blendDescriptor.sourceRGBBlendFactor = backend::BlendFactor::ONE;
|
|
|
|
blendDescriptor.destinationRGBBlendFactor = backend::BlendFactor::ONE_MINUS_SRC_ALPHA;
|
|
|
|
blendDescriptor.sourceAlphaBlendFactor = backend::BlendFactor::ONE;
|
2019-11-23 20:27:39 +08:00
|
|
|
blendDescriptor.destinationAlphaBlendFactor = backend::BlendFactor::ONE_MINUS_SRC_ALPHA;
|
|
|
|
setOpacityModifyRGB(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void DrawNode::updateUniforms(const Mat4& transform, CustomCommand& cmd)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
auto& pipelineDescriptor = cmd.getPipelineDescriptor();
|
2021-12-25 10:04:45 +08:00
|
|
|
const auto& matrixP = _director->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
|
|
|
|
Mat4 matrixMVP = matrixP * transform;
|
|
|
|
auto mvpLocation = pipelineDescriptor.programState->getUniformLocation("u_MVPMatrix");
|
2019-11-23 20:27:39 +08:00
|
|
|
pipelineDescriptor.programState->setUniform(mvpLocation, matrixMVP.m, sizeof(matrixMVP.m));
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
float alpha = _displayedOpacity / 255.0f;
|
2019-11-23 20:27:39 +08:00
|
|
|
auto alphaUniformLocation = pipelineDescriptor.programState->getUniformLocation("u_alpha");
|
|
|
|
pipelineDescriptor.programState->setUniform(alphaUniformLocation, &alpha, sizeof(alpha));
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void DrawNode::draw(Renderer* renderer, const Mat4& transform, uint32_t flags)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_bufferCountTriangle)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2020-10-26 14:49:14 +08:00
|
|
|
updateBlendState(_customCommandTriangle);
|
|
|
|
updateUniforms(transform, _customCommandTriangle);
|
|
|
|
_customCommandTriangle.init(_globalZOrder);
|
|
|
|
renderer->addCommand(&_customCommandTriangle);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
if (_bufferCountPoint)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2020-10-26 14:49:14 +08:00
|
|
|
updateBlendState(_customCommandPoint);
|
|
|
|
updateUniforms(transform, _customCommandPoint);
|
|
|
|
_customCommandPoint.init(_globalZOrder);
|
|
|
|
renderer->addCommand(&_customCommandPoint);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
if (_bufferCountLine)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2020-10-26 14:49:14 +08:00
|
|
|
updateBlendState(_customCommandLine);
|
|
|
|
updateUniforms(transform, _customCommandLine);
|
|
|
|
_customCommandLine.init(_globalZOrder);
|
|
|
|
renderer->addCommand(&_customCommandLine);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-06 21:31:18 +08:00
|
|
|
void DrawNode::drawPoint(const Vec2& position,
|
|
|
|
const float pointSize,
|
|
|
|
const Color4B& color,
|
|
|
|
const DrawNode::PointType pointType)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2024-09-06 21:31:18 +08:00
|
|
|
if (pointSize <= 0.0f)
|
|
|
|
return;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2024-09-06 21:31:18 +08:00
|
|
|
_drawPoint(position, pointSize, color, pointType);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2024-09-06 21:31:18 +08:00
|
|
|
void DrawNode::drawPoints(const Vec2* position,
|
|
|
|
unsigned int numberOfPoints,
|
|
|
|
const Color4B& color,
|
|
|
|
const DrawNode::PointType pointType)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2024-09-06 21:31:18 +08:00
|
|
|
_drawPoints(position, numberOfPoints, 1.0f, color, pointType);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void DrawNode::drawPoints(const Vec2* position,
|
|
|
|
unsigned int numberOfPoints,
|
|
|
|
const float pointSize,
|
2024-09-06 21:31:18 +08:00
|
|
|
const Color4B& color,
|
|
|
|
const DrawNode::PointType pointType)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2024-09-06 21:31:18 +08:00
|
|
|
if (pointSize <= 0.0f)
|
|
|
|
return;
|
|
|
|
_drawPoints(position, numberOfPoints, pointSize, color, pointType);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2024-09-06 21:31:18 +08:00
|
|
|
void DrawNode::drawLine(const Vec2& origin,
|
|
|
|
const Vec2& destination,
|
|
|
|
const Color4B& color,
|
|
|
|
float thickness,
|
|
|
|
DrawNode::EndType etStart,
|
|
|
|
DrawNode::EndType etEnd)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2024-09-06 21:31:18 +08:00
|
|
|
if (thickness <= 0.0f)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2024-09-06 21:31:18 +08:00
|
|
|
AXLOGW("{}: thickness <= 0", __FUNCTION__);
|
|
|
|
return;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2024-09-06 21:31:18 +08:00
|
|
|
_drawSegment(origin, destination, color, thickness, etStart, etEnd);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2024-09-06 21:31:18 +08:00
|
|
|
void DrawNode::drawPoly(const Vec2* poli,
|
|
|
|
unsigned int numberOfPoints,
|
|
|
|
bool closedPolygon,
|
|
|
|
const Color4B& color,
|
|
|
|
float thickness)
|
|
|
|
{
|
|
|
|
if (thickness <= 0.0f)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2024-09-06 21:31:18 +08:00
|
|
|
AXLOGW("{}: thickness <= 0", __FUNCTION__);
|
|
|
|
return;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2024-09-06 21:31:18 +08:00
|
|
|
_drawPoly(poli, numberOfPoints, closedPolygon, color, thickness);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void DrawNode::drawCircle(const Vec2& center,
|
|
|
|
float radius,
|
|
|
|
float angle,
|
|
|
|
unsigned int segments,
|
|
|
|
bool drawLineToCenter,
|
|
|
|
float scaleX,
|
|
|
|
float scaleY,
|
2023-01-06 23:25:31 +08:00
|
|
|
const Color4B& color,
|
2024-09-06 21:31:18 +08:00
|
|
|
float thickness)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2024-09-06 21:31:18 +08:00
|
|
|
if (thickness <= 0.0f)
|
2023-01-06 23:25:31 +08:00
|
|
|
{
|
2024-09-06 21:31:18 +08:00
|
|
|
AXLOGW("{}: thickness <= 0", __FUNCTION__);
|
|
|
|
return;
|
2023-01-06 23:25:31 +08:00
|
|
|
}
|
2024-09-06 21:31:18 +08:00
|
|
|
_drawCircle(center, radius, angle, segments, drawLineToCenter, scaleX, scaleY, color, Color4B(), false, thickness);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void DrawNode::drawCircle(const Vec2& center,
|
|
|
|
float radius,
|
|
|
|
float angle,
|
|
|
|
unsigned int segments,
|
|
|
|
bool drawLineToCenter,
|
2023-01-06 23:25:31 +08:00
|
|
|
const Color4B& color,
|
2024-09-06 21:31:18 +08:00
|
|
|
float thickness)
|
|
|
|
{
|
|
|
|
if (thickness <= 0.0f)
|
|
|
|
{
|
|
|
|
AXLOGW("{}: thickness <= 0", __FUNCTION__);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_drawCircle(center, radius, angle, segments, drawLineToCenter, 1.0f, 1.0f, color, color, false, thickness);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DrawNode::drawStar(const Vec2& center,
|
|
|
|
float radiusI,
|
|
|
|
float radiusO,
|
|
|
|
unsigned int segments,
|
|
|
|
const Color4B& color,
|
|
|
|
float thickness)
|
|
|
|
{
|
|
|
|
if (thickness <= 0.0f)
|
|
|
|
{
|
|
|
|
AXLOGW("{}: thickness <= 0", __FUNCTION__);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_drawAStar(center, radiusI, radiusO, segments, color, color, thickness, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DrawNode::drawSolidStar(const Vec2& center,
|
|
|
|
float radiusI, // inner
|
|
|
|
float radiusO, // outer
|
|
|
|
unsigned int segments,
|
|
|
|
const Color4B& color,
|
|
|
|
const Color4B& filledColor,
|
|
|
|
float thickness)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2024-09-06 21:31:18 +08:00
|
|
|
if (thickness < 0.0f)
|
|
|
|
{
|
|
|
|
AXLOGW("{}: thickness < 0, changed to 0", __FUNCTION__);
|
|
|
|
thickness = 0.0f;
|
|
|
|
}
|
|
|
|
_drawAStar(center, radiusI, radiusO, segments, color, filledColor, thickness, true);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void DrawNode::drawQuadBezier(const Vec2& origin,
|
|
|
|
const Vec2& control,
|
|
|
|
const Vec2& destination,
|
|
|
|
unsigned int segments,
|
2024-09-06 21:31:18 +08:00
|
|
|
const Color4B& color,
|
|
|
|
float thickness)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2024-09-06 21:31:18 +08:00
|
|
|
if (thickness <= 0.0f)
|
|
|
|
{
|
|
|
|
AXLOGW("{}: thickness <= 0", __FUNCTION__);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vec2* _vertices = _abuf.get<Vec2>(segments + 1);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
float t = 0.0f;
|
2021-12-25 10:04:45 +08:00
|
|
|
for (unsigned int i = 0; i < segments; i++)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2024-09-06 21:31:18 +08:00
|
|
|
_vertices[i].x = powf(1.0f - t, 2.0f) * origin.x + 2.0f * (1.0f - t) * t * control.x + t * t * destination.x;
|
|
|
|
_vertices[i].y = powf(1.0f - t, 2.0f) * origin.y + 2.0f * (1.0f - t) * t * control.y + t * t * destination.y;
|
2019-11-23 20:27:39 +08:00
|
|
|
t += 1.0f / segments;
|
|
|
|
}
|
2024-09-06 21:31:18 +08:00
|
|
|
_vertices[segments].x = destination.x;
|
|
|
|
_vertices[segments].y = destination.y;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2024-09-06 21:31:18 +08:00
|
|
|
_drawPoly(_vertices, segments + 1, false, color, thickness, false);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void DrawNode::drawCubicBezier(const Vec2& origin,
|
|
|
|
const Vec2& control1,
|
|
|
|
const Vec2& control2,
|
|
|
|
const Vec2& destination,
|
|
|
|
unsigned int segments,
|
2024-09-06 21:31:18 +08:00
|
|
|
const Color4B& color,
|
|
|
|
float thickness)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2024-09-06 21:31:18 +08:00
|
|
|
if (thickness <= 0.0f)
|
|
|
|
{
|
|
|
|
AXLOGW("{}: thickness <= 0", __FUNCTION__);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vec2* _vertices = _abuf.get<Vec2>(segments + 1);
|
2021-12-08 00:11:53 +08:00
|
|
|
|
2024-09-06 21:31:18 +08:00
|
|
|
float t = 0.0f;
|
2019-11-23 20:27:39 +08:00
|
|
|
for (unsigned int i = 0; i < segments; i++)
|
|
|
|
{
|
2024-09-06 21:31:18 +08:00
|
|
|
_vertices[i].x = powf(1.0f - t, 3.0f) * origin.x + 3.0f * powf(1.0f - t, 2.0f) * t * control1.x +
|
|
|
|
3.0f * (1 - t) * t * t * control2.x + t * t * t * destination.x;
|
|
|
|
_vertices[i].y = powf(1.0f - t, 3.0f) * origin.y + 3.0f * powf(1.0f - t, 2.0f) * t * control1.y +
|
|
|
|
3.0f * (1 - t) * t * t * control2.y + t * t * t * destination.y;
|
2019-11-23 20:27:39 +08:00
|
|
|
t += 1.0f / segments;
|
|
|
|
}
|
2024-09-06 21:31:18 +08:00
|
|
|
_vertices[segments].x = destination.x;
|
|
|
|
_vertices[segments].y = destination.y;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2024-09-06 21:31:18 +08:00
|
|
|
_drawPoly(_vertices, segments + 1, false, color, thickness, true);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2024-09-06 21:31:18 +08:00
|
|
|
void DrawNode::drawCardinalSpline(PointArray* config,
|
|
|
|
float tension,
|
|
|
|
unsigned int segments,
|
|
|
|
const Color4B& color,
|
|
|
|
float thickness)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2024-09-06 21:31:18 +08:00
|
|
|
if (thickness <= 0.0f)
|
|
|
|
{
|
|
|
|
AXLOGW("{}: thickness <= 0", __FUNCTION__);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vec2* _vertices = _abuf.get<Vec2>(segments);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
ssize_t p;
|
|
|
|
float lt;
|
|
|
|
float deltaT = 1.0f / config->count();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2024-09-06 21:31:18 +08:00
|
|
|
for (unsigned int i = 0; i < segments; i++)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
float dt = (float)i / segments;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// border
|
2021-12-25 10:04:45 +08:00
|
|
|
if (dt == 1)
|
|
|
|
{
|
|
|
|
p = config->count() - 1;
|
2019-11-23 20:27:39 +08:00
|
|
|
lt = 1;
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
p = static_cast<ssize_t>(dt / deltaT);
|
2019-11-23 20:27:39 +08:00
|
|
|
lt = (dt - deltaT * (float)p) / deltaT;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Interpolate
|
2021-12-25 10:04:45 +08:00
|
|
|
Vec2 pp0 = config->getControlPointAtIndex(p - 1);
|
|
|
|
Vec2 pp1 = config->getControlPointAtIndex(p + 0);
|
|
|
|
Vec2 pp2 = config->getControlPointAtIndex(p + 1);
|
|
|
|
Vec2 pp3 = config->getControlPointAtIndex(p + 2);
|
|
|
|
|
2024-09-06 21:31:18 +08:00
|
|
|
Vec2 newPos = ccCardinalSplineAt(pp0, pp1, pp2, pp3, tension, lt);
|
|
|
|
_vertices[i].x = newPos.x;
|
|
|
|
_vertices[i].y = newPos.y;
|
|
|
|
if (newPos == config->getControlPointAtIndex(config->count() - 1) && i > 0)
|
|
|
|
{
|
|
|
|
segments = i + 1;
|
|
|
|
break;
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2024-09-06 21:31:18 +08:00
|
|
|
_drawPoly(_vertices, segments, false, color, thickness, true);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2024-09-06 21:31:18 +08:00
|
|
|
void DrawNode::drawCatmullRom(PointArray* points, unsigned int segments, const Color4B& color, float thickness)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2024-09-06 21:31:18 +08:00
|
|
|
if (thickness <= 0.0f)
|
|
|
|
{
|
|
|
|
AXLOGW("{}: thickness <= 0", __FUNCTION__);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
drawCardinalSpline(points, 0.5f, segments, color, thickness);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void DrawNode::drawDot(const Vec2& pos, float radius, const Color4B& color)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2024-09-06 21:31:18 +08:00
|
|
|
if (radius <= 0.0f)
|
|
|
|
{
|
|
|
|
AXLOGW("{}: radius <= 0", __FUNCTION__);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_drawDot(pos, radius, color);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2024-09-06 21:31:18 +08:00
|
|
|
void DrawNode::drawRect(const Vec2& p1,
|
|
|
|
const Vec2& p2,
|
|
|
|
const Vec2& p3,
|
|
|
|
const Vec2& p4,
|
|
|
|
const Color4B& color,
|
|
|
|
float thickness)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2024-09-06 21:31:18 +08:00
|
|
|
if (thickness <= 0.0f)
|
|
|
|
{
|
|
|
|
AXLOGW("{}: thickness <= 0", __FUNCTION__);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vec2 line[5] = {p1, p2, p3, p4, p1};
|
|
|
|
_drawPoly(line, 5, false, color, thickness, true);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2024-09-06 21:31:18 +08:00
|
|
|
void DrawNode::drawRect(const Vec2& origin, const Vec2& destination, const Color4B& color, float thickness)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2024-09-06 21:31:18 +08:00
|
|
|
if (thickness <= 0.0f)
|
|
|
|
{
|
|
|
|
AXLOGW("{}: thickness <= 0", __FUNCTION__);
|
|
|
|
return;
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2024-09-06 21:31:18 +08:00
|
|
|
Vec2 line[5] = {origin, Vec2(destination.x, origin.y), destination, Vec2(origin.x, destination.y), origin};
|
|
|
|
_drawPoly(line, 5, false, color, thickness, true);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2024-09-06 21:31:18 +08:00
|
|
|
void DrawNode::drawSegment(const Vec2& from,
|
|
|
|
const Vec2& to,
|
|
|
|
float thickness,
|
|
|
|
const Color4B& color,
|
|
|
|
DrawNode::EndType etStart,
|
|
|
|
DrawNode::EndType etEnd)
|
|
|
|
{
|
|
|
|
if (thickness <= 0.0f)
|
|
|
|
{
|
|
|
|
AXLOGW("{}: thickness <= 0", __FUNCTION__);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
_drawSegment(from, to, color, thickness, etStart, etEnd);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2024-09-06 21:31:18 +08:00
|
|
|
void DrawNode::drawPolygon(Vec2* verts,
|
2021-12-25 10:04:45 +08:00
|
|
|
int count,
|
|
|
|
const Color4B& fillColor,
|
2024-09-06 21:31:18 +08:00
|
|
|
float thickness,
|
|
|
|
const Color4B& borderColor,
|
|
|
|
bool isconvex)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2024-09-06 21:31:18 +08:00
|
|
|
if (thickness < 0.0f)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2024-09-06 21:31:18 +08:00
|
|
|
AXLOGW("{}: thickness < 0, changed to 0", __FUNCTION__);
|
|
|
|
thickness = 0.0f;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2024-09-06 21:31:18 +08:00
|
|
|
_drawPolygon(verts, count, fillColor, borderColor, true, thickness, isconvex);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2024-09-06 21:31:18 +08:00
|
|
|
void DrawNode::drawPolygon(Vec2* verts, int count, float thickness, const Color4B& borderColor, bool isconvex)
|
|
|
|
{
|
|
|
|
if (thickness < 0.0f)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2024-09-06 21:31:18 +08:00
|
|
|
AXLOGW("{}: thickness < 0, changed to 0", __FUNCTION__);
|
|
|
|
thickness = 0.0f;
|
|
|
|
}
|
|
|
|
_drawPolygon(verts, count, Color4B::TRANSPARENT, borderColor, true, thickness, isconvex);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2024-09-06 21:31:18 +08:00
|
|
|
void DrawNode::drawSolidPolygon(Vec2* verts,
|
|
|
|
int count,
|
|
|
|
const Color4B& fillColor,
|
|
|
|
float thickness,
|
|
|
|
const Color4B& borderColor,
|
|
|
|
bool isconvex)
|
|
|
|
{
|
|
|
|
if (thickness < 0.0f)
|
|
|
|
{
|
|
|
|
AXLOGW("{}: thickness < 0, changed to 0", __FUNCTION__);
|
|
|
|
thickness = 0.0f;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2024-09-06 21:31:18 +08:00
|
|
|
_drawPolygon(verts, count, fillColor, borderColor, true, thickness, isconvex);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2024-09-06 21:31:18 +08:00
|
|
|
void DrawNode::drawSolidRect(const Vec2& origin,
|
|
|
|
const Vec2& destination,
|
|
|
|
const Color4B& fillColor,
|
|
|
|
float thickness,
|
|
|
|
const Color4B& borderColor)
|
|
|
|
{
|
|
|
|
if (thickness < 0.0f)
|
|
|
|
{
|
|
|
|
AXLOGW("{}: thickness < 0, changed to 0", __FUNCTION__);
|
|
|
|
thickness = 0.0f;
|
|
|
|
}
|
|
|
|
Vec2 _vertices[] = {origin, Vec2(destination.x, origin.y), destination, Vec2(origin.x, destination.y), origin};
|
|
|
|
_drawPolygon(_vertices, 5, fillColor, borderColor, true, thickness, true);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2024-09-06 21:31:18 +08:00
|
|
|
void DrawNode::drawSolidPoly(const Vec2* poli,
|
|
|
|
unsigned int numberOfPoints,
|
|
|
|
const Color4B& color,
|
|
|
|
float thickness,
|
|
|
|
const Color4B& borderColor,
|
|
|
|
bool isconvex)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2024-09-06 21:31:18 +08:00
|
|
|
if (thickness < 0.0f)
|
|
|
|
{
|
|
|
|
AXLOGW("{}: thickness < 0, changed to 0", __FUNCTION__);
|
|
|
|
thickness = 0.0f;
|
|
|
|
}
|
|
|
|
_drawPolygon(poli, numberOfPoints, color, borderColor, true, thickness, isconvex);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2024-09-06 21:31:18 +08:00
|
|
|
void DrawNode::drawPie(const Vec2& center,
|
|
|
|
float radius,
|
|
|
|
float rotation,
|
|
|
|
int startAngle,
|
|
|
|
int endAngle,
|
|
|
|
float scaleX,
|
|
|
|
float scaleY,
|
|
|
|
const Color4B& fillColor,
|
|
|
|
const Color4B& borderColor,
|
|
|
|
DrawMode drawMode,
|
|
|
|
float thickness)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2024-09-06 21:31:18 +08:00
|
|
|
if (thickness < 0.0f)
|
|
|
|
{
|
|
|
|
AXLOGW("{}: thickness < 0, changed to 0", __FUNCTION__);
|
|
|
|
thickness = 0.0f;
|
|
|
|
}
|
|
|
|
_drawPie(center, radius, rotation, startAngle, endAngle, scaleX, scaleY, fillColor, borderColor, drawMode,
|
|
|
|
thickness);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2023-12-05 17:20:31 +08:00
|
|
|
void DrawNode::drawPie(const Vec2& center,
|
|
|
|
float radius,
|
|
|
|
float angle,
|
|
|
|
int startAngle,
|
|
|
|
int endAngle,
|
|
|
|
float scaleX,
|
|
|
|
float scaleY,
|
|
|
|
const Color4B& color,
|
|
|
|
DrawMode drawMode)
|
|
|
|
{
|
2024-09-06 21:31:18 +08:00
|
|
|
_drawPie(center, radius, angle, startAngle, endAngle, scaleX, scaleY, Color4B::TRANSPARENT, color, drawMode, 1.0f);
|
|
|
|
}
|
2023-12-05 17:20:31 +08:00
|
|
|
|
2024-09-06 21:31:18 +08:00
|
|
|
void DrawNode::drawSolidCircle(const Vec2& center,
|
|
|
|
float radius,
|
|
|
|
float angle,
|
|
|
|
unsigned int segments,
|
|
|
|
float scaleX,
|
|
|
|
float scaleY,
|
|
|
|
const Color4B& fillColor,
|
|
|
|
float thickness,
|
|
|
|
const Color4B& borderColor)
|
|
|
|
{
|
|
|
|
if (thickness < 0.0f)
|
2023-12-05 17:20:31 +08:00
|
|
|
{
|
2024-09-06 21:31:18 +08:00
|
|
|
AXLOGW("{}: thickness < 0, changed to 0", __FUNCTION__);
|
|
|
|
thickness = 0.0f;
|
2023-12-05 17:20:31 +08:00
|
|
|
}
|
2024-09-06 21:31:18 +08:00
|
|
|
_drawCircle(center, radius, angle, segments, false, scaleX, scaleY, borderColor, fillColor, true, thickness);
|
|
|
|
}
|
2023-12-05 17:20:31 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void DrawNode::drawSolidCircle(const Vec2& center,
|
|
|
|
float radius,
|
|
|
|
float angle,
|
|
|
|
unsigned int segments,
|
|
|
|
float scaleX,
|
|
|
|
float scaleY,
|
2024-09-06 21:31:18 +08:00
|
|
|
const Color4B& color)
|
2021-08-19 14:35:44 +08:00
|
|
|
{
|
2024-09-06 21:31:18 +08:00
|
|
|
if (radius < 0.0f)
|
2021-08-19 14:35:44 +08:00
|
|
|
{
|
2024-09-06 21:31:18 +08:00
|
|
|
AXLOGW("{}: radius < 0, changed to 0", __FUNCTION__);
|
|
|
|
radius = 0.0f;
|
2021-08-19 14:35:44 +08:00
|
|
|
}
|
2024-09-06 21:31:18 +08:00
|
|
|
_drawCircle(center, radius, angle, segments, false, scaleX, scaleY, Color4B(), color, true);
|
2021-08-19 14:35:44 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void DrawNode::drawSolidCircle(const Vec2& center,
|
|
|
|
float radius,
|
|
|
|
float angle,
|
|
|
|
unsigned int segments,
|
|
|
|
const Color4B& color)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2024-09-06 21:31:18 +08:00
|
|
|
if (radius < 0.0f)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2024-09-06 21:31:18 +08:00
|
|
|
AXLOGW("{}: radius < 0, changed to 0", __FUNCTION__);
|
|
|
|
radius = 0.0f;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2024-09-06 21:31:18 +08:00
|
|
|
_drawCircle(center, radius, angle, segments, false, 1.0f, 1.0f, Color4B(), color, true);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2024-09-06 21:31:18 +08:00
|
|
|
void DrawNode::drawTriangle(const Vec2* _vertices3, const Color4B& color, float thickness)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2024-09-06 21:31:18 +08:00
|
|
|
_drawTriangle(_vertices3, Color4B::TRANSPARENT, color, false, thickness);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2024-09-06 21:31:18 +08:00
|
|
|
void DrawNode::drawTriangle(const Vec2& p1, const Vec2& p2, const Vec2& p3, const Color4B& color, float thickness)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2024-09-06 21:31:18 +08:00
|
|
|
if (thickness <= 0.0f)
|
|
|
|
{
|
|
|
|
AXLOGW("{}: thickness <= 0", __FUNCTION__);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Vec2 _vertices3[3] = {p1, p2, p3};
|
|
|
|
_drawTriangle(_vertices3, Color4B::TRANSPARENT, color, false, thickness);
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2024-09-06 21:31:18 +08:00
|
|
|
void DrawNode::drawSolidTriangle(const Vec2* _vertices3,
|
|
|
|
const Color4B& fillColor,
|
|
|
|
const Color4B& borderColor,
|
|
|
|
float thickness)
|
|
|
|
{
|
|
|
|
if (thickness < 0.0f)
|
|
|
|
{
|
|
|
|
AXLOGW("{}: thickness < 0, changed to 0", __FUNCTION__);
|
|
|
|
thickness = 0.0f;
|
|
|
|
}
|
|
|
|
_drawTriangle(_vertices3, fillColor, borderColor, true, thickness);
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2024-09-06 21:31:18 +08:00
|
|
|
void DrawNode::drawSolidTriangle(const Vec2& p1,
|
|
|
|
const Vec2& p2,
|
|
|
|
const Vec2& p3,
|
|
|
|
const Color4B& fillColor,
|
|
|
|
const Color4B& borderColor,
|
|
|
|
float thickness)
|
|
|
|
{
|
|
|
|
if (thickness < 0.0f)
|
|
|
|
{
|
|
|
|
AXLOGW("{}: thickness < 0, changed to 0", __FUNCTION__);
|
|
|
|
thickness = 0.0f;
|
|
|
|
}
|
|
|
|
Vec2 _vertices3[3] = {p1, p2, p3};
|
|
|
|
_drawTriangle(_vertices3, fillColor, borderColor, false, thickness);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void DrawNode::clear()
|
|
|
|
{
|
2020-10-26 14:49:14 +08:00
|
|
|
_bufferCountTriangle = 0;
|
2021-12-25 10:04:45 +08:00
|
|
|
_dirtyTriangle = true;
|
|
|
|
_bufferCountLine = 0;
|
|
|
|
_dirtyLine = true;
|
|
|
|
_bufferCountPoint = 0;
|
|
|
|
_dirtyPoint = true;
|
2024-09-06 21:31:18 +08:00
|
|
|
|
|
|
|
_lineWidth = _defaultLineWidth;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const BlendFunc& DrawNode::getBlendFunc() const
|
|
|
|
{
|
|
|
|
return _blendFunc;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void DrawNode::setBlendFunc(const BlendFunc& blendFunc)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_blendFunc = blendFunc;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DrawNode::setLineWidth(float lineWidth)
|
|
|
|
{
|
2024-09-06 21:31:18 +08:00
|
|
|
_defaultLineWidth = lineWidth;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
float DrawNode::getLineWidth()
|
|
|
|
{
|
2024-09-06 21:31:18 +08:00
|
|
|
return _defaultLineWidth;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void DrawNode::visit(Renderer* renderer, const Mat4& parentTransform, uint32_t parentFlags)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (_isolated)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// ignore `parentTransform` from parent
|
2019-11-23 20:27:39 +08:00
|
|
|
Node::visit(renderer, Mat4::IDENTITY, parentFlags);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Node::visit(renderer, parentTransform, parentFlags);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-06 21:31:18 +08:00
|
|
|
void DrawNode::_drawPolygon(const Vec2* verts,
|
|
|
|
unsigned int count,
|
|
|
|
const Color4B& fillColor,
|
|
|
|
const Color4B& borderColor,
|
|
|
|
bool closedPolygon,
|
|
|
|
float thickness,
|
|
|
|
bool isconvex)
|
|
|
|
{
|
|
|
|
AXASSERT(count >= 0, "invalid count value");
|
|
|
|
|
|
|
|
bool outline = (thickness != 0.0f);
|
|
|
|
|
|
|
|
Vec2* _vertices = _transform(verts, count, closedPolygon);
|
|
|
|
|
|
|
|
std::vector<V2F_C4B_T2F_Triangle> triangleList;
|
|
|
|
|
|
|
|
int vertex_count = 0;
|
|
|
|
|
|
|
|
// calculate the memory (important for correct drawing stuff)
|
|
|
|
if (closedPolygon && !isconvex && fillColor.a > 0.0f && !isConvex(_vertices, count) && count >= 3)
|
|
|
|
{
|
|
|
|
std::vector<p2t::Point> p2pointsStorage;
|
|
|
|
p2pointsStorage.reserve(count);
|
|
|
|
std::vector<p2t::Point*> p2points;
|
|
|
|
p2points.reserve(count);
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < count - 1;
|
|
|
|
i++) // count-1 is needed because of: _vertices[0] = _vertices[i < count]
|
|
|
|
{
|
|
|
|
p2points.emplace_back(&p2pointsStorage.emplace_back((float)_vertices[i].x, (float)_vertices[i].y));
|
|
|
|
}
|
|
|
|
p2t::CDT cdt(p2points);
|
|
|
|
cdt.Triangulate();
|
|
|
|
std::vector<p2t::Triangle*> tris = cdt.GetTriangles();
|
|
|
|
|
|
|
|
vertex_count += tris.size();
|
|
|
|
for (auto&& t : tris) // use it later; only one calculate!!!
|
|
|
|
{
|
|
|
|
p2t::Point* vec1 = t->GetPoint(0);
|
|
|
|
p2t::Point* vec2 = t->GetPoint(1);
|
|
|
|
p2t::Point* vec3 = t->GetPoint(2);
|
|
|
|
|
|
|
|
V2F_C4B_T2F_Triangle triangle = {
|
|
|
|
{Vec2(vec1->x, vec1->y), fillColor, Vec2::ZERO},
|
|
|
|
{Vec2(vec2->x, vec2->y), fillColor, Vec2::ZERO},
|
|
|
|
{Vec2(vec3->x, vec3->y), fillColor, Vec2::ZERO},
|
|
|
|
};
|
|
|
|
triangleList.emplace_back(triangle); // use it for drawing later
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (fillColor.a > 0.0f)
|
|
|
|
{
|
|
|
|
vertex_count += count - 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (outline)
|
|
|
|
{
|
|
|
|
if (thickness != 1.0f || properties.drawOrder)
|
|
|
|
{
|
|
|
|
vertex_count += 6 * (count - 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
vertex_count += 2 * count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
vertex_count *= 3;
|
|
|
|
ensureCapacityTriangle(vertex_count);
|
|
|
|
V2F_C4B_T2F_Triangle* triangles = (V2F_C4B_T2F_Triangle*)(_bufferTriangle + _bufferCountTriangle);
|
|
|
|
|
|
|
|
// start drawing...
|
|
|
|
int ii = 0;
|
|
|
|
if (closedPolygon && !isconvex && fillColor.a > 0.0f && !isConvex(_vertices, count) && count >= 3)
|
|
|
|
{
|
|
|
|
for (auto&& t : triangleList)
|
|
|
|
{
|
|
|
|
triangles[ii++] = t;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (fillColor.a > 0.0f)
|
|
|
|
{
|
|
|
|
for (unsigned int i = 0; i < count - 2; i++)
|
|
|
|
{
|
|
|
|
triangles[ii++] = {
|
|
|
|
{_vertices[0], fillColor, Vec2::ZERO},
|
|
|
|
{_vertices[i + 1], fillColor, Vec2::ZERO},
|
|
|
|
{_vertices[i + 2], fillColor, Vec2::ZERO},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (outline)
|
|
|
|
{
|
|
|
|
if (thickness != 1.0f || properties.drawOrder)
|
|
|
|
{
|
|
|
|
thickness *= properties.factor;
|
|
|
|
|
|
|
|
for (unsigned int i = 1; i < (count); i++)
|
|
|
|
{
|
|
|
|
Vec2 a = _vertices[i - 1];
|
|
|
|
Vec2 b = _vertices[i];
|
|
|
|
Vec2 n = ((b - a).getPerp()).getNormalized();
|
|
|
|
Vec2 t = n.getPerp();
|
|
|
|
Vec2 nw = n * thickness;
|
|
|
|
Vec2 tw = t * thickness;
|
|
|
|
Vec2 v0 = b - (nw + tw);
|
|
|
|
Vec2 v1 = b + (nw - tw);
|
|
|
|
Vec2 v2 = b - nw;
|
|
|
|
Vec2 v3 = b + nw;
|
|
|
|
Vec2 v4 = a - nw;
|
|
|
|
Vec2 v5 = a + nw;
|
|
|
|
Vec2 v6 = a - (nw - tw);
|
|
|
|
Vec2 v7 = a + (nw + tw);
|
|
|
|
|
|
|
|
{
|
|
|
|
triangles[ii++] = {
|
|
|
|
{v0, borderColor, -(n + t)},
|
|
|
|
{v1, borderColor, n - t},
|
|
|
|
{v2, borderColor, -n},
|
|
|
|
};
|
|
|
|
triangles[ii++] = {
|
|
|
|
{v3, borderColor, n},
|
|
|
|
{v1, borderColor, n - t},
|
|
|
|
{v2, borderColor, -n},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
triangles[ii++] = {
|
|
|
|
{v3, borderColor, n},
|
|
|
|
{v4, borderColor, -n},
|
|
|
|
{v2, borderColor, -n},
|
|
|
|
};
|
|
|
|
triangles[ii++] = {
|
|
|
|
{v3, borderColor, n},
|
|
|
|
{v4, borderColor, -n},
|
|
|
|
{v5, borderColor, n},
|
|
|
|
};
|
|
|
|
|
|
|
|
{
|
|
|
|
triangles[ii++] = {
|
|
|
|
{v6, borderColor, t - n},
|
|
|
|
{v4, borderColor, -n},
|
|
|
|
{v5, borderColor, n},
|
|
|
|
};
|
|
|
|
triangles[ii++] = {
|
|
|
|
{v6, borderColor, t - n},
|
|
|
|
{v7, borderColor, t + n},
|
|
|
|
{v5, borderColor, n},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
struct ExtrudeVerts
|
|
|
|
{
|
|
|
|
Vec2 offset, n;
|
|
|
|
};
|
|
|
|
struct ExtrudeVerts* extrude = _abuf.get<struct ExtrudeVerts>(sizeof(struct ExtrudeVerts) * count);
|
|
|
|
|
|
|
|
int closeCount = count - ((closedPolygon) ? 0 : 1);
|
|
|
|
for (unsigned int i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
Vec2 v0 = _vertices[(i - 1 + count) % count];
|
|
|
|
Vec2 v1 = _vertices[i];
|
|
|
|
Vec2 v2 = _vertices[(i + 1) % count];
|
|
|
|
|
|
|
|
Vec2 n1 = ((v1 - v0).getPerp()).getNormalized();
|
|
|
|
Vec2 n2 = ((v2 - v1).getPerp()).getNormalized();
|
|
|
|
|
|
|
|
Vec2 offset = (n1 + n2) * (1.0f / (Vec2::dot(n1, n2) + 1.0f));
|
|
|
|
extrude[i] = {offset, n2};
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < closeCount; i++)
|
|
|
|
{
|
|
|
|
int j = (i + 1) % count;
|
|
|
|
Vec2 v0 = _vertices[i];
|
|
|
|
Vec2 v1 = _vertices[j];
|
|
|
|
|
|
|
|
Vec2 n0 = extrude[i].n;
|
|
|
|
|
|
|
|
Vec2 offset0 = extrude[i].offset;
|
|
|
|
Vec2 offset1 = extrude[j].offset;
|
|
|
|
|
|
|
|
Vec2 inner0 = v0 - offset0 * thickness;
|
|
|
|
Vec2 inner1 = v1 - offset1 * thickness;
|
|
|
|
Vec2 outer0 = v0 + offset0 * thickness;
|
|
|
|
Vec2 outer1 = v1 + offset1 * thickness;
|
|
|
|
|
|
|
|
triangles[ii++] = {{inner0, borderColor, -n0}, {inner1, borderColor, -n0}, {outer1, borderColor, n0}};
|
|
|
|
|
|
|
|
triangles[ii++] = {{inner0, borderColor, -n0}, {outer0, borderColor, n0}, {outer1, borderColor, n0}};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_customCommandTriangle.updateVertexBuffer(triangles, _bufferCountTriangle * sizeof(V2F_C4B_T2F),
|
|
|
|
vertex_count * sizeof(V2F_C4B_T2F));
|
|
|
|
_bufferCountTriangle += vertex_count;
|
|
|
|
_customCommandTriangle.setVertexDrawInfo(0, _bufferCountTriangle);
|
|
|
|
_dirtyTriangle = true;
|
|
|
|
|
|
|
|
AX_SAFE_DELETE_ARRAY(_vertices);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DrawNode::_drawPoly(const Vec2* verts,
|
|
|
|
unsigned int count,
|
|
|
|
bool closedPolygon,
|
|
|
|
const Color4B& color,
|
|
|
|
float thickness,
|
|
|
|
bool isconvex)
|
|
|
|
{
|
|
|
|
if (thickness == 1.0f && !properties.drawOrder)
|
|
|
|
{
|
|
|
|
Vec2* _vertices = _transform(verts, count);
|
|
|
|
|
|
|
|
unsigned int vertex_count = (closedPolygon) ? 2 * count : 2 * (count - 1);
|
|
|
|
|
|
|
|
ensureCapacityLine(vertex_count);
|
|
|
|
V2F_C4B_T2F* line = _bufferLine + _bufferCountLine;
|
|
|
|
|
|
|
|
int ii = 0;
|
|
|
|
for (unsigned int i = 0; i < count - 1; i++)
|
|
|
|
{
|
|
|
|
line[ii++] = {_vertices[i], color, Vec2::ZERO};
|
|
|
|
line[ii++] = {_vertices[i + 1], color, Vec2::ZERO};
|
|
|
|
}
|
|
|
|
if (closedPolygon)
|
|
|
|
{
|
|
|
|
line[ii++] = {_vertices[count - 1], color, Vec2::ZERO};
|
|
|
|
line[ii++] = line[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
_customCommandLine.updateVertexBuffer(line, _bufferCountLine * sizeof(V2F_C4B_T2F),
|
|
|
|
vertex_count * sizeof(V2F_C4B_T2F));
|
|
|
|
_bufferCountLine += vertex_count;
|
|
|
|
_customCommandLine.setVertexDrawInfo(0, _bufferCountLine);
|
|
|
|
|
|
|
|
AX_SAFE_DELETE_ARRAY(_vertices);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_drawPolygon(verts, count, Color4B::TRANSPARENT, color, closedPolygon, thickness, isconvex);
|
|
|
|
}
|
2024-08-26 00:25:33 +08:00
|
|
|
}
|
2024-09-06 21:31:18 +08:00
|
|
|
|
|
|
|
void DrawNode::_drawSegment(const Vec2& from,
|
|
|
|
const Vec2& to,
|
|
|
|
const Color4B& color,
|
|
|
|
float thickness,
|
|
|
|
DrawNode::EndType etStart,
|
|
|
|
DrawNode::EndType etEnd)
|
|
|
|
{
|
|
|
|
if (thickness == 1.0f && !properties.drawOrder)
|
|
|
|
{
|
|
|
|
unsigned int count = 2;
|
|
|
|
Vec2 aLine[] = {from, to};
|
|
|
|
|
|
|
|
Vec2* _vertices = _transform(aLine, count, false);
|
|
|
|
|
|
|
|
ensureCapacityLine(count);
|
|
|
|
|
|
|
|
V2F_C4B_T2F* line = _bufferLine + _bufferCountLine;
|
|
|
|
|
|
|
|
line[0] = {_vertices[0], color, Vec2::ZERO};
|
|
|
|
line[1] = {_vertices[1], color, Vec2::ZERO};
|
|
|
|
|
|
|
|
_customCommandLine.updateVertexBuffer(line, _bufferCountLine * sizeof(V2F_C4B_T2F), 2 * sizeof(V2F_C4B_T2F));
|
|
|
|
_bufferCountLine += count;
|
|
|
|
_dirtyLine = true;
|
|
|
|
_customCommandLine.setVertexDrawInfo(0, _bufferCountLine);
|
|
|
|
|
|
|
|
AX_SAFE_DELETE_ARRAY(_vertices);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int count = 2;
|
|
|
|
Vec2 line[] = {from, to};
|
|
|
|
|
|
|
|
Vec2* _vertices = _transform(line, count, false);
|
|
|
|
|
|
|
|
Vec2 a = _vertices[0];
|
|
|
|
Vec2 b = _vertices[1];
|
|
|
|
Vec2 n = ((b - a).getPerp()).getNormalized();
|
|
|
|
Vec2 t = n.getPerp();
|
|
|
|
Vec2 nw = n * thickness;
|
|
|
|
Vec2 tw = t * thickness;
|
|
|
|
Vec2 v0 = b - (nw + tw);
|
|
|
|
Vec2 v1 = b + (nw - tw);
|
|
|
|
Vec2 v2 = b - nw;
|
|
|
|
Vec2 v3 = b + nw;
|
|
|
|
Vec2 v4 = a - nw;
|
|
|
|
Vec2 v5 = a + nw;
|
|
|
|
Vec2 v6 = a - (nw - tw);
|
|
|
|
Vec2 v7 = a + (nw + tw);
|
|
|
|
|
|
|
|
unsigned int vertex_count =
|
|
|
|
3 * ((etStart != DrawNode::EndType::Butt) ? 2 : 0) + 3 * 2 + 3 * ((etEnd != DrawNode::EndType::Butt) ? 2 : 0);
|
|
|
|
ensureCapacityTriangle(vertex_count);
|
|
|
|
V2F_C4B_T2F_Triangle* triangles = (V2F_C4B_T2F_Triangle*)(_bufferTriangle + _bufferCountTriangle);
|
|
|
|
|
|
|
|
int ii = 0;
|
|
|
|
switch (etEnd)
|
|
|
|
{
|
|
|
|
case DrawNode::EndType::Butt:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DrawNode::EndType::Square:
|
|
|
|
triangles[ii++] = {
|
|
|
|
{v0, color, Vec2::ZERO},
|
|
|
|
{v1, color, -n},
|
|
|
|
{v2, color, n},
|
|
|
|
};
|
|
|
|
|
|
|
|
triangles[ii++] = {
|
|
|
|
{v3, color, n},
|
|
|
|
{v1, color, Vec2::ZERO},
|
|
|
|
{v2, color, -n},
|
|
|
|
};
|
|
|
|
|
|
|
|
break;
|
|
|
|
case DrawNode::EndType::Round:
|
|
|
|
triangles[ii++] = {
|
|
|
|
{v0, color, -(n + t)},
|
|
|
|
{v1, color, n - t},
|
|
|
|
{v2, color, -n},
|
|
|
|
};
|
|
|
|
|
|
|
|
triangles[ii++] = {
|
|
|
|
{v3, color, n},
|
|
|
|
{v1, color, n - t},
|
|
|
|
{v2, color, -n},
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// BODY
|
|
|
|
triangles[ii++] = {
|
|
|
|
{v3, color, n},
|
|
|
|
{v4, color, -n},
|
|
|
|
{v2, color, -n},
|
|
|
|
};
|
|
|
|
|
|
|
|
triangles[ii++] = {
|
|
|
|
{v3, color, n},
|
|
|
|
{v4, color, -n},
|
|
|
|
{v5, color, n},
|
|
|
|
};
|
|
|
|
|
|
|
|
switch (etStart)
|
|
|
|
{
|
|
|
|
case DrawNode::EndType::Butt:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DrawNode::EndType::Square:
|
|
|
|
triangles[ii++] = {
|
|
|
|
{v6, color, Vec2::ZERO},
|
|
|
|
{v4, color, -n},
|
|
|
|
{v5, color, n},
|
|
|
|
};
|
|
|
|
|
|
|
|
triangles[ii++] = {
|
|
|
|
{v6, color, -n},
|
|
|
|
{v7, color, Vec2::ZERO},
|
|
|
|
{v5, color, n},
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DrawNode::EndType::Round:
|
|
|
|
triangles[ii++] = {
|
|
|
|
{v6, color, t - n},
|
|
|
|
{v4, color, -n},
|
|
|
|
{v5, color, n},
|
|
|
|
};
|
|
|
|
|
|
|
|
triangles[ii++] = {
|
|
|
|
{v6, color, t - n},
|
|
|
|
{v7, color, t + n},
|
|
|
|
{v5, color, n},
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
_customCommandTriangle.updateVertexBuffer(triangles, _bufferCountTriangle * sizeof(V2F_C4B_T2F),
|
|
|
|
vertex_count * sizeof(V2F_C4B_T2F));
|
|
|
|
_bufferCountTriangle += vertex_count; // ii * 3;
|
|
|
|
_dirtyTriangle = true;
|
|
|
|
_customCommandTriangle.setVertexDrawInfo(0, _bufferCountTriangle);
|
|
|
|
|
|
|
|
AX_SAFE_DELETE_ARRAY(_vertices);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DrawNode::_drawDot(const Vec2& pos, float radius, const Color4B& color)
|
|
|
|
{
|
|
|
|
unsigned int vertex_count = 2 * 3;
|
|
|
|
ensureCapacityTriangle(vertex_count);
|
|
|
|
|
|
|
|
V2F_C4B_T2F a = {Vec2(pos.x - radius, pos.y - radius), color, Vec2(-1.0f, -1.0f)};
|
|
|
|
V2F_C4B_T2F b = {Vec2(pos.x - radius, pos.y + radius), color, Vec2(-1.0f, 1.0f)};
|
|
|
|
V2F_C4B_T2F c = {Vec2(pos.x + radius, pos.y + radius), color, Vec2(1.0f, 1.0f)};
|
|
|
|
V2F_C4B_T2F d = {Vec2(pos.x + radius, pos.y - radius), color, Vec2(1.0f, -1.0f)};
|
|
|
|
|
|
|
|
V2F_C4B_T2F_Triangle* triangles = (V2F_C4B_T2F_Triangle*)(_bufferTriangle + _bufferCountTriangle);
|
|
|
|
triangles[0] = {a, b, c};
|
|
|
|
triangles[1] = {a, c, d};
|
|
|
|
|
|
|
|
_customCommandTriangle.updateVertexBuffer(triangles, _bufferCountTriangle * sizeof(V2F_C4B_T2F),
|
|
|
|
vertex_count * sizeof(V2F_C4B_T2F));
|
|
|
|
_bufferCountTriangle += vertex_count;
|
|
|
|
_dirtyTriangle = true;
|
|
|
|
_customCommandTriangle.setVertexDrawInfo(0, _bufferCountTriangle);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DrawNode::_drawCircle(const Vec2& center,
|
|
|
|
float radius,
|
|
|
|
float angle,
|
|
|
|
unsigned int segments,
|
|
|
|
bool drawLineToCenter,
|
|
|
|
float scaleX,
|
|
|
|
float scaleY,
|
|
|
|
const Color4B& borderColor,
|
|
|
|
const Color4B& fillColor,
|
|
|
|
bool solid,
|
|
|
|
float thickness)
|
|
|
|
{
|
|
|
|
const float coef = 2.0f * (float)M_PI / segments;
|
|
|
|
|
|
|
|
Vec2* _vertices = new Vec2[segments + 2];
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < segments; i++)
|
|
|
|
{
|
|
|
|
float rads = i * coef;
|
|
|
|
float j = radius * cosf(rads + angle) * scaleX + center.x;
|
|
|
|
float k = radius * sinf(rads + angle) * scaleY + center.y;
|
|
|
|
|
|
|
|
_vertices[i].x = j;
|
|
|
|
_vertices[i].y = k;
|
|
|
|
}
|
|
|
|
_vertices[segments] = _vertices[0];
|
|
|
|
|
|
|
|
if (solid)
|
|
|
|
{
|
|
|
|
_drawPolygon(_vertices, segments, fillColor, borderColor, false, thickness, true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (drawLineToCenter)
|
|
|
|
_vertices[++segments] = center;
|
|
|
|
_drawPoly(_vertices, segments + 1, false, borderColor, thickness, true);
|
|
|
|
}
|
|
|
|
AX_SAFE_DELETE_ARRAY(_vertices);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DrawNode::_drawTriangle(const Vec2* _vertices3,
|
|
|
|
const Color4B& borderColor,
|
|
|
|
const Color4B& fillColor,
|
|
|
|
bool solid,
|
|
|
|
float thickness)
|
|
|
|
{
|
|
|
|
unsigned int vertex_count = 3;
|
|
|
|
|
|
|
|
if (thickness != 1.0f)
|
|
|
|
{
|
|
|
|
_drawPolygon(_vertices3, vertex_count, Color4B::BLUE, Color4B::BLUE, true, thickness, true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Vec2* _vertices = _transform(_vertices3, vertex_count, false);
|
|
|
|
|
|
|
|
ensureCapacityTriangle(vertex_count);
|
|
|
|
|
|
|
|
V2F_C4B_T2F_Triangle* triangles = (V2F_C4B_T2F_Triangle*)(_bufferTriangle + _bufferCountTriangle);
|
|
|
|
triangles[0] = {{_vertices[0], fillColor, Vec2::ZERO},
|
|
|
|
{_vertices[1], fillColor, Vec2::ZERO},
|
|
|
|
{_vertices[2], fillColor, Vec2::ZERO}};
|
|
|
|
|
|
|
|
_customCommandTriangle.updateVertexBuffer(triangles, _bufferCountTriangle * sizeof(V2F_C4B_T2F),
|
|
|
|
vertex_count * sizeof(V2F_C4B_T2F));
|
|
|
|
_bufferCountTriangle += vertex_count;
|
|
|
|
_dirtyTriangle = true;
|
|
|
|
_customCommandTriangle.setVertexDrawInfo(0, _bufferCountTriangle);
|
|
|
|
|
|
|
|
AX_SAFE_DELETE_ARRAY(_vertices);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DrawNode::_drawAStar(const Vec2& center,
|
|
|
|
float radiusI, // inner
|
|
|
|
float radiusO, // outer
|
|
|
|
unsigned int segments,
|
|
|
|
const Color4B& color,
|
|
|
|
const Color4B& filledColor,
|
|
|
|
float thickness,
|
|
|
|
bool solid)
|
|
|
|
{
|
|
|
|
const float coef = 2.0f * (float)M_PI / segments;
|
|
|
|
float halfAngle = coef / 2.0f;
|
|
|
|
|
|
|
|
auto _vertices = _abuf.get<Vec2>(segments * 2 + 1);
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
for (unsigned int a = 0; a < segments; a++)
|
|
|
|
{
|
|
|
|
float rads = a * coef;
|
|
|
|
_vertices[i++] = Vec2(center.x + cos(rads) * radiusO, center.y + sin(rads) * radiusO);
|
|
|
|
_vertices[i++] = Vec2(center.x + cos(rads + halfAngle) * radiusI, center.y + sin(rads + halfAngle) * radiusI);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (solid)
|
|
|
|
{
|
|
|
|
_drawPolygon(_vertices, i, filledColor, color, true, thickness, false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_vertices[i++] = _vertices[0];
|
|
|
|
_drawPoly(_vertices, i, true, color, thickness, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DrawNode::_drawPoints(const Vec2* position,
|
|
|
|
unsigned int numberOfPoints,
|
|
|
|
const float pointSize,
|
|
|
|
const Color4B& color,
|
|
|
|
const DrawNode::PointType pointType)
|
|
|
|
{
|
|
|
|
if (properties.drawOrder == true)
|
|
|
|
{
|
|
|
|
float pointSize4 = pointSize * 0.25f;
|
|
|
|
Vec2 vec2Size4 = Vec2(pointSize4, pointSize4);
|
|
|
|
for (unsigned int i = 0; i < numberOfPoints; i++)
|
|
|
|
{
|
|
|
|
switch (pointType)
|
|
|
|
{
|
|
|
|
case PointType::Circle:
|
|
|
|
{
|
|
|
|
_drawCircle(position[i], pointSize4, 90, 32, false, 1.0f, 1.0f, Color4B(), color, true);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PointType::Rect:
|
|
|
|
{
|
|
|
|
Vec2 origin = position[i] - vec2Size4;
|
|
|
|
Vec2 destination = position[i] + vec2Size4;
|
|
|
|
Vec2 _vertices[] = {origin, Vec2(destination.x, origin.y), destination, Vec2(origin.x, destination.y),
|
|
|
|
origin};
|
|
|
|
_drawPolygon(_vertices, 5, color, color, false, 0.0f, true);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ensureCapacityPoint(numberOfPoints);
|
|
|
|
|
|
|
|
V2F_C4B_T2F* point = _bufferPoint + _bufferCountPoint;
|
|
|
|
for (unsigned int i = 0; i < numberOfPoints; i++)
|
|
|
|
{
|
|
|
|
*(point + i) = {position[i], color, Vec2(pointSize, 0.0f)};
|
|
|
|
}
|
|
|
|
|
|
|
|
_customCommandPoint.updateVertexBuffer(point, _bufferCountPoint * sizeof(V2F_C4B_T2F),
|
|
|
|
numberOfPoints * sizeof(V2F_C4B_T2F));
|
|
|
|
_bufferCountPoint += numberOfPoints;
|
|
|
|
_dirtyPoint = true;
|
|
|
|
_customCommandPoint.setVertexDrawInfo(0, _bufferCountPoint);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DrawNode::_drawPoint(const Vec2& position,
|
|
|
|
const float pointSize,
|
|
|
|
const Color4B& color,
|
|
|
|
const DrawNode::PointType pointType)
|
|
|
|
{
|
|
|
|
if (properties.drawOrder == true)
|
|
|
|
{
|
|
|
|
float pointSize4 = pointSize * 0.25f;
|
|
|
|
Vec2 vec2Size4 = Vec2(pointSize4, pointSize4);
|
|
|
|
|
|
|
|
switch (pointType)
|
|
|
|
{
|
|
|
|
case PointType::Circle:
|
|
|
|
{
|
|
|
|
_drawCircle(position, pointSize4, 90, 32, false, 1.0f, 1.0f, Color4B(), color, true);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case PointType::Rect:
|
|
|
|
{
|
|
|
|
Vec2 origin = position - vec2Size4;
|
|
|
|
Vec2 destination = position + vec2Size4;
|
|
|
|
Vec2 _vertices[] = {origin, Vec2(destination.x, origin.y), destination, Vec2(origin.x, destination.y),
|
|
|
|
origin};
|
|
|
|
_drawPolygon(_vertices, 5, color, color, false, 0.0f, true);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (properties.drawOrder == true)
|
|
|
|
{
|
|
|
|
float pointSize4 = pointSize * 0.25f;
|
|
|
|
Vec2 origin = position - Vec2(pointSize4, pointSize4);
|
|
|
|
Vec2 destination = position + Vec2(pointSize4, pointSize4);
|
|
|
|
Vec2 _vertices[] = {origin, Vec2(destination.x, origin.y), destination, Vec2(origin.x, destination.y)};
|
|
|
|
_drawPolygon(_vertices, 4, color, color, false, 0.0f, true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ensureCapacityPoint(1);
|
|
|
|
|
|
|
|
V2F_C4B_T2F* point = _bufferPoint + _bufferCountPoint;
|
|
|
|
*point = {position, color, Vec2(pointSize, 0.0f)};
|
|
|
|
|
|
|
|
_customCommandPoint.updateVertexBuffer(point, _bufferCountPoint * sizeof(V2F_C4B_T2F), sizeof(V2F_C4B_T2F));
|
|
|
|
_bufferCountPoint += 1;
|
|
|
|
_dirtyPoint = true;
|
|
|
|
_customCommandPoint.setVertexDrawInfo(0, _bufferCountPoint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DrawNode::_drawPie(const Vec2& center,
|
|
|
|
float radius,
|
|
|
|
float rotation,
|
|
|
|
int startAngle,
|
|
|
|
int endAngle,
|
|
|
|
float scaleX,
|
|
|
|
float scaleY,
|
|
|
|
const Color4B& fillColor,
|
|
|
|
const Color4B& borderColor,
|
|
|
|
DrawMode drawMode,
|
|
|
|
float thickness)
|
|
|
|
{
|
|
|
|
#define DEGREES 360
|
|
|
|
|
|
|
|
// Not a real line!
|
|
|
|
if (startAngle == endAngle)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Its a circle?
|
|
|
|
if (MAX((startAngle - endAngle), (endAngle - startAngle)) == DEGREES)
|
|
|
|
{
|
|
|
|
switch (drawMode)
|
|
|
|
{
|
|
|
|
case DrawMode::Fill:
|
|
|
|
_drawCircle(center, radius, 0.0f, 360, false, scaleX, scaleY, borderColor, fillColor, true, thickness);
|
|
|
|
break;
|
|
|
|
case DrawMode::Outline:
|
|
|
|
_drawCircle(center, radius, 0.0f, 360, false, scaleX, scaleY, borderColor, Color4B::TRANSPARENT, true,
|
|
|
|
thickness);
|
|
|
|
break;
|
|
|
|
case DrawMode::Line:
|
|
|
|
_drawCircle(center, radius, 0.0f, 360, false, scaleX, scaleY, borderColor, Color4B::TRANSPARENT, true,
|
|
|
|
thickness);
|
|
|
|
break;
|
|
|
|
case DrawMode::Semi:
|
|
|
|
_drawCircle(center, radius, 0.0f, 360, false, scaleX, scaleY, borderColor, fillColor, true, thickness);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const float coef = 2.0f * (float)M_PI / DEGREES;
|
|
|
|
|
|
|
|
auto _vertices = _abuf.get<Vec2>(DEGREES + 2);
|
|
|
|
|
|
|
|
int n = 0;
|
|
|
|
float rads = 0.0f;
|
|
|
|
float _angle = AX_DEGREES_TO_RADIANS(rotation);
|
|
|
|
|
|
|
|
if (startAngle > endAngle)
|
|
|
|
{
|
|
|
|
std::swap(endAngle, startAngle);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i <= DEGREES; i++)
|
|
|
|
{
|
|
|
|
if (startAngle <= i && endAngle >= i)
|
|
|
|
{
|
|
|
|
rads = i * coef;
|
|
|
|
|
|
|
|
float j = radius * cosf(rads + _angle) * scaleX + center.x;
|
|
|
|
float k = radius * sinf(rads + _angle) * scaleY + center.y;
|
|
|
|
|
|
|
|
_vertices[n].x = j;
|
|
|
|
_vertices[n].y = k;
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
switch (drawMode)
|
|
|
|
{
|
|
|
|
case DrawMode::Fill:
|
|
|
|
_vertices[n++] = center;
|
|
|
|
_vertices[n++] = _vertices[0];
|
|
|
|
_drawPolygon(_vertices, n, fillColor, borderColor, true, thickness, false);
|
|
|
|
break;
|
|
|
|
case DrawMode::Outline:
|
|
|
|
_vertices[n++] = center;
|
|
|
|
_vertices[n++] = _vertices[0];
|
|
|
|
_drawPolygon(_vertices, n, Color4B::TRANSPARENT, borderColor, false, thickness, false);
|
|
|
|
break;
|
|
|
|
case DrawMode::Line:
|
|
|
|
_drawPolygon(_vertices, n - 1, Color4B::TRANSPARENT, borderColor, false, thickness, false);
|
|
|
|
break;
|
|
|
|
case DrawMode::Semi:
|
|
|
|
_drawPolygon(_vertices, n - 1, fillColor, borderColor, true, thickness, false);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Vec2* DrawNode::_transform(const Vec2* _vertices, unsigned int& count, bool closedPolygon)
|
|
|
|
{
|
|
|
|
Vec2 vert0 = _vertices[0];
|
|
|
|
int closedCounter = 0;
|
|
|
|
|
|
|
|
if (closedPolygon && _vertices[0] != _vertices[count - 1])
|
|
|
|
{
|
|
|
|
closedCounter = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vec2* vert = new Vec2[count + closedCounter];
|
|
|
|
AXASSERT(vert != nullptr, "DrawNode::transform: NO MEMORY");
|
|
|
|
|
|
|
|
if (properties.transform == false)
|
|
|
|
{
|
|
|
|
memcpy(vert, _vertices, count * sizeof(Vec2));
|
|
|
|
if (closedCounter)
|
|
|
|
{
|
|
|
|
vert[count++] = vert0;
|
|
|
|
}
|
|
|
|
return vert;
|
|
|
|
}
|
|
|
|
|
|
|
|
const float sinRot = sin(properties.rotation);
|
|
|
|
const float cosRot = cos(properties.rotation);
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
if (properties.rotation == 0.0f)
|
|
|
|
{
|
|
|
|
vert[i].x = _vertices[i].x * properties.scale.x + properties.position.x;
|
|
|
|
vert[i].y = _vertices[i].y * properties.scale.y + properties.position.y;
|
|
|
|
}
|
|
|
|
else // https://stackoverflow.com/questions/2259476/rotating-a-point-about-another-point-2d
|
|
|
|
{
|
|
|
|
|
|
|
|
// translate point back to origin:
|
|
|
|
vert[i].x = _vertices[i].x - properties.center.x;
|
|
|
|
vert[i].y = _vertices[i].y - properties.center.y;
|
|
|
|
|
|
|
|
// rotate point
|
|
|
|
float xnew = vert[i].x * cosRot - vert[i].y * sinRot;
|
|
|
|
float ynew = vert[i].x * sinRot + vert[i].y * cosRot;
|
|
|
|
|
|
|
|
// translate point back:
|
|
|
|
vert[i].x = xnew + properties.center.x;
|
|
|
|
vert[i].y = ynew + properties.center.y;
|
|
|
|
|
|
|
|
// scale and position
|
|
|
|
vert[i].x = vert[i].x * properties.scale.x + properties.position.x;
|
|
|
|
vert[i].y = vert[i].y * properties.scale.y + properties.position.y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (closedCounter)
|
|
|
|
{
|
|
|
|
vert[count++] = vert[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
return vert;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
# pragma pop_macro("TRANSPARENT")
|
|
|
|
#endif
|
|
|
|
} // namespace ax
|