2019-11-23 20:27:39 +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
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
http://www.cocos2d-x.org
|
2021-12-25 10:04:45 +08:00
|
|
|
|
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:
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27: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-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27: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 "physics3d/CCPhysics3D.h"
|
2021-12-25 10:04:45 +08:00
|
|
|
#include <stddef.h> // offsetof
|
2019-11-23 20:27:39 +08:00
|
|
|
#include "base/ccTypes.h"
|
|
|
|
#include "base/CCConfiguration.h"
|
|
|
|
#include "base/ccMacros.h"
|
|
|
|
#include "base/CCDirector.h"
|
|
|
|
#include "base/ccUtils.h"
|
|
|
|
#include "renderer/CCRenderer.h"
|
|
|
|
#include "renderer/CCRenderState.h"
|
|
|
|
#include "renderer/ccShaders.h"
|
|
|
|
#include "renderer/backend/Buffer.h"
|
|
|
|
|
|
|
|
#if CC_USE_3D_PHYSICS
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
# if (CC_ENABLE_BULLET_INTEGRATION)
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Physics3DDebugDrawer::drawLine(const btVector3& from, const btVector3& to, const btVector3& color)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
Vec3 col = convertbtVector3ToVec3(color);
|
|
|
|
|
|
|
|
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-11-23 20:27:39 +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-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
_buffer.emplace_back(a);
|
|
|
|
_buffer.emplace_back(b);
|
|
|
|
|
|
|
|
_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)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
drawLine(PointOnB, PointOnB + normalOnB * distance, color);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Physics3DDebugDrawer::reportErrorWarning(const char* warningString)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
CCLOG("%s", warningString);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Physics3DDebugDrawer::draw3dText(const btVector3& /*location*/, const char* /*textString*/) {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Physics3DDebugDrawer::setDebugMode(int debugMode)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_debugMode = debugMode;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
int Physics3DDebugDrawer::getDebugMode() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return _debugMode;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Physics3DDebugDrawer::draw(Renderer* renderer)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
auto& transform = Director::getInstance()->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
_programState->setUniform(_locMVP, transform.m, sizeof(transform.m));
|
|
|
|
_customCommand.init(0, Mat4::IDENTITY, 0);
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
auto& blend = _customCommand.getPipelineDescriptor().blendDescriptor;
|
|
|
|
blend.blendEnabled = true;
|
2019-11-23 20:27:39 +08:00
|
|
|
blend.sourceAlphaBlendFactor = blend.sourceRGBBlendFactor = _blendFunc.src;
|
|
|
|
blend.destinationAlphaBlendFactor = blend.destinationRGBBlendFactor = _blendFunc.dst;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_customCommand.getVertexBuffer() == nullptr ||
|
|
|
|
_customCommand.getVertexBuffer()->getSize() < _buffer.size() * sizeof(_buffer[0]))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_customCommand.createVertexBuffer(sizeof(_buffer[0]), _buffer.size(), CustomCommand::BufferUsage::DYNAMIC);
|
|
|
|
_dirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_dirty)
|
|
|
|
{
|
|
|
|
_customCommand.updateVertexBuffer(_buffer.data(), _buffer.size() * sizeof(_buffer[0]));
|
|
|
|
_dirty = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
_customCommand.setVertexDrawInfo(0, _buffer.size());
|
|
|
|
|
|
|
|
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, _buffer.size());
|
|
|
|
|
|
|
|
renderer->addCommand(&_customCommand);
|
|
|
|
}
|
|
|
|
|
|
|
|
Physics3DDebugDrawer::Physics3DDebugDrawer()
|
|
|
|
{
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
Physics3DDebugDrawer::~Physics3DDebugDrawer()
|
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE(_programState);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Physics3DDebugDrawer::init()
|
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE_NULL(_programState);
|
|
|
|
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");
|
|
|
|
|
|
|
|
auto attributes = _programState->getProgram()->getActiveAttributes();
|
2019-11-23 20:27:39 +08:00
|
|
|
auto locPosition = attributes["a_position"];
|
2021-12-25 10:04:45 +08:00
|
|
|
auto locColor = attributes["a_color"];
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
auto layout = _programState->getVertexLayout();
|
2021-12-25 10:04:45 +08:00
|
|
|
layout->setAttribute(locPosition.attributeName.c_str(), locPosition.location, backend::VertexFormat::FLOAT3,
|
|
|
|
offsetof(V3F_V4F, vertex), false);
|
|
|
|
layout->setAttribute(locColor.attributeName.c_str(), locColor.location, backend::VertexFormat::FLOAT4,
|
|
|
|
offsetof(V3F_V4F, color), false);
|
2019-11-23 20:27:39 +08:00
|
|
|
layout->setLayout(sizeof(V3F_V4F));
|
|
|
|
|
|
|
|
_buffer.reserve(512);
|
|
|
|
|
|
|
|
_customCommand.getPipelineDescriptor().programState = _programState;
|
|
|
|
_customCommand.setPrimitiveType(CustomCommand::PrimitiveType::LINE);
|
|
|
|
_customCommand.setDrawType(CustomCommand::DrawType::ARRAY);
|
|
|
|
_customCommand.setBeforeCallback(CC_CALLBACK_0(Physics3DDebugDrawer::onBeforeDraw, this));
|
|
|
|
_customCommand.setAfterCallback(CC_CALLBACK_0(Physics3DDebugDrawer::onAfterDraw, this));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Physics3DDebugDrawer::onBeforeDraw()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto* renderer = Director::getInstance()->getRenderer();
|
2019-11-23 20:27:39 +08:00
|
|
|
_oldDepthTestEnabled = renderer->getDepthTest();
|
|
|
|
renderer->setDepthTest(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Physics3DDebugDrawer::onAfterDraw()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto* renderer = Director::getInstance()->getRenderer();
|
2019-11-23 20:27:39 +08:00
|
|
|
renderer->setDepthTest(_oldDepthTestEnabled);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Physics3DDebugDrawer::clear()
|
|
|
|
{
|
|
|
|
_buffer.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
# endif // CC_ENABLE_BULLET_INTEGRATION
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
#endif // CC_USE_3D_PHYSICS
|