diff --git a/libraries/model/src/model/Skybox.cpp b/libraries/model/src/model/Skybox.cpp index 00a64c9606..08dcc491cf 100755 --- a/libraries/model/src/model/Skybox.cpp +++ b/libraries/model/src/model/Skybox.cpp @@ -52,6 +52,8 @@ void Skybox::render(gpu::Batch& batch, const ViewFrustum& viewFrustum, const Sky static gpu::PipelinePointer thePipeline; static gpu::BufferPointer theBuffer; static gpu::Stream::FormatPointer theFormat; + static gpu::BufferPointer theConstants; + const int SKYBOX_CONSTANTS_SLOT = 3; if (!thePipeline) { auto skyVS = gpu::ShaderPointer(gpu::Shader::createVertex(std::string(Skybox_vert))); auto skyFS = gpu::ShaderPointer(gpu::Shader::createPixel(std::string(Skybox_frag))); @@ -59,7 +61,8 @@ void Skybox::render(gpu::Batch& batch, const ViewFrustum& viewFrustum, const Sky gpu::Shader::BindingSet bindings; bindings.insert(gpu::Shader::Binding(std::string("cubeMap"), 0)); - + bindings.insert(gpu::Shader::Binding(std::string("skyboxBuffer"), SKYBOX_CONSTANTS_SLOT)); + if (!gpu::Shader::makeProgram(*skyShader, bindings)) { } @@ -74,6 +77,9 @@ void Skybox::render(gpu::Batch& batch, const ViewFrustum& viewFrustum, const Sky theFormat.reset(new gpu::Stream::Format()); theFormat->setAttribute(gpu::Stream::POSITION, gpu::Stream::POSITION, gpu::Element(gpu::VEC2, gpu::FLOAT, gpu::XYZ)); + + auto color = glm::vec4(1.0f); + theConstants.reset(new gpu::Buffer(sizeof(color), (const gpu::Byte*) &color)); } glm::mat4 projMat; @@ -82,11 +88,19 @@ void Skybox::render(gpu::Batch& batch, const ViewFrustum& viewFrustum, const Sky Transform viewTransform; viewFrustum.evalViewTransform(viewTransform); + if (glm::all(glm::equal(skybox.getColor(), glm::vec3(0.0f)))) { + auto color = glm::vec4(1.0f); + theConstants->setSubData(0, sizeof(color), (const gpu::Byte*) &color); + } else { + theConstants->setSubData(0, sizeof(Color), (const gpu::Byte*) &skybox.getColor()); + } + batch.setProjectionTransform(projMat); batch.setViewTransform(viewTransform); batch.setModelTransform(Transform()); // only for Mac batch.setPipeline(thePipeline); batch.setInputBuffer(gpu::Stream::POSITION, theBuffer, 0, 8); + batch.setUniformBuffer(SKYBOX_CONSTANTS_SLOT, theConstants, 0, theConstants->getSize()); batch.setInputFormat(theFormat); batch.setUniformTexture(0, skybox.getCubemap()); batch.draw(gpu::TRIANGLE_STRIP, 4); diff --git a/libraries/model/src/model/Skybox.h b/libraries/model/src/model/Skybox.h index de0ede8d90..b739032f39 100755 --- a/libraries/model/src/model/Skybox.h +++ b/libraries/model/src/model/Skybox.h @@ -38,6 +38,7 @@ public: protected: gpu::TexturePointer _cubemap; + Color _color{1.0f, 1.0f, 1.0f}; }; typedef std::shared_ptr< Skybox > SkyboxPointer; diff --git a/libraries/model/src/model/Skybox.slv b/libraries/model/src/model/Skybox.slv index 14c066905e..c21863f9c7 100755 --- a/libraries/model/src/model/Skybox.slv +++ b/libraries/model/src/model/Skybox.slv @@ -15,6 +15,27 @@ <$declareStandardTransform()$> +struct Skybox { + vec4 _color; +}; + +<@if GPU_FEATURE_PROFILE == GPU_CORE @> +uniform skyboxBuffer { + Skybox _skybox; +}; +Skybox getSkybox() { + return _skybox; +} +<@else@> +uniform vec4 skyboxBuffer[1]; +Skybox getSkybox() { + Skybox _skybox; + _skybox._color = skyboxBuffer[0]; + + return _skybox; +} +<@endif@> + varying vec3 normal; varying vec2 texcoord; varying vec3 color; @@ -22,7 +43,8 @@ varying vec3 color; void main(void) { texcoord = gl_Vertex.xy; - color = vec3(1.0, 1.0, 1.0); + Skybox skybox = getSkybox(); + color = skybox._color.xyz; // standard transform TransformCamera cam = getTransformCamera();