axmol/core/2d/CCCameraBackgroundBrush.cpp

491 lines
17 KiB
C++
Raw Normal View History

/****************************************************************************
Copyright (c) 2015-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2021-12-25 10:04:45 +08:00
https://axis-project.github.io/
2021-12-25 10:04:45 +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
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
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.
****************************************************************************/
2015-08-21 10:27:19 +08:00
#include "2d/CCCameraBackgroundBrush.h"
2021-12-25 10:04:45 +08:00
#include <stddef.h> // offsetof
#include "base/ccTypes.h"
#include "2d/CCCamera.h"
#include "base/ccMacros.h"
2018-09-11 14:39:30 +08:00
#include "base/ccUtils.h"
#include "base/CCConfiguration.h"
#include "base/CCDirector.h"
#include "renderer/CCRenderer.h"
#include "renderer/CCRenderState.h"
#include "renderer/CCTextureCube.h"
#include "renderer/ccShaders.h"
2022-07-15 19:17:01 +08:00
#if AX_ENABLE_CACHE_TEXTURE_DATA
2021-12-25 10:04:45 +08:00
# include "base/CCEventCustom.h"
# include "base/CCEventListenerCustom.h"
# include "base/CCEventType.h"
# include "base/CCEventDispatcher.h"
#endif
NS_AX_BEGIN
2021-12-25 10:04:45 +08:00
CameraBackgroundBrush::CameraBackgroundBrush() {}
2015-08-21 10:27:19 +08:00
CameraBackgroundBrush::~CameraBackgroundBrush()
{
2022-07-15 19:17:01 +08:00
AX_SAFE_RELEASE_NULL(_programState);
}
2015-08-21 10:27:19 +08:00
CameraBackgroundBrush* CameraBackgroundBrush::createNoneBrush()
{
2021-12-08 00:11:53 +08:00
auto ret = new CameraBackgroundBrush();
ret->init();
2021-12-25 10:04:45 +08:00
ret->autorelease();
return ret;
}
2015-08-21 10:27:19 +08:00
CameraBackgroundColorBrush* CameraBackgroundBrush::createColorBrush(const Color4F& color, float depth)
{
2015-08-21 10:27:19 +08:00
return CameraBackgroundColorBrush::create(color, depth);
}
2015-08-21 10:27:19 +08:00
CameraBackgroundDepthBrush* CameraBackgroundBrush::createDepthBrush(float depth)
{
2015-08-21 10:27:19 +08:00
return CameraBackgroundDepthBrush::create(depth);
}
2021-12-26 23:26:34 +08:00
CameraBackgroundSkyBoxBrush* CameraBackgroundBrush::createSkyboxBrush(std::string_view positive_x,
std::string_view negative_x,
std::string_view positive_y,
std::string_view negative_y,
std::string_view positive_z,
std::string_view negative_z)
{
2015-08-21 10:27:19 +08:00
return CameraBackgroundSkyBoxBrush::create(positive_x, negative_x, positive_y, negative_y, positive_z, negative_z);
}
//////////////////////////////////////////////////////////////////////////////////////////
2015-08-21 10:27:19 +08:00
CameraBackgroundDepthBrush::CameraBackgroundDepthBrush()
2021-12-25 10:04:45 +08:00
: _depth(0.f)
, _clearColor(false)
2022-07-15 19:17:01 +08:00
#if AX_ENABLE_CACHE_TEXTURE_DATA
, _backToForegroundListener(nullptr)
#endif
{
2022-07-15 19:17:01 +08:00
#if AX_ENABLE_CACHE_TEXTURE_DATA
2021-12-25 10:04:45 +08:00
_backToForegroundListener =
EventListenerCustom::create(EVENT_RENDERER_RECREATED, [this](EventCustom*) { initBuffer(); });
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, -1);
#endif
}
2015-08-21 10:27:19 +08:00
CameraBackgroundDepthBrush::~CameraBackgroundDepthBrush()
{
2022-07-15 19:17:01 +08:00
#if AX_ENABLE_CACHE_TEXTURE_DATA
Director::getInstance()->getEventDispatcher()->removeEventListener(_backToForegroundListener);
#endif
}
2015-08-21 10:27:19 +08:00
CameraBackgroundDepthBrush* CameraBackgroundDepthBrush::create(float depth)
{
2021-12-08 00:11:53 +08:00
auto ret = new CameraBackgroundDepthBrush();
2021-12-25 10:04:45 +08:00
2021-12-08 00:11:53 +08:00
if (ret->init())
{
ret->_depth = depth;
ret->autorelease();
}
else
{
2022-07-15 19:17:01 +08:00
AX_SAFE_DELETE(ret);
}
return ret;
}
2015-08-21 10:27:19 +08:00
bool CameraBackgroundDepthBrush::init()
{
2022-07-15 19:17:01 +08:00
AX_SAFE_RELEASE_NULL(_programState);
auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::CAMERA_CLEAR);
_programState = new backend::ProgramState(program);
_locDepth = _programState->getUniformLocation("dpeth");
2021-12-25 10:04:45 +08:00
auto& pipelineDescriptor = _customCommand.getPipelineDescriptor();
pipelineDescriptor.programState = _programState;
2021-12-25 10:04:45 +08:00
auto layout = _programState->getVertexLayout();
2019-03-12 19:40:13 +08:00
const auto& attributeInfo = _programState->getProgram()->getActiveAttributes();
2021-12-25 10:04:45 +08:00
auto iter = attributeInfo.find("a_position");
if (iter != attributeInfo.end())
2019-03-12 19:40:13 +08:00
{
2021-12-25 10:04:45 +08:00
layout->setAttribute("a_position", iter->second.location, backend::VertexFormat::FLOAT3,
offsetof(V3F_C4B_T2F, vertices), false);
2019-03-12 19:40:13 +08:00
}
iter = attributeInfo.find("a_color");
2021-12-25 10:04:45 +08:00
if (iter != attributeInfo.end())
2019-03-12 19:40:13 +08:00
{
2021-12-25 10:04:45 +08:00
layout->setAttribute("a_color", iter->second.location, backend::VertexFormat::UBYTE4,
offsetof(V3F_C4B_T2F, colors), true);
2019-03-12 19:40:13 +08:00
}
iter = attributeInfo.find("a_texCoord");
2021-12-25 10:04:45 +08:00
if (iter != attributeInfo.end())
2019-03-12 19:40:13 +08:00
{
2021-12-25 10:04:45 +08:00
layout->setAttribute("a_texCoord", iter->second.location, backend::VertexFormat::FLOAT2,
offsetof(V3F_C4B_T2F, texCoords), true);
2019-03-12 19:40:13 +08:00
}
layout->setLayout(sizeof(_vertices[0]));
_vertices.resize(4);
_vertices[0].vertices = Vec3(-1, -1, 0);
_vertices[1].vertices = Vec3(1, -1, 0);
_vertices[2].vertices = Vec3(1, 1, 0);
_vertices[3].vertices = Vec3(-1, 1, 0);
_vertices[0].colors = _vertices[1].colors = _vertices[2].colors = _vertices[3].colors = Color4B(0, 0, 0, 1);
_vertices[0].texCoords = Tex2F(0, 0);
_vertices[1].texCoords = Tex2F(1, 0);
_vertices[2].texCoords = Tex2F(1, 1);
_vertices[3].texCoords = Tex2F(0, 1);
2022-07-15 19:17:01 +08:00
_customCommand.setBeforeCallback(AX_CALLBACK_0(CameraBackgroundDepthBrush::onBeforeDraw, this));
_customCommand.setAfterCallback(AX_CALLBACK_0(CameraBackgroundDepthBrush::onAfterDraw, this));
initBuffer();
return true;
}
void CameraBackgroundDepthBrush::initBuffer()
{
2021-12-25 10:04:45 +08:00
uint16_t indices[6] = {0, 1, 2, 2, 3, 0};
_customCommand.createVertexBuffer(sizeof(_vertices[0]), _vertices.size(), CustomCommand::BufferUsage::STATIC);
2021-12-25 10:04:45 +08:00
_customCommand.createIndexBuffer(CustomCommand::IndexFormat::U_SHORT, sizeof(indices) / sizeof(indices[0]),
CustomCommand::BufferUsage::STATIC);
_customCommand.updateVertexBuffer(_vertices.data(), sizeof(_vertices[0]) * _vertices.size());
_customCommand.updateIndexBuffer(indices, sizeof(indices));
}
void CameraBackgroundDepthBrush::drawBackground(Camera* /*camera*/)
{
// `clear screen` should be executed before other commands
_groupCommand.init(-1.0f);
_customCommand.init(0.0f);
2021-12-25 10:04:45 +08:00
auto* renderer = Director::getInstance()->getRenderer();
renderer->addCommand(&_groupCommand);
renderer->pushGroup(_groupCommand.getRenderQueueID());
2021-12-25 10:04:45 +08:00
auto& pipelineDescriptor = _customCommand.getPipelineDescriptor();
auto& blend = pipelineDescriptor.blendDescriptor;
blend.writeMask = _clearColor ? backend::ColorWriteMask::ALL : backend::ColorWriteMask::NONE;
2021-12-25 10:04:45 +08:00
// draw
_programState->setUniform(_locDepth, &_depth, sizeof(_depth));
2021-12-25 10:04:45 +08:00
renderer->addCommand(&_customCommand);
renderer->popGroup();
}
void CameraBackgroundDepthBrush::onBeforeDraw()
{
2021-12-25 10:04:45 +08:00
auto* renderer = Director::getInstance()->getRenderer();
_stateBlock.stencilWriteMask = renderer->getStencilWriteMask();
_stateBlock.depthTest = renderer->getDepthTest();
_stateBlock.compareFunc = renderer->getDepthCompareFunction();
renderer->setStencilWriteMask(0);
renderer->setDepthTest(true);
renderer->setDepthCompareFunction(backend::CompareFunction::ALWAYS);
}
void CameraBackgroundDepthBrush::onAfterDraw()
{
2021-12-25 10:04:45 +08:00
auto* renderer = Director::getInstance()->getRenderer();
renderer->setStencilWriteMask(_stateBlock.stencilWriteMask);
renderer->setDepthTest(_stateBlock.depthTest);
renderer->setDepthCompareFunction(_stateBlock.compareFunc);
}
//////////////////////////////////////////////////////////////////////////////////////////
2021-12-25 10:04:45 +08:00
CameraBackgroundColorBrush::CameraBackgroundColorBrush() : _color(0.f, 0.f, 0.f, 0.f) {}
2021-12-25 10:04:45 +08:00
CameraBackgroundColorBrush::~CameraBackgroundColorBrush() {}
bool CameraBackgroundColorBrush::init()
{
CameraBackgroundDepthBrush::init();
2019-06-05 17:58:33 +08:00
this->_clearColor = true;
return true;
}
void CameraBackgroundColorBrush::drawBackground(Camera* camera)
{
2021-12-25 10:04:45 +08:00
BlendFunc op = {BlendFunc::ALPHA_NON_PREMULTIPLIED.src, BlendFunc::ALPHA_NON_PREMULTIPLIED.dst};
2021-12-25 10:04:45 +08:00
auto& blend = _customCommand.getPipelineDescriptor().blendDescriptor;
blend.sourceRGBBlendFactor = blend.sourceAlphaBlendFactor = op.src;
blend.destinationRGBBlendFactor = blend.destinationAlphaBlendFactor = op.dst;
2021-12-25 10:04:45 +08:00
blend.blendEnabled = true;
CameraBackgroundDepthBrush::drawBackground(camera);
}
2015-08-21 10:27:19 +08:00
void CameraBackgroundColorBrush::setColor(const Color4F& color)
{
for (auto&& vert : _vertices)
{
vert.colors = Color4B(color);
}
_customCommand.updateVertexBuffer(_vertices.data(), sizeof(_vertices[0]) * _vertices.size());
}
2015-08-21 10:27:19 +08:00
CameraBackgroundColorBrush* CameraBackgroundColorBrush::create(const Color4F& color, float depth)
{
2021-12-08 00:11:53 +08:00
auto ret = new CameraBackgroundColorBrush();
2021-12-08 00:11:53 +08:00
if (ret->init())
{
ret->setColor(color);
ret->setDepth(depth);
ret->autorelease();
}
else
{
2022-07-15 19:17:01 +08:00
AX_SAFE_DELETE(ret);
}
return ret;
}
/////////////////////////////////////////////////////////////////////////////////////////////
2015-08-21 10:27:19 +08:00
CameraBackgroundSkyBoxBrush::CameraBackgroundSkyBoxBrush()
2021-12-25 10:04:45 +08:00
: _texture(nullptr)
, _actived(true)
, _textureValid(true)
2022-07-15 19:17:01 +08:00
#if AX_ENABLE_CACHE_TEXTURE_DATA
2021-12-25 10:04:45 +08:00
, _backToForegroundListener(nullptr)
#endif
{
2022-07-15 19:17:01 +08:00
#if AX_ENABLE_CACHE_TEXTURE_DATA
2021-12-25 10:04:45 +08:00
_backToForegroundListener =
EventListenerCustom::create(EVENT_RENDERER_RECREATED, [this](EventCustom*) { initBuffer(); });
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, -1);
#endif
}
2015-08-21 10:27:19 +08:00
CameraBackgroundSkyBoxBrush::~CameraBackgroundSkyBoxBrush()
{
2022-07-15 19:17:01 +08:00
AX_SAFE_RELEASE(_texture);
#if AX_ENABLE_CACHE_TEXTURE_DATA
Director::getInstance()->getEventDispatcher()->removeEventListener(_backToForegroundListener);
#endif
}
2021-12-26 23:26:34 +08:00
CameraBackgroundSkyBoxBrush* CameraBackgroundSkyBoxBrush::create(std::string_view positive_x,
std::string_view negative_x,
std::string_view positive_y,
std::string_view negative_y,
std::string_view positive_z,
std::string_view negative_z)
{
CameraBackgroundSkyBoxBrush* ret = nullptr;
2021-12-25 10:04:45 +08:00
auto texture = TextureCube::create(positive_x, negative_x, positive_y, negative_y, positive_z, negative_z);
if (texture != nullptr)
{
Texture2D::TexParams tRepeatParams;
2021-12-25 10:04:45 +08:00
tRepeatParams.magFilter = backend::SamplerFilter::LINEAR;
tRepeatParams.minFilter = backend::SamplerFilter::LINEAR;
2019-03-13 10:54:26 +08:00
tRepeatParams.sAddressMode = backend::SamplerAddressMode::CLAMP_TO_EDGE;
tRepeatParams.tAddressMode = backend::SamplerAddressMode::CLAMP_TO_EDGE;
texture->setTexParameters(tRepeatParams);
2021-12-08 00:11:53 +08:00
ret = new CameraBackgroundSkyBoxBrush;
2021-12-08 00:11:53 +08:00
if (ret->init())
{
ret->setTexture(texture);
ret->autorelease();
}
else
{
2022-07-15 19:17:01 +08:00
AX_SAFE_DELETE(texture);
AX_SAFE_DELETE(ret);
}
}
return ret;
}
2015-08-21 10:27:19 +08:00
CameraBackgroundSkyBoxBrush* CameraBackgroundSkyBoxBrush::create()
{
2021-12-08 00:11:53 +08:00
auto ret = new CameraBackgroundSkyBoxBrush();
2021-12-25 10:04:45 +08:00
2021-12-08 00:11:53 +08:00
if (ret->init())
{
ret->autorelease();
}
else
{
2022-07-15 19:17:01 +08:00
AX_SAFE_DELETE(ret);
}
return ret;
}
2015-08-21 10:27:19 +08:00
void CameraBackgroundSkyBoxBrush::drawBackground(Camera* camera)
{
// `clear screen` should be executed before other commands
_groupCommand.init(-1.0f);
_customCommand.init(0.0f);
if (!_actived)
return;
Mat4 cameraModelMat = camera->getNodeToWorldTransform();
2021-12-25 10:04:45 +08:00
auto& pipelineDescriptor = _customCommand.getPipelineDescriptor();
pipelineDescriptor.blendDescriptor.blendEnabled = false;
Vec4 color(1.f, 1.f, 1.f, 1.f);
cameraModelMat.m[12] = cameraModelMat.m[13] = cameraModelMat.m[14] = 0;
_programState->setUniform(_uniformColorLoc, &color, sizeof(color));
_programState->setUniform(_uniformCameraRotLoc, cameraModelMat.m, sizeof(cameraModelMat.m));
2021-12-25 10:04:45 +08:00
auto* renderer = Director::getInstance()->getRenderer();
renderer->addCommand(&_groupCommand);
renderer->pushGroup(_groupCommand.getRenderQueueID());
renderer->addCommand(&_customCommand);
renderer->popGroup();
2021-12-25 10:04:45 +08:00
2022-07-15 19:17:01 +08:00
AX_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, 8);
}
2015-08-21 10:27:19 +08:00
bool CameraBackgroundSkyBoxBrush::init()
{
2022-07-15 19:17:01 +08:00
_customCommand.setBeforeCallback(AX_CALLBACK_0(CameraBackgroundSkyBoxBrush::onBeforeDraw, this));
_customCommand.setAfterCallback(AX_CALLBACK_0(CameraBackgroundSkyBoxBrush::onAfterDraw, this));
2022-07-15 19:17:01 +08:00
AX_SAFE_RELEASE_NULL(_programState);
2021-12-25 10:04:45 +08:00
auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::SKYBOX_3D);
_programState = new backend::ProgramState(program);
_uniformColorLoc = _programState->getUniformLocation("u_color");
_uniformCameraRotLoc = _programState->getUniformLocation("u_cameraRot");
_uniformEnvLoc = _programState->getUniformLocation("u_Env");
auto& pipelineDescriptor = _customCommand.getPipelineDescriptor();
auto layout = _programState->getVertexLayout();
pipelineDescriptor.programState = _programState;
// disable blend
2021-12-25 10:04:45 +08:00
pipelineDescriptor.blendDescriptor.blendEnabled = false;
2020-01-06 09:35:12 +08:00
auto attrNameLoc = _programState->getAttributeLocation(shaderinfos::attribute::ATTRIBUTE_NAME_POSITION);
2021-12-25 10:04:45 +08:00
layout->setAttribute(shaderinfos::attribute::ATTRIBUTE_NAME_POSITION, attrNameLoc, backend::VertexFormat::FLOAT3, 0,
false);
layout->setLayout(sizeof(Vec3));
initBuffer();
return true;
}
2015-08-21 10:27:19 +08:00
void CameraBackgroundSkyBoxBrush::initBuffer()
{
// init vertex buffer object
2021-12-25 10:04:45 +08:00
Vec3 vexBuf[] = {Vec3(1, -1, 1), Vec3(1, 1, 1), Vec3(-1, 1, 1), Vec3(-1, -1, 1),
Vec3(1, -1, -1), Vec3(1, 1, -1), Vec3(-1, 1, -1), Vec3(-1, -1, -1)};
2021-12-25 10:04:45 +08:00
_customCommand.createVertexBuffer(sizeof(vexBuf[0]), sizeof(vexBuf) / sizeof(vexBuf[0]),
CustomCommand::BufferUsage::STATIC);
_customCommand.updateVertexBuffer(vexBuf, sizeof(vexBuf));
// init index buffer object
2021-12-25 10:04:45 +08:00
uint16_t idxBuf[] = {
2, 1, 0, 3, 2, 0, // font
1, 5, 4, 1, 4, 0, // right
4, 5, 6, 4, 6, 7, // back
7, 6, 2, 7, 2, 3, // left
2, 6, 5, 2, 5, 1, // up
3, 0, 4, 3, 4, 7 // down
};
2021-12-25 10:04:45 +08:00
_customCommand.createIndexBuffer(CustomCommand::IndexFormat::U_SHORT, sizeof(idxBuf) / sizeof(idxBuf[0]),
CustomCommand::BufferUsage::STATIC);
_customCommand.updateIndexBuffer(idxBuf, sizeof(idxBuf));
}
2021-12-25 10:04:45 +08:00
void CameraBackgroundSkyBoxBrush::setTexture(TextureCube* texture)
{
2022-07-15 19:17:01 +08:00
AX_SAFE_RETAIN(texture);
AX_SAFE_RELEASE(_texture);
_texture = texture;
_programState->setTexture(_uniformEnvLoc, 0, _texture->getBackendTexture());
}
2015-11-12 10:57:22 +08:00
bool CameraBackgroundSkyBoxBrush::isActived() const
{
return _actived;
}
void CameraBackgroundSkyBoxBrush::setActived(bool actived)
{
2021-12-25 10:04:45 +08:00
_actived = actived;
}
void CameraBackgroundSkyBoxBrush::setTextureValid(bool valid)
{
2021-12-25 10:04:45 +08:00
_textureValid = valid;
}
bool CameraBackgroundSkyBoxBrush::isValid()
{
return _actived;
}
void CameraBackgroundSkyBoxBrush::onBeforeDraw()
{
2021-12-25 10:04:45 +08:00
auto* renderer = Director::getInstance()->getRenderer();
_stateBlock.depthTest = renderer->getDepthTest();
_stateBlock.depthWrite = renderer->getDepthWrite();
_stateBlock.depthFunc = renderer->getDepthCompareFunction();
_stateBlock.cullMode = renderer->getCullMode();
}
void CameraBackgroundSkyBoxBrush::onAfterDraw()
{
2021-12-25 10:04:45 +08:00
auto* renderer = Director::getInstance()->getRenderer();
renderer->setDepthTest(_stateBlock.depthTest);
renderer->setDepthWrite(_stateBlock.depthWrite);
renderer->setDepthCompareFunction(_stateBlock.depthFunc);
renderer->setCullMode(_stateBlock.cullMode);
}
NS_AX_END