diff --git a/libraries/model/src/model/Skybox.cpp b/libraries/model/src/model/Skybox.cpp index 0f9d3ca4de..aff4361338 100755 --- a/libraries/model/src/model/Skybox.cpp +++ b/libraries/model/src/model/Skybox.cpp @@ -15,8 +15,8 @@ #include #include -#include "Skybox_vert.h" -#include "Skybox_frag.h" +#include "skybox_vert.h" +#include "skybox_frag.h" using namespace model; @@ -26,7 +26,7 @@ Skybox::Skybox() { } void Skybox::setColor(const Color& color) { - _schemaBuffer.edit()._color = color; + _schemaBuffer.edit().color = color; } void Skybox::setCubemap(const gpu::TexturePointer& cubemap) { @@ -44,8 +44,18 @@ void Skybox::updateSchemaBuffer() const { } } - if (blend != _schemaBuffer.get()._blend) { - _schemaBuffer.edit()._blend = blend; + if (blend != _schemaBuffer.get().blend) { + _schemaBuffer.edit().blend = blend; + } +} + +void Skybox::prepare(gpu::Batch& batch, int textureSlot, int bufferSlot) const { + batch.setUniformBuffer(bufferSlot, _schemaBuffer); + + gpu::TexturePointer skymap = getCubemap(); + // FIXME: skymap->isDefined may not be threadsafe + if (skymap && skymap->isDefined()) { + batch.setResourceTexture(textureSlot, skymap); } } @@ -58,13 +68,11 @@ void Skybox::render(gpu::Batch& batch, const ViewFrustum& viewFrustum, const Sky // Create the static shared elements used to render the skybox static gpu::BufferPointer theConstants; static gpu::PipelinePointer thePipeline; - const int SKYBOX_SKYMAP_SLOT = 0; - const int SKYBOX_CONSTANTS_SLOT = 0; static std::once_flag once; std::call_once(once, [&] { { - auto skyVS = gpu::Shader::createVertex(std::string(Skybox_vert)); - auto skyFS = gpu::Shader::createPixel(std::string(Skybox_frag)); + auto skyVS = gpu::Shader::createVertex(std::string(skybox_vert)); + auto skyFS = gpu::Shader::createPixel(std::string(skybox_frag)); auto skyShader = gpu::Shader::createProgram(skyVS, skyFS); gpu::Shader::BindingSet bindings; @@ -93,14 +101,7 @@ void Skybox::render(gpu::Batch& batch, const ViewFrustum& viewFrustum, const Sky batch.setModelTransform(Transform()); // only for Mac batch.setPipeline(thePipeline); - batch.setUniformBuffer(SKYBOX_CONSTANTS_SLOT, skybox._schemaBuffer); - - gpu::TexturePointer skymap = skybox.getCubemap(); - // FIXME: skymap->isDefined may not be threadsafe - if (skymap && skymap->isDefined()) { - batch.setResourceTexture(SKYBOX_SKYMAP_SLOT, skymap); - } - + skybox.prepare(batch); batch.draw(gpu::TRIANGLE_STRIP, 4); batch.setResourceTexture(SKYBOX_SKYMAP_SLOT, nullptr); diff --git a/libraries/model/src/model/Skybox.h b/libraries/model/src/model/Skybox.h index 7382ffabdd..e9e7ee8b26 100755 --- a/libraries/model/src/model/Skybox.h +++ b/libraries/model/src/model/Skybox.h @@ -30,29 +30,33 @@ public: virtual ~Skybox() {}; void setColor(const Color& color); - const Color getColor() const { return _schemaBuffer.get()._color; } + const Color getColor() const { return _schemaBuffer.get().color; } void setCubemap(const gpu::TexturePointer& cubemap); const gpu::TexturePointer& getCubemap() const { return _cubemap; } + void prepare(gpu::Batch& batch, int textureSlot = SKYBOX_SKYMAP_SLOT, int bufferSlot = SKYBOX_CONSTANTS_SLOT) const; virtual void render(gpu::Batch& batch, const ViewFrustum& frustum) const; static void render(gpu::Batch& batch, const ViewFrustum& frustum, const Skybox& skybox); protected: + static const int SKYBOX_SKYMAP_SLOT { 0 }; + static const int SKYBOX_CONSTANTS_SLOT { 0 }; + gpu::TexturePointer _cubemap; class Schema { public: - glm::vec3 _color { 1.0f, 1.0f, 1.0f }; - float _blend { 0.0f }; + glm::vec3 color { 1.0f, 1.0f, 1.0f }; + float blend { 0.0f }; }; mutable gpu::BufferView _schemaBuffer; void updateSchemaBuffer() const; }; -typedef std::shared_ptr< Skybox > SkyboxPointer; +typedef std::shared_ptr SkyboxPointer; }; diff --git a/libraries/model/src/model/Skybox.slf b/libraries/model/src/model/Skybox.slf deleted file mode 100755 index 93f68e3406..0000000000 --- a/libraries/model/src/model/Skybox.slf +++ /dev/null @@ -1,41 +0,0 @@ -<@include gpu/Config.slh@> -<$VERSION_HEADER$> -// Generated on <$_SCRIBE_DATE$> -// skybox.frag -// fragment shader -// -// Created by Sam Gateau on 5/5/2015. -// Copyright 2015 High Fidelity, Inc. -// -// Distributed under the Apache License, Version 2.0. -// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html -// - -uniform samplerCube cubeMap; - -struct Skybox { - vec3 _color; - float _blend; -}; - -uniform skyboxBuffer { - Skybox _skybox; -}; - -in vec3 _normal; -out vec4 _fragColor; - -void main(void) { - vec3 coord = normalize(_normal); - vec3 color = _skybox._color; - - // blend is only set if there is a cubemap - if (_skybox._blend > 0.0) { - color = texture(cubeMap, coord).rgb; - if (_skybox._blend < 1.0) { - color *= _skybox._color; - } - } - - _fragColor = vec4(color, 0.0); -} diff --git a/libraries/procedural/src/procedural/ProceduralSkybox.slf b/libraries/model/src/model/skybox.slf old mode 100644 new mode 100755 similarity index 76% rename from libraries/procedural/src/procedural/ProceduralSkybox.slf rename to libraries/model/src/model/skybox.slf index 7ad6f6b5a1..6eb3cf3963 --- a/libraries/procedural/src/procedural/ProceduralSkybox.slf +++ b/libraries/model/src/model/skybox.slf @@ -14,11 +14,11 @@ uniform samplerCube cubeMap; struct Skybox { - vec4 _color; + vec4 color; }; uniform skyboxBuffer { - Skybox _skybox; + Skybox skybox; }; in vec3 _normal; @@ -40,10 +40,16 @@ void main(void) { _fragColor = vec4(color, 0.0); #else - vec3 coord = normalize(_normal); - vec3 texel = texture(cubeMap, coord).rgb; - vec3 color = texel * _skybox._color.rgb; + vec3 color = skybox.color.rgb; + + // blend is only set if there is a cubemap + if (skybox.color.a > 0.0) { + color = texture(cubeMap, coord).rgb; + if (skybox.color.a < 1.0) { + color *= skybox.color.rgb; + } + } _fragColor = vec4(color, 0.0); #endif diff --git a/libraries/model/src/model/Skybox.slv b/libraries/model/src/model/skybox.slv similarity index 100% rename from libraries/model/src/model/Skybox.slv rename to libraries/model/src/model/skybox.slv diff --git a/libraries/procedural/src/procedural/Procedural.h b/libraries/procedural/src/procedural/Procedural.h index 1b02fbd435..0d3ab63773 100644 --- a/libraries/procedural/src/procedural/Procedural.h +++ b/libraries/procedural/src/procedural/Procedural.h @@ -35,6 +35,7 @@ struct Procedural { void parse(const QJsonObject&); bool ready(); void prepare(gpu::Batch& batch, const glm::vec3& position, const glm::vec3& size); + const gpu::ShaderPointer& getShader() const { return _shader; } void setupUniforms(); glm::vec4 getColor(const glm::vec4& entityColor); diff --git a/libraries/procedural/src/procedural/ProceduralSkybox.cpp b/libraries/procedural/src/procedural/ProceduralSkybox.cpp index 95deaa4dc5..cf57dbb574 100644 --- a/libraries/procedural/src/procedural/ProceduralSkybox.cpp +++ b/libraries/procedural/src/procedural/ProceduralSkybox.cpp @@ -15,8 +15,8 @@ #include #include -#include "ProceduralSkybox_vert.h" -#include "ProceduralSkybox_frag.h" +#include +#include ProceduralSkybox::ProceduralSkybox() : model::Skybox() { } @@ -30,8 +30,8 @@ ProceduralSkybox::ProceduralSkybox(const ProceduralSkybox& skybox) : void ProceduralSkybox::setProcedural(const ProceduralPointer& procedural) { _procedural = procedural; if (_procedural) { - _procedural->_vertexSource = ProceduralSkybox_vert; - _procedural->_fragmentSource = ProceduralSkybox_frag; + _procedural->_vertexSource = skybox_vert; + _procedural->_fragmentSource = skybox_frag; // Adjust the pipeline state for background using the stencil test _procedural->_state->setStencilTest(true, 0xFF, gpu::State::StencilTest(0, 0xFF, gpu::EQUAL, gpu::State::STENCIL_OP_KEEP, gpu::State::STENCIL_OP_KEEP, gpu::State::STENCIL_OP_KEEP)); } @@ -56,7 +56,11 @@ void ProceduralSkybox::render(gpu::Batch& batch, const ViewFrustum& viewFrustum, batch.setViewTransform(viewTransform); batch.setModelTransform(Transform()); // only for Mac - skybox._procedural->prepare(batch, glm::vec3(0), glm::vec3(1)); + const auto& procedural = skybox._procedural; + procedural->prepare(batch, glm::vec3(0), glm::vec3(1)); + auto textureSlot = procedural->getShader()->getTextures().findLocation("cubeMap"); + auto bufferSlot = procedural->getShader()->getBuffers().findLocation("skyboxBuffer"); + skybox.prepare(batch, textureSlot, bufferSlot); batch.draw(gpu::TRIANGLE_STRIP, 4); } } diff --git a/libraries/procedural/src/procedural/ProceduralSkybox.slv b/libraries/procedural/src/procedural/ProceduralSkybox.slv deleted file mode 100644 index 810afb1033..0000000000 --- a/libraries/procedural/src/procedural/ProceduralSkybox.slv +++ /dev/null @@ -1,39 +0,0 @@ -<@include gpu/Config.slh@> -<$VERSION_HEADER$> -// Generated on <$_SCRIBE_DATE$> -// skybox.vert -// vertex shader -// -// Created by Sam Gateau on 5/5/2015. -// Copyright 2015 High Fidelity, Inc. -// -// Distributed under the Apache License, Version 2.0. -// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html -// - -<@include gpu/Transform.slh@> - -<$declareStandardTransform()$> - -out vec3 _normal; - -void main(void) { - const float depth = 0.0; - const vec4 UNIT_QUAD[4] = vec4[4]( - vec4(-1.0, -1.0, depth, 1.0), - vec4(1.0, -1.0, depth, 1.0), - vec4(-1.0, 1.0, depth, 1.0), - vec4(1.0, 1.0, depth, 1.0) - ); - vec4 inPosition = UNIT_QUAD[gl_VertexID]; - - // standard transform - TransformCamera cam = getTransformCamera(); - vec3 clipDir = vec3(inPosition.xy, 0.0); - vec3 eyeDir; - <$transformClipToEyeDir(cam, clipDir, eyeDir)$> - <$transformEyeToWorldDir(cam, eyeDir, _normal)$> - - // Position is supposed to come in clip space - gl_Position = vec4(inPosition.xy, 0.0, 1.0); -} \ No newline at end of file