2015-01-29 09:28:14 +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-03-26 13:05:42 +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-03-26 13:05:42 +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-03-26 13:05:42 +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-01-29 09:28:14 +08:00
|
|
|
****************************************************************************/
|
2019-02-19 11:57:13 +08:00
|
|
|
#include "3d/CCSkybox.h"
|
2015-04-01 17:34:57 +08:00
|
|
|
#include "base/ccMacros.h"
|
|
|
|
#include "base/CCConfiguration.h"
|
|
|
|
#include "base/CCDirector.h"
|
|
|
|
#include "renderer/CCRenderer.h"
|
2015-06-13 06:03:48 +08:00
|
|
|
#include "renderer/CCRenderState.h"
|
2015-08-24 10:49:10 +08:00
|
|
|
#include "renderer/CCTextureCube.h"
|
2019-02-19 10:15:24 +08:00
|
|
|
#include "renderer/ccShaders.h"
|
2015-04-14 01:39:23 +08:00
|
|
|
#include "2d/CCCamera.h"
|
|
|
|
|
2015-01-29 09:28:14 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Skybox::Skybox() : _texture(nullptr) {}
|
2015-01-29 09:28:14 +08:00
|
|
|
|
|
|
|
Skybox::~Skybox()
|
|
|
|
{
|
|
|
|
_texture->release();
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
Skybox* Skybox::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)
|
2015-04-01 17:34:57 +08:00
|
|
|
{
|
2021-12-08 00:11:53 +08:00
|
|
|
auto ret = new Skybox();
|
2015-04-01 17:34:57 +08:00
|
|
|
ret->init(positive_x, negative_x, positive_y, negative_y, positive_z, negative_z);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-04-01 17:34:57 +08:00
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-01-29 09:28:14 +08:00
|
|
|
bool Skybox::init()
|
|
|
|
{
|
2019-03-25 10:08:04 +08:00
|
|
|
_customCommand.setTransparent(false);
|
|
|
|
_customCommand.set3D(true);
|
|
|
|
|
|
|
|
_customCommand.setBeforeCallback(CC_CALLBACK_0(Skybox::onBeforeDraw, this));
|
|
|
|
_customCommand.setAfterCallback(CC_CALLBACK_0(Skybox::onAfterDraw, this));
|
|
|
|
|
2015-01-29 17:31:57 +08:00
|
|
|
// create and set our custom shader
|
2019-10-15 09:40:59 +08:00
|
|
|
auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::SKYBOX_3D);
|
2020-10-16 16:23:14 +08:00
|
|
|
setProgramState(new backend::ProgramState(program), false);
|
2019-02-19 10:15:24 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
auto& pipelineDescriptor = _customCommand.getPipelineDescriptor();
|
|
|
|
auto layout = _programState->getVertexLayout();
|
2019-02-19 10:15:24 +08:00
|
|
|
|
|
|
|
pipelineDescriptor.programState = _programState;
|
|
|
|
// disable blend
|
2021-12-25 10:04:45 +08:00
|
|
|
pipelineDescriptor.blendDescriptor.blendEnabled = false;
|
|
|
|
const auto& attributeInfo = _programState->getProgram()->getActiveAttributes();
|
2019-03-12 19:40:13 +08:00
|
|
|
const auto& iter = attributeInfo.find(shaderinfos::attribute::ATTRIBUTE_NAME_POSITION);
|
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(shaderinfos::attribute::ATTRIBUTE_NAME_POSITION, iter->second.location,
|
|
|
|
backend::VertexFormat::FLOAT3, 0, false);
|
2019-03-12 19:40:13 +08:00
|
|
|
}
|
2019-08-15 16:51:49 +08:00
|
|
|
layout->setLayout(sizeof(Vec3));
|
2019-02-19 10:15:24 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
_uniformColorLoc = _programState->getUniformLocation("u_color");
|
2019-02-19 10:15:24 +08:00
|
|
|
_uniformCameraRotLoc = _programState->getUniformLocation("u_cameraRot");
|
2021-12-25 10:04:45 +08:00
|
|
|
_uniformEnvLoc = _programState->getUniformLocation("u_Env");
|
2015-01-29 09:28:14 +08:00
|
|
|
|
2015-01-29 17:31:57 +08:00
|
|
|
initBuffers();
|
|
|
|
return true;
|
|
|
|
}
|
2015-01-29 09:28:14 +08:00
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
bool Skybox::init(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-04-01 17:34:57 +08:00
|
|
|
{
|
|
|
|
auto texture = TextureCube::create(positive_x, negative_x, positive_y, negative_y, positive_z, negative_z);
|
2015-04-01 17:58:41 +08:00
|
|
|
if (texture == nullptr)
|
|
|
|
return false;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-04-01 17:58:41 +08:00
|
|
|
init();
|
|
|
|
setTexture(texture);
|
2019-02-19 10:15:24 +08:00
|
|
|
texture->release();
|
2015-04-01 17:34:57 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-01-29 17:31:57 +08:00
|
|
|
void Skybox::initBuffers()
|
|
|
|
{
|
2019-02-19 10:15:24 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// The skybox is rendered using a purpose-built shader which makes use of
|
|
|
|
// the shader language's inherent support for cubemaps. Hence there is no
|
|
|
|
// need to build a cube mesh. All that is needed is a single quad that
|
|
|
|
// covers the entire screen. The vertex shader will draw the appropriate
|
|
|
|
// view of the cubemap onto that quad.
|
|
|
|
//
|
|
|
|
// The vertex shader does not apply either the model/view matrix or the
|
|
|
|
// projection matrix, so the appropriate quad is one with unit coordinates
|
|
|
|
// in the x and y dimensions. Such a quad will exactly cover the screen.
|
|
|
|
// To ensure that the skybox is rendered behind all other objects, z needs
|
|
|
|
// to be 1.0, but the vertex shader overwrites z to 1.0, so - for the sake
|
|
|
|
// of z-buffering - it is unimportant what we set it to for the vertices
|
|
|
|
// of the quad.
|
|
|
|
//
|
|
|
|
// The quad vertex positions are also used in deriving a direction
|
|
|
|
// vector for the cubemap lookup. We choose z = -1 which matches the
|
|
|
|
// negative-z pointing direction of the camera and gives a field of
|
|
|
|
// view of 90deg in both x and y, if not otherwise adjusted. That fov
|
|
|
|
// is then adjusted to exactly match the camera by applying a prescaling
|
|
|
|
// to the camera's world transformation before sending it to the shader.
|
2015-03-24 15:43:59 +08:00
|
|
|
|
2015-01-29 17:31:57 +08:00
|
|
|
// 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)};
|
2015-01-29 09:28:14 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
uint16_t idxBuf[] = {0, 1, 2, 0, 2, 3};
|
2015-01-29 17:31:57 +08:00
|
|
|
|
2019-02-19 10:15:24 +08:00
|
|
|
_customCommand.createVertexBuffer(sizeof(Vec3), sizeof(vexBuf), CustomCommand::BufferUsage::STATIC);
|
|
|
|
_customCommand.createIndexBuffer(CustomCommand::IndexFormat::U_SHORT, 6, CustomCommand::BufferUsage::STATIC);
|
2015-03-24 15:43:59 +08:00
|
|
|
|
2019-02-19 10:15:24 +08:00
|
|
|
_customCommand.updateVertexBuffer(&vexBuf[0], sizeof(vexBuf));
|
|
|
|
_customCommand.updateIndexBuffer(&idxBuf[0], sizeof(idxBuf));
|
2015-01-29 09:28:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Skybox::draw(Renderer* renderer, const Mat4& transform, uint32_t flags)
|
|
|
|
{
|
|
|
|
_customCommand.init(_globalZOrder);
|
2019-02-19 10:15:24 +08:00
|
|
|
|
2015-01-29 09:28:14 +08:00
|
|
|
renderer->addCommand(&_customCommand);
|
|
|
|
|
2015-05-26 17:14:26 +08:00
|
|
|
auto camera = Camera::getVisitingCamera();
|
2019-02-19 10:15:24 +08:00
|
|
|
|
2015-05-26 17:14:26 +08:00
|
|
|
Mat4 cameraModelMat = camera->getNodeToWorldTransform();
|
2021-12-25 10:04:45 +08:00
|
|
|
Mat4 projectionMat = camera->getProjectionMatrix();
|
2016-10-17 13:46:26 +08:00
|
|
|
// Ignore the translation
|
|
|
|
cameraModelMat.m[12] = cameraModelMat.m[13] = cameraModelMat.m[14] = 0;
|
|
|
|
// prescale the matrix to account for the camera fov
|
|
|
|
cameraModelMat.scale(1 / projectionMat.m[0], 1 / projectionMat.m[5], 1.0);
|
2015-04-14 01:39:23 +08:00
|
|
|
|
2015-03-27 17:20:46 +08:00
|
|
|
Vec4 color(_displayedColor.r / 255.f, _displayedColor.g / 255.f, _displayedColor.b / 255.f, 1.f);
|
2019-02-19 10:15:24 +08:00
|
|
|
_programState->setUniform(_uniformColorLoc, &color, sizeof(color));
|
|
|
|
_programState->setUniform(_uniformCameraRotLoc, cameraModelMat.m, sizeof(cameraModelMat.m));
|
2015-01-29 09:28:14 +08:00
|
|
|
|
2016-10-17 13:46:26 +08:00
|
|
|
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, 4);
|
2015-01-29 09:28:14 +08:00
|
|
|
}
|
|
|
|
|
2015-01-30 17:23:30 +08:00
|
|
|
void Skybox::setTexture(TextureCube* texture)
|
2015-01-29 09:28:14 +08:00
|
|
|
{
|
|
|
|
CCASSERT(texture != nullptr, __FUNCTION__);
|
2019-02-19 10:15:24 +08:00
|
|
|
CC_SAFE_RELEASE_NULL(_texture);
|
2015-03-24 15:43:59 +08:00
|
|
|
texture->retain();
|
2015-01-29 09:28:14 +08:00
|
|
|
_texture = texture;
|
2019-02-19 10:15:24 +08:00
|
|
|
_programState->setTexture(_uniformEnvLoc, 0, _texture->getBackendTexture());
|
2015-03-24 15:43:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Skybox::reload()
|
|
|
|
{
|
|
|
|
initBuffers();
|
2015-01-29 09:28:14 +08:00
|
|
|
}
|
|
|
|
|
2019-02-19 10:15:24 +08:00
|
|
|
void Skybox::onBeforeDraw()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto* renderer = _director->getRenderer();
|
|
|
|
|
2019-02-19 10:15:24 +08:00
|
|
|
_rendererDepthTestEnabled = renderer->getDepthTest();
|
2021-12-25 10:04:45 +08:00
|
|
|
_rendererDepthCmpFunc = renderer->getDepthCompareFunction();
|
|
|
|
_rendererCullMode = renderer->getCullMode();
|
2019-02-19 10:15:24 +08:00
|
|
|
|
|
|
|
renderer->setDepthTest(true);
|
2019-03-25 10:08:04 +08:00
|
|
|
renderer->setDepthCompareFunction(backend::CompareFunction::LESS_EQUAL);
|
2019-02-19 10:15:24 +08:00
|
|
|
renderer->setCullMode(CullMode::BACK);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Skybox::onAfterDraw()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto* renderer = _director->getRenderer();
|
2019-02-19 10:15:24 +08:00
|
|
|
renderer->setDepthTest(_rendererDepthTestEnabled);
|
|
|
|
renderer->setDepthCompareFunction(_rendererDepthCmpFunc);
|
|
|
|
renderer->setCullMode(_rendererCullMode);
|
|
|
|
}
|
|
|
|
|
2015-01-29 09:28:14 +08:00
|
|
|
NS_CC_END
|