axmol/cocos/3d/CCSkybox.cpp

252 lines
8.2 KiB
C++
Raw Normal View History

2015-01-29 09:28:14 +08:00
/****************************************************************************
Copyright (c) 2015-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2015-03-26 13:05:42 +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-01-29 09:28:14 +08:00
****************************************************************************/
2015-04-01 17:34:57 +08:00
#include "base/ccMacros.h"
#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"
#include "renderer/CCTextureCube.h"
2015-03-26 13:05:42 +08:00
#include "3d/CCSkybox.h"
#include "2d/CCCamera.h"
2015-01-29 09:28:14 +08:00
NS_CC_BEGIN
Skybox::Skybox()
: _vao(0)
2015-01-30 17:23:30 +08:00
, _vertexBuffer(0)
, _indexBuffer(0)
2015-01-29 09:28:14 +08:00
,_texture(nullptr)
{
}
Skybox::~Skybox()
{
2015-01-30 17:23:30 +08:00
glDeleteBuffers(1, &_vertexBuffer);
glDeleteBuffers(1, &_indexBuffer);
2015-01-29 17:31:57 +08:00
2015-01-30 17:23:30 +08:00
_vertexBuffer = 0;
_indexBuffer = 0;
2015-01-29 09:28:14 +08:00
if (Configuration::getInstance()->supportsShareableVAO())
{
glDeleteVertexArrays(1, &_vao);
2018-09-11 14:39:30 +08:00
glBindVertexArray(0);
2015-01-29 09:28:14 +08:00
_vao = 0;
}
_texture->release();
}
2015-04-01 17:34:57 +08:00
Skybox* Skybox::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)
{
auto ret = new (std::nothrow) Skybox();
ret->init(positive_x, negative_x, positive_y, negative_y, positive_z, negative_z);
ret->autorelease();
return ret;
}
2015-01-29 09:28:14 +08:00
bool Skybox::init()
{
2015-01-29 17:31:57 +08:00
// create and set our custom shader
2015-03-26 11:59:26 +08:00
auto shader = GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_3D_SKYBOX);
2015-01-29 09:28:14 +08:00
auto state = GLProgramState::create(shader);
2015-02-02 15:44:47 +08:00
state->setVertexAttribPointer(GLProgram::ATTRIBUTE_NAME_POSITION, 3, GL_FLOAT, GL_FALSE, sizeof(Vec3), nullptr);
2015-01-29 09:28:14 +08:00
setGLProgramState(state);
2015-01-29 17:31:57 +08:00
initBuffers();
2015-01-29 09:28:14 +08:00
CHECK_GL_ERROR_DEBUG();
2015-01-29 17:31:57 +08:00
return true;
}
2015-01-29 09:28:14 +08:00
2015-04-01 17:34:57 +08:00
bool Skybox::init(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)
{
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;
2015-04-01 17:34:57 +08:00
2015-04-01 17:58:41 +08:00
init();
setTexture(texture);
2015-04-01 17:34:57 +08:00
return true;
}
2015-01-29 17:31:57 +08:00
void Skybox::initBuffers()
{
if (Configuration::getInstance()->supportsShareableVAO())
{
glGenVertexArrays(1, &_vao);
2018-09-11 14:39:30 +08:00
glBindVertexArray(_vao);
}
Correct the Skybox fov (#16655) * Remove undrawn quads from the skybox mesh CCSkybox had been implemented using a combination of two inconsistent techniques. The rendering was being achieved via use of the vertex shader's inherent support for cubemaps. That technique requires only a single screen-covering quad, but the implemtation defined a cube. Defining a cube mesh would be appropriate if one were simply mapping the cubemap's 6 textures to faces, but is unnecessary if using the shader's cubemap feature. Not only was the use of a cube mesh unnecessary, but the particular way the cube was defined and used meant that only one face would ever contribute to the rendering. One of the other faces would always be culled and the other four would be viewed edge on, mapping the the infinitesimally thin lines defining the edges of the screen. This commit simply removes the never-rendered faces, and adds comments explaining the technique. * Within test code, remove setScale calls applied to skyboxes. A Skybox is defined in such a way that it's position, rotation and scaling has no effect on it's rendering, so setScale has no effect. The calls are removed from test code to avoid confusing anyone using it as a template for their own programs. * Make the Skybox correctly account for the camera's fov The Skybox does not use the model/view and projection matricies. Instead a single quad that maps exactly to the screen is rendered and the camera's world matrix is passed into a shader that renders using cubemap lookups. The way that works hardwires the fov to 90deg in both the horizontal and vertical. That shows up particularly badly when the camera is pointed directly downwards and rotated: the image deforms as it rotates. This commit corrects the problem by using scaling factors from the camera's projection matrix to prescale the matrix passed into the shader.
2016-10-17 13:46:26 +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
2017-01-11 09:31:45 +08:00
// in the x and y dimensions. Such a quad will exactly cover the screen.
Correct the Skybox fov (#16655) * Remove undrawn quads from the skybox mesh CCSkybox had been implemented using a combination of two inconsistent techniques. The rendering was being achieved via use of the vertex shader's inherent support for cubemaps. That technique requires only a single screen-covering quad, but the implemtation defined a cube. Defining a cube mesh would be appropriate if one were simply mapping the cubemap's 6 textures to faces, but is unnecessary if using the shader's cubemap feature. Not only was the use of a cube mesh unnecessary, but the particular way the cube was defined and used meant that only one face would ever contribute to the rendering. One of the other faces would always be culled and the other four would be viewed edge on, mapping the the infinitesimally thin lines defining the edges of the screen. This commit simply removes the never-rendered faces, and adds comments explaining the technique. * Within test code, remove setScale calls applied to skyboxes. A Skybox is defined in such a way that it's position, rotation and scaling has no effect on it's rendering, so setScale has no effect. The calls are removed from test code to avoid confusing anyone using it as a template for their own programs. * Make the Skybox correctly account for the camera's fov The Skybox does not use the model/view and projection matricies. Instead a single quad that maps exactly to the screen is rendered and the camera's world matrix is passed into a shader that renders using cubemap lookups. The way that works hardwires the fov to 90deg in both the horizontal and vertical. That shows up particularly badly when the camera is pointed directly downwards and rotated: the image deforms as it rotates. This commit corrects the problem by using scaling factors from the camera's projection matrix to prescale the matrix passed into the shader.
2016-10-17 13:46:26 +08:00
// 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
2017-01-11 09:31:45 +08:00
// of z-buffering - it is unimportant what we set it to for the vertices
Correct the Skybox fov (#16655) * Remove undrawn quads from the skybox mesh CCSkybox had been implemented using a combination of two inconsistent techniques. The rendering was being achieved via use of the vertex shader's inherent support for cubemaps. That technique requires only a single screen-covering quad, but the implemtation defined a cube. Defining a cube mesh would be appropriate if one were simply mapping the cubemap's 6 textures to faces, but is unnecessary if using the shader's cubemap feature. Not only was the use of a cube mesh unnecessary, but the particular way the cube was defined and used meant that only one face would ever contribute to the rendering. One of the other faces would always be culled and the other four would be viewed edge on, mapping the the infinitesimally thin lines defining the edges of the screen. This commit simply removes the never-rendered faces, and adds comments explaining the technique. * Within test code, remove setScale calls applied to skyboxes. A Skybox is defined in such a way that it's position, rotation and scaling has no effect on it's rendering, so setScale has no effect. The calls are removed from test code to avoid confusing anyone using it as a template for their own programs. * Make the Skybox correctly account for the camera's fov The Skybox does not use the model/view and projection matricies. Instead a single quad that maps exactly to the screen is rendered and the camera's world matrix is passed into a shader that renders using cubemap lookups. The way that works hardwires the fov to 90deg in both the horizontal and vertical. That shows up particularly badly when the camera is pointed directly downwards and rotated: the image deforms as it rotates. This commit corrects the problem by using scaling factors from the camera's projection matrix to prescale the matrix passed into the shader.
2016-10-17 13:46:26 +08:00
// 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-01-29 17:31:57 +08:00
// init vertex buffer object
Vec3 vexBuf[] =
2015-01-29 09:28:14 +08:00
{
2015-01-29 17:31:57 +08:00
Vec3(1, -1, -1), Vec3(1, 1, -1), Vec3(-1, 1, -1), Vec3(-1, -1, -1)
};
2015-01-29 09:28:14 +08:00
2015-01-30 17:23:30 +08:00
glGenBuffers(1, &_vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
2015-02-02 15:44:47 +08:00
glBufferData(GL_ARRAY_BUFFER, sizeof(vexBuf), vexBuf, GL_STATIC_DRAW);
2015-01-29 09:28:14 +08:00
2015-01-29 17:31:57 +08:00
// init index buffer object
Correct the Skybox fov (#16655) * Remove undrawn quads from the skybox mesh CCSkybox had been implemented using a combination of two inconsistent techniques. The rendering was being achieved via use of the vertex shader's inherent support for cubemaps. That technique requires only a single screen-covering quad, but the implemtation defined a cube. Defining a cube mesh would be appropriate if one were simply mapping the cubemap's 6 textures to faces, but is unnecessary if using the shader's cubemap feature. Not only was the use of a cube mesh unnecessary, but the particular way the cube was defined and used meant that only one face would ever contribute to the rendering. One of the other faces would always be culled and the other four would be viewed edge on, mapping the the infinitesimally thin lines defining the edges of the screen. This commit simply removes the never-rendered faces, and adds comments explaining the technique. * Within test code, remove setScale calls applied to skyboxes. A Skybox is defined in such a way that it's position, rotation and scaling has no effect on it's rendering, so setScale has no effect. The calls are removed from test code to avoid confusing anyone using it as a template for their own programs. * Make the Skybox correctly account for the camera's fov The Skybox does not use the model/view and projection matricies. Instead a single quad that maps exactly to the screen is rendered and the camera's world matrix is passed into a shader that renders using cubemap lookups. The way that works hardwires the fov to 90deg in both the horizontal and vertical. That shows up particularly badly when the camera is pointed directly downwards and rotated: the image deforms as it rotates. This commit corrects the problem by using scaling factors from the camera's projection matrix to prescale the matrix passed into the shader.
2016-10-17 13:46:26 +08:00
const unsigned char idxBuf[] = {0, 1, 2, 0, 2, 3};
2015-01-29 17:31:57 +08:00
2015-01-30 17:23:30 +08:00
glGenBuffers(1, &_indexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
2015-01-29 17:31:57 +08:00
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(idxBuf), idxBuf, GL_STATIC_DRAW);
if (Configuration::getInstance()->supportsShareableVAO())
{
glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_POSITION);
getGLProgramState()->applyAttributes(false);
2018-09-11 14:39:30 +08:00
glBindVertexArray(0);
}
2015-01-29 09:28:14 +08:00
}
void Skybox::draw(Renderer* renderer, const Mat4& transform, uint32_t flags)
{
_customCommand.init(_globalZOrder);
_customCommand.func = CC_CALLBACK_0(Skybox::onDraw, this, transform, flags);
2015-08-19 18:25:34 +08:00
_customCommand.setTransparent(false);
_customCommand.set3D(true);
2015-01-29 09:28:14 +08:00
renderer->addCommand(&_customCommand);
}
void Skybox::onDraw(const Mat4& transform, uint32_t /*flags*/)
2015-01-29 09:28:14 +08:00
{
auto camera = Camera::getVisitingCamera();
Mat4 cameraModelMat = camera->getNodeToWorldTransform();
Correct the Skybox fov (#16655) * Remove undrawn quads from the skybox mesh CCSkybox had been implemented using a combination of two inconsistent techniques. The rendering was being achieved via use of the vertex shader's inherent support for cubemaps. That technique requires only a single screen-covering quad, but the implemtation defined a cube. Defining a cube mesh would be appropriate if one were simply mapping the cubemap's 6 textures to faces, but is unnecessary if using the shader's cubemap feature. Not only was the use of a cube mesh unnecessary, but the particular way the cube was defined and used meant that only one face would ever contribute to the rendering. One of the other faces would always be culled and the other four would be viewed edge on, mapping the the infinitesimally thin lines defining the edges of the screen. This commit simply removes the never-rendered faces, and adds comments explaining the technique. * Within test code, remove setScale calls applied to skyboxes. A Skybox is defined in such a way that it's position, rotation and scaling has no effect on it's rendering, so setScale has no effect. The calls are removed from test code to avoid confusing anyone using it as a template for their own programs. * Make the Skybox correctly account for the camera's fov The Skybox does not use the model/view and projection matricies. Instead a single quad that maps exactly to the screen is rendered and the camera's world matrix is passed into a shader that renders using cubemap lookups. The way that works hardwires the fov to 90deg in both the horizontal and vertical. That shows up particularly badly when the camera is pointed directly downwards and rotated: the image deforms as it rotates. This commit corrects the problem by using scaling factors from the camera's projection matrix to prescale the matrix passed into the shader.
2016-10-17 13:46:26 +08:00
Mat4 projectionMat = camera->getProjectionMatrix();
// 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-01-29 09:28:14 +08:00
auto state = getGLProgramState();
2015-06-25 15:14:44 +08:00
state->apply(transform);
2015-03-27 17:20:46 +08:00
Vec4 color(_displayedColor.r / 255.f, _displayedColor.g / 255.f, _displayedColor.b / 255.f, 1.f);
state->setUniformVec4("u_color", color);
2015-06-25 15:01:39 +08:00
state->setUniformMat4("u_cameraRot", cameraModelMat);
2015-01-30 17:23:30 +08:00
2015-01-29 09:28:14 +08:00
glEnable(GL_DEPTH_TEST);
RenderState::StateBlock::_defaultState->setDepthTest(true);
2015-01-29 09:28:14 +08:00
glDepthFunc(GL_LEQUAL);
RenderState::StateBlock::_defaultState->setDepthFunction(RenderState::DEPTH_LEQUAL);
glEnable(GL_CULL_FACE);
RenderState::StateBlock::_defaultState->setCullFace(true);
glCullFace(GL_BACK);
RenderState::StateBlock::_defaultState->setCullFaceSide(RenderState::CULL_FACE_SIDE_BACK);
2015-08-25 14:32:25 +08:00
glDisable(GL_BLEND);
RenderState::StateBlock::_defaultState->setBlend(false);
2015-01-29 09:28:14 +08:00
if (Configuration::getInstance()->supportsShareableVAO())
{
2018-09-11 14:39:30 +08:00
glBindVertexArray(_vao);
2015-01-29 09:28:14 +08:00
}
else
{
2018-09-11 14:39:30 +08:00
glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_POSITION);
2015-01-29 09:28:14 +08:00
2015-01-30 17:23:30 +08:00
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
2015-01-29 17:31:57 +08:00
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, sizeof(Vec3), nullptr);
2015-01-29 09:28:14 +08:00
2015-01-30 17:23:30 +08:00
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
2015-01-29 09:28:14 +08:00
}
Correct the Skybox fov (#16655) * Remove undrawn quads from the skybox mesh CCSkybox had been implemented using a combination of two inconsistent techniques. The rendering was being achieved via use of the vertex shader's inherent support for cubemaps. That technique requires only a single screen-covering quad, but the implemtation defined a cube. Defining a cube mesh would be appropriate if one were simply mapping the cubemap's 6 textures to faces, but is unnecessary if using the shader's cubemap feature. Not only was the use of a cube mesh unnecessary, but the particular way the cube was defined and used meant that only one face would ever contribute to the rendering. One of the other faces would always be culled and the other four would be viewed edge on, mapping the the infinitesimally thin lines defining the edges of the screen. This commit simply removes the never-rendered faces, and adds comments explaining the technique. * Within test code, remove setScale calls applied to skyboxes. A Skybox is defined in such a way that it's position, rotation and scaling has no effect on it's rendering, so setScale has no effect. The calls are removed from test code to avoid confusing anyone using it as a template for their own programs. * Make the Skybox correctly account for the camera's fov The Skybox does not use the model/view and projection matricies. Instead a single quad that maps exactly to the screen is rendered and the camera's world matrix is passed into a shader that renders using cubemap lookups. The way that works hardwires the fov to 90deg in both the horizontal and vertical. That shows up particularly badly when the camera is pointed directly downwards and rotated: the image deforms as it rotates. This commit corrects the problem by using scaling factors from the camera's projection matrix to prescale the matrix passed into the shader.
2016-10-17 13:46:26 +08:00
glDrawElements(GL_TRIANGLES, (GLsizei)6, GL_UNSIGNED_BYTE, nullptr);
2015-01-29 09:28:14 +08:00
2015-01-29 17:31:57 +08:00
if (Configuration::getInstance()->supportsShareableVAO())
{
2018-09-11 14:39:30 +08:00
glBindVertexArray(0);
2015-01-29 17:31:57 +08:00
}
else
{
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
2015-01-29 09:28:14 +08:00
Correct the Skybox fov (#16655) * Remove undrawn quads from the skybox mesh CCSkybox had been implemented using a combination of two inconsistent techniques. The rendering was being achieved via use of the vertex shader's inherent support for cubemaps. That technique requires only a single screen-covering quad, but the implemtation defined a cube. Defining a cube mesh would be appropriate if one were simply mapping the cubemap's 6 textures to faces, but is unnecessary if using the shader's cubemap feature. Not only was the use of a cube mesh unnecessary, but the particular way the cube was defined and used meant that only one face would ever contribute to the rendering. One of the other faces would always be culled and the other four would be viewed edge on, mapping the the infinitesimally thin lines defining the edges of the screen. This commit simply removes the never-rendered faces, and adds comments explaining the technique. * Within test code, remove setScale calls applied to skyboxes. A Skybox is defined in such a way that it's position, rotation and scaling has no effect on it's rendering, so setScale has no effect. The calls are removed from test code to avoid confusing anyone using it as a template for their own programs. * Make the Skybox correctly account for the camera's fov The Skybox does not use the model/view and projection matricies. Instead a single quad that maps exactly to the screen is rendered and the camera's world matrix is passed into a shader that renders using cubemap lookups. The way that works hardwires the fov to 90deg in both the horizontal and vertical. That shows up particularly badly when the camera is pointed directly downwards and rotated: the image deforms as it rotates. This commit corrects the problem by using scaling factors from the camera's projection matrix to prescale the matrix passed into the shader.
2016-10-17 13:46:26 +08:00
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, 4);
2015-01-29 17:31:57 +08:00
2015-01-29 09:28:14 +08:00
CHECK_GL_ERROR_DEBUG();
}
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__);
texture->retain();
if (_texture)
_texture->release();
2015-01-29 09:28:14 +08:00
_texture = texture;
getGLProgramState()->setUniformTexture("u_Env", _texture);
}
void Skybox::reload()
{
initBuffers();
2015-01-29 09:28:14 +08:00
}
NS_CC_END