From 6d02d4d25e29d354b0614b491dc663aab4f85de7 Mon Sep 17 00:00:00 2001 From: Sam Gateau Date: Tue, 5 May 2015 11:40:00 -0700 Subject: [PATCH] adding skybox step by step --- interface/src/Application.cpp | 68 +++++++++++-------- interface/src/devices/KeyboardMouseDevice.cpp | 6 +- libraries/model/src/model/Material.cpp | 16 ++++- libraries/model/src/model/Material.h | 16 +++-- libraries/model/src/model/Stage.cpp | 13 ++-- libraries/model/src/model/Stage.h | 28 ++++---- 6 files changed, 86 insertions(+), 61 deletions(-) diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index a63e2f2005..fa684a86f1 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -3071,45 +3071,53 @@ void Application::displaySide(Camera& theCamera, bool selfAvatarOnly, RenderArgs glTexGenfv(GL_R, GL_EYE_PLANE, (const GLfloat*)&_shadowMatrices[i][2]); } - if (!selfAvatarOnly && Menu::getInstance()->isOptionChecked(MenuOption::Stars)) { - PerformanceTimer perfTimer("stars"); - PerformanceWarning warn(Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings), - "Application::displaySide() ... stars..."); - if (!_stars.isStarsLoaded()) { - _stars.generate(STARFIELD_NUM_STARS, STARFIELD_SEED); - } - // should be the first rendering pass - w/o depth buffer / lighting - - // compute starfield alpha based on distance from atmosphere - float alpha = 1.0f; - if (Menu::getInstance()->isOptionChecked(MenuOption::Atmosphere)) { - const EnvironmentData& closestData = _environment.getClosestData(theCamera.getPosition()); - float height = glm::distance(theCamera.getPosition(), - closestData.getAtmosphereCenter(theCamera.getPosition())); - if (height < closestData.getAtmosphereInnerRadius()) { - alpha = 0.0f; - - } else if (height < closestData.getAtmosphereOuterRadius()) { - alpha = (height - closestData.getAtmosphereInnerRadius()) / - (closestData.getAtmosphereOuterRadius() - closestData.getAtmosphereInnerRadius()); + // Render the stars and sky only if sky dome mode + auto skyStage = DependencyManager::get()->getSkyStage(); + if (skyStage->getBackgroundMode() == model::SunSkyStage::NO_BACKGROUND) { + } else if (skyStage->getBackgroundMode() == model::SunSkyStage::SKY_DOME) { + if (!selfAvatarOnly && Menu::getInstance()->isOptionChecked(MenuOption::Stars)) { + PerformanceTimer perfTimer("stars"); + PerformanceWarning warn(Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings), + "Application::displaySide() ... stars..."); + if (!_stars.isStarsLoaded()) { + _stars.generate(STARFIELD_NUM_STARS, STARFIELD_SEED); } + // should be the first rendering pass - w/o depth buffer / lighting + + // compute starfield alpha based on distance from atmosphere + float alpha = 1.0f; + if (Menu::getInstance()->isOptionChecked(MenuOption::Atmosphere)) { + const EnvironmentData& closestData = _environment.getClosestData(theCamera.getPosition()); + float height = glm::distance(theCamera.getPosition(), + closestData.getAtmosphereCenter(theCamera.getPosition())); + if (height < closestData.getAtmosphereInnerRadius()) { + alpha = 0.0f; + + } else if (height < closestData.getAtmosphereOuterRadius()) { + alpha = (height - closestData.getAtmosphereInnerRadius()) / + (closestData.getAtmosphereOuterRadius() - closestData.getAtmosphereInnerRadius()); + } + } + + // finally render the starfield + _stars.render(theCamera.getFieldOfView(), theCamera.getAspectRatio(), theCamera.getNearClip(), alpha); } - // finally render the starfield - _stars.render(theCamera.getFieldOfView(), theCamera.getAspectRatio(), theCamera.getNearClip(), alpha); + // draw the sky dome + if (!selfAvatarOnly && Menu::getInstance()->isOptionChecked(MenuOption::Atmosphere)) { + PerformanceTimer perfTimer("atmosphere"); + PerformanceWarning warn(Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings), + "Application::displaySide() ... atmosphere..."); + _environment.renderAtmospheres(theCamera); + } + } else if (skyStage->getBackgroundMode() == model::SunSkyStage::SKY_BOX) { + } if (Menu::getInstance()->isOptionChecked(MenuOption::Wireframe)) { glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); } - // draw the sky dome - if (!selfAvatarOnly && Menu::getInstance()->isOptionChecked(MenuOption::Atmosphere)) { - PerformanceTimer perfTimer("atmosphere"); - PerformanceWarning warn(Menu::getInstance()->isOptionChecked(MenuOption::PipelineWarnings), - "Application::displaySide() ... atmosphere..."); - _environment.renderAtmospheres(theCamera); - } glEnable(GL_LIGHTING); glEnable(GL_DEPTH_TEST); diff --git a/interface/src/devices/KeyboardMouseDevice.cpp b/interface/src/devices/KeyboardMouseDevice.cpp index 622fc4b5dc..7b0f8c056c 100755 --- a/interface/src/devices/KeyboardMouseDevice.cpp +++ b/interface/src/devices/KeyboardMouseDevice.cpp @@ -123,7 +123,11 @@ void KeyboardMouseDevice::touchUpdateEvent(const QTouchEvent* event) { } UserInputMapper::Input KeyboardMouseDevice::makeInput(Qt::Key code) { - return UserInputMapper::Input(_deviceID, code & KEYBOARD_MASK, UserInputMapper::ChannelType::BUTTON); + auto shortCode = (UserInputMapper::uint16)(code & KEYBOARD_MASK); + if (shortCode != code) { + shortCode |= 0x0800; // add this bit instead of the way Qt::Key add a bit on the 3rd byte for some keys + } + return UserInputMapper::Input(_deviceID, shortCode, UserInputMapper::ChannelType::BUTTON); } UserInputMapper::Input KeyboardMouseDevice::makeInput(Qt::MouseButton code) { diff --git a/libraries/model/src/model/Material.cpp b/libraries/model/src/model/Material.cpp index 1ce50174e0..ff0691442c 100755 --- a/libraries/model/src/model/Material.cpp +++ b/libraries/model/src/model/Material.cpp @@ -11,6 +11,7 @@ #include "Material.h" using namespace model; +using namespace gpu; Material::Material() : _flags(0), @@ -86,7 +87,20 @@ void Material::setOpacity(float opacity) { _schemaBuffer.edit()._opacity = opacity; } -void Material::setTextureView(MapChannel channel, const TextureView& view) { +void Material::setTextureView(MapChannel channel, const gpu::TextureView& view) { _flags.set(DIFFUSE_MAP_BIT + channel); _textureMap[channel] = view; } + +// TextureStorage +TextureStorage::TextureStorage(const QUrl& url) : gpu::Texture::Storage(), _url(url) { + init(); +} + +TextureStorage::~TextureStorage() { +} + +void TextureStorage::init() { + _gpuTexture = TexturePointer(Texture::createFromStorage(this)); +} + diff --git a/libraries/model/src/model/Material.h b/libraries/model/src/model/Material.h index 02fb7ae1f3..7e4417b55b 100755 --- a/libraries/model/src/model/Material.h +++ b/libraries/model/src/model/Material.h @@ -23,12 +23,14 @@ namespace model { +typedef glm::vec3 Color; + // TextureStorage is a specialized version of the gpu::Texture::Storage // It adds the URL and the notion that it owns the gpu::Texture -class LoadedTexture : public gpu::Texture::Storage { +class TextureStorage : public gpu::Texture::Storage { public: - LoadedTexture(const QUrl& url); - ~LoadedTexture(); + TextureStorage(const QUrl& url); + ~TextureStorage(); const QUrl& getUrl() const { return _url; } const gpu::TexturePointer& getGPUTexture() const { return _gpuTexture; } @@ -36,14 +38,16 @@ public: protected: gpu::TexturePointer _gpuTexture; QUrl _url; + void init(); }; -typedef std::shared_ptr< LoadedTexture > LoadedTexturePointer; +typedef std::shared_ptr< TextureStorage > TextureStoragePointer; + -typedef gpu::BufferView UniformBufferView; -typedef gpu::TextureView TextureView; class Material { public: + typedef gpu::BufferView UniformBufferView; + typedef gpu::TextureView TextureView; typedef glm::vec3 Color; diff --git a/libraries/model/src/model/Stage.cpp b/libraries/model/src/model/Stage.cpp index 77bc3f03f0..453bc0c154 100644 --- a/libraries/model/src/model/Stage.cpp +++ b/libraries/model/src/model/Stage.cpp @@ -184,14 +184,6 @@ void Atmosphere::setInnerOuterRadiuses(float inner, float outer) { data._scales.z = data._scales.x / data._scales.y; } -Skybox::Skybox() { -} - -void Skybox::setCubemap(const gpu::TexturePointer& cubemap) { - _cubemap = cubemap; -} - - const int NUM_DAYS_PER_YEAR = 365; const float NUM_HOURS_PER_DAY = 24.0f; @@ -301,6 +293,11 @@ void SunSkyStage::updateGraphicsObject() const { } } +void SunSkyStage::setBackgroundMode(BackgroundMode mode) { + _backgroundMode = mode; + invalidate(); +} + void SunSkyStage::setSkybox(const SkyboxPointer& skybox) { _skybox = skybox; invalidate(); diff --git a/libraries/model/src/model/Stage.h b/libraries/model/src/model/Stage.h index ee7cad4176..b28c20fc65 100644 --- a/libraries/model/src/model/Stage.h +++ b/libraries/model/src/model/Stage.h @@ -14,6 +14,7 @@ #include "gpu/Pipeline.h" #include "Light.h" +#include "Skybox.h" namespace model { @@ -160,21 +161,6 @@ protected: }; typedef std::shared_ptr< Atmosphere > AtmospherePointer; - -class Skybox { -public: - Skybox(); - Skybox& operator= (const Atmosphere& Skybox); - virtual ~Skybox() {}; - - void setCubemap(const gpu::TexturePointer& cubemap); - const gpu::TexturePointer& getCubemap() const { return _cubemap; } - -protected: - gpu::TexturePointer _cubemap; -}; -typedef std::shared_ptr< Skybox > SkyboxPointer; - // Sun sky stage generates the rendering primitives to display a scene realistically // at the specified location and time around earth class SunSkyStage { @@ -222,11 +208,23 @@ public: LightPointer getSunLight() const { valid(); return _sunLight; } AtmospherePointer getAtmosphere() const { valid(); return _atmosphere; } + enum BackgroundMode { + NO_BACKGROUND = 0, + SKY_DOME, + SKY_BOX, + + NUM_BACKGROUND_MODES, + }; + void setBackgroundMode(BackgroundMode mode); + BackgroundMode getBackgroundMode() const { return _backgroundMode; } + // Skybox void setSkybox(const SkyboxPointer& skybox); const SkyboxPointer& getSkybox() const { valid(); return _skybox; } protected: + BackgroundMode _backgroundMode = SKY_DOME; + LightPointer _sunLight; AtmospherePointer _atmosphere; SkyboxPointer _skybox;