2015-05-08 15:49: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-08 15:49: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-08 15:49: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-08 15:49: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.
|
|
|
|
****************************************************************************/
|
2016-03-20 21:53:44 +08:00
|
|
|
#include "physics3d/CCPhysics3D.h"
|
2021-12-25 10:04:45 +08:00
|
|
|
#include <stddef.h> // offsetof
|
2019-11-06 14:24:06 +08:00
|
|
|
#include "base/ccTypes.h"
|
2015-05-08 15:49:33 +08:00
|
|
|
#include "base/CCConfiguration.h"
|
|
|
|
#include "base/ccMacros.h"
|
|
|
|
#include "base/CCDirector.h"
|
2018-09-11 14:39:30 +08:00
|
|
|
#include "base/ccUtils.h"
|
2015-05-08 15:49:33 +08:00
|
|
|
#include "renderer/CCRenderer.h"
|
2015-06-13 06:03:48 +08:00
|
|
|
#include "renderer/CCRenderState.h"
|
2019-03-20 14:34:35 +08:00
|
|
|
#include "renderer/ccShaders.h"
|
|
|
|
#include "renderer/backend/Buffer.h"
|
2015-05-08 15:49:33 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
#if AX_USE_3D_PHYSICS
|
2015-05-08 15:49:33 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
# if (AX_ENABLE_BULLET_INTEGRATION)
|
2015-05-08 15:49:33 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2015-05-08 15:49:33 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Physics3DDebugDrawer::drawLine(const btVector3& from, const btVector3& to, const btVector3& color)
|
2015-05-08 15:49:33 +08:00
|
|
|
{
|
|
|
|
Vec3 col = convertbtVector3ToVec3(color);
|
|
|
|
|
2019-03-20 14:34:35 +08:00
|
|
|
V3F_V4F a, b;
|
|
|
|
a.vertex = convertbtVector3ToVec3(from);
|
2021-12-25 10:04:45 +08:00
|
|
|
a.color = Vec4(col.x, col.y, col.z, 1.0f);
|
2019-03-20 14:34:35 +08:00
|
|
|
b.vertex = convertbtVector3ToVec3(to);
|
2021-12-25 10:04:45 +08:00
|
|
|
b.color = Vec4(col.x, col.y, col.z, 1.0f);
|
2019-03-20 14:34:35 +08:00
|
|
|
|
|
|
|
_buffer.emplace_back(a);
|
|
|
|
_buffer.emplace_back(b);
|
2015-05-08 15:49:33 +08:00
|
|
|
|
|
|
|
_dirty = true;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Physics3DDebugDrawer::drawContactPoint(const btVector3& PointOnB,
|
|
|
|
const btVector3& normalOnB,
|
|
|
|
btScalar distance,
|
|
|
|
int /*lifeTime*/,
|
|
|
|
const btVector3& color)
|
2015-05-08 15:49:33 +08:00
|
|
|
{
|
|
|
|
drawLine(PointOnB, PointOnB + normalOnB * distance, color);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Physics3DDebugDrawer::reportErrorWarning(const char* warningString)
|
2015-05-08 15:49:33 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("%s", warningString);
|
2015-05-08 15:49:33 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Physics3DDebugDrawer::draw3dText(const btVector3& /*location*/, const char* /*textString*/) {}
|
2015-05-08 15:49:33 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Physics3DDebugDrawer::setDebugMode(int debugMode)
|
2015-05-08 15:49:33 +08:00
|
|
|
{
|
|
|
|
_debugMode = debugMode;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
int Physics3DDebugDrawer::getDebugMode() const
|
2015-05-08 15:49:33 +08:00
|
|
|
{
|
|
|
|
return _debugMode;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Physics3DDebugDrawer::draw(Renderer* renderer)
|
2015-05-08 15:49:33 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
auto& transform = Director::getInstance()->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
|
2019-03-20 14:34:35 +08:00
|
|
|
|
|
|
|
_programState->setUniform(_locMVP, transform.m, sizeof(transform.m));
|
2015-05-08 15:49:33 +08:00
|
|
|
_customCommand.init(0, Mat4::IDENTITY, 0);
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
auto& blend = _customCommand.getPipelineDescriptor().blendDescriptor;
|
|
|
|
blend.blendEnabled = true;
|
2019-03-20 14:34:35 +08:00
|
|
|
blend.sourceAlphaBlendFactor = blend.sourceRGBBlendFactor = _blendFunc.src;
|
|
|
|
blend.destinationAlphaBlendFactor = blend.destinationRGBBlendFactor = _blendFunc.dst;
|
2015-05-08 15:49:33 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_customCommand.getVertexBuffer() == nullptr ||
|
|
|
|
_customCommand.getVertexBuffer()->getSize() < _buffer.size() * sizeof(_buffer[0]))
|
2015-05-08 15:49:33 +08:00
|
|
|
{
|
2019-03-20 14:34:35 +08:00
|
|
|
_customCommand.createVertexBuffer(sizeof(_buffer[0]), _buffer.size(), CustomCommand::BufferUsage::DYNAMIC);
|
|
|
|
_dirty = true;
|
2015-05-08 15:49:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_dirty)
|
|
|
|
{
|
2019-03-20 14:34:35 +08:00
|
|
|
_customCommand.updateVertexBuffer(_buffer.data(), _buffer.size() * sizeof(_buffer[0]));
|
2015-05-08 15:49:33 +08:00
|
|
|
_dirty = false;
|
|
|
|
}
|
|
|
|
|
2019-09-24 14:28:44 +08:00
|
|
|
_customCommand.setVertexDrawInfo(0, _buffer.size());
|
2015-05-08 15:49:33 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, _buffer.size());
|
2019-03-20 14:34:35 +08:00
|
|
|
|
|
|
|
renderer->addCommand(&_customCommand);
|
2015-05-08 15:49:33 +08:00
|
|
|
}
|
|
|
|
|
2019-03-20 14:34:35 +08:00
|
|
|
Physics3DDebugDrawer::Physics3DDebugDrawer()
|
2015-05-08 15:49:33 +08:00
|
|
|
{
|
2019-03-20 14:34:35 +08:00
|
|
|
init();
|
|
|
|
}
|
2015-05-08 15:49:33 +08:00
|
|
|
|
2019-03-20 14:34:35 +08:00
|
|
|
Physics3DDebugDrawer::~Physics3DDebugDrawer()
|
|
|
|
{
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_RELEASE(_programState);
|
2019-03-20 14:34:35 +08:00
|
|
|
}
|
2015-05-08 15:49:33 +08:00
|
|
|
|
2019-03-20 14:34:35 +08:00
|
|
|
void Physics3DDebugDrawer::init()
|
|
|
|
{
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_RELEASE_NULL(_programState);
|
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
|
|
|
_buffer.reserve(512);
|
|
|
|
|
|
|
|
_customCommand.getPipelineDescriptor().programState = _programState;
|
|
|
|
_customCommand.setPrimitiveType(CustomCommand::PrimitiveType::LINE);
|
|
|
|
_customCommand.setDrawType(CustomCommand::DrawType::ARRAY);
|
2022-07-15 19:17:01 +08:00
|
|
|
_customCommand.setBeforeCallback(AX_CALLBACK_0(Physics3DDebugDrawer::onBeforeDraw, this));
|
|
|
|
_customCommand.setAfterCallback(AX_CALLBACK_0(Physics3DDebugDrawer::onAfterDraw, this));
|
2019-03-20 14:34:35 +08:00
|
|
|
}
|
2015-05-08 15:49:33 +08:00
|
|
|
|
2019-03-20 14:34:35 +08:00
|
|
|
void Physics3DDebugDrawer::onBeforeDraw()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto* renderer = Director::getInstance()->getRenderer();
|
2019-03-20 14:34:35 +08:00
|
|
|
_oldDepthTestEnabled = renderer->getDepthTest();
|
|
|
|
renderer->setDepthTest(true);
|
|
|
|
}
|
2015-05-08 15:49:33 +08:00
|
|
|
|
2019-03-20 14:34:35 +08:00
|
|
|
void Physics3DDebugDrawer::onAfterDraw()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto* renderer = Director::getInstance()->getRenderer();
|
2019-03-20 14:34:35 +08:00
|
|
|
renderer->setDepthTest(_oldDepthTestEnabled);
|
2015-05-08 15:49:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Physics3DDebugDrawer::clear()
|
|
|
|
{
|
2019-03-20 14:34:35 +08:00
|
|
|
_buffer.clear();
|
2015-05-08 15:49:33 +08:00
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|
2015-05-08 15:49:33 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
# endif // AX_ENABLE_BULLET_INTEGRATION
|
2015-05-08 15:49:33 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
#endif // AX_USE_3D_PHYSICS
|