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.
|
2015-08-20 16:19:57 +08:00
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
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"
|
2015-08-20 16:19:57 +08:00
|
|
|
#include "2d/CCCamera.h"
|
|
|
|
#include "base/ccMacros.h"
|
2018-09-11 14:39:30 +08:00
|
|
|
#include "base/ccUtils.h"
|
2015-08-20 16:19:57 +08:00
|
|
|
#include "base/CCConfiguration.h"
|
|
|
|
#include "base/CCDirector.h"
|
|
|
|
#include "renderer/CCGLProgram.h"
|
|
|
|
#include "renderer/CCGLProgramCache.h"
|
|
|
|
#include "renderer/CCGLProgramState.h"
|
|
|
|
#include "renderer/CCRenderer.h"
|
|
|
|
#include "renderer/CCRenderState.h"
|
2015-08-24 10:49:10 +08:00
|
|
|
#include "renderer/CCTextureCube.h"
|
2015-08-20 16:19:57 +08:00
|
|
|
|
2018-09-10 15:04:35 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
2015-08-20 16:19:57 +08:00
|
|
|
#include "base/CCEventCustom.h"
|
|
|
|
#include "base/CCEventListenerCustom.h"
|
|
|
|
#include "base/CCEventType.h"
|
|
|
|
#include "base/CCEventDispatcher.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2015-08-21 10:27:19 +08:00
|
|
|
CameraBackgroundBrush::CameraBackgroundBrush()
|
2015-08-20 16:19:57 +08:00
|
|
|
: _glProgramState(nullptr)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-08-21 10:27:19 +08:00
|
|
|
CameraBackgroundBrush::~CameraBackgroundBrush()
|
2015-08-20 16:19:57 +08:00
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE(_glProgramState);
|
|
|
|
}
|
|
|
|
|
2015-08-21 10:27:19 +08:00
|
|
|
CameraBackgroundBrush* CameraBackgroundBrush::createNoneBrush()
|
2015-08-20 16:19:57 +08:00
|
|
|
{
|
2015-08-21 10:27:19 +08:00
|
|
|
auto ret = new (std::nothrow) CameraBackgroundBrush();
|
2015-08-20 16:19:57 +08:00
|
|
|
ret->init();
|
|
|
|
|
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-08-21 10:27:19 +08:00
|
|
|
CameraBackgroundColorBrush* CameraBackgroundBrush::createColorBrush(const Color4F& color, float depth)
|
2015-08-20 16:19:57 +08:00
|
|
|
{
|
2015-08-21 10:27:19 +08:00
|
|
|
return CameraBackgroundColorBrush::create(color, depth);
|
2015-08-20 16:19:57 +08:00
|
|
|
}
|
|
|
|
|
2015-08-21 10:27:19 +08:00
|
|
|
CameraBackgroundDepthBrush* CameraBackgroundBrush::createDepthBrush(float depth)
|
2015-08-20 16:19:57 +08:00
|
|
|
{
|
2015-08-21 10:27:19 +08:00
|
|
|
return CameraBackgroundDepthBrush::create(depth);
|
2015-08-20 16:19:57 +08:00
|
|
|
}
|
|
|
|
|
2015-08-21 10:27:19 +08:00
|
|
|
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)
|
2015-08-20 16:19:57 +08:00
|
|
|
{
|
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-20 16:19:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-08-21 10:27:19 +08:00
|
|
|
CameraBackgroundDepthBrush::CameraBackgroundDepthBrush()
|
2015-08-20 16:19:57 +08:00
|
|
|
: _depth(0.f)
|
|
|
|
, _clearColor(GL_FALSE)
|
2017-07-17 15:16:42 +08:00
|
|
|
, _vao(0)
|
|
|
|
, _vertexBuffer(0)
|
|
|
|
, _indexBuffer(0)
|
2018-09-18 09:14:27 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
|
|
|
, _backToForegroundListener(nullptr)
|
|
|
|
#endif
|
2015-08-20 16:19:57 +08:00
|
|
|
{
|
2018-09-18 09:14:27 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
|
|
|
_backToForegroundListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, [this](EventCustom*)
|
|
|
|
{
|
|
|
|
_vao = 0;
|
|
|
|
_vertexBuffer = 0;
|
|
|
|
_indexBuffer = 0;
|
|
|
|
initBuffer();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, -1);
|
|
|
|
#endif
|
2015-08-20 16:19:57 +08:00
|
|
|
}
|
2015-08-21 10:27:19 +08:00
|
|
|
CameraBackgroundDepthBrush::~CameraBackgroundDepthBrush()
|
2015-08-20 16:19:57 +08:00
|
|
|
{
|
2017-07-17 15:16:42 +08:00
|
|
|
glDeleteBuffers(1, &_vertexBuffer);
|
|
|
|
glDeleteBuffers(1, &_indexBuffer);
|
2015-08-20 16:19:57 +08:00
|
|
|
|
2017-07-17 15:16:42 +08:00
|
|
|
_vertexBuffer = 0;
|
|
|
|
_indexBuffer = 0;
|
|
|
|
|
|
|
|
if (Configuration::getInstance()->supportsShareableVAO())
|
|
|
|
{
|
|
|
|
glDeleteVertexArrays(1, &_vao);
|
2018-09-11 14:39:30 +08:00
|
|
|
glBindVertexArray(0);
|
2017-07-17 15:16:42 +08:00
|
|
|
_vao = 0;
|
|
|
|
}
|
2018-09-18 09:14:27 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
|
|
|
Director::getInstance()->getEventDispatcher()->removeEventListener(_backToForegroundListener);
|
|
|
|
#endif
|
2015-08-20 16:19:57 +08:00
|
|
|
}
|
|
|
|
|
2015-08-21 10:27:19 +08:00
|
|
|
CameraBackgroundDepthBrush* CameraBackgroundDepthBrush::create(float depth)
|
2015-08-20 16:19:57 +08:00
|
|
|
{
|
2015-08-21 10:27:19 +08:00
|
|
|
auto ret = new (std::nothrow) CameraBackgroundDepthBrush();
|
2016-11-14 10:05:26 +08:00
|
|
|
|
|
|
|
if (nullptr != ret && ret->init())
|
|
|
|
{
|
|
|
|
ret->_depth = depth;
|
|
|
|
ret->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(ret);
|
|
|
|
}
|
|
|
|
|
2015-08-20 16:19:57 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-08-21 10:27:19 +08:00
|
|
|
bool CameraBackgroundDepthBrush::init()
|
2015-08-20 16:19:57 +08:00
|
|
|
{
|
|
|
|
auto shader = GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_CAMERA_CLEAR);
|
|
|
|
_glProgramState = GLProgramState::getOrCreateWithGLProgram(shader);
|
|
|
|
_glProgramState->retain();
|
|
|
|
|
|
|
|
_quad.bl.vertices = Vec3(-1,-1,0);
|
|
|
|
_quad.br.vertices = Vec3(1,-1,0);
|
|
|
|
_quad.tl.vertices = Vec3(-1,1,0);
|
|
|
|
_quad.tr.vertices = Vec3(1,1,0);
|
|
|
|
|
|
|
|
_quad.bl.colors = _quad.br.colors = _quad.tl.colors = _quad.tr.colors = Color4B(0,0,0,1);
|
|
|
|
|
|
|
|
_quad.bl.texCoords = Tex2F(0,0);
|
|
|
|
_quad.br.texCoords = Tex2F(1,0);
|
|
|
|
_quad.tl.texCoords = Tex2F(0,1);
|
|
|
|
_quad.tr.texCoords = Tex2F(1,1);
|
2017-07-17 15:16:42 +08:00
|
|
|
|
2018-09-18 09:14:27 +08:00
|
|
|
initBuffer();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CameraBackgroundDepthBrush::initBuffer()
|
|
|
|
{
|
2017-07-17 15:16:42 +08:00
|
|
|
auto supportVAO = Configuration::getInstance()->supportsShareableVAO();
|
|
|
|
if (supportVAO)
|
|
|
|
{
|
|
|
|
glGenVertexArrays(1, &_vao);
|
2018-09-11 14:39:30 +08:00
|
|
|
glBindVertexArray(_vao);
|
2017-07-17 15:16:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
glGenBuffers(1, &_vertexBuffer);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(_quad), &_quad, GL_STATIC_DRAW);
|
|
|
|
|
|
|
|
GLshort indices[6] = {0, 1, 2, 3, 2, 1};
|
|
|
|
glGenBuffers(1, &_indexBuffer);
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
|
|
|
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
|
|
|
|
|
|
|
|
if (supportVAO)
|
|
|
|
{
|
|
|
|
// vertices
|
2017-09-18 15:47:50 +08:00
|
|
|
glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_POSITION);
|
2017-07-28 09:09:41 +08:00
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, sizeof(V3F_C4B_T2F), (GLvoid*)offsetof( V3F_C4B_T2F, vertices));
|
2017-09-18 15:47:50 +08:00
|
|
|
|
2017-07-17 15:16:42 +08:00
|
|
|
// colors
|
2017-09-18 15:47:50 +08:00
|
|
|
glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_COLOR);
|
2017-07-28 09:09:41 +08:00
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V3F_C4B_T2F), (GLvoid*)offsetof( V3F_C4B_T2F, colors));
|
2017-09-18 15:47:50 +08:00
|
|
|
|
2017-07-17 15:16:42 +08:00
|
|
|
// tex coords
|
2017-09-18 15:47:50 +08:00
|
|
|
glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_TEX_COORD);
|
2017-07-28 09:09:41 +08:00
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, sizeof(V3F_C4B_T2F), (GLvoid*)offsetof( V3F_C4B_T2F, texCoords));
|
2017-07-17 15:16:42 +08:00
|
|
|
}
|
2017-09-18 15:47:50 +08:00
|
|
|
|
2017-07-17 15:16:42 +08:00
|
|
|
if (supportVAO)
|
2018-09-11 14:39:30 +08:00
|
|
|
glBindVertexArray(0);
|
2017-09-18 15:47:50 +08:00
|
|
|
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
2015-08-20 16:19:57 +08:00
|
|
|
}
|
|
|
|
|
2016-11-09 10:34:50 +08:00
|
|
|
void CameraBackgroundDepthBrush::drawBackground(Camera* /*camera*/)
|
2015-08-20 16:19:57 +08:00
|
|
|
{
|
|
|
|
GLboolean oldDepthTest;
|
|
|
|
GLint oldDepthFunc;
|
|
|
|
GLboolean oldDepthMask;
|
|
|
|
{
|
|
|
|
glColorMask(_clearColor, _clearColor, _clearColor, _clearColor);
|
|
|
|
glStencilMask(0);
|
|
|
|
|
|
|
|
oldDepthTest = glIsEnabled(GL_DEPTH_TEST);
|
|
|
|
glGetIntegerv(GL_DEPTH_FUNC, &oldDepthFunc);
|
|
|
|
glGetBooleanv(GL_DEPTH_WRITEMASK, &oldDepthMask);
|
|
|
|
|
|
|
|
glDepthMask(GL_TRUE);
|
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
glDepthFunc(GL_ALWAYS);
|
|
|
|
}
|
|
|
|
|
|
|
|
//draw
|
|
|
|
|
|
|
|
_glProgramState->setUniformFloat("depth", _depth);
|
|
|
|
_glProgramState->apply(Mat4::IDENTITY);
|
|
|
|
|
2017-07-17 15:16:42 +08:00
|
|
|
auto supportVAO = Configuration::getInstance()->supportsShareableVAO();
|
|
|
|
if (supportVAO)
|
2018-09-11 14:39:30 +08:00
|
|
|
glBindVertexArray(_vao);
|
2017-07-17 15:16:42 +08:00
|
|
|
else
|
2015-08-20 16:19:57 +08:00
|
|
|
{
|
2017-07-17 15:16:42 +08:00
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
|
2018-09-11 14:39:30 +08:00
|
|
|
glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_POSITION);
|
|
|
|
glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_COLOR);
|
|
|
|
glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_TEX_COORD);
|
2017-07-17 15:16:42 +08:00
|
|
|
|
2015-08-20 16:19:57 +08:00
|
|
|
// vertices
|
2017-07-28 09:09:41 +08:00
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, sizeof(V3F_C4B_T2F), (GLvoid*)offsetof(V3F_C4B_T2F, vertices));
|
2015-08-20 16:19:57 +08:00
|
|
|
|
|
|
|
// colors
|
2017-07-28 09:09:41 +08:00
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V3F_C4B_T2F), (GLvoid*)offsetof(V3F_C4B_T2F, colors));
|
2015-08-20 16:19:57 +08:00
|
|
|
|
|
|
|
// tex coords
|
2017-07-28 09:09:41 +08:00
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, sizeof(V3F_C4B_T2F), (GLvoid*)offsetof(V3F_C4B_T2F, texCoords));
|
2015-08-20 16:19:57 +08:00
|
|
|
|
2017-07-17 15:16:42 +08:00
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, nullptr);
|
|
|
|
|
|
|
|
if (supportVAO)
|
2018-09-11 14:39:30 +08:00
|
|
|
glBindVertexArray(0);
|
2017-07-17 15:16:42 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
2015-08-20 16:19:57 +08:00
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
if(GL_FALSE == oldDepthTest)
|
|
|
|
{
|
|
|
|
glDisable(GL_DEPTH_TEST);
|
|
|
|
}
|
|
|
|
glDepthFunc(oldDepthFunc);
|
|
|
|
|
|
|
|
if(GL_FALSE == oldDepthMask)
|
|
|
|
{
|
|
|
|
glDepthMask(GL_FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* IMPORTANT: We only need to update the states that are not restored.
|
|
|
|
Since we don't know what was the previous value of the mask, we update the RenderState
|
|
|
|
after setting it.
|
|
|
|
The other values don't need to be updated since they were restored to their original values
|
|
|
|
*/
|
|
|
|
glStencilMask(0xFFFFF);
|
|
|
|
// RenderState::StateBlock::_defaultState->setStencilWrite(0xFFFFF);
|
|
|
|
|
|
|
|
/* BUG: RenderState does not support glColorMask yet. */
|
|
|
|
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-08-21 10:27:19 +08:00
|
|
|
CameraBackgroundColorBrush::CameraBackgroundColorBrush()
|
2015-08-20 16:19:57 +08:00
|
|
|
: _color(0.f, 0.f, 0.f, 0.f)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-08-21 10:27:19 +08:00
|
|
|
CameraBackgroundColorBrush::~CameraBackgroundColorBrush()
|
2015-08-20 16:19:57 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-08-21 12:18:36 +08:00
|
|
|
bool CameraBackgroundColorBrush::init()
|
|
|
|
{
|
2015-08-24 10:23:59 +08:00
|
|
|
CameraBackgroundDepthBrush::init();
|
2015-08-21 12:18:36 +08:00
|
|
|
this->_clearColor = GL_TRUE;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-10-20 09:43:45 +08:00
|
|
|
void CameraBackgroundColorBrush::drawBackground(Camera* camera)
|
|
|
|
{
|
2018-09-11 14:39:30 +08:00
|
|
|
utils::setBlending(BlendFunc::ALPHA_NON_PREMULTIPLIED.src, BlendFunc::ALPHA_NON_PREMULTIPLIED.dst);
|
2017-10-20 09:43:45 +08:00
|
|
|
|
|
|
|
CameraBackgroundDepthBrush::drawBackground(camera);
|
|
|
|
}
|
|
|
|
|
2015-08-21 10:27:19 +08:00
|
|
|
void CameraBackgroundColorBrush::setColor(const Color4F& color)
|
2015-08-20 16:19:57 +08:00
|
|
|
{
|
|
|
|
_quad.bl.colors = _quad.br.colors = _quad.tl.colors = _quad.tr.colors = Color4B(color);
|
2017-09-19 09:41:10 +08:00
|
|
|
|
|
|
|
if (_vertexBuffer)
|
|
|
|
{
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(_quad), &_quad, GL_STATIC_DRAW);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
|
|
}
|
2015-08-20 16:19:57 +08:00
|
|
|
}
|
|
|
|
|
2015-08-21 10:27:19 +08:00
|
|
|
CameraBackgroundColorBrush* CameraBackgroundColorBrush::create(const Color4F& color, float depth)
|
2015-08-20 16:19:57 +08:00
|
|
|
{
|
2015-08-21 10:27:19 +08:00
|
|
|
auto ret = new (std::nothrow) CameraBackgroundColorBrush();
|
2016-11-14 10:05:26 +08:00
|
|
|
|
|
|
|
if (nullptr != ret && ret->init())
|
|
|
|
{
|
|
|
|
ret->setColor(color);
|
|
|
|
ret->setDepth(depth);
|
|
|
|
ret->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(ret);
|
|
|
|
}
|
|
|
|
|
2015-08-20 16:19:57 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////
|
2015-08-21 10:27:19 +08:00
|
|
|
CameraBackgroundSkyBoxBrush::CameraBackgroundSkyBoxBrush()
|
2015-08-20 16:19:57 +08:00
|
|
|
: _vao(0)
|
|
|
|
, _vertexBuffer(0)
|
|
|
|
, _indexBuffer(0)
|
|
|
|
, _texture(nullptr)
|
2015-12-07 18:03:22 +08:00
|
|
|
, _actived(true)
|
|
|
|
, _textureValid(true)
|
2018-09-10 15:04:35 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
|
|
|
, _backToForegroundListener(nullptr)
|
|
|
|
#endif
|
2015-08-20 16:19:57 +08:00
|
|
|
{
|
2018-09-10 15:04:35 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
2015-08-20 16:19:57 +08:00
|
|
|
_backToForegroundListener = EventListenerCustom::create(EVENT_RENDERER_RECREATED,
|
|
|
|
[this](EventCustom*)
|
|
|
|
{
|
2018-09-10 15:04:35 +08:00
|
|
|
_vao = 0;
|
|
|
|
_vertexBuffer = 0;
|
|
|
|
_indexBuffer = 0;
|
2015-08-20 16:19:57 +08:00
|
|
|
initBuffer();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, -1);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-08-21 10:27:19 +08:00
|
|
|
CameraBackgroundSkyBoxBrush::~CameraBackgroundSkyBoxBrush()
|
2015-08-20 16:19:57 +08:00
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE(_texture);
|
|
|
|
|
|
|
|
glDeleteBuffers(1, &_vertexBuffer);
|
|
|
|
glDeleteBuffers(1, &_indexBuffer);
|
|
|
|
|
|
|
|
_vertexBuffer = 0;
|
|
|
|
_indexBuffer = 0;
|
|
|
|
|
|
|
|
if (Configuration::getInstance()->supportsShareableVAO())
|
|
|
|
{
|
|
|
|
glDeleteVertexArrays(1, &_vao);
|
2018-09-11 14:39:30 +08:00
|
|
|
glBindVertexArray(0);
|
2015-08-20 16:19:57 +08:00
|
|
|
_vao = 0;
|
|
|
|
}
|
2018-09-10 15:04:35 +08:00
|
|
|
#if CC_ENABLE_CACHE_TEXTURE_DATA
|
|
|
|
Director::getInstance()->getEventDispatcher()->removeEventListener(_backToForegroundListener);
|
|
|
|
#endif
|
2015-08-20 16:19:57 +08:00
|
|
|
}
|
|
|
|
|
2016-11-14 10:05:26 +08:00
|
|
|
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
|
|
|
|
)
|
2015-08-20 16:19:57 +08:00
|
|
|
{
|
2016-11-14 10:05:26 +08:00
|
|
|
CameraBackgroundSkyBoxBrush* ret = nullptr;
|
|
|
|
|
|
|
|
auto texture = TextureCube::create(positive_x,
|
|
|
|
negative_x,
|
|
|
|
positive_y,
|
|
|
|
negative_y,
|
|
|
|
positive_z,
|
|
|
|
negative_z);
|
|
|
|
|
|
|
|
if (texture != nullptr)
|
|
|
|
{
|
|
|
|
|
|
|
|
Texture2D::TexParams tRepeatParams;
|
|
|
|
tRepeatParams.magFilter = GL_LINEAR;
|
|
|
|
tRepeatParams.minFilter = GL_LINEAR;
|
|
|
|
tRepeatParams.wrapS = GL_CLAMP_TO_EDGE;
|
|
|
|
tRepeatParams.wrapT = GL_CLAMP_TO_EDGE;
|
|
|
|
texture->setTexParameters(tRepeatParams);
|
|
|
|
|
|
|
|
ret = new (std::nothrow) CameraBackgroundSkyBoxBrush;
|
|
|
|
|
|
|
|
if (nullptr != ret && ret->init())
|
|
|
|
{
|
|
|
|
ret->setTexture(texture);
|
|
|
|
ret->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(texture);
|
|
|
|
CC_SAFE_DELETE(ret);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-20 16:19:57 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-08-21 10:27:19 +08:00
|
|
|
CameraBackgroundSkyBoxBrush* CameraBackgroundSkyBoxBrush::create()
|
2015-08-20 16:19:57 +08:00
|
|
|
{
|
2016-11-14 10:05:26 +08:00
|
|
|
auto ret = new (std::nothrow) CameraBackgroundSkyBoxBrush();
|
2015-08-20 16:19:57 +08:00
|
|
|
|
2016-11-14 10:05:26 +08:00
|
|
|
if (nullptr != ret && ret->init())
|
|
|
|
{
|
|
|
|
ret->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(ret);
|
|
|
|
}
|
|
|
|
|
2015-08-20 16:19:57 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-08-21 10:27:19 +08:00
|
|
|
void CameraBackgroundSkyBoxBrush::drawBackground(Camera* camera)
|
2015-08-20 16:19:57 +08:00
|
|
|
{
|
2015-11-12 09:49:49 +08:00
|
|
|
if (!_actived)
|
|
|
|
return;
|
|
|
|
|
2015-08-20 16:19:57 +08:00
|
|
|
Mat4 cameraModelMat = camera->getNodeToWorldTransform();
|
|
|
|
|
|
|
|
Vec4 color(1.f, 1.f, 1.f, 1.f);
|
|
|
|
_glProgramState->setUniformVec4("u_color", color);
|
|
|
|
cameraModelMat.m[12] = cameraModelMat.m[13] = cameraModelMat.m[14] = 0;
|
|
|
|
_glProgramState->setUniformMat4("u_cameraRot", cameraModelMat);
|
|
|
|
|
2015-09-17 18:14:17 +08:00
|
|
|
_glProgramState->apply(Mat4::IDENTITY);
|
|
|
|
|
2019-02-15 14:30:10 +08:00
|
|
|
//TODO arnold
|
|
|
|
//glEnable(GL_DEPTH_TEST);
|
|
|
|
//RenderState::StateBlock::_globalState->setDepthTest(true);
|
|
|
|
//
|
|
|
|
//glDepthMask(GL_TRUE);
|
|
|
|
//RenderState::StateBlock::_globalState->setDepthWrite(true);
|
|
|
|
//
|
|
|
|
//glDepthFunc(GL_ALWAYS);
|
|
|
|
//RenderState::StateBlock::_globalState->setDepthFunction(DepthFunction::ALWAYS);
|
|
|
|
//
|
|
|
|
//glEnable(GL_CULL_FACE);
|
|
|
|
//RenderState::StateBlock::_globalState->setCullFace(true);
|
|
|
|
//
|
|
|
|
//glCullFace(GL_BACK);
|
|
|
|
//RenderState::StateBlock::_globalState->setCullFaceSide(CullMode::BACK);
|
|
|
|
//
|
|
|
|
//glDisable(GL_BLEND);
|
|
|
|
//RenderState::StateBlock::_globalState->setBlend(false);
|
2015-08-25 14:32:25 +08:00
|
|
|
|
2015-08-20 16:19:57 +08:00
|
|
|
if (Configuration::getInstance()->supportsShareableVAO())
|
|
|
|
{
|
2018-09-11 14:39:30 +08:00
|
|
|
glBindVertexArray(_vao);
|
2015-08-20 16:19:57 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-09-11 14:39:30 +08:00
|
|
|
glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_POSITION);
|
2015-08-20 16:19:57 +08:00
|
|
|
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
|
|
|
|
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, sizeof(Vec3), nullptr);
|
|
|
|
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
glDrawElements(GL_TRIANGLES, (GLsizei)36, GL_UNSIGNED_BYTE, nullptr);
|
|
|
|
|
|
|
|
if (Configuration::getInstance()->supportsShareableVAO())
|
|
|
|
{
|
2018-09-11 14:39:30 +08:00
|
|
|
glBindVertexArray(0);
|
2015-08-20 16:19:57 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, 8);
|
|
|
|
|
|
|
|
CHECK_GL_ERROR_DEBUG();
|
|
|
|
}
|
|
|
|
|
2015-08-21 10:27:19 +08:00
|
|
|
bool CameraBackgroundSkyBoxBrush::init()
|
2015-08-20 16:19:57 +08:00
|
|
|
{
|
|
|
|
auto shader = GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_3D_SKYBOX);
|
|
|
|
_glProgramState = GLProgramState::create(shader);
|
|
|
|
_glProgramState->setVertexAttribPointer(GLProgram::ATTRIBUTE_NAME_POSITION, 3, GL_FLOAT, GL_FALSE, sizeof(Vec3), nullptr);
|
|
|
|
_glProgramState->retain();
|
|
|
|
|
|
|
|
initBuffer();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-08-21 10:27:19 +08:00
|
|
|
void CameraBackgroundSkyBoxBrush::initBuffer()
|
2015-08-20 16:19:57 +08:00
|
|
|
{
|
|
|
|
if (_vertexBuffer)
|
|
|
|
glDeleteBuffers(1, &_vertexBuffer);
|
|
|
|
if (_indexBuffer)
|
|
|
|
glDeleteBuffers(1, &_indexBuffer);
|
|
|
|
|
|
|
|
if (Configuration::getInstance()->supportsShareableVAO() && _vao)
|
|
|
|
{
|
|
|
|
glDeleteVertexArrays(1, &_vao);
|
2018-09-11 14:39:30 +08:00
|
|
|
glBindVertexArray(0);
|
2015-08-20 16:19:57 +08:00
|
|
|
_vao = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Configuration::getInstance()->supportsShareableVAO())
|
|
|
|
{
|
|
|
|
glGenVertexArrays(1, &_vao);
|
2018-09-11 14:39:30 +08:00
|
|
|
glBindVertexArray(_vao);
|
2015-08-20 16:19:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// init vertex buffer object
|
|
|
|
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)
|
|
|
|
};
|
|
|
|
|
|
|
|
glGenBuffers(1, &_vertexBuffer);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(vexBuf), vexBuf, GL_STATIC_DRAW);
|
|
|
|
|
|
|
|
// init index buffer object
|
|
|
|
const unsigned char 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
|
|
|
|
};
|
|
|
|
|
|
|
|
glGenBuffers(1, &_indexBuffer);
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
|
|
|
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(idxBuf), idxBuf, GL_STATIC_DRAW);
|
|
|
|
|
|
|
|
if (Configuration::getInstance()->supportsShareableVAO())
|
|
|
|
{
|
|
|
|
glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_POSITION);
|
|
|
|
_glProgramState->applyAttributes(false);
|
|
|
|
|
2018-09-11 14:39:30 +08:00
|
|
|
glBindVertexArray(0);
|
2015-08-20 16:19:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-21 10:27:19 +08:00
|
|
|
void CameraBackgroundSkyBoxBrush::setTexture(TextureCube* texture)
|
2015-08-20 16:19:57 +08:00
|
|
|
{
|
|
|
|
CC_SAFE_RETAIN(texture);
|
|
|
|
CC_SAFE_RELEASE(_texture);
|
|
|
|
_texture = texture;
|
2019-02-19 10:15:24 +08:00
|
|
|
//TODO arnold
|
|
|
|
//_glProgramState->setUniformTexture("u_Env", _texture->getBackendTexture());
|
2015-08-20 16:19:57 +08:00
|
|
|
}
|
|
|
|
|
2015-11-12 10:57:22 +08:00
|
|
|
bool CameraBackgroundSkyBoxBrush::isActived() const
|
2015-11-12 09:49:49 +08:00
|
|
|
{
|
|
|
|
return _actived;
|
|
|
|
}
|
|
|
|
void CameraBackgroundSkyBoxBrush::setActived(bool actived)
|
|
|
|
{
|
|
|
|
_actived = actived;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CameraBackgroundSkyBoxBrush::setTextureValid(bool valid)
|
|
|
|
{
|
|
|
|
_textureValid = valid;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CameraBackgroundSkyBoxBrush::isValid()
|
|
|
|
{
|
|
|
|
return _actived;
|
|
|
|
}
|
|
|
|
|
2015-08-20 16:19:57 +08:00
|
|
|
NS_CC_END
|