axmol/cocos/2d/CCCameraBackgroundBrush.cpp

491 lines
17 KiB
C++
Raw Normal View History

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 "2d/CCCameraBackgroundBrush.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 "2d/CCCamera.h"
#include "base/ccMacros.h"
#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"
#if CC_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"
2019-11-23 20:27:39 +08:00
#endif
NS_CC_BEGIN
2021-12-25 10:04:45 +08:00
CameraBackgroundBrush::CameraBackgroundBrush() {}
2019-11-23 20:27:39 +08:00
CameraBackgroundBrush::~CameraBackgroundBrush()
{
CC_SAFE_RELEASE_NULL(_programState);
}
CameraBackgroundBrush* CameraBackgroundBrush::createNoneBrush()
{
2021-12-08 00:11:53 +08:00
auto ret = new CameraBackgroundBrush();
2019-11-23 20:27:39 +08:00
ret->init();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
ret->autorelease();
return ret;
}
CameraBackgroundColorBrush* CameraBackgroundBrush::createColorBrush(const Color4F& color, float depth)
{
return CameraBackgroundColorBrush::create(color, depth);
}
CameraBackgroundDepthBrush* CameraBackgroundBrush::createDepthBrush(float depth)
{
return CameraBackgroundDepthBrush::create(depth);
}
CameraBackgroundSkyBoxBrush* CameraBackgroundBrush::createSkyboxBrush(const std::string& positive_x,
const std::string& negative_x,
const std::string& positive_y,
const std::string& negative_y,
const std::string& positive_z,
const std::string& negative_z)
2019-11-23 20:27:39 +08:00
{
return CameraBackgroundSkyBoxBrush::create(positive_x, negative_x, positive_y, negative_y, positive_z, negative_z);
}
//////////////////////////////////////////////////////////////////////////////////////////
CameraBackgroundDepthBrush::CameraBackgroundDepthBrush()
2021-12-25 10:04:45 +08:00
: _depth(0.f)
, _clearColor(false)
2019-11-23 20:27:39 +08:00
#if CC_ENABLE_CACHE_TEXTURE_DATA
, _backToForegroundListener(nullptr)
#endif
{
#if CC_ENABLE_CACHE_TEXTURE_DATA
2021-12-25 10:04:45 +08:00
_backToForegroundListener =
EventListenerCustom::create(EVENT_RENDERER_RECREATED, [this](EventCustom*) { initBuffer(); });
2019-11-23 20:27:39 +08:00
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, -1);
#endif
}
CameraBackgroundDepthBrush::~CameraBackgroundDepthBrush()
{
#if CC_ENABLE_CACHE_TEXTURE_DATA
Director::getInstance()->getEventDispatcher()->removeEventListener(_backToForegroundListener);
#endif
}
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())
2019-11-23 20:27:39 +08:00
{
ret->_depth = depth;
ret->autorelease();
}
else
{
CC_SAFE_DELETE(ret);
}
return ret;
}
bool CameraBackgroundDepthBrush::init()
{
CC_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();
2019-11-23 20:27:39 +08:00
pipelineDescriptor.programState = _programState;
2021-12-25 10:04:45 +08:00
auto layout = _programState->getVertexLayout();
2019-11-23 20:27:39 +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-11-23 20:27:39 +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-11-23 20:27:39 +08:00
}
iter = attributeInfo.find("a_color");
2021-12-25 10:04:45 +08:00
if (iter != attributeInfo.end())
2019-11-23 20:27:39 +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-11-23 20:27:39 +08:00
}
iter = attributeInfo.find("a_texCoord");
2021-12-25 10:04:45 +08:00
if (iter != attributeInfo.end())
2019-11-23 20:27:39 +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-11-23 20:27:39 +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);
_customCommand.setBeforeCallback(CC_CALLBACK_0(CameraBackgroundDepthBrush::onBeforeDraw, this));
_customCommand.setAfterCallback(CC_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};
2019-11-23 20:27:39 +08:00
_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);
2019-11-23 20:27:39 +08:00
_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();
2019-11-23 20:27:39 +08:00
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;
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
// draw
2019-11-23 20:27:39 +08:00
_programState->setUniform(_locDepth, &_depth, sizeof(_depth));
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +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();
2019-11-23 20:27:39 +08:00
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();
2019-11-23 20:27:39 +08:00
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) {}
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
CameraBackgroundColorBrush::~CameraBackgroundColorBrush() {}
2019-11-23 20:27:39 +08:00
bool CameraBackgroundColorBrush::init()
{
CameraBackgroundDepthBrush::init();
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};
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
auto& blend = _customCommand.getPipelineDescriptor().blendDescriptor;
blend.sourceRGBBlendFactor = blend.sourceAlphaBlendFactor = op.src;
2019-11-23 20:27:39 +08:00
blend.destinationRGBBlendFactor = blend.destinationAlphaBlendFactor = op.dst;
2021-12-25 10:04:45 +08:00
blend.blendEnabled = true;
2019-11-23 20:27:39 +08:00
CameraBackgroundDepthBrush::drawBackground(camera);
}
void CameraBackgroundColorBrush::setColor(const Color4F& color)
{
2021-12-25 10:04:45 +08:00
for (auto& vert : _vertices)
2019-11-23 20:27:39 +08:00
{
vert.colors = Color4B(color);
}
_customCommand.updateVertexBuffer(_vertices.data(), sizeof(_vertices[0]) * _vertices.size());
}
CameraBackgroundColorBrush* CameraBackgroundColorBrush::create(const Color4F& color, float depth)
{
2021-12-08 00:11:53 +08:00
auto ret = new CameraBackgroundColorBrush();
2019-11-23 20:27:39 +08:00
2021-12-08 00:11:53 +08:00
if (ret->init())
2019-11-23 20:27:39 +08:00
{
ret->setColor(color);
ret->setDepth(depth);
ret->autorelease();
}
else
{
CC_SAFE_DELETE(ret);
}
return ret;
}
/////////////////////////////////////////////////////////////////////////////////////////////
CameraBackgroundSkyBoxBrush::CameraBackgroundSkyBoxBrush()
2021-12-25 10:04:45 +08:00
: _texture(nullptr)
, _actived(true)
, _textureValid(true)
2019-11-23 20:27:39 +08:00
#if CC_ENABLE_CACHE_TEXTURE_DATA
2021-12-25 10:04:45 +08:00
, _backToForegroundListener(nullptr)
2019-11-23 20:27:39 +08:00
#endif
{
#if CC_ENABLE_CACHE_TEXTURE_DATA
2021-12-25 10:04:45 +08:00
_backToForegroundListener =
EventListenerCustom::create(EVENT_RENDERER_RECREATED, [this](EventCustom*) { initBuffer(); });
2019-11-23 20:27:39 +08:00
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, -1);
#endif
}
CameraBackgroundSkyBoxBrush::~CameraBackgroundSkyBoxBrush()
{
CC_SAFE_RELEASE(_texture);
#if CC_ENABLE_CACHE_TEXTURE_DATA
Director::getInstance()->getEventDispatcher()->removeEventListener(_backToForegroundListener);
#endif
}
CameraBackgroundSkyBoxBrush* CameraBackgroundSkyBoxBrush::create(const std::string& positive_x,
const std::string& negative_x,
const std::string& positive_y,
const std::string& negative_y,
const std::string& positive_z,
const std::string& negative_z)
2019-11-23 20:27:39 +08:00
{
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);
2019-11-23 20:27:39 +08:00
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-11-23 20:27:39 +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;
2019-11-23 20:27:39 +08:00
2021-12-08 00:11:53 +08:00
if (ret->init())
2019-11-23 20:27:39 +08:00
{
ret->setTexture(texture);
ret->autorelease();
}
else
{
CC_SAFE_DELETE(texture);
CC_SAFE_DELETE(ret);
}
}
return ret;
}
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())
2019-11-23 20:27:39 +08:00
{
ret->autorelease();
}
else
{
CC_SAFE_DELETE(ret);
}
return ret;
}
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();
2019-11-23 20:27:39 +08:00
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();
2019-11-23 20:27:39 +08:00
renderer->addCommand(&_groupCommand);
renderer->pushGroup(_groupCommand.getRenderQueueID());
renderer->addCommand(&_customCommand);
renderer->popGroup();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, 8);
}
bool CameraBackgroundSkyBoxBrush::init()
{
_customCommand.setBeforeCallback(CC_CALLBACK_0(CameraBackgroundSkyBoxBrush::onBeforeDraw, this));
_customCommand.setAfterCallback(CC_CALLBACK_0(CameraBackgroundSkyBoxBrush::onAfterDraw, this));
CC_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;
2019-11-23 20:27:39 +08:00
// disable blend
2021-12-25 10:04:45 +08:00
pipelineDescriptor.blendDescriptor.blendEnabled = false;
2019-11-23 20:27:39 +08:00
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);
2019-11-23 20:27:39 +08:00
layout->setLayout(sizeof(Vec3));
initBuffer();
return true;
}
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)};
2019-11-23 20:27:39 +08:00
2021-12-25 10:04:45 +08:00
_customCommand.createVertexBuffer(sizeof(vexBuf[0]), sizeof(vexBuf) / sizeof(vexBuf[0]),
CustomCommand::BufferUsage::STATIC);
2019-11-23 20:27:39 +08:00
_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
2019-11-23 20:27:39 +08:00
};
2021-12-25 10:04:45 +08:00
_customCommand.createIndexBuffer(CustomCommand::IndexFormat::U_SHORT, sizeof(idxBuf) / sizeof(idxBuf[0]),
CustomCommand::BufferUsage::STATIC);
2019-11-23 20:27:39 +08:00
_customCommand.updateIndexBuffer(idxBuf, sizeof(idxBuf));
}
2021-12-25 10:04:45 +08:00
void CameraBackgroundSkyBoxBrush::setTexture(TextureCube* texture)
2019-11-23 20:27:39 +08:00
{
CC_SAFE_RETAIN(texture);
CC_SAFE_RELEASE(_texture);
_texture = texture;
_programState->setTexture(_uniformEnvLoc, 0, _texture->getBackendTexture());
}
bool CameraBackgroundSkyBoxBrush::isActived() const
{
return _actived;
}
void CameraBackgroundSkyBoxBrush::setActived(bool actived)
{
2021-12-25 10:04:45 +08:00
_actived = actived;
2019-11-23 20:27:39 +08:00
}
void CameraBackgroundSkyBoxBrush::setTextureValid(bool valid)
{
2021-12-25 10:04:45 +08:00
_textureValid = valid;
2019-11-23 20:27:39 +08:00
}
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();
2019-11-23 20:27:39 +08:00
}
void CameraBackgroundSkyBoxBrush::onAfterDraw()
{
2021-12-25 10:04:45 +08:00
auto* renderer = Director::getInstance()->getRenderer();
2019-11-23 20:27:39 +08:00
renderer->setDepthTest(_stateBlock.depthTest);
renderer->setDepthWrite(_stateBlock.depthWrite);
renderer->setDepthCompareFunction(_stateBlock.depthFunc);
renderer->setCullMode(_stateBlock.cullMode);
}
NS_CC_END