123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- #include "base/ccMacros.h"
- #include "base/CCConfiguration.h"
- #include "base/CCDirector.h"
- #include "renderer/ccGLStateCache.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"
- #include "3d/CCSkybox.h"
- #include "2d/CCCamera.h"
- NS_CC_BEGIN
- Skybox::Skybox()
- : _vao(0)
- , _vertexBuffer(0)
- , _indexBuffer(0)
- ,_texture(nullptr)
- {
- }
- Skybox::~Skybox()
- {
- glDeleteBuffers(1, &_vertexBuffer);
- glDeleteBuffers(1, &_indexBuffer);
- _vertexBuffer = 0;
- _indexBuffer = 0;
- if (Configuration::getInstance()->supportsShareableVAO())
- {
- glDeleteVertexArrays(1, &_vao);
- GL::bindVAO(0);
- _vao = 0;
- }
- _texture->release();
- }
- 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;
- }
- bool Skybox::init()
- {
-
- auto shader = GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_3D_SKYBOX);
- auto state = GLProgramState::create(shader);
- state->setVertexAttribPointer(GLProgram::ATTRIBUTE_NAME_POSITION, 3, GL_FLOAT, GL_FALSE, sizeof(Vec3), nullptr);
- setGLProgramState(state);
- initBuffers();
- CHECK_GL_ERROR_DEBUG();
- return true;
- }
- 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);
- if (texture == nullptr)
- return false;
-
- init();
- setTexture(texture);
- return true;
- }
- void Skybox::initBuffers()
- {
- if (Configuration::getInstance()->supportsShareableVAO())
- {
- glGenVertexArrays(1, &_vao);
- GL::bindVAO(_vao);
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- Vec3 vexBuf[] =
- {
- 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);
-
- const unsigned char idxBuf[] = {0, 1, 2, 0, 2, 3};
- 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);
- getGLProgramState()->applyAttributes(false);
- GL::bindVAO(0);
- }
- }
- void Skybox::draw(Renderer* renderer, const Mat4& transform, uint32_t flags)
- {
- _customCommand.init(_globalZOrder);
- _customCommand.func = CC_CALLBACK_0(Skybox::onDraw, this, transform, flags);
- _customCommand.setTransparent(false);
- _customCommand.set3D(true);
- renderer->addCommand(&_customCommand);
- }
- void Skybox::onDraw(const Mat4& transform, uint32_t )
- {
- auto camera = Camera::getVisitingCamera();
-
- Mat4 cameraModelMat = camera->getNodeToWorldTransform();
- Mat4 projectionMat = camera->getProjectionMatrix();
-
- cameraModelMat.m[12] = cameraModelMat.m[13] = cameraModelMat.m[14] = 0;
-
- cameraModelMat.scale(1 / projectionMat.m[0], 1 / projectionMat.m[5], 1.0);
-
- auto state = getGLProgramState();
- state->apply(transform);
- Vec4 color(_displayedColor.r / 255.f, _displayedColor.g / 255.f, _displayedColor.b / 255.f, 1.f);
- state->setUniformVec4("u_color", color);
- state->setUniformMat4("u_cameraRot", cameraModelMat);
- glEnable(GL_DEPTH_TEST);
- RenderState::StateBlock::_defaultState->setDepthTest(true);
- 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);
-
- glDisable(GL_BLEND);
- RenderState::StateBlock::_defaultState->setBlend(false);
- if (Configuration::getInstance()->supportsShareableVAO())
- {
- GL::bindVAO(_vao);
- }
- else
- {
- GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POSITION);
- 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)6, GL_UNSIGNED_BYTE, nullptr);
- if (Configuration::getInstance()->supportsShareableVAO())
- {
- GL::bindVAO(0);
- }
- else
- {
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
- }
- CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, 4);
- CHECK_GL_ERROR_DEBUG();
- }
- void Skybox::setTexture(TextureCube* texture)
- {
- CCASSERT(texture != nullptr, __FUNCTION__);
- texture->retain();
- if (_texture)
- _texture->release();
- _texture = texture;
- getGLProgramState()->setUniformTexture("u_Env", _texture);
- }
- void Skybox::reload()
- {
- initBuffers();
- }
- NS_CC_END
|