axmol/core/2d/DrawNode.cpp

814 lines
27 KiB
C++
Raw Normal View History

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.
* Copyright (c) 2021 Bytedance Inc.
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/DrawNode.h"
2021-12-25 10:04:45 +08:00
#include <stddef.h> // offsetof
#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"
NS_AX_BEGIN
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
static inline Tex2F v2ToTex2F(const Vec2& v)
2019-11-23 20:27:39 +08:00
{
return {v.x, v.y};
}
// implementation of DrawNode
2021-12-25 10:04:45 +08:00
DrawNode::DrawNode(float lineWidth) : _lineWidth(lineWidth), _defaultLineWidth(lineWidth)
2019-11-23 20:27:39 +08:00
{
_blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
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
// auto listener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, [this](EventCustom* event){
// /** listen the event that renderer was recreated on Android/WP8 */
// this->setupBuffer();
// });
// _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
#endif
}
DrawNode::~DrawNode()
{
2022-07-16 10:43:05 +08:00
AX_SAFE_FREE(_bufferTriangle);
AX_SAFE_FREE(_bufferPoint);
AX_SAFE_FREE(_bufferLine);
2021-12-25 10:04:45 +08:00
freeShaderInternal(_customCommandTriangle);
freeShaderInternal(_customCommandPoint);
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
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
return ret;
}
void DrawNode::ensureCapacity(int count)
{
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
{
_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);
_customCommandTriangle.updateVertexBuffer(_bufferTriangle, _bufferCapacityTriangle * sizeof(V2F_C4B_T2F));
2019-11-23 20:27:39 +08:00
}
}
void DrawNode::ensureCapacityGLPoint(int count)
{
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
{
_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
}
}
void DrawNode::ensureCapacityGLLine(int count)
{
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
{
_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();
ensureCapacity(512);
ensureCapacityGLPoint(64);
ensureCapacityGLLine(256);
2021-12-25 10:04:45 +08:00
_dirtyTriangle = true;
2021-12-25 10:04:45 +08:00
_dirtyLine = true;
_dirtyPoint = true;
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);
updateShaderInternal(_customCommandPoint, backend::ProgramType::POSITION_COLOR_TEXTURE_AS_POINTSIZE,
2021-12-25 10:04:45 +08:00
CustomCommand::DrawType::ARRAY, CustomCommand::PrimitiveType::POINT);
updateShaderInternal(_customCommandLine, backend::ProgramType::POSITION_COLOR_LENGTH_TEXTURE,
2021-12-25 10:04:45 +08:00
CustomCommand::DrawType::ARRAY, CustomCommand::PrimitiveType::LINE);
}
2021-12-25 10:04:45 +08:00
void DrawNode::updateShaderInternal(CustomCommand& cmd,
uint32_t programType,
CustomCommand::DrawType drawType,
CustomCommand::PrimitiveType primitiveType)
{
auto& pipelinePS = cmd.getPipelineDescriptor().programState;
2022-07-16 10:43:05 +08:00
AX_SAFE_RELEASE(pipelinePS);
auto program = backend::Program::getBuiltinProgram(programType);
2021-12-25 10:04:45 +08:00
pipelinePS = new backend::ProgramState(program);
setVertexLayout(cmd);
cmd.setPrimitiveType(primitiveType);
cmd.setDrawType(drawType);
}
void DrawNode::setVertexLayout(CustomCommand& cmd)
{
auto* programState = cmd.getPipelineDescriptor().programState;
2023-08-25 16:57:38 +08:00
programState->validateSharedVertexLayout(VertexLayoutHelper::setupDrawNode);
}
void DrawNode::freeShaderInternal(CustomCommand& cmd)
2023-08-25 16:57:38 +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
{
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
{
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
{
updateBlendState(_customCommandLine);
updateUniforms(transform, _customCommandLine);
2023-08-25 16:57:38 +08:00
_customCommandLine.setLineWidth(_lineWidth);
_customCommandLine.init(_globalZOrder);
renderer->addCommand(&_customCommandLine);
2019-11-23 20:27:39 +08:00
}
}
2021-12-25 10:04:45 +08:00
void DrawNode::drawPoint(const Vec2& position, const float pointSize, const Color4B& color)
2019-11-23 20:27:39 +08:00
{
ensureCapacityGLPoint(1);
2021-12-25 10:04:45 +08:00
V2F_C4B_T2F* point = _bufferPoint + _bufferCountPoint;
*point = {position, color, Tex2F(pointSize, 0)};
_customCommandPoint.updateVertexBuffer(point, _bufferCountPoint * sizeof(V2F_C4B_T2F), sizeof(V2F_C4B_T2F));
_bufferCountPoint += 1;
_dirtyPoint = true;
_customCommandPoint.setVertexDrawInfo(0, _bufferCountPoint);
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 Color4B& color)
2019-11-23 20:27:39 +08:00
{
drawPoints(position, numberOfPoints, 1.0, color);
}
2021-12-25 10:04:45 +08:00
void DrawNode::drawPoints(const Vec2* position,
unsigned int numberOfPoints,
const float pointSize,
const Color4B& color)
2019-11-23 20:27:39 +08:00
{
ensureCapacityGLPoint(numberOfPoints);
2021-12-25 10:04:45 +08:00
V2F_C4B_T2F* point = _bufferPoint + _bufferCountPoint;
for (unsigned int i = 0; i < numberOfPoints; i++)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
*(point + i) = {position[i], color, Tex2F(pointSize, 0)};
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
_customCommandPoint.updateVertexBuffer(point, _bufferCountPoint * sizeof(V2F_C4B_T2F),
numberOfPoints * sizeof(V2F_C4B_T2F));
_bufferCountPoint += numberOfPoints;
_dirtyPoint = true;
_customCommandPoint.setVertexDrawInfo(0, _bufferCountPoint);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
void DrawNode::drawLine(const Vec2& origin, const Vec2& destination, const Color4B& color)
2019-11-23 20:27:39 +08:00
{
ensureCapacityGLLine(2);
2021-12-25 10:04:45 +08:00
V2F_C4B_T2F* point = _bufferLine + _bufferCountLine;
*point = {origin, color, Tex2F(0.0, 0.0)};
*(point + 1) = {destination, color, Tex2F(0.0, 0.0)};
_customCommandLine.updateVertexBuffer(point, _bufferCountLine * sizeof(V2F_C4B_T2F), 2 * sizeof(V2F_C4B_T2F));
_bufferCountLine += 2;
_dirtyLine = true;
_customCommandLine.setVertexDrawInfo(0, _bufferCountLine);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
void DrawNode::drawRect(const Vec2& origin, const Vec2& destination, const Color4B& color)
2019-11-23 20:27:39 +08:00
{
drawLine(origin, Vec2(destination.x, origin.y), color);
drawLine(Vec2(destination.x, origin.y), destination, color);
drawLine(destination, Vec2(origin.x, destination.y), color);
drawLine(Vec2(origin.x, destination.y), origin, color);
}
2021-12-25 10:04:45 +08:00
void DrawNode::drawPoly(const Vec2* poli, unsigned int numberOfPoints, bool closePolygon, const Color4B& color)
2019-11-23 20:27:39 +08:00
{
unsigned int vertex_count;
2021-12-25 10:04:45 +08:00
if (closePolygon)
2019-11-23 20:27:39 +08:00
{
vertex_count = 2 * numberOfPoints;
ensureCapacityGLLine(vertex_count);
}
else
{
vertex_count = 2 * (numberOfPoints - 1);
ensureCapacityGLLine(vertex_count);
}
2021-12-25 10:04:45 +08:00
V2F_C4B_T2F* point = _bufferLine + _bufferCountLine;
V2F_C4B_T2F* cursor = point;
2019-11-23 20:27:39 +08:00
unsigned int i = 0;
2021-12-25 10:04:45 +08:00
for (; i < numberOfPoints - 1; i++)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
*point = {poli[i], color, Tex2F(0.0, 0.0)};
*(point + 1) = {poli[i + 1], color, Tex2F(0.0, 0.0)};
2019-11-23 20:27:39 +08:00
point += 2;
}
2021-12-25 10:04:45 +08:00
if (closePolygon)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
*point = {poli[i], color, Tex2F(0.0, 0.0)};
*(point + 1) = {poli[0], color, Tex2F(0.0, 0.0)};
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
_customCommandLine.updateVertexBuffer(cursor, _bufferCountLine * sizeof(V2F_C4B_T2F),
vertex_count * sizeof(V2F_C4B_T2F));
_bufferCountLine += vertex_count;
_customCommandLine.setVertexDrawInfo(0, _bufferCountLine);
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,
const Color4B& color,
float threshold)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
const float coef = 2.0f * (float)M_PI / segments;
2021-12-08 15:16:19 +08:00
auto vertices = _abuf.get<Vec2>(segments + 2);
2021-12-25 10:04:45 +08:00
for (unsigned int i = 0; i <= segments; i++)
{
float rads = i * coef;
2021-08-14 10:28:28 +08:00
vertices[i].x = radius * cosf(rads + angle) * scaleX + center.x;
vertices[i].y = radius * sinf(rads + angle) * scaleY + center.y;
2019-11-23 20:27:39 +08:00
}
if (_lineWidth > threshold)
2019-11-23 20:27:39 +08:00
{
drawPolygon(vertices, segments, Color4B(1.0f, 0.0f, 0.0f, 1.0f), _lineWidth/4, color);
2019-11-23 20:27:39 +08:00
}
else
{
if (drawLineToCenter)
{
vertices[segments + 1] = center;
drawPoly(vertices, segments + 2, true, color);
}
else
drawPoly(vertices, segments + 1, true, color);
}
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,
const Color4B& color,
float threshold)
2019-11-23 20:27:39 +08:00
{
drawCircle(center, radius, angle, segments, drawLineToCenter, 1.0f, 1.0f, color, threshold);
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,
const Color4B& color)
2019-11-23 20:27:39 +08:00
{
2021-12-08 15:16:19 +08:00
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
{
vertices[i].x = powf(1 - t, 2) * origin.x + 2.0f * (1 - t) * t * control.x + t * t * destination.x;
vertices[i].y = powf(1 - t, 2) * origin.y + 2.0f * (1 - t) * t * control.y + t * t * destination.y;
t += 1.0f / segments;
}
vertices[segments].x = destination.x;
vertices[segments].y = destination.y;
2021-12-25 10:04:45 +08:00
drawPoly(vertices, segments + 1, false, color);
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,
const Color4B& color)
2019-11-23 20:27:39 +08:00
{
2021-12-08 15:16:19 +08:00
Vec2* vertices = _abuf.get<Vec2>(segments + 1);
2021-12-08 00:11:53 +08:00
2019-11-23 20:27:39 +08:00
float t = 0;
for (unsigned int i = 0; i < segments; i++)
{
2021-12-25 10:04:45 +08:00
vertices[i].x = powf(1 - t, 3) * origin.x + 3.0f * powf(1 - t, 2) * t * control1.x +
3.0f * (1 - t) * t * t * control2.x + t * t * t * destination.x;
vertices[i].y = powf(1 - t, 3) * origin.y + 3.0f * powf(1 - t, 2) * 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;
}
vertices[segments].x = destination.x;
vertices[segments].y = destination.y;
2021-12-25 10:04:45 +08:00
drawPoly(vertices, segments + 1, false, color);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
void DrawNode::drawCardinalSpline(PointArray* config, float tension, unsigned int segments, const Color4B& color)
2019-11-23 20:27:39 +08:00
{
2021-12-08 15:16:19 +08:00
Vec2* vertices = _abuf.get<Vec2>(segments + 1);
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
for (unsigned int i = 0; i < segments + 1; i++)
{
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);
Vec2 newPos = ccCardinalSplineAt(pp0, pp1, pp2, pp3, tension, lt);
2019-11-23 20:27:39 +08:00
vertices[i].x = newPos.x;
vertices[i].y = newPos.y;
}
2021-12-25 10:04:45 +08:00
drawPoly(vertices, segments + 1, false, color);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
void DrawNode::drawCatmullRom(PointArray* points, unsigned int segments, const Color4B& color)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
drawCardinalSpline(points, 0.5f, segments, color);
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
{
2021-12-25 10:04:45 +08:00
unsigned int vertex_count = 2 * 3;
2019-11-23 20:27:39 +08:00
ensureCapacity(vertex_count);
2021-12-25 10:04:45 +08:00
V2F_C4B_T2F a = {Vec2(pos.x - radius, pos.y - radius), color, Tex2F(-1.0, -1.0)};
V2F_C4B_T2F b = {Vec2(pos.x - radius, pos.y + radius), color, Tex2F(-1.0, 1.0)};
V2F_C4B_T2F c = {Vec2(pos.x + radius, pos.y + radius), color, Tex2F(1.0, 1.0)};
V2F_C4B_T2F d = {Vec2(pos.x + radius, pos.y - radius), color, Tex2F(1.0, -1.0)};
V2F_C4B_T2F_Triangle* triangles = (V2F_C4B_T2F_Triangle*)(_bufferTriangle + _bufferCountTriangle);
V2F_C4B_T2F_Triangle triangle0 = {a, b, c};
V2F_C4B_T2F_Triangle triangle1 = {a, c, d};
triangles[0] = triangle0;
triangles[1] = triangle1;
_customCommandTriangle.updateVertexBuffer(triangles, _bufferCountTriangle * sizeof(V2F_C4B_T2F),
vertex_count * sizeof(V2F_C4B_T2F));
_bufferCountTriangle += vertex_count;
_dirtyTriangle = true;
_customCommandTriangle.setVertexDrawInfo(0, _bufferCountTriangle);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
void DrawNode::drawRect(const Vec2& p1, const Vec2& p2, const Vec2& p3, const Vec2& p4, const Color4B& color)
2019-11-23 20:27:39 +08:00
{
drawLine(p1, p2, color);
drawLine(p2, p3, color);
drawLine(p3, p4, color);
drawLine(p4, p1, color);
}
2021-12-25 10:04:45 +08:00
void DrawNode::drawSegment(const Vec2& from, const Vec2& to, float radius, const Color4B& color)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
unsigned int vertex_count = 6 * 3;
2019-11-23 20:27:39 +08:00
ensureCapacity(vertex_count);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
Vec2 a = from;
Vec2 b = to;
Vec2 n = ((b - a).getPerp()).getNormalized();
Vec2 t = n.getPerp();
Vec2 nw = n * radius;
Vec2 tw = t * radius;
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);
2021-12-25 10:04:45 +08:00
V2F_C4B_T2F_Triangle* triangles = (V2F_C4B_T2F_Triangle*)(_bufferTriangle + _bufferCountTriangle);
2019-11-23 20:27:39 +08:00
V2F_C4B_T2F_Triangle triangles0 = {
{v0, color, v2ToTex2F(-(n + t))},
{v1, color, v2ToTex2F(n - t)},
{v2, color, v2ToTex2F(-n)},
2019-11-23 20:27:39 +08:00
};
triangles[0] = triangles0;
V2F_C4B_T2F_Triangle triangles1 = {
{v3, color, v2ToTex2F(n)},
{v1, color, v2ToTex2F(n - t)},
{v2, color, v2ToTex2F(-n)},
2019-11-23 20:27:39 +08:00
};
triangles[1] = triangles1;
V2F_C4B_T2F_Triangle triangles2 = {
{v3, color, v2ToTex2F(n)},
{v4, color, v2ToTex2F(-n)},
{v2, color, v2ToTex2F(-n)},
2019-11-23 20:27:39 +08:00
};
triangles[2] = triangles2;
V2F_C4B_T2F_Triangle triangles3 = {
{v3, color, v2ToTex2F(n)},
{v4, color, v2ToTex2F(-n)},
2021-12-25 10:04:45 +08:00
{v5, color, v2ToTex2F(n)},
2019-11-23 20:27:39 +08:00
};
triangles[3] = triangles3;
V2F_C4B_T2F_Triangle triangles4 = {
{v6, color, v2ToTex2F(t - n)},
2021-12-25 10:04:45 +08:00
{v4, color, v2ToTex2F(-n)},
{v5, color, v2ToTex2F(n)},
2019-11-23 20:27:39 +08:00
};
triangles[4] = triangles4;
V2F_C4B_T2F_Triangle triangles5 = {
{v6, color, v2ToTex2F(t - n)},
{v7, color, v2ToTex2F(t + n)},
{v5, color, v2ToTex2F(n)},
2019-11-23 20:27:39 +08:00
};
triangles[5] = triangles5;
2021-12-25 10:04:45 +08:00
_customCommandTriangle.updateVertexBuffer(triangles, _bufferCountTriangle * sizeof(V2F_C4B_T2F),
vertex_count * sizeof(V2F_C4B_T2F));
_bufferCountTriangle += vertex_count;
_dirtyTriangle = true;
_customCommandTriangle.setVertexDrawInfo(0, _bufferCountTriangle);
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
void DrawNode::drawPolygon(const Vec2* verts,
int count,
const Color4B& fillColor,
float borderWidth,
const Color4B& borderColor)
2019-11-23 20:27:39 +08:00
{
2022-07-16 10:43:05 +08:00
AXASSERT(count >= 0, "invalid count value");
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
bool outline = (borderColor.a > 0.0f && borderWidth > 0.0f);
2021-12-25 10:04:45 +08:00
auto triangle_count = outline ? (3 * count - 2) : (count - 2);
auto vertex_count = 3 * triangle_count;
2019-11-23 20:27:39 +08:00
ensureCapacity(vertex_count);
2021-12-25 10:04:45 +08:00
V2F_C4B_T2F_Triangle* triangles = (V2F_C4B_T2F_Triangle*)(_bufferTriangle + _bufferCountTriangle);
V2F_C4B_T2F_Triangle* cursor = triangles;
for (int i = 0; i < count - 2; i++)
2019-11-23 20:27:39 +08:00
{
V2F_C4B_T2F_Triangle tmp = {
{verts[0], fillColor, v2ToTex2F(Vec2::ZERO)},
2021-12-25 10:04:45 +08:00
{verts[i + 1], fillColor, v2ToTex2F(Vec2::ZERO)},
{verts[i + 2], fillColor, v2ToTex2F(Vec2::ZERO)},
2019-11-23 20:27:39 +08:00
};
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
*cursor++ = tmp;
}
2021-12-25 10:04:45 +08:00
if (outline)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
struct ExtrudeVerts
{
Vec2 offset, n;
};
2019-11-23 20:27:39 +08:00
struct ExtrudeVerts* extrude = (struct ExtrudeVerts*)malloc(sizeof(struct ExtrudeVerts) * count);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
for (int i = 0; i < count; i++)
{
2021-12-25 10:04:45 +08:00
Vec2 v0 = verts[(i - 1 + count) % count];
2019-11-23 20:27:39 +08:00
Vec2 v1 = verts[i];
2021-12-25 10:04:45 +08:00
Vec2 v2 = verts[(i + 1) % count];
2019-11-23 20:27:39 +08:00
Vec2 n1 = ((v1 - v0).getPerp()).getNormalized();
Vec2 n2 = ((v2 - v1).getPerp()).getNormalized();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
Vec2 offset = (n1 + n2) * (1.0f / (Vec2::dot(n1, n2) + 1.0f));
2021-12-25 10:04:45 +08:00
extrude[i] = {offset, n2};
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
for (int i = 0; i < count; i++)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
int j = (i + 1) % count;
2019-11-23 20:27:39 +08:00
Vec2 v0 = verts[i];
Vec2 v1 = verts[j];
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
Vec2 n0 = extrude[i].n;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
Vec2 offset0 = extrude[i].offset;
Vec2 offset1 = extrude[j].offset;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
Vec2 inner0 = v0 - offset0 * borderWidth;
Vec2 inner1 = v1 - offset1 * borderWidth;
Vec2 outer0 = v0 + offset0 * borderWidth;
Vec2 outer1 = v1 + offset1 * borderWidth;
2021-12-25 10:04:45 +08:00
V2F_C4B_T2F_Triangle tmp1 = {{inner0, borderColor, v2ToTex2F(-n0)},
{inner1, borderColor, v2ToTex2F(-n0)},
{outer1, borderColor, v2ToTex2F(n0)}};
*cursor++ = tmp1;
V2F_C4B_T2F_Triangle tmp2 = {{inner0, borderColor, v2ToTex2F(-n0)},
{outer0, borderColor, v2ToTex2F(n0)},
{outer1, borderColor, v2ToTex2F(n0)}};
*cursor++ = tmp2;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
free(extrude);
}
2021-12-25 10:04:45 +08:00
_customCommandTriangle.updateVertexBuffer(triangles, _bufferCountTriangle * sizeof(V2F_C4B_T2F),
vertex_count * sizeof(V2F_C4B_T2F));
_bufferCountTriangle += vertex_count;
_customCommandTriangle.setVertexDrawInfo(0, _bufferCountTriangle);
_dirtyTriangle = true;
2019-11-23 20:27:39 +08:00
}
2021-12-25 10:04:45 +08:00
void DrawNode::drawSolidRect(const Vec2& origin, const Vec2& destination, const Color4B& color)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
Vec2 vertices[] = {origin, Vec2(destination.x, origin.y), destination, Vec2(origin.x, destination.y)};
2019-11-23 20:27:39 +08:00
drawSolidPoly(vertices, 4, color);
}
2021-12-25 10:04:45 +08:00
void DrawNode::drawSolidPoly(const Vec2* poli, unsigned int numberOfPoints, const Color4B& color)
2019-11-23 20:27:39 +08:00
{
drawPolygon(poli, numberOfPoints, color, 0.0, Color4B());
2019-11-23 20:27:39 +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,
const Color4B& fillColor,
float borderWidth,
const Color4B& borderColor)
{
const float coef = 2.0f * (float)M_PI / segments;
2021-12-08 15:16:19 +08:00
Vec2* vertices = _abuf.get<Vec2>(segments);
for (unsigned int i = 0; i < segments; i++)
{
float rads = i * coef;
2021-12-25 10:04:45 +08:00
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;
}
drawPolygon(vertices, segments, fillColor, borderWidth, borderColor);
}
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,
const Color4B& color)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
const float coef = 2.0f * (float)M_PI / segments;
Vec2* vertices = _abuf.get<Vec2>(segments);
for (unsigned int i = 0; i < segments; i++)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
float rads = i * coef;
float j = radius * cosf(rads + angle) * scaleX + center.x;
float k = radius * sinf(rads + angle) * scaleY + center.y;
2019-11-23 20:27:39 +08:00
vertices[i].x = j;
vertices[i].y = k;
}
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
drawSolidPoly(vertices, segments, color);
}
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
{
drawSolidCircle(center, radius, angle, segments, 1.0f, 1.0f, color);
}
2021-12-25 10:04:45 +08:00
void DrawNode::drawTriangle(const Vec2& p1, const Vec2& p2, const Vec2& p3, const Color4B& color)
2019-11-23 20:27:39 +08:00
{
unsigned int vertex_count = 3;
ensureCapacity(vertex_count);
2021-12-25 10:04:45 +08:00
V2F_C4B_T2F a = {p1, color, Tex2F(0.0, 0.0)};
V2F_C4B_T2F b = {p2, color, Tex2F(0.0, 0.0)};
V2F_C4B_T2F c = {p3, color, Tex2F(0.0, 0.0)};
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
V2F_C4B_T2F_Triangle* triangles = (V2F_C4B_T2F_Triangle*)(_bufferTriangle + _bufferCountTriangle);
V2F_C4B_T2F_Triangle triangle = {a, b, c};
triangles[0] = triangle;
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
_customCommandTriangle.updateVertexBuffer(triangles, _bufferCountTriangle * sizeof(V2F_C4B_T2F),
vertex_count * sizeof(V2F_C4B_T2F));
_bufferCountTriangle += vertex_count;
_dirtyTriangle = true;
_customCommandTriangle.setVertexDrawInfo(0, _bufferCountTriangle);
2019-11-23 20:27:39 +08:00
}
void DrawNode::clear()
{
_bufferCountTriangle = 0;
2021-12-25 10:04:45 +08:00
_dirtyTriangle = true;
_bufferCountLine = 0;
_dirtyLine = true;
_bufferCountPoint = 0;
_dirtyPoint = true;
_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)
{
_lineWidth = lineWidth;
}
float DrawNode::getLineWidth()
{
return this->_lineWidth;
}
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);
}
}
NS_AX_END