2015-08-20 16:19:57 +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-01-04 12:36:20 +08:00
|
|
|
https://adxeproject.github.io/
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-08-20 16:19:57 +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-08-20 16:19:57 +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-08-20 16:19:57 +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.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-08-20 16:19:57 +08:00
|
|
|
****************************************************************************/
|
2019-03-12 15:41:32 +08:00
|
|
|
#pragma once
|
2015-08-20 16:19:57 +08:00
|
|
|
|
|
|
|
#include "base/ccTypes.h"
|
|
|
|
#include "base/CCRef.h"
|
2019-02-20 16:56:35 +08:00
|
|
|
#include "base/CCEventListenerCustom.h"
|
2015-08-20 16:19:57 +08:00
|
|
|
#include "3d/CCFrustum.h"
|
|
|
|
#include "renderer/CCQuadCommand.h"
|
|
|
|
#include "renderer/CCCustomCommand.h"
|
2019-03-12 15:41:32 +08:00
|
|
|
#include "renderer/CCGroupCommand.h"
|
2019-03-04 09:14:45 +08:00
|
|
|
#include "renderer/backend/Types.h"
|
|
|
|
#include <vector>
|
2015-08-20 16:19:57 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2015-08-21 10:27:19 +08:00
|
|
|
class CameraBackgroundColorBrush;
|
|
|
|
class CameraBackgroundDepthBrush;
|
|
|
|
class CameraBackgroundSkyBoxBrush;
|
2015-08-20 16:19:57 +08:00
|
|
|
class Camera;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
namespace backend
|
|
|
|
{
|
|
|
|
class ProgramState;
|
|
|
|
class Buffer;
|
|
|
|
} // namespace backend
|
2019-03-04 09:14:45 +08:00
|
|
|
|
2015-08-20 16:19:57 +08:00
|
|
|
/**
|
|
|
|
* Defines a brush to clear the background of camera.
|
2021-12-25 10:04:45 +08:00
|
|
|
* There are 4 types of brush. None brush do nothing, Depth brush clear background with given depth, Color brush clear
|
|
|
|
* background with given color and depth, Skybox brush clear the background with a skybox. Camera uses depth brush by
|
|
|
|
* default.
|
2015-08-20 16:19:57 +08:00
|
|
|
*/
|
2015-08-21 10:27:19 +08:00
|
|
|
class CC_DLL CameraBackgroundBrush : public Ref
|
2015-08-20 16:19:57 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
2021-12-25 10:04:45 +08:00
|
|
|
* Brush types. There are 4 types of brush. See CameraBackgroundDepthBrush, CameraBackgroundColorBrush,
|
|
|
|
* CameraBackgroundSkyBoxBrush for more information.
|
2015-08-20 16:19:57 +08:00
|
|
|
*/
|
|
|
|
enum class BrushType
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
NONE, // none brush
|
|
|
|
DEPTH, // depth brush. See CameraBackgroundDepthBrush
|
|
|
|
COLOR, // color brush. See CameraBackgroundColorBrush
|
|
|
|
SKYBOX, // skybox brush. See CameraBackgroundSkyBoxBrush
|
2015-08-20 16:19:57 +08:00
|
|
|
};
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-08-20 16:19:57 +08:00
|
|
|
/**
|
|
|
|
* get brush type
|
|
|
|
* @return BrushType
|
|
|
|
*/
|
|
|
|
virtual BrushType getBrushType() const { return BrushType::NONE; }
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-08-20 16:19:57 +08:00
|
|
|
/**
|
|
|
|
* Creates a none brush, it does nothing when clear the background
|
|
|
|
* @return Created brush.
|
|
|
|
*/
|
2015-08-21 10:27:19 +08:00
|
|
|
static CameraBackgroundBrush* createNoneBrush();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-08-20 16:19:57 +08:00
|
|
|
/**
|
|
|
|
* Creates a depth brush, which clears depth buffer with a given depth.
|
|
|
|
* @param depth Depth used to clear depth buffer
|
|
|
|
* @return Created brush
|
|
|
|
*/
|
2015-08-21 10:27:19 +08:00
|
|
|
static CameraBackgroundDepthBrush* createDepthBrush(float depth = 1.f);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-08-20 16:19:57 +08:00
|
|
|
/**
|
|
|
|
* Creates a color brush
|
|
|
|
* @param color Color of brush
|
|
|
|
* @param depth Depth used to clear depth buffer
|
|
|
|
* @return Created brush
|
|
|
|
*/
|
2015-08-21 10:27:19 +08:00
|
|
|
static CameraBackgroundColorBrush* createColorBrush(const Color4F& color, float depth);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-08-20 16:19:57 +08:00
|
|
|
/** Creates a Skybox brush with 6 textures.
|
|
|
|
@param positive_x texture for the right side of the texture cube face.
|
|
|
|
@param negative_x texture for the up side of the texture cube face.
|
|
|
|
@param positive_y texture for the top side of the texture cube face
|
|
|
|
@param negative_y texture for the bottom side of the texture cube face
|
|
|
|
@param positive_z texture for the forward side of the texture cube face.
|
|
|
|
@param negative_z texture for the rear side of the texture cube face.
|
|
|
|
@return A new brush inited with given parameters.
|
|
|
|
*/
|
2021-12-26 23:26:34 +08:00
|
|
|
static CameraBackgroundSkyBoxBrush* 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-20 16:19:57 +08:00
|
|
|
/**
|
|
|
|
* draw the background
|
|
|
|
*/
|
2016-11-09 10:34:50 +08:00
|
|
|
virtual void drawBackground(Camera* /*camera*/) {}
|
2015-11-12 09:49:49 +08:00
|
|
|
|
|
|
|
virtual bool isValid() { return true; }
|
|
|
|
|
2022-03-18 21:46:07 +08:00
|
|
|
CameraBackgroundBrush();
|
2015-08-21 10:27:19 +08:00
|
|
|
virtual ~CameraBackgroundBrush();
|
2015-08-21 12:18:36 +08:00
|
|
|
|
2015-08-20 16:19:57 +08:00
|
|
|
virtual bool init() { return true; }
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-08-21 12:18:36 +08:00
|
|
|
protected:
|
2019-03-04 09:14:45 +08:00
|
|
|
backend::ProgramState* _programState = nullptr;
|
2015-08-20 16:19:57 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Depth brush clear depth buffer with given depth
|
|
|
|
*/
|
2015-08-21 10:27:19 +08:00
|
|
|
class CC_DLL CameraBackgroundDepthBrush : public CameraBackgroundBrush
|
2015-08-20 16:19:57 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Create a depth brush
|
|
|
|
* @param depth Depth used to clear the depth buffer
|
|
|
|
* @return Created brush
|
|
|
|
*/
|
2015-08-21 10:27:19 +08:00
|
|
|
static CameraBackgroundDepthBrush* create(float depth);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-08-20 16:19:57 +08:00
|
|
|
/**
|
|
|
|
* Get brush type. Should be BrushType::DEPTH
|
|
|
|
* @return brush type
|
|
|
|
*/
|
|
|
|
virtual BrushType getBrushType() const override { return BrushType::DEPTH; }
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-08-20 16:19:57 +08:00
|
|
|
/**
|
|
|
|
* Draw background
|
|
|
|
*/
|
2015-08-21 10:27:19 +08:00
|
|
|
virtual void drawBackground(Camera* camera) override;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-08-20 16:19:57 +08:00
|
|
|
/**
|
|
|
|
* Set depth
|
|
|
|
* @param depth Depth used to clear depth buffer
|
|
|
|
*/
|
|
|
|
void setDepth(float depth) { _depth = depth; }
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-03-18 21:46:07 +08:00
|
|
|
CameraBackgroundDepthBrush();
|
2015-08-21 10:27:19 +08:00
|
|
|
virtual ~CameraBackgroundDepthBrush();
|
2015-08-21 12:18:36 +08:00
|
|
|
|
2015-08-20 16:19:57 +08:00
|
|
|
virtual bool init() override;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-03-04 09:14:45 +08:00
|
|
|
private:
|
|
|
|
void onBeforeDraw();
|
|
|
|
void onAfterDraw();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2018-09-18 09:14:27 +08:00
|
|
|
protected:
|
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
|
|
|
EventListenerCustom* _backToForegroundListener;
|
|
|
|
#endif
|
|
|
|
void initBuffer();
|
|
|
|
|
2015-08-21 12:18:36 +08:00
|
|
|
protected:
|
2015-08-20 16:19:57 +08:00
|
|
|
float _depth;
|
2019-03-04 09:14:45 +08:00
|
|
|
backend::UniformLocation _locDepth;
|
|
|
|
CustomCommand _customCommand;
|
2019-03-12 15:41:32 +08:00
|
|
|
GroupCommand _groupCommand;
|
|
|
|
|
2019-06-05 17:58:33 +08:00
|
|
|
bool _clearColor;
|
2019-03-04 09:14:45 +08:00
|
|
|
std::vector<V3F_C4B_T2F> _vertices;
|
2021-12-25 10:04:45 +08:00
|
|
|
struct
|
|
|
|
{
|
|
|
|
uint32_t stencilWriteMask = 0;
|
|
|
|
bool depthTest = true;
|
2019-03-04 09:14:45 +08:00
|
|
|
backend::CompareFunction compareFunc = backend::CompareFunction::ALWAYS;
|
|
|
|
} _stateBlock;
|
2015-08-20 16:19:57 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Color brush clear buffer with given depth and color
|
|
|
|
*/
|
2015-08-21 10:27:19 +08:00
|
|
|
class CC_DLL CameraBackgroundColorBrush : public CameraBackgroundDepthBrush
|
2015-08-20 16:19:57 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Get brush type. Should be BrushType::COLOR
|
|
|
|
* @return brush type
|
|
|
|
*/
|
|
|
|
virtual BrushType getBrushType() const override { return BrushType::COLOR; }
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-08-20 16:19:57 +08:00
|
|
|
/**
|
|
|
|
* Create a color brush
|
|
|
|
* @param color Color used to clear the color buffer
|
|
|
|
* @param depth Depth used to clear the depth buffer
|
|
|
|
* @return Created brush
|
|
|
|
*/
|
2015-08-21 10:27:19 +08:00
|
|
|
static CameraBackgroundColorBrush* create(const Color4F& color, float depth);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2017-10-20 09:43:45 +08:00
|
|
|
/**
|
|
|
|
* Draw background
|
|
|
|
*/
|
|
|
|
virtual void drawBackground(Camera* camera) override;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-08-20 16:19:57 +08:00
|
|
|
/**
|
|
|
|
* Set clear color
|
|
|
|
* @param color Color used to clear the color buffer
|
|
|
|
*/
|
|
|
|
void setColor(const Color4F& color);
|
2018-09-18 09:14:27 +08:00
|
|
|
|
2022-03-18 21:46:07 +08:00
|
|
|
CameraBackgroundColorBrush();
|
2015-08-21 10:27:19 +08:00
|
|
|
virtual ~CameraBackgroundColorBrush();
|
2015-08-21 12:18:36 +08:00
|
|
|
|
|
|
|
virtual bool init() override;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-08-20 16:19:57 +08:00
|
|
|
protected:
|
|
|
|
Color4F _color;
|
|
|
|
};
|
|
|
|
|
|
|
|
class TextureCube;
|
|
|
|
class GLProgramState;
|
|
|
|
class EventListenerCustom;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Skybox brush clear buffer with a skybox
|
|
|
|
*/
|
2015-08-21 10:27:19 +08:00
|
|
|
class CC_DLL CameraBackgroundSkyBoxBrush : public CameraBackgroundBrush
|
2015-08-20 16:19:57 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Get brush type. Should be BrushType::SKYBOX
|
|
|
|
* @return brush type
|
|
|
|
*/
|
|
|
|
virtual BrushType getBrushType() const override { return BrushType::SKYBOX; }
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-08-20 16:19:57 +08:00
|
|
|
/** Creates a Skybox brush with 6 textures.
|
|
|
|
@param positive_x texture for the right side of the texture cube face.
|
|
|
|
@param negative_x texture for the up side of the texture cube face.
|
|
|
|
@param positive_y texture for the top side of the texture cube face
|
|
|
|
@param negative_y texture for the bottom side of the texture cube face
|
|
|
|
@param positive_z texture for the forward side of the texture cube face.
|
|
|
|
@param negative_z texture for the rear side of the texture cube face.
|
|
|
|
@return A new brush inited with given parameters.
|
|
|
|
*/
|
2021-12-26 23:26:34 +08:00
|
|
|
static 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);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-08-20 16:19:57 +08:00
|
|
|
/** Creates a Skybox brush with 6 textures.
|
|
|
|
*/
|
2015-08-21 10:27:19 +08:00
|
|
|
static CameraBackgroundSkyBoxBrush* create();
|
2015-08-20 16:19:57 +08:00
|
|
|
/**
|
2021-12-25 10:04:45 +08:00
|
|
|
* Set skybox texture
|
2015-08-20 16:19:57 +08:00
|
|
|
* @param texture Skybox texture
|
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
void setTexture(TextureCube* texture);
|
2020-10-07 00:07:53 +08:00
|
|
|
|
|
|
|
TextureCube* getTexture() const { return _texture; }
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-08-20 16:19:57 +08:00
|
|
|
/**
|
|
|
|
* Draw background
|
|
|
|
*/
|
2015-08-21 10:27:19 +08:00
|
|
|
virtual void drawBackground(Camera* camera) override;
|
2015-11-12 09:49:49 +08:00
|
|
|
|
2015-11-12 10:57:22 +08:00
|
|
|
bool isActived() const;
|
2015-11-12 09:49:49 +08:00
|
|
|
void setActived(bool actived);
|
|
|
|
virtual void setTextureValid(bool valid);
|
2021-12-25 10:04:45 +08:00
|
|
|
virtual bool isValid() override;
|
2015-11-12 09:49:49 +08:00
|
|
|
|
2022-03-18 21:46:07 +08:00
|
|
|
CameraBackgroundSkyBoxBrush();
|
2015-08-21 10:27:19 +08:00
|
|
|
virtual ~CameraBackgroundSkyBoxBrush();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-08-20 16:19:57 +08:00
|
|
|
/**
|
|
|
|
* init Skybox.
|
|
|
|
*/
|
|
|
|
virtual bool init() override;
|
2019-03-04 09:14:45 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
void onBeforeDraw();
|
|
|
|
void onAfterDraw();
|
|
|
|
|
2015-08-21 12:18:36 +08:00
|
|
|
protected:
|
2015-08-20 16:19:57 +08:00
|
|
|
void initBuffer();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
TextureCube* _texture;
|
|
|
|
|
2018-09-10 15:04:35 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
2015-08-20 16:19:57 +08:00
|
|
|
EventListenerCustom* _backToForegroundListener;
|
|
|
|
#endif
|
2015-11-12 09:49:49 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
bool _actived;
|
|
|
|
bool _textureValid;
|
2019-03-04 09:14:45 +08:00
|
|
|
|
|
|
|
CustomCommand _customCommand;
|
2019-03-12 15:41:32 +08:00
|
|
|
GroupCommand _groupCommand;
|
2019-03-04 09:14:45 +08:00
|
|
|
|
|
|
|
backend::UniformLocation _uniformColorLoc;
|
|
|
|
backend::UniformLocation _uniformCameraRotLoc;
|
|
|
|
backend::UniformLocation _uniformEnvLoc;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
struct
|
|
|
|
{
|
|
|
|
bool depthTest = true;
|
|
|
|
bool depthWrite = true;
|
2019-03-04 09:14:45 +08:00
|
|
|
backend::CompareFunction depthFunc = backend::CompareFunction::ALWAYS;
|
2021-12-25 10:04:45 +08:00
|
|
|
backend::CullMode cullMode = backend::CullMode::BACK;
|
|
|
|
} _stateBlock;
|
2015-08-20 16:19:57 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
NS_CC_END
|