mirror of
https://github.com/overte-org/overte.git
synced 2025-04-20 04:44:11 +02:00
Add texture, color to px skybox
This commit is contained in:
parent
b627a17ce9
commit
4eca43027a
8 changed files with 47 additions and 111 deletions
|
@ -15,8 +15,8 @@
|
|||
#include <gpu/Context.h>
|
||||
#include <ViewFrustum.h>
|
||||
|
||||
#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<Schema>()._color = color;
|
||||
_schemaBuffer.edit<Schema>().color = color;
|
||||
}
|
||||
|
||||
void Skybox::setCubemap(const gpu::TexturePointer& cubemap) {
|
||||
|
@ -44,8 +44,18 @@ void Skybox::updateSchemaBuffer() const {
|
|||
}
|
||||
}
|
||||
|
||||
if (blend != _schemaBuffer.get<Schema>()._blend) {
|
||||
_schemaBuffer.edit<Schema>()._blend = blend;
|
||||
if (blend != _schemaBuffer.get<Schema>().blend) {
|
||||
_schemaBuffer.edit<Schema>().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);
|
||||
|
|
|
@ -30,29 +30,33 @@ public:
|
|||
virtual ~Skybox() {};
|
||||
|
||||
void setColor(const Color& color);
|
||||
const Color getColor() const { return _schemaBuffer.get<Schema>()._color; }
|
||||
const Color getColor() const { return _schemaBuffer.get<Schema>().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<Skybox> SkyboxPointer;
|
||||
|
||||
};
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
16
libraries/procedural/src/procedural/ProceduralSkybox.slf → libraries/model/src/model/skybox.slf
Normal file → Executable file
16
libraries/procedural/src/procedural/ProceduralSkybox.slf → libraries/model/src/model/skybox.slf
Normal file → Executable file
|
@ -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
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -15,8 +15,8 @@
|
|||
#include <gpu/Context.h>
|
||||
#include <ViewFrustum.h>
|
||||
|
||||
#include "ProceduralSkybox_vert.h"
|
||||
#include "ProceduralSkybox_frag.h"
|
||||
#include <model/skybox_vert.h>
|
||||
#include <model/skybox_frag.h>
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
Loading…
Reference in a new issue