Enable heightmaps for RenderablePlanet (closing #21)

This commit is contained in:
Alexander Bock
2016-05-18 12:12:28 +02:00
parent 2f69520331
commit caeb8618a8
5 changed files with 79 additions and 11 deletions

View File

@@ -37,7 +37,7 @@ return {
Type = "simple",
Color = "textures/earth_bluemarble.jpg",
Night = "textures/earth_night.jpg",
-- Depth = "textures/earth_depth.png"
Height = "textures/earth_bluemarble_height.jpg"
},
Atmosphere = {
Type = "Nishita", -- for example, values missing etc etc

View File

@@ -58,15 +58,19 @@ namespace openspace {
RenderablePlanet::RenderablePlanet(const ghoul::Dictionary& dictionary)
: Renderable(dictionary)
, _colorTexturePath("colorTexture", "Color Texture")
, _nightTexturePath("nightTexture", "Night Texture")
, _heightMapTexturePath("heightMap", "Heightmap Texture")
, _heightExaggeration("heightExaggeration", "Height Exaggeration", 1.f, 0.f, 100.f)
, _programObject(nullptr)
, _texture(nullptr)
, _nightTexture(nullptr)
, _heightMapTexture(nullptr)
, _geometry(nullptr)
, _performShading("performShading", "Perform Shading", true)
, _rotation("rotation", "Rotation", 0, 0, 360)
, _alpha(1.f)
, _nightTexturePath("")
, _hasNightTexture(false)
, _hasHeightTexture(false)
{
std::string name;
bool success = dictionary.getValue(SceneGraphNode::KeyName, name);
@@ -100,17 +104,31 @@ RenderablePlanet::RenderablePlanet(const ghoul::Dictionary& dictionary)
std::string nightTexturePath = "";
dictionary.getValue("Textures.Night", nightTexturePath);
if (nightTexturePath != ""){
_hasNightTexture = true;
_nightTexturePath = absPath(nightTexturePath);
}
std::string heightMapTexturePath = "";
dictionary.getValue("Textures.Height", heightMapTexturePath);
if (heightMapTexturePath != "") {
_hasHeightTexture = true;
_heightMapTexturePath = absPath(heightMapTexturePath);
}
addPropertySubOwner(_geometry);
addProperty(_colorTexturePath);
_colorTexturePath.onChange(std::bind(&RenderablePlanet::loadTexture, this));
addProperty(_nightTexturePath);
_nightTexturePath.onChange(std::bind(&RenderablePlanet::loadTexture, this));
addProperty(_heightMapTexturePath);
_heightMapTexturePath.onChange(std::bind(&RenderablePlanet::loadTexture, this));
addProperty(_heightExaggeration);
if (dictionary.hasKeyAndValue<bool>(keyShading)) {
bool shading;
dictionary.getValue(keyShading, shading);
@@ -142,11 +160,13 @@ bool RenderablePlanet::initialize() {
"pscstandard",
"${MODULE_BASE}/shaders/pscstandard_vs.glsl",
"${MODULE_BASE}/shaders/pscstandard_fs.glsl");
if (!_programObject) return false;
if (!_programObject)
return false;
}
using IgnoreError = ghoul::opengl::ProgramObject::IgnoreError;
_programObject->setIgnoreSubroutineUniformLocationError(IgnoreError::Yes);
_programObject->setIgnoreUniformLocationError(IgnoreError::Yes);
loadTexture();
_geometry->initialize(this);
@@ -218,20 +238,32 @@ void RenderablePlanet::render(const RenderData& data)
_programObject->setUniform("_performShading", _performShading);
_programObject->setUniform("_hasHeightMap", _hasHeightTexture);
_programObject->setUniform("_heightExaggeration", _heightExaggeration);
// Bind texture
ghoul::opengl::TextureUnit dayUnit;
ghoul::opengl::TextureUnit nightUnit;
ghoul::opengl::TextureUnit heightUnit;
dayUnit.activate();
_texture->bind();
_programObject->setUniform("texture1", dayUnit);
// Bind possible night texture
if (_hasNightTexture) {
ghoul::opengl::TextureUnit nightUnit;
nightUnit.activate();
_nightTexture->bind();
_programObject->setUniform("nightTex", nightUnit);
}
if (_hasHeightTexture) {
heightUnit.activate();
_heightMapTexture->bind();
_programObject->setUniform("heightTex", heightUnit);
}
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
@@ -264,7 +296,7 @@ void RenderablePlanet::loadTexture() {
}
if (_hasNightTexture) {
_nightTexture = nullptr;
if (_nightTexturePath != "") {
if (_nightTexturePath.value() != "") {
_nightTexture = std::move(ghoul::io::TextureReader::ref().loadTexture(absPath(_nightTexturePath)));
if (_nightTexture) {
LDEBUG("Loaded texture from '" << _nightTexturePath << "'");
@@ -274,6 +306,19 @@ void RenderablePlanet::loadTexture() {
}
}
}
if (_hasHeightTexture) {
_heightMapTexture = nullptr;
if (_heightMapTexturePath.value() != "") {
_heightMapTexture = std::move(ghoul::io::TextureReader::ref().loadTexture(absPath(_heightMapTexturePath)));
if (_heightMapTexture) {
LDEBUG("Loaded texture from '" << _heightMapTexturePath << "'");
_heightMapTexture->uploadTexture();
_heightMapTexture->setFilter(ghoul::opengl::Texture::FilterMode::Linear);
//_nightTexture->setFilter(ghoul::opengl::Texture::FilterMode::AnisotropicMipMap);
}
}
}
}
} // namespace openspace

View File

@@ -62,19 +62,26 @@ protected:
private:
properties::StringProperty _colorTexturePath;
properties::StringProperty _nightTexturePath;
properties::StringProperty _heightMapTexturePath;
std::unique_ptr<ghoul::opengl::ProgramObject> _programObject;
std::unique_ptr<ghoul::opengl::Texture> _texture;
std::unique_ptr<ghoul::opengl::Texture> _nightTexture;
std::unique_ptr<ghoul::opengl::Texture> _heightMapTexture;
properties::FloatProperty _heightExaggeration;
planetgeometry::PlanetGeometry* _geometry;
properties::BoolProperty _performShading;
properties::IntProperty _rotation;
float _alpha;
glm::dmat3 _stateMatrix;
std::string _nightTexturePath;
std::string _frame;
std::string _target;
bool _hasNightTexture;
bool _hasHeightTexture;
double _time;
};

View File

@@ -34,6 +34,7 @@ uniform int shadows;
uniform float time;
uniform sampler2D texture1;
uniform sampler2D nightTex;
uniform sampler2D heightTex;
in vec2 vs_st;
in vec2 vs_nightTex;

View File

@@ -24,21 +24,26 @@
#version __CONTEXT__
uniform mat4 ViewProjection;
uniform mat4 ModelTransform;
#include "PowerScaling/powerScaling_vs.hglsl"
layout(location = 0) in vec4 in_position;
layout(location = 1) in vec2 in_st;
layout(location = 2) in vec3 in_normal;
//layout(location = 3) in vec2 in_nightTex;
out vec2 vs_st;
out vec4 vs_normal;
out vec4 vs_position;
out float s;
#include "PowerScaling/powerScaling_vs.hglsl"
uniform mat4 ViewProjection;
uniform mat4 ModelTransform;
uniform sampler2D heightTex;
uniform bool _hasHeightMap;
uniform float _heightExaggeration;
void main()
{
@@ -53,5 +58,15 @@ void main()
vec4 position = pscTransform(tmp, ModelTransform);
vs_position = tmp;
position = ViewProjection * position;
if (_hasHeightMap) {
// Get the height of the height value
float height = texture(heightTex, in_st).r;
// Displace the position along the position vector (being the normal) by that height * the
// exaggeration factor
position.xyz = position.xyz + height * normalize(position.xyz) * _heightExaggeration;
}
gl_Position = z_normalization(position);
}