Skip to content

Commit

Permalink
Refactor bumpmap extensions into a layer
Browse files Browse the repository at this point in the history
  • Loading branch information
gwaldron committed Jun 3, 2024
1 parent db75a70 commit cde928c
Show file tree
Hide file tree
Showing 10 changed files with 214 additions and 309 deletions.
59 changes: 31 additions & 28 deletions src/osgEarthDrivers/bumpmap/BumpMap.frag.progressive.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -34,37 +34,40 @@ in float oe_bumpmap_range;
// Entry point for progressive blended bump maps
void oe_bumpmap_fragment(inout vec4 color)
{
// sample the bump map
const float amplitudeDecay = 1.0; // no decay.
float maxLOD = float(oe_bumpmap_octaves)+1.0;

// starter vector:
vec3 bump = vec3(0.0);
float scale = 1.0;
float amplitude = 1.0;
float limit = oe_bumpmap_range;
float range = oe_bumpmap_maxRange;
float lastRange = oe_bumpmap_maxRange;
for(float lod = 1.0; lod < maxLOD; lod += 1.0, scale *= 2.0, amplitude *= amplitudeDecay)
if (oe_bumpmap_octaves > 0)
{
float fadeIn = 1.0;
if ( range <= limit && limit < oe_bumpmap_maxRange )
fadeIn = clamp((lastRange-limit)/(lastRange-range), 0.0, 1.0);
bump += (texture(oe_bumpmap_tex, oe_bumpmap_coords*scale).xyz*2.0-1.0)*amplitude*fadeIn;
if ( range <= limit )
break;
lastRange = range;
range = oe_bumpmap_maxRange/exp(lod);
}
// sample the bump map
const float amplitudeDecay = 1.0; // no decay.
float maxLOD = float(oe_bumpmap_octaves) + 1.0;

// starter vector:
vec3 bump = vec3(0.0);
float scale = 1.0;
float amplitude = 1.0;
float limit = oe_bumpmap_range;
float range = oe_bumpmap_maxRange;
float lastRange = oe_bumpmap_maxRange;
for (float lod = 1.0; lod < maxLOD; lod += 1.0, scale *= 2.0, amplitude *= amplitudeDecay)
{
float fadeIn = 1.0;
if (range <= limit && limit < oe_bumpmap_maxRange)
fadeIn = clamp((lastRange - limit) / (lastRange - range), 0.0, 1.0);
bump += (texture(oe_bumpmap_tex, oe_bumpmap_coords * scale).xyz * 2.0 - 1.0) * amplitude * fadeIn;
if (range <= limit)
break;
lastRange = range;
range = oe_bumpmap_maxRange / exp(lod);
}

// finally, transform into view space and normalize the vector.
bump = normalize(oe_bumpmap_normalMatrix*bump);

// calculate slope from normal:
float slope = mix(1.0, 1.0 - dot(oe_UpVectorView, vp_Normal), oe_bumpmap_slopeFactor);
// finally, transform into view space and normalize the vector.
bump = normalize(oe_bumpmap_normalMatrix * bump);

// permute the normal with the bump.
vp_Normal = normalize(vp_Normal + bump*oe_bumpmap_intensity*slope);
// calculate slope from normal:
float slope = mix(1.0, 1.0 - dot(oe_UpVectorView, vp_Normal), oe_bumpmap_slopeFactor);

// permute the normal with the bump.
vp_Normal = normalize(vp_Normal + bump * oe_bumpmap_intensity * slope);
}
}

#endif
24 changes: 14 additions & 10 deletions src/osgEarthDrivers/bumpmap/BumpMap.frag.simple.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,24 @@ flat in mat3 oe_bumpmap_normalMatrix;
in vec3 oe_UpVectorView;

uniform sampler2D oe_bumpmap_tex;
uniform float oe_bumpmap_intensity;
uniform float oe_bumpmap_slopeFactor;
uniform float oe_bumpmap_intensity;
uniform float oe_bumpmap_slopeFactor;
uniform int oe_bumpmap_octaves;


void oe_bumpmap_fragment(inout vec4 color)
{
// sample the bump map
vec3 bump = oe_bumpmap_normalMatrix * normalize(texture(oe_bumpmap_tex, oe_bumpmap_coords).xyz*2.0-1.0);

// calculate slope from normal:
float slope = clamp( (1.0-dot(oe_UpVectorView, vp_Normal))*oe_bumpmap_slopeFactor, 0.0, 1.0);

// permute the normal with the bump.
vp_Normal = normalize(vp_Normal + bump*oe_bumpmap_intensity*slope);
if (oe_bumpmap_octaves > 0)
{
// sample the bump map
vec3 bump = oe_bumpmap_normalMatrix * normalize(texture(oe_bumpmap_tex, oe_bumpmap_coords).xyz * 2.0 - 1.0);

// calculate slope from normal:
float slope = clamp((1.0 - dot(oe_UpVectorView, vp_Normal)) * oe_bumpmap_slopeFactor, 0.0, 1.0);

// permute the normal with the bump.
vp_Normal = normalize(vp_Normal + bump * oe_bumpmap_intensity * slope);
}
}

#endif
70 changes: 0 additions & 70 deletions src/osgEarthDrivers/bumpmap/BumpMapExtension

This file was deleted.

106 changes: 0 additions & 106 deletions src/osgEarthDrivers/bumpmap/BumpMapExtension.cpp

This file was deleted.

41 changes: 41 additions & 0 deletions src/osgEarthDrivers/bumpmap/BumpMapLayer
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* -*-c++-*- */
/* osgEarth - Geospatial SDK for OpenSceneGraph
* Copyright 2020 Pelican Mapping
* http://osgearth.org
*
* osgEarth is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/
#pragma once

#include "BumpMapOptions"
#include "BumpMapTerrainEffect"
#include <osgEarth/VisibleLayer>

namespace osgEarth
{
class TerrainEngine;

/**
* Layer that installs a bumpmapping terrain effect.
*/
class BumpMapLayer : public VisibleLayer
{
public:
META_Layer(osgEarth, BumpMapLayer, BumpMapOptions, VisibleLayer, bumpmap);

void prepareForRendering(TerrainEngine* engine) override;

osg::ref_ptr<BumpMapTerrainEffect> _effect;
};
}
69 changes: 69 additions & 0 deletions src/osgEarthDrivers/bumpmap/BumpMapLayer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/* -*-c++-*- */
/* osgEarth - Geospatial SDK for OpenSceneGraph
* Copyright 2020 Pelican Mapping
* http://osgearth.org
*
* osgEarth is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/
#include "BumpMapLayer"
#include "BumpMapTerrainEffect"

#include <osgEarth/TerrainEngineNode>

using namespace osgEarth;

#define LC "[BumpMapLayer] "

REGISTER_OSGEARTH_LAYER(bumpmap, BumpMapLayer);


void
BumpMapLayer::prepareForRendering(TerrainEngine* engine)
{
if ( !engine )
return;

osg::ref_ptr<osg::Image> image = options().imageURI()->getImage(getReadOptions());
if ( !image.valid() )
{
OE_WARN << LC << "Failed; unable to load normal map image from "
<< options().imageURI()->full() << "\n";
return;
}

_effect = new BumpMapTerrainEffect();

_effect->setBumpMapImage(image.get());

if (options().intensity().isSet())
_effect->getIntensityUniform()->set(options().intensity().get());

if (options().scale().isSet())
_effect->getScaleUniform()->set(options().scale().get());

if (options().octaves().isSet())
_effect->setOctaves(options().octaves().get());

if (options().baseLOD().isSet())
_effect->setBaseLOD(options().baseLOD().get());

engine->addEffect(_effect);

OE_DEBUG << LC << "Installed.\n";

onVisibleChanged([&](auto* layer)
{
_effect->setActive(layer->getVisible());
});
}
Loading

0 comments on commit cde928c

Please sign in to comment.