2014-08-08 17:54:39 +08:00
|
|
|
/****************************************************************************
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2014-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2022-02-24 18:45:25 +08:00
|
|
|
https://adxeproject.github.io/
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2014-08-08 17:54: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:
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2014-08-08 17:54:39 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2014-08-08 17:54:39 +08:00
|
|
|
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 "DrawNode3D.h"
|
2019-04-03 17:27:15 +08:00
|
|
|
#include "renderer/backend/Buffer.h"
|
2014-08-08 17:54:39 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
DrawNode3D::DrawNode3D()
|
|
|
|
{
|
|
|
|
_blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
|
|
|
|
}
|
|
|
|
|
|
|
|
DrawNode3D::~DrawNode3D()
|
|
|
|
{
|
2019-02-15 14:30:10 +08:00
|
|
|
CC_SAFE_RELEASE_NULL(_programStateLine);
|
|
|
|
CC_SAFE_DELETE(_depthstencilDescriptor);
|
2014-08-08 17:54:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
DrawNode3D* DrawNode3D::create()
|
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
DrawNode3D* ret = new DrawNode3D();
|
|
|
|
if (ret->init())
|
2014-08-08 17:54:39 +08:00
|
|
|
{
|
|
|
|
ret->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(ret);
|
|
|
|
}
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2014-08-08 17:54:39 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DrawNode3D::ensureCapacity(int count)
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
CCASSERT(count >= 0, "capacity must be >= 0");
|
|
|
|
|
2019-10-25 16:40:30 +08:00
|
|
|
auto EXTENDED_SIZE = _bufferLines.size() + count;
|
2019-04-03 17:27:15 +08:00
|
|
|
|
|
|
|
_bufferLines.reserve(EXTENDED_SIZE);
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
if (!_customCommand.getVertexBuffer() ||
|
|
|
|
_customCommand.getVertexBuffer()->getSize() < (EXTENDED_SIZE * sizeof(_bufferLines[0])))
|
2019-04-03 17:27:15 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
_customCommand.createVertexBuffer(sizeof(V3F_C4B), EXTENDED_SIZE + (EXTENDED_SIZE >> 1),
|
|
|
|
CustomCommand::BufferUsage::DYNAMIC);
|
2019-04-03 17:27:15 +08:00
|
|
|
}
|
2014-08-08 17:54:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DrawNode3D::init()
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
_blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
|
|
|
|
auto& pd = _customCommand.getPipelineDescriptor();
|
|
|
|
auto program = backend::Program::getBuiltinProgram(backend::ProgramType::LINE_COLOR_3D);
|
2019-10-15 09:40:59 +08:00
|
|
|
_programStateLine = new backend::ProgramState(program);
|
2021-12-28 16:06:23 +08:00
|
|
|
pd.programState = _programStateLine;
|
|
|
|
|
2019-02-15 14:30:10 +08:00
|
|
|
_locMVPMatrix = _programStateLine->getUniformLocation("u_MVPMatrix");
|
|
|
|
|
2019-04-04 16:13:17 +08:00
|
|
|
_customCommand.setBeforeCallback(CC_CALLBACK_0(DrawNode3D::onBeforeDraw, this));
|
|
|
|
_customCommand.setAfterCallback(CC_CALLBACK_0(DrawNode3D::onAfterDraw, this));
|
2019-02-15 14:30:10 +08:00
|
|
|
|
2019-08-15 16:51:49 +08:00
|
|
|
auto layout = _programStateLine->getVertexLayout();
|
2019-02-15 14:30:10 +08:00
|
|
|
#define INITIAL_VERTEX_BUFFER_LENGTH 512
|
|
|
|
|
|
|
|
ensureCapacity(INITIAL_VERTEX_BUFFER_LENGTH);
|
|
|
|
|
|
|
|
_customCommand.setDrawType(CustomCommand::DrawType::ARRAY);
|
|
|
|
_customCommand.setPrimitiveType(CustomCommand::PrimitiveType::LINE);
|
|
|
|
|
2019-03-12 19:40:13 +08:00
|
|
|
const auto& attributeInfo = _programStateLine->getProgram()->getActiveAttributes();
|
2021-12-28 16:06:23 +08:00
|
|
|
auto iter = attributeInfo.find("a_position");
|
|
|
|
if (iter != attributeInfo.end())
|
2019-03-12 19:40:13 +08:00
|
|
|
{
|
2019-08-15 16:51:49 +08:00
|
|
|
layout->setAttribute("a_position", iter->second.location, backend::VertexFormat::FLOAT3, 0, false);
|
2019-03-12 19:40:13 +08:00
|
|
|
}
|
|
|
|
iter = attributeInfo.find("a_color");
|
2021-12-28 16:06:23 +08:00
|
|
|
if (iter != attributeInfo.end())
|
2019-03-12 19:40:13 +08:00
|
|
|
{
|
2019-08-15 16:51:49 +08:00
|
|
|
layout->setAttribute("a_color", iter->second.location, backend::VertexFormat::UBYTE4, sizeof(Vec3), true);
|
2019-03-12 19:40:13 +08:00
|
|
|
}
|
2019-08-15 16:51:49 +08:00
|
|
|
layout->setLayout(sizeof(V3F_C4B));
|
2021-12-28 16:06:23 +08:00
|
|
|
|
|
|
|
_customCommand.createVertexBuffer(sizeof(V3F_C4B), INITIAL_VERTEX_BUFFER_LENGTH,
|
|
|
|
CustomCommand::BufferUsage::DYNAMIC);
|
2019-02-15 14:30:10 +08:00
|
|
|
_isDirty = true;
|
|
|
|
|
2014-08-08 17:54:39 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
|
|
|
// Need to listen the event only when not use batchnode, because it will use VBO
|
2021-12-28 16:06:23 +08:00
|
|
|
auto listener = EventListenerCustom::create(EVENT_COME_TO_FOREGROUND, [this](EventCustom* event) {
|
|
|
|
/** listen the event that coming to foreground on Android */
|
2014-08-08 17:54:39 +08:00
|
|
|
this->init();
|
|
|
|
});
|
|
|
|
|
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
|
|
|
#endif
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2014-08-08 17:54:39 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
void DrawNode3D::draw(Renderer* renderer, const Mat4& transform, uint32_t flags)
|
2014-08-08 17:54:39 +08:00
|
|
|
{
|
2015-01-16 06:00:49 +08:00
|
|
|
_customCommand.init(_globalZOrder, transform, flags);
|
2014-08-08 17:54:39 +08:00
|
|
|
|
2019-02-15 14:30:10 +08:00
|
|
|
updateCommand(renderer, transform, flags);
|
2014-08-08 17:54:39 +08:00
|
|
|
|
2019-02-15 14:30:10 +08:00
|
|
|
if (_isDirty && !_bufferLines.empty())
|
2014-08-08 17:54:39 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
_customCommand.updateVertexBuffer(_bufferLines.data(),
|
|
|
|
(unsigned int)(_bufferLines.size() * sizeof(_bufferLines[0])));
|
2019-04-03 17:27:15 +08:00
|
|
|
_customCommand.setVertexDrawInfo(0, _bufferLines.size());
|
2019-02-15 14:30:10 +08:00
|
|
|
_isDirty = false;
|
2014-08-08 17:54:39 +08:00
|
|
|
}
|
2019-02-15 14:30:10 +08:00
|
|
|
|
|
|
|
if (!_bufferLines.empty())
|
2014-08-08 17:54:39 +08:00
|
|
|
{
|
2019-02-15 14:30:10 +08:00
|
|
|
renderer->addCommand(&_customCommand);
|
2014-08-08 17:54:39 +08:00
|
|
|
}
|
2019-02-15 14:30:10 +08:00
|
|
|
}
|
2014-08-08 17:54:39 +08:00
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
void DrawNode3D::updateCommand(cocos2d::Renderer* renderer, const Mat4& transform, uint32_t flags)
|
2019-02-15 14:30:10 +08:00
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
auto& matrixP = Director::getInstance()->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
|
|
|
|
auto mvp = matrixP * transform;
|
2014-08-08 17:54:39 +08:00
|
|
|
|
2019-02-15 14:30:10 +08:00
|
|
|
_programStateLine->setUniform(_locMVPMatrix, mvp.m, sizeof(mvp.m));
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
auto& blend = _customCommand.getPipelineDescriptor().blendDescriptor;
|
|
|
|
blend.blendEnabled = true;
|
2019-03-20 09:45:09 +08:00
|
|
|
blend.sourceRGBBlendFactor = blend.sourceAlphaBlendFactor = _blendFunc.src;
|
|
|
|
blend.destinationRGBBlendFactor = blend.destinationAlphaBlendFactor = _blendFunc.dst;
|
2014-08-08 17:54:39 +08:00
|
|
|
|
2019-02-15 14:30:10 +08:00
|
|
|
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, _bufferLines.size());
|
2014-08-08 17:54:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
void DrawNode3D::drawLine(const Vec3& from, const Vec3& to, const Color4F& color)
|
2014-08-08 17:54:39 +08:00
|
|
|
{
|
|
|
|
unsigned int vertex_count = 2;
|
|
|
|
ensureCapacity(vertex_count);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2014-08-08 17:54:39 +08:00
|
|
|
Color4B col = Color4B(color);
|
2021-12-28 16:06:23 +08:00
|
|
|
V3F_C4B a = {Vec3(from.x, from.y, from.z), col};
|
|
|
|
V3F_C4B b = {
|
|
|
|
Vec3(to.x, to.y, to.z),
|
|
|
|
col,
|
|
|
|
};
|
2014-08-08 17:54:39 +08:00
|
|
|
|
2019-02-15 14:30:10 +08:00
|
|
|
_bufferLines.push_back(a);
|
|
|
|
_bufferLines.push_back(b);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2019-02-15 14:30:10 +08:00
|
|
|
_isDirty = true;
|
2014-08-08 17:54:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
void DrawNode3D::drawCube(Vec3* vertices, const Color4F& color)
|
2014-08-08 17:54:39 +08:00
|
|
|
{
|
|
|
|
// front face
|
|
|
|
drawLine(vertices[0], vertices[1], color);
|
|
|
|
drawLine(vertices[1], vertices[2], color);
|
|
|
|
drawLine(vertices[2], vertices[3], color);
|
|
|
|
drawLine(vertices[3], vertices[0], color);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2014-08-08 17:54:39 +08:00
|
|
|
// back face
|
|
|
|
drawLine(vertices[4], vertices[5], color);
|
|
|
|
drawLine(vertices[5], vertices[6], color);
|
|
|
|
drawLine(vertices[6], vertices[7], color);
|
|
|
|
drawLine(vertices[7], vertices[4], color);
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2014-08-08 17:54:39 +08:00
|
|
|
// edge
|
|
|
|
drawLine(vertices[0], vertices[7], color);
|
|
|
|
drawLine(vertices[1], vertices[6], color);
|
|
|
|
drawLine(vertices[2], vertices[5], color);
|
|
|
|
drawLine(vertices[3], vertices[4], color);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DrawNode3D::clear()
|
|
|
|
{
|
2019-02-15 14:30:10 +08:00
|
|
|
_bufferLines.clear();
|
|
|
|
_isDirty = true;
|
2014-08-08 17:54:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const BlendFunc& DrawNode3D::getBlendFunc() const
|
|
|
|
{
|
|
|
|
return _blendFunc;
|
|
|
|
}
|
|
|
|
|
2021-12-28 16:06:23 +08:00
|
|
|
void DrawNode3D::setBlendFunc(const BlendFunc& blendFunc)
|
2014-08-08 17:54:39 +08:00
|
|
|
{
|
|
|
|
_blendFunc = blendFunc;
|
|
|
|
}
|
|
|
|
|
2019-02-15 14:30:10 +08:00
|
|
|
void DrawNode3D::onBeforeDraw()
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
auto* renderer = Director::getInstance()->getRenderer();
|
2019-02-15 14:30:10 +08:00
|
|
|
_rendererDepthTestEnabled = renderer->getDepthTest();
|
|
|
|
renderer->setDepthTest(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DrawNode3D::onAfterDraw()
|
|
|
|
{
|
2021-12-28 16:06:23 +08:00
|
|
|
auto* renderer = Director::getInstance()->getRenderer();
|
2019-02-15 14:30:10 +08:00
|
|
|
renderer->setDepthTest(_rendererDepthTestEnabled);
|
|
|
|
}
|
|
|
|
|
2014-08-08 17:54:39 +08:00
|
|
|
NS_CC_END
|