2015-05-29 16:45:33 +08:00
|
|
|
/****************************************************************************
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2015-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
https://axys1.github.io/
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-05-29 16:45:33 +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-25 10:04:45 +08:00
|
|
|
|
2015-05-29 16:45:33 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-05-29 16:45:33 +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 "navmesh/CCNavMeshDebugDraw.h"
|
2022-07-15 19:17:01 +08:00
|
|
|
#if AX_USE_NAVMESH
|
2021-12-25 10:04:45 +08:00
|
|
|
# include <stddef.h> // offsetof
|
|
|
|
# include "base/ccTypes.h"
|
|
|
|
# include "renderer/backend/ProgramState.h"
|
|
|
|
# include "renderer/backend/Device.h"
|
|
|
|
# include "renderer/CCRenderer.h"
|
|
|
|
# include "renderer/CCRenderState.h"
|
|
|
|
# include "renderer/ccShaders.h"
|
|
|
|
# include "base/CCDirector.h"
|
|
|
|
# include "base/ccMacros.h"
|
2015-05-29 16:45:33 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2015-05-29 16:45:33 +08:00
|
|
|
|
|
|
|
NavMeshDebugDraw::NavMeshDebugDraw()
|
2019-03-20 14:34:35 +08:00
|
|
|
{
|
2019-10-15 09:40:59 +08:00
|
|
|
auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_COLOR);
|
|
|
|
_programState = new backend::ProgramState(program);
|
2021-12-25 10:04:45 +08:00
|
|
|
_locMVP = _programState->getUniformLocation("u_MVPMatrix");
|
2019-03-20 14:34:35 +08:00
|
|
|
|
2019-08-15 16:51:49 +08:00
|
|
|
auto vertexLayout = _programState->getVertexLayout();
|
2021-12-25 10:04:45 +08:00
|
|
|
vertexLayout->setAttribute("a_position", _programState->getAttributeLocation("a_position"),
|
|
|
|
backend::VertexFormat::FLOAT3, offsetof(V3F_C4F, position), false);
|
|
|
|
vertexLayout->setAttribute("a_color", _programState->getAttributeLocation("a_color"), backend::VertexFormat::FLOAT4,
|
|
|
|
offsetof(V3F_C4F, color), false);
|
2019-08-15 16:51:49 +08:00
|
|
|
vertexLayout->setLayout(sizeof(V3F_C4F));
|
2019-03-20 14:34:35 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void NavMeshDebugDraw::initCustomCommand(CustomCommand& command)
|
2019-03-20 14:34:35 +08:00
|
|
|
{
|
|
|
|
command.set3D(true);
|
|
|
|
command.setTransparent(true);
|
|
|
|
command.init(0, Mat4::IDENTITY, Node::FLAGS_RENDER_AS_3D);
|
|
|
|
command.setDrawType(CustomCommand::DrawType::ARRAY);
|
2021-12-25 10:04:45 +08:00
|
|
|
auto& pipelineDescriptor = command.getPipelineDescriptor();
|
2019-03-20 14:34:35 +08:00
|
|
|
pipelineDescriptor.programState = _programState;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
auto& blend = pipelineDescriptor.blendDescriptor;
|
|
|
|
blend.blendEnabled = true;
|
2019-03-20 14:34:35 +08:00
|
|
|
blend.sourceRGBBlendFactor = blend.sourceAlphaBlendFactor = BlendFunc::ALPHA_NON_PREMULTIPLIED.src;
|
|
|
|
blend.destinationRGBBlendFactor = blend.destinationAlphaBlendFactor = BlendFunc::ALPHA_NON_PREMULTIPLIED.dst;
|
2015-05-29 16:45:33 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void NavMeshDebugDraw::vertex(const float /*x*/,
|
|
|
|
const float /*y*/,
|
|
|
|
const float /*z*/,
|
|
|
|
unsigned int /*color*/,
|
|
|
|
const float /*u*/,
|
|
|
|
const float /*v*/)
|
|
|
|
{}
|
2015-05-29 16:45:33 +08:00
|
|
|
|
|
|
|
void NavMeshDebugDraw::vertex(const float* pos, unsigned int color, const float* uv)
|
|
|
|
{
|
|
|
|
vertex(pos[0], pos[1], pos[2], color, uv[0], uv[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void NavMeshDebugDraw::vertex(const float x, const float y, const float z, unsigned int color)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!_currentPrimitive)
|
|
|
|
return;
|
|
|
|
V3F_C4F vertex = {Vec3(x, y, z), getColor(color)};
|
2022-08-08 13:18:33 +08:00
|
|
|
_vertices.emplace_back(vertex);
|
2015-05-29 16:45:33 +08:00
|
|
|
_dirtyBuffer = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NavMeshDebugDraw::vertex(const float* pos, unsigned int color)
|
|
|
|
{
|
|
|
|
vertex(pos[0], pos[1], pos[2], color);
|
|
|
|
}
|
|
|
|
|
|
|
|
NavMeshDebugDraw::~NavMeshDebugDraw()
|
|
|
|
{
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& iter : _primitiveList)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2015-05-29 16:45:33 +08:00
|
|
|
delete iter;
|
|
|
|
}
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_RELEASE_NULL(_programState);
|
|
|
|
AX_SAFE_RELEASE_NULL(_vertexBuffer);
|
2015-05-29 16:45:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void NavMeshDebugDraw::depthMask(bool state)
|
|
|
|
{
|
|
|
|
_currentDepthMask = state;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NavMeshDebugDraw::begin(duDebugDrawPrimitives prim, float size /*= 1.0f*/)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_currentPrimitive)
|
|
|
|
return;
|
|
|
|
_currentPrimitive = new Primitive;
|
|
|
|
_currentPrimitive->type = getPrimitiveType(prim);
|
2015-05-29 16:45:33 +08:00
|
|
|
_currentPrimitive->depthMask = _currentDepthMask;
|
2021-12-25 10:04:45 +08:00
|
|
|
_currentPrimitive->start = _vertices.size();
|
|
|
|
_currentPrimitive->size = size;
|
2015-05-29 16:45:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void NavMeshDebugDraw::end()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!_currentPrimitive)
|
|
|
|
return;
|
2015-05-29 16:45:33 +08:00
|
|
|
_currentPrimitive->end = _vertices.size();
|
2022-08-08 13:18:33 +08:00
|
|
|
_primitiveList.emplace_back(_currentPrimitive);
|
2015-05-29 16:45:33 +08:00
|
|
|
_currentPrimitive = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vec4 NavMeshDebugDraw::getColor(unsigned int col)
|
|
|
|
{
|
|
|
|
const unsigned int r = col & 0xff;
|
|
|
|
const unsigned int g = (col >> 8) & 0xff;
|
|
|
|
const unsigned int b = (col >> 16) & 0xff;
|
|
|
|
const unsigned int a = (col >> 24) & 0xff;
|
|
|
|
|
|
|
|
float factor = 1.0f / 255.0f;
|
|
|
|
return Vec4(r, g, b, a) * factor;
|
|
|
|
}
|
|
|
|
|
2019-03-20 14:34:35 +08:00
|
|
|
backend::PrimitiveType NavMeshDebugDraw::getPrimitiveType(duDebugDrawPrimitives prim)
|
2015-05-29 16:45:33 +08:00
|
|
|
{
|
|
|
|
switch (prim)
|
|
|
|
{
|
|
|
|
case DU_DRAW_POINTS:
|
2019-03-20 14:34:35 +08:00
|
|
|
return backend::PrimitiveType::POINT;
|
2015-05-29 16:45:33 +08:00
|
|
|
case DU_DRAW_LINES:
|
2019-03-20 14:34:35 +08:00
|
|
|
return backend::PrimitiveType::LINE;
|
2015-05-29 16:45:33 +08:00
|
|
|
case DU_DRAW_TRIS:
|
2019-03-20 14:34:35 +08:00
|
|
|
return backend::PrimitiveType::TRIANGLE;
|
2015-05-29 16:45:33 +08:00
|
|
|
default:
|
2019-03-20 14:34:35 +08:00
|
|
|
return backend::PrimitiveType::POINT;
|
2015-05-29 16:45:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-20 14:34:35 +08:00
|
|
|
void NavMeshDebugDraw::draw(Renderer* renderer)
|
2015-05-29 16:45:33 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto& transform = Director::getInstance()->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
|
2022-06-24 14:18:48 +08:00
|
|
|
auto beforeCommand = renderer->nextCallbackCommand();
|
|
|
|
auto afterCommand = renderer->nextCallbackCommand();
|
|
|
|
|
|
|
|
beforeCommand->init(0, Mat4::IDENTITY, Node::FLAGS_RENDER_AS_3D);
|
|
|
|
afterCommand->init(0, Mat4::IDENTITY, Node::FLAGS_RENDER_AS_3D);
|
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
beforeCommand->func = AX_CALLBACK_0(NavMeshDebugDraw::onBeforeVisitCmd, this);
|
|
|
|
afterCommand->func = AX_CALLBACK_0(NavMeshDebugDraw::onAfterVisitCmd, this);
|
2019-03-20 14:34:35 +08:00
|
|
|
|
2022-06-24 14:18:48 +08:00
|
|
|
beforeCommand->set3D(true);
|
|
|
|
beforeCommand->setTransparent(true);
|
|
|
|
afterCommand->set3D(true);
|
|
|
|
afterCommand->setTransparent(true);
|
2015-05-29 16:45:33 +08:00
|
|
|
|
2019-03-20 14:34:35 +08:00
|
|
|
_programState->setUniform(_locMVP, transform.m, sizeof(transform.m));
|
|
|
|
|
2022-06-24 14:18:48 +08:00
|
|
|
renderer->addCommand(beforeCommand);
|
2019-03-20 14:34:35 +08:00
|
|
|
|
|
|
|
if (!_vertexBuffer || _vertexBuffer->getSize() < _vertices.size() * sizeof(_vertices[0]))
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_vertexBuffer = backend::Device::getInstance()->newBuffer(
|
|
|
|
_vertices.size() * sizeof(_vertices[0]), backend::BufferType::VERTEX, backend::BufferUsage::STATIC);
|
2019-03-20 14:34:35 +08:00
|
|
|
_dirtyBuffer = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_dirtyBuffer)
|
|
|
|
{
|
|
|
|
_vertexBuffer->updateData(_vertices.data(), sizeof(_vertices[0]) * _vertices.size());
|
2015-05-29 16:45:33 +08:00
|
|
|
_dirtyBuffer = false;
|
|
|
|
}
|
2019-03-20 14:34:35 +08:00
|
|
|
int idx = 0;
|
|
|
|
if (_commands.size() < _primitiveList.size())
|
|
|
|
{
|
|
|
|
_commands.resize(_primitiveList.size());
|
|
|
|
}
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& iter : _primitiveList)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2019-03-20 14:34:35 +08:00
|
|
|
if (iter->type == backend::PrimitiveType::POINT)
|
|
|
|
continue;
|
|
|
|
if (iter->end - iter->start <= 0)
|
2015-12-25 16:02:03 +08:00
|
|
|
continue;
|
2019-03-20 14:34:35 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
auto& command = _commands[idx];
|
2019-03-20 14:34:35 +08:00
|
|
|
|
|
|
|
initCustomCommand(command);
|
2022-07-15 19:17:01 +08:00
|
|
|
command.setBeforeCallback(AX_CALLBACK_0(NavMeshDebugDraw::onBeforeEachCommand, this, iter->depthMask));
|
2019-03-20 14:34:35 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (iter->type == backend::PrimitiveType::LINE)
|
|
|
|
{
|
2019-03-20 14:34:35 +08:00
|
|
|
command.setLineWidth(iter->size);
|
2015-05-29 16:45:33 +08:00
|
|
|
}
|
2019-03-20 14:34:35 +08:00
|
|
|
|
|
|
|
command.setVertexBuffer(_vertexBuffer);
|
|
|
|
command.setPrimitiveType(iter->type);
|
|
|
|
command.setVertexDrawInfo(iter->start, iter->end - iter->start);
|
|
|
|
|
|
|
|
renderer->addCommand(&command);
|
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, iter->end - iter->start);
|
2019-03-20 14:34:35 +08:00
|
|
|
idx++;
|
2015-05-29 16:45:33 +08:00
|
|
|
}
|
2019-03-20 14:34:35 +08:00
|
|
|
|
2022-06-24 14:18:48 +08:00
|
|
|
renderer->addCommand(afterCommand);
|
2015-05-29 16:45:33 +08:00
|
|
|
}
|
|
|
|
|
2019-03-20 14:34:35 +08:00
|
|
|
void NavMeshDebugDraw::onBeforeVisitCmd()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto* renderer = Director::getInstance()->getRenderer();
|
|
|
|
|
2019-03-20 14:34:35 +08:00
|
|
|
_rendererDepthTestEnabled = renderer->getDepthTest();
|
2021-12-25 10:04:45 +08:00
|
|
|
_rendererDepthCmpFunc = renderer->getDepthCompareFunction();
|
|
|
|
_rendererCullMode = renderer->getCullMode();
|
2019-03-20 14:34:35 +08:00
|
|
|
|
|
|
|
_rendererDepthWrite = renderer->getDepthWrite();
|
2021-12-25 10:04:45 +08:00
|
|
|
_rendererWinding = renderer->getWinding();
|
2019-03-20 14:34:35 +08:00
|
|
|
|
|
|
|
renderer->setCullMode(backend::CullMode::NONE);
|
|
|
|
renderer->setDepthTest(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void NavMeshDebugDraw::onAfterVisitCmd()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto* renderer = Director::getInstance()->getRenderer();
|
2019-03-20 14:34:35 +08:00
|
|
|
renderer->setDepthTest(_rendererDepthTestEnabled);
|
|
|
|
renderer->setDepthCompareFunction(_rendererDepthCmpFunc);
|
|
|
|
renderer->setCullMode(_rendererCullMode);
|
|
|
|
renderer->setDepthWrite(_rendererDepthWrite);
|
|
|
|
renderer->setWinding(_rendererWinding);
|
|
|
|
}
|
|
|
|
|
|
|
|
void NavMeshDebugDraw::onBeforeEachCommand(bool enableDepthWrite)
|
2015-05-29 16:45:33 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto* renderer = Director::getInstance()->getRenderer();
|
2019-03-20 14:34:35 +08:00
|
|
|
renderer->setDepthWrite(enableDepthWrite);
|
2015-05-29 16:45:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void NavMeshDebugDraw::clear()
|
|
|
|
{
|
|
|
|
_vertices.clear();
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& iter : _primitiveList)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
2015-05-29 16:45:33 +08:00
|
|
|
delete iter;
|
|
|
|
}
|
|
|
|
_primitiveList.clear();
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|
2015-05-29 16:45:33 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
#endif // AX_USE_NAVMESH
|