Allow px/color skybox without tex

This commit is contained in:
Zach Pomerantz 2016-03-10 12:58:49 -08:00
parent d7d351fc63
commit b627a17ce9
6 changed files with 42 additions and 84 deletions

View file

@ -3757,19 +3757,19 @@ namespace render {
switch (backgroundMode) { switch (backgroundMode) {
case model::SunSkyStage::SKY_BOX: { case model::SunSkyStage::SKY_BOX: {
auto skybox = skyStage->getSkybox(); auto skybox = skyStage->getSkybox();
if (skybox && skybox->getCubemap() && skybox->getCubemap()->isDefined()) { if (skybox) {
PerformanceTimer perfTimer("skybox"); PerformanceTimer perfTimer("skybox");
skybox->render(batch, *(args->_viewFrustum)); skybox->render(batch, *(args->_viewFrustum));
break; break;
} }
// If no skybox texture is available, render the SKY_DOME while it loads
} }
// fall through to next case
// Fall through: if no skybox is available, render the SKY_DOME
case model::SunSkyStage::SKY_DOME: { case model::SunSkyStage::SKY_DOME: {
if (Menu::getInstance()->isOptionChecked(MenuOption::Stars)) { if (Menu::getInstance()->isOptionChecked(MenuOption::Stars)) {
PerformanceTimer perfTimer("stars"); PerformanceTimer perfTimer("stars");
PerformanceWarning warn(Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings), PerformanceWarning warn(Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings),
"Application::payloadRender<BackgroundRenderData>() ... stars..."); "Application::payloadRender<BackgroundRenderData>() ... My god, it's full of stars...");
// should be the first rendering pass - w/o depth buffer / lighting // should be the first rendering pass - w/o depth buffer / lighting
static const float alpha = 1.0f; static const float alpha = 1.0f;
@ -3777,6 +3777,7 @@ namespace render {
} }
} }
break; break;
case model::SunSkyStage::NO_BACKGROUND: case model::SunSkyStage::NO_BACKGROUND:
default: default:
// this line intentionally left blank // this line intentionally left blank

View file

@ -101,8 +101,6 @@ private:
/// A simple object wrapper for an OpenGL texture. /// A simple object wrapper for an OpenGL texture.
class Texture { class Texture {
public: public:
friend class TextureCache;
gpu::TexturePointer getGPUTexture() const { return _textureSource->getGPUTexture(); } gpu::TexturePointer getGPUTexture() const { return _textureSource->getGPUTexture(); }
gpu::TextureSourcePointer _textureSource; gpu::TextureSourcePointer _textureSource;
}; };

View file

@ -21,54 +21,39 @@
using namespace model; using namespace model;
Skybox::Skybox() { Skybox::Skybox() {
Data data; Schema schema;
_dataBuffer = gpu::BufferView(std::make_shared<gpu::Buffer>(sizeof(Data), (const gpu::Byte*) &data)); _schemaBuffer = gpu::BufferView(std::make_shared<gpu::Buffer>(sizeof(Schema), (const gpu::Byte*) &schema));
/* // PLease create a default engineer skybox
_cubemap.reset( gpu::Texture::createCube(gpu::Element::COLOR_RGBA_32, 1));
unsigned char texels[] = {
255, 0, 0, 255,
0, 255, 255, 255,
0, 0, 255, 255,
255, 255, 0, 255,
0, 255, 0, 255,
255, 0, 255, 255,
};
_cubemap->assignStoredMip(0, gpu::Element::COLOR_RGBA_32, sizeof(texels), texels);*/
} }
void Skybox::setColor(const Color& color) { void Skybox::setColor(const Color& color) {
_dataBuffer.edit<Data>()._color = color; _schemaBuffer.edit<Schema>()._color = color;
} }
void Skybox::setCubemap(const gpu::TexturePointer& cubemap) { void Skybox::setCubemap(const gpu::TexturePointer& cubemap) {
_cubemap = cubemap; _cubemap = cubemap;
} }
void Skybox::updateSchemaBuffer() const {
void Skybox::updateDataBuffer() const {
auto blend = 0.0f; auto blend = 0.0f;
if (getCubemap() && getCubemap()->isDefined()) { if (getCubemap() && getCubemap()->isDefined()) {
blend = 1.0f; blend = 0.5f;
// If pitch black neutralize the color // If pitch black neutralize the color
if (glm::all(glm::equal(getColor(), glm::vec3(0.0f)))) { if (glm::all(glm::equal(getColor(), glm::vec3(0.0f)))) {
blend = 2.0f; blend = 1.0f;
} }
} }
if (blend != _dataBuffer.get<Data>()._blend) { if (blend != _schemaBuffer.get<Schema>()._blend) {
_dataBuffer.edit<Data>()._blend = blend; _schemaBuffer.edit<Schema>()._blend = blend;
} }
} }
void Skybox::render(gpu::Batch& batch, const ViewFrustum& frustum) const { void Skybox::render(gpu::Batch& batch, const ViewFrustum& frustum) const {
updateDataBuffer(); updateSchemaBuffer();
Skybox::render(batch, frustum, (*this)); Skybox::render(batch, frustum, (*this));
} }
void Skybox::render(gpu::Batch& batch, const ViewFrustum& viewFrustum, const Skybox& skybox) { void Skybox::render(gpu::Batch& batch, const ViewFrustum& viewFrustum, const Skybox& skybox) {
// Create the static shared elements used to render the skybox // Create the static shared elements used to render the skybox
static gpu::BufferPointer theConstants; static gpu::BufferPointer theConstants;
@ -98,10 +83,6 @@ void Skybox::render(gpu::Batch& batch, const ViewFrustum& viewFrustum, const Sky
// Render // Render
gpu::TexturePointer skymap = skybox.getCubemap();
// FIXME: skymap->isDefined may not be threadsafe
assert(skymap && skymap->isDefined());
glm::mat4 projMat; glm::mat4 projMat;
viewFrustum.evalProjectionMatrix(projMat); viewFrustum.evalProjectionMatrix(projMat);
@ -112,11 +93,15 @@ void Skybox::render(gpu::Batch& batch, const ViewFrustum& viewFrustum, const Sky
batch.setModelTransform(Transform()); // only for Mac batch.setModelTransform(Transform()); // only for Mac
batch.setPipeline(thePipeline); batch.setPipeline(thePipeline);
batch.setUniformBuffer(SKYBOX_CONSTANTS_SLOT, skybox._dataBuffer); batch.setUniformBuffer(SKYBOX_CONSTANTS_SLOT, skybox._schemaBuffer);
batch.setResourceTexture(SKYBOX_SKYMAP_SLOT, skymap);
gpu::TexturePointer skymap = skybox.getCubemap();
// FIXME: skymap->isDefined may not be threadsafe
if (skymap && skymap->isDefined()) {
batch.setResourceTexture(SKYBOX_SKYMAP_SLOT, skymap);
}
batch.draw(gpu::TRIANGLE_STRIP, 4); batch.draw(gpu::TRIANGLE_STRIP, 4);
batch.setResourceTexture(SKYBOX_SKYMAP_SLOT, nullptr); batch.setResourceTexture(SKYBOX_SKYMAP_SLOT, nullptr);
} }

View file

@ -30,28 +30,27 @@ public:
virtual ~Skybox() {}; virtual ~Skybox() {};
void setColor(const Color& color); void setColor(const Color& color);
const Color getColor() const { return _dataBuffer.get<Data>()._color; } const Color getColor() const { return _schemaBuffer.get<Schema>()._color; }
void setCubemap(const gpu::TexturePointer& cubemap); void setCubemap(const gpu::TexturePointer& cubemap);
const gpu::TexturePointer& getCubemap() const { return _cubemap; } const gpu::TexturePointer& getCubemap() const { return _cubemap; }
virtual void render(gpu::Batch& batch, const ViewFrustum& frustum) const; virtual void render(gpu::Batch& batch, const ViewFrustum& frustum) const;
static void render(gpu::Batch& batch, const ViewFrustum& frustum, const Skybox& skybox); static void render(gpu::Batch& batch, const ViewFrustum& frustum, const Skybox& skybox);
protected: protected:
gpu::TexturePointer _cubemap; gpu::TexturePointer _cubemap;
class Data { class Schema {
public: public:
glm::vec3 _color{ 1.0f, 1.0f, 1.0f }; glm::vec3 _color { 1.0f, 1.0f, 1.0f };
float _blend = 1.0f; float _blend { 0.0f };
}; };
mutable gpu::BufferView _dataBuffer; mutable gpu::BufferView _schemaBuffer;
void updateDataBuffer() const; void updateSchemaBuffer() const;
}; };
typedef std::shared_ptr< Skybox > SkyboxPointer; typedef std::shared_ptr< Skybox > SkyboxPointer;

View file

@ -14,7 +14,8 @@
uniform samplerCube cubeMap; uniform samplerCube cubeMap;
struct Skybox { struct Skybox {
vec4 _color; vec3 _color;
float _blend;
}; };
uniform skyboxBuffer { uniform skyboxBuffer {
@ -24,36 +25,17 @@ uniform skyboxBuffer {
in vec3 _normal; in vec3 _normal;
out vec4 _fragColor; out vec4 _fragColor;
//PROCEDURAL_COMMON_BLOCK
#line 1001
//PROCEDURAL_BLOCK
#line 2033
void main(void) { void main(void) {
#ifdef PROCEDURAL
vec3 color = getSkyboxColor();
_fragColor = vec4(color, 0.0);
#else
vec3 coord = normalize(_normal); vec3 coord = normalize(_normal);
vec3 color = _skybox._color;
// Skybox color or blend with skymap // blend is only set if there is a cubemap
vec3 color = _skybox._color.rgb; if (_skybox._blend > 0.0) {
if (_skybox._color.a > 0.0) { color = texture(cubeMap, coord).rgb;
vec3 texel = texture(cubeMap, coord).rgb; if (_skybox._blend < 1.0) {
if (_skybox._color.a < 2.0) { color *= _skybox._color;
color *= texel;
} else {
color = texel;
} }
} }
_fragColor = vec4(color, 0.0); _fragColor = vec4(color, 0.0);
#endif
} }

View file

@ -38,20 +38,15 @@ void ProceduralSkybox::setProcedural(const ProceduralPointer& procedural) {
} }
void ProceduralSkybox::render(gpu::Batch& batch, const ViewFrustum& frustum) const { void ProceduralSkybox::render(gpu::Batch& batch, const ViewFrustum& frustum) const {
ProceduralSkybox::render(batch, frustum, (*this)); if (_procedural) {
ProceduralSkybox::render(batch, frustum, (*this));
} else {
Skybox::render(batch, frustum);
}
} }
void ProceduralSkybox::render(gpu::Batch& batch, const ViewFrustum& viewFrustum, const ProceduralSkybox& skybox) { void ProceduralSkybox::render(gpu::Batch& batch, const ViewFrustum& viewFrustum, const ProceduralSkybox& skybox) {
if (!(skybox._procedural)) {
skybox.updateDataBuffer();
Skybox::render(batch, viewFrustum, skybox);
}
if (skybox._procedural && skybox._procedural->_enabled && skybox._procedural->ready()) { if (skybox._procedural && skybox._procedural->_enabled && skybox._procedural->ready()) {
gpu::TexturePointer skymap = skybox.getCubemap();
// FIXME: skymap->isDefined may not be threadsafe
assert(skymap && skymap->isDefined());
glm::mat4 projMat; glm::mat4 projMat;
viewFrustum.evalProjectionMatrix(projMat); viewFrustum.evalProjectionMatrix(projMat);
@ -60,10 +55,8 @@ void ProceduralSkybox::render(gpu::Batch& batch, const ViewFrustum& viewFrustum,
batch.setProjectionTransform(projMat); batch.setProjectionTransform(projMat);
batch.setViewTransform(viewTransform); batch.setViewTransform(viewTransform);
batch.setModelTransform(Transform()); // only for Mac batch.setModelTransform(Transform()); // only for Mac
batch.setResourceTexture(0, skybox.getCubemap());
skybox._procedural->prepare(batch, glm::vec3(0), glm::vec3(1)); skybox._procedural->prepare(batch, glm::vec3(0), glm::vec3(1));
batch.draw(gpu::TRIANGLE_STRIP, 4); batch.draw(gpu::TRIANGLE_STRIP, 4);
} }
} }